Explorar o código

proxy works for the most part, not sure really what the client needs it to do.

AETH-erial hai 1 ano
pai
achega
a0f91ae685
Modificáronse 6 ficheiros con 70 adicións e 35 borrados
  1. 3 2
      .gitignore
  2. 1 0
      go.mod
  3. 2 0
      go.sum
  4. 21 24
      pkg/controller.go
  5. 37 8
      pkg/include.go
  6. 6 1
      pkg/routing.go

+ 3 - 2
.gitignore

@@ -23,7 +23,7 @@ _testmain.go
 *.exe
 *.test
 *.prof
-
+build/*
 
 # ignoring the PHP scripts the client provided me.
 inc/
@@ -35,6 +35,7 @@ inc/**
 # ignoring real config files
 .config.*
 
-
+# cookies.json file
+cookies.json
 
 

+ 1 - 0
go.mod

@@ -19,6 +19,7 @@ require (
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.8 // indirect
+	github.com/pinecat/netcookiejar v0.3.0 // indirect
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
 	golang.org/x/arch v0.3.0 // indirect

+ 2 - 0
go.sum

@@ -39,6 +39,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
 github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
 github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
 github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
+github.com/pinecat/netcookiejar v0.3.0 h1:jdwHpDp+bI+YT7HSV7BnlnF6CwUnBNP7jVmOmSopr8s=
+github.com/pinecat/netcookiejar v0.3.0/go.mod h1:Op1h9WZAmwHgxih26Uy35IgKy6Ivsxf7zy632+bNy74=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=

+ 21 - 24
pkg/controller.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"io"
 	"net/http"
+	"strings"
 
 	"github.com/gin-gonic/gin"
 )
@@ -25,8 +26,9 @@ 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) {
-	url := fmt.Sprintf("https://%s%s", c.Config.RevProxySite, ctx.Param("ProxiedPath"))
-	req, err := http.NewRequest("GET", url, nil)
+
+	url := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, ctx.Param("ProxiedPath"))
+	req, err := http.NewRequest(ctx.Request.Method, url, ctx.Request.Body)
 	if err != nil {
 		ctx.JSON(500, map[string]string{
 			"Error": "Error creating the request.",
@@ -35,9 +37,17 @@ func (c *Controller) Get(ctx *gin.Context) {
 		return
 	}
 	req.URL.Path = ctx.Param("ProxiedPath")
-	req.Header.Add("user-agent", c.Config.UserAgent)
-	req.Header.Add("Referer", c.Config.AllowedDomain)
-	// fmt.Printf("%+v\n", req)
+	req.Header.Add("User-Agent", c.Config.UserAgent)
+	req.Header.Set("Referer", c.Config.AllowedDomain)
+	for idx := range c.Config.CookieJar {
+		req.AddCookie(c.Config.CookieJar[idx])
+	}
+	if ctx.Param("ProxiedPath") == "/" {
+		req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/overview/")
+	}
+	if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
+		req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
+	}
 	client := &http.Client{}
 	resp, err := client.Do(req)
 	if err != nil {
@@ -56,25 +66,12 @@ func (c *Controller) Get(ctx *gin.Context) {
 		})
 		return
 	}
-	ctx.Data(200, "text/html", b)
 
-}
-
-/*
-This handler will be responsible for proxying any POST requests
-*/
-func (c *Controller) Post(ctx *gin.Context) {
-	url := fmt.Sprintf("%s/%s", c.Config.RevProxySite, ctx.FullPath())
-	req, err := http.NewRequest("POST", url, ctx.Request.Body)
-	if err != nil {
-		ctx.JSON(500, map[string]string{
-			"Error": "Error creating the request.",
-			"Msg:":  err.Error(),
-		})
-		return
-	}
-	req.Header.Add("user-agent", c.Config.UserAgent)
-	req.Header.Add("Referer", c.Config.AllowedDomain)
-	fmt.Printf("%+v\n", req)
+	newBody := strings.ReplaceAll(string(b), "\"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, "www.semrush.com", "sem.bunnytools.shop")
+	ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
 
 }

+ 37 - 8
pkg/include.go

@@ -2,6 +2,7 @@ package httpserver
 
 import (
 	"encoding/json"
+	"net/http"
 	"os"
 )
 
@@ -11,19 +12,26 @@ type HttpServerConfig struct {
 	AllowedDomain string `json:"allowed_domain"`
 	UserAgent     string `json:"user_agent"`
 	UseSsl        bool   `json:"use_ssl"`
-	RevProxySite  string `json:"rev_proxy_site"`
+	ProxyAddr     string `json:"proxy_addr"`
+	CookieJar     []*http.Cookie
 }
 
 type Cookie struct {
-	Name     string `json:"name"`
-	Value    string `json:"value"`
-	MaxAge   int    `json:"max_age"`
-	Path     string `json:"path"`
-	Domain   string `json:"domain"`
-	Secure   bool   `json:"secure"`
-	HttpOnly bool   `json:"http_only"`
+	Name       string `json:"name"`
+	Value      string `json:"value"`
+	MaxAge     int    `json:"max_age"`
+	Path       string `json:"path"`
+	Domain     string `json:"domain"`
+	Secure     bool   `json:"secure"`
+	IncludeSub bool   `json:"include_sub"`
 }
 
+/*
+Reads the server configuration file, along with the cookie file so that the correlated account can be
+accessed through the proxy
+
+	:param loc: the location of the config file
+*/
 func ReadConfig(loc string) (*HttpServerConfig, error) {
 
 	f, err := os.ReadFile(loc)
@@ -35,6 +43,27 @@ func ReadConfig(loc string) (*HttpServerConfig, error) {
 	if err != nil {
 		return nil, err
 	}
+	cf, err := os.ReadFile("./cookies.json")
+	if err != nil {
+		return nil, err
+	}
+	var cookies []Cookie
+	err = json.Unmarshal(cf, &cookies)
+	if err != nil {
+		return nil, err
+	}
+	for idx := range cookies {
+		httpCookie := &http.Cookie{
+			Domain: cookies[idx].Domain,
+			MaxAge: cookies[idx].MaxAge,
+			Name:   cookies[idx].Name,
+			Value:  cookies[idx].Value,
+			Path:   cookies[idx].Path,
+			Secure: cookies[idx].Secure,
+		}
+		cfg.CookieJar = append(cfg.CookieJar, httpCookie)
+
+	}
 
 	return &cfg, err
 

+ 6 - 1
pkg/routing.go

@@ -2,10 +2,15 @@ package httpserver
 
 import "github.com/gin-gonic/gin"
 
+/*
+Registers the exposed route to utilize the handler function
+
+	:param e: pointer to a gin.Engine struct
+	:param cfg: pointer to an HttpServerConfig struct
+*/
 func RegisterRoutes(e *gin.Engine, cfg *HttpServerConfig) {
 	c := NewController(cfg)
 	web := e.Group("")
-	//	web.POST("/", c.Post)
 	web.Any("/*ProxiedPath", c.Get)
 
 }