Browse Source

adding routes to the rewriter

AETH-erial 1 year ago
parent
commit
ec686f80f7
2 changed files with 47 additions and 45 deletions
  1. 4 1
      pkg/client.go
  2. 43 44
      pkg/controller.go

+ 4 - 1
pkg/client.go

@@ -19,6 +19,9 @@ func (c *Controller) RetrieveStaticResource(path string) ([]byte, string, error)
 	}
 	req.AddCookie(c.Config.PhpSession)
 	req.AddCookie(c.Config.SsoToken)
+	req.Header.Set("User-Agent", c.Config.UserAgent)
+	req.Header.Set("Referer", "https://www.semrush.com")
+	req.Header.Set("Origin", "https://www.semrush.com")
 	resp, err := c.Client.Do(req)
 	if err != nil {
 		return nil, "", err
@@ -42,13 +45,13 @@ Perform a call against the siteaudit api
 func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader) ([]byte, string, error) {
 	query = strings.ReplaceAll(query, "https://sem.bunnytools.shop", "https://www.semrush.com")
 	url := fmt.Sprintf("https://www.semrush.com%s?%s", path, query)
-	fmt.Println(url)
 	req, err := http.NewRequest(method, url, body)
 	if err != nil {
 		return nil, "", err
 	}
 	req.AddCookie(c.Config.PhpSession)
 	req.AddCookie(c.Config.SsoToken)
+	req.Header.Set("User-Agent", c.Config.UserAgent)
 	req.Header.Set("Referer", "https://www.semrush.com")
 	req.Header.Set("Origin", "https://www.semrush.com")
 	resp, err := c.Client.Do(req)

+ 43 - 44
pkg/controller.go

@@ -13,6 +13,28 @@ import (
 	"golang.org/x/net/publicsuffix"
 )
 
+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/",
+	"/ajst/",
+	"/listing-management/landings/",
+	"/listing-management/landing-reviews/",
+	"/messaging/apps/",
+}
+
+var apiRoutes = [...]string{
+	"/projects/api/",
+	"/siteaudit/api/",
+}
+
 // Implementing a 'set'
 var NonmutableHeaders = map[string]struct{}{
 	"Cookie":          struct{}{},
@@ -58,50 +80,29 @@ func NewController(cfg *HttpServerConfig) *Controller {
 This handler will be responsible for proxying out the GET requests that the server recieves
 */
 func (c *Controller) Get(ctx *gin.Context) {
-	var allowedDomain string
-	if ctx.Param("ProxiedPath") == "/siteaudit/i18n/messages_en.fd3cadbc.json" {
-		data, ctype, err := c.RetrieveStaticResource(ctx.Param("ProxiedPath"))
-		if err != nil {
-			log.Fatal(err, "failed to get site config")
+	incomingPath := ctx.Param("ProxiedPath")
+	for idx := range staticRoutes {
+		if strings.Contains(incomingPath, staticRoutes[idx]) {
+			data, ctype, err := c.RetrieveStaticResource(incomingPath)
+			if err != nil {
+				log.Fatal(err, "failed to get site config")
+			}
+			ctx.Data(200, ctype, data)
+			return
 		}
-		ctx.Data(200, ctype, data)
-		return
-	}
-	if strings.Contains(ctx.Param("ProxiedPath"), "/siteaudit/index/") {
-		data, ctype, err := c.RetrieveStaticResource(ctx.Param("ProxiedPath"))
-		if err != nil {
-			log.Fatal(err, "failed to get site config")
-		}
-		ctx.Data(200, ctype, data)
-		return
 	}
-	if strings.Contains(ctx.Param("ProxiedPath"), "/siteaudit/review/") {
-		data, ctype, err := c.RetrieveStaticResource(ctx.Param("ProxiedPath"))
-		if err != nil {
-			log.Fatal(err, "failed to get site config")
+	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)
+			if err != nil {
+				log.Fatal(err, "failed to get site config")
+			}
+			ctx.Data(200, ctype, data)
+			return
 		}
-		ctx.Data(200, ctype, data)
-		return
-	}
-	if strings.Contains(ctx.Param("ProxiedPath"), "/projects/api/") {
-		data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, ctx.Param("ProxiedPath"), ctx.Request.URL.RawQuery, ctx.Request.Body)
-		if err != nil {
-			log.Fatal(err, "failed to get site config")
-		}
-		ctx.Data(200, ctype, data)
-		return
-	}
-	if strings.Contains(ctx.Param("ProxiedPath"), "/siteaudit/api/") {
-		data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, ctx.Param("ProxiedPath"), ctx.Request.URL.RawQuery, ctx.Request.Body)
-		if err != nil {
-			log.Fatal(err, "failed to get site config")
-		}
-		ctx.Data(200, ctype, data)
-		return
 	}
 
-	allowedDomain = c.Config.AllowedDomain
-	reqUrl := fmt.Sprintf("https://%s%s", allowedDomain, ctx.Param("ProxiedPath"))
+	reqUrl := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, incomingPath)
 	req, err := http.NewRequest(ctx.Request.Method, reqUrl, ctx.Request.Body)
 	if err != nil {
 		ctx.JSON(500, map[string]string{
@@ -110,15 +111,13 @@ func (c *Controller) Get(ctx *gin.Context) {
 		})
 		return
 	}
-	req.URL.Path = ctx.Param("ProxiedPath")
-	cookie, err := ctx.Cookie("csrftoken")
+	req.URL.Path = incomingPath
 	req.Header.Add("User-Agent", c.Config.UserAgent)
-	req.AddCookie(&http.Cookie{Name: "csrftoken", Value: cookie, Path: "/siteaudit", MaxAge: 3600, HttpOnly: true, Secure: true, SameSite: http.SameSiteNoneMode})
 	req.Header.Set("Referer", c.Config.AllowedDomain)
-	if ctx.Param("ProxiedPath") == "/" {
+	if incomingPath == "/" {
 		ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
 	}
-	if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
+	if incomingPath == "/_compatibility/traffic/overview/" {
 		ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
 	}
 	for k, v := range ctx.Request.Header {