123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package httpserver
- import (
- "log"
- "net/http"
- "net/http/cookiejar"
- "net/url"
- "strings"
- "github.com/gin-gonic/gin"
- "golang.org/x/net/publicsuffix"
- )
- // TODO: does tihs need to be a configuration thing? How would i handle doing rewrite rules?
- var staticRoutes = [...]string{
- "/siteaudit/i18n/messages_en",
- "/siteaudit/index/",
- "/siteaudit/review/",
- "/seo-dashboard/release/",
- "/competitive-list-widget/",
- "/backlink-audit/landing/",
- "/link-building-tool/landing/",
- "/keyword-overview/",
- "/keyword-gap/",
- "/oti/prod/organic_traffic_insights/",
- "/oti/prod/organic-traffic-insights",
- "/ajst/",
- "/listing-management/landings/",
- "/listing-management/landing-reviews/",
- "/messaging/apps/",
- "/domain-overview/",
- "/traffic-analytics/",
- "/organic-research/",
- "/keyword-magic/kmt_",
- "/keyword-manager-assets/",
- "/position-tracking/landing/",
- }
- var apiRoutes = [...]string{
- "/projects/api/",
- "/siteaudit/api/",
- }
- // Implementing a 'set'
- var NonmutableHeaders = map[string]struct{}{
- "Cookie": struct{}{},
- "User-Agent": struct{}{},
- "Accept-Encoding": struct{}{},
- "Referer": struct{}{},
- "X-Proxy-Url": struct{}{},
- "Host": struct{}{},
- }
- type Controller struct {
- Config *HttpServerConfig
- Client *http.Client
- SiteUrl *url.URL
- }
- type ProxyCookies struct {
- ck map[*url.URL][]*http.Cookie
- }
- /*
- Returns a new Controller struct to register routes to the gin router
- :param cfg: A pointer to an HttpServerConfig struct
- */
- func NewController(cfg *HttpServerConfig) *Controller {
- jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
- if err != nil {
- log.Fatal(err)
- }
- sessCookies := cfg.CookieJar
- domain, err := url.Parse(cfg.FullDomain)
- if err != nil {
- log.Fatal(err)
- }
- jar.SetCookies(domain, sessCookies)
- return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain}
- }
- /*
- This handler will be responsible for proxying out the GET requests that the server recieves
- */
- func (c *Controller) Get(ctx *gin.Context) {
- incomingPath := ctx.Param("ProxiedPath")
- for idx := range staticRoutes {
- if strings.Contains(incomingPath, staticRoutes[idx]) {
- data, ctype, rcode, err := c.RetrieveStaticResource(incomingPath, ctx)
- if err != nil {
- log.Fatal(err, "reroute to the static domain")
- }
- ctx.Data(rcode, ctype, data)
- return
- }
- }
- for idx := range apiRoutes {
- if strings.Contains(incomingPath, apiRoutes[idx]) {
- data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body, ctx)
- if err != nil {
- log.Fatal(err, "failed to route to the root domain")
- }
- ctx.Data(200, ctype, data)
- return
- }
- }
- data, ctype, rcode, err := c.SemrushGeneric(ctx.Request.Method, incomingPath, ctx.Request.Body, ctx)
- if err != nil {
- ctx.JSON(rcode, map[string]string{
- "Error": err.Error(),
- })
- return
- }
- if incomingPath == "/" {
- ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
- }
- if incomingPath == "/_compatibility/traffic/overview/" {
- ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
- }
- // TODO: this should also honestly be a configuration thing, so that i can extend page modifications
- newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
- newBody = strings.ReplaceAll(newBody, "static.semrush.com", c.Config.ProxyAddr)
- ctx.Data(rcode, ctype, []byte(newBody))
- }
|