package httpserver import ( "log" "net/http" "net/http/cookiejar" "net/url" "strings" "time" "github.com/gin-gonic/gin" "github.com/patrickmn/go-cache" "golang.org/x/net/publicsuffix" ) // TODO: does tihs need to be a configuration thing? How would i handle doing rewrite rules? // TODO: make these routes cached on the proxy, so that when a client requests them, they arent being tunneled through to the actual site var staticRoutes = map[string]struct{}{ "/siteaudit/i18n": struct{}{}, "/siteaudit/index": struct{}{}, "/siteaudit/review": struct{}{}, "/seo-dashboard/release": struct{}{}, "/competitive-list-widget": struct{}{}, "/backlink-audit/landing": struct{}{}, "/link-building-tool/landing": struct{}{}, "/keyword-overview": struct{}{}, "/keyword-gap": struct{}{}, "/oti/prod/organic_traffic_insights": struct{}{}, "/oti/prod/organic-traffic-insights": struct{}{}, "/ajst": struct{}{}, "/listing-management/landings": struct{}{}, "/listing-management/landing-reviews": struct{}{}, "/messaging/apps/": struct{}{}, "/domain-overview": struct{}{}, "/traffic-analytics": struct{}{}, "/organic-research": struct{}{}, "/keyword-magic/kmt_": struct{}{}, "/keyword-manager-assets": struct{}{}, "/position-tracking/landing": struct{}{}, } var apiRoutes = map[string]struct{}{ "/siteaudit/api/campaigns/seolist": struct{}{}, "/siteaudit/api/system-status": struct{}{}, "/siteaudit/api/limits": struct{}{}, "/projects/api/limits": struct{}{}, } // 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 RouteMaps map[string]*RouteMapping Client *http.Client SiteUrl *url.URL cache *cache.Cache } 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, routeMapping map[string]*RouteMapping) *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) cache := cache.New(24*time.Hour, 10*time.Minute) return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain, cache: cache, RouteMaps: routeMapping} } /* 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") routeSplit := strings.Split(incomingPath, "/") if len(routeSplit) > 2 { baseRoute := strings.Join(routeSplit[:len(routeSplit)-1], "/") _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[baseRoute] if ok { data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx) if err != nil { log.Fatal(err, " failed to reroute to the static domain") } ctx.Data(rcode, ctype, data) return } } _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[incomingPath] if ok { data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx) if err != nil { log.Fatal(err, " failed to reroute to the static domain") } ctx.Data(rcode, ctype, data) return } _, ok = c.RouteMaps[c.Config.AllowedDomain].RouteSet[incomingPath] if ok { data, ctype, rcode, 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(rcode, ctype, data) return } data, ctype, rcode, err := c.SemrushGeneric(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, "