Explorar el Código

working on cookie handling

AETH-erial hace 1 año
padre
commit
88f9e36511
Se han modificado 1 ficheros con 60 adiciones y 6 borrados
  1. 60 6
      pkg/controller.go

+ 60 - 6
pkg/controller.go

@@ -3,14 +3,46 @@ package httpserver
 import (
 	"fmt"
 	"io"
+	"log"
 	"net/http"
+	"net/url"
 	"strings"
 
 	"github.com/gin-gonic/gin"
 )
 
+// 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
+}
+
+type ProxyCookies struct {
+	ck map[*url.URL][]*http.Cookie
+}
+
+func (p *ProxyCookies) SetCookies(u *url.URL, cookies []*http.Cookie) {
+	p.ck[u] = cookies
+}
+
+func (p *ProxyCookies) Cookies(u *url.URL) []*http.Cookie {
+	cookies, ok := p.ck[u]
+	if !ok {
+		return []*http.Cookie{
+			nil,
+		}
+	} else {
+		return cookies
+	}
 }
 
 /*
@@ -19,7 +51,17 @@ 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 {
-	return &Controller{Config: cfg}
+	ckJar := &ProxyCookies{}
+	for idx := range cfg.CookieJar {
+		ck := cfg.CookieJar[idx]
+		url, err := url.Parse(ck.Domain)
+		if err != nil {
+			log.Fatal("required cookie could not be patsed: ", ck, err)
+		}
+		ckJar.SetCookies(url, ck)
+	}
+
+	return &Controller{Config: cfg, Client: &http.Client{Jar: ckJar}}
 }
 
 /*
@@ -39,17 +81,20 @@ func (c *Controller) Get(ctx *gin.Context) {
 	req.URL.Path = ctx.Param("ProxiedPath")
 	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)
+	for k, v := range ctx.Request.Header {
+		_, ok := NonmutableHeaders[k]
+		if !ok {
+			req.Header.Add(k, v[0])
+		}
+	}
+	fmt.Printf("%+v\n", req.Header)
+	resp, err := c.Client.Do(req)
 	if err != nil {
 		ctx.JSON(500, map[string]string{
 			"Error": "Error creating the request.",
@@ -66,6 +111,15 @@ func (c *Controller) Get(ctx *gin.Context) {
 		})
 		return
 	}
+	responseCookies := resp.Cookies()
+	for idx := range responseCookies {
+		url, err := url.Parse(ck.Domain)
+		if err != nil {
+			fmt.Printf("required cookie could not be patsed: %+v\n, error: %s\n", ck, err)
+		}
+
+		c.Client.Jar.SetCookies(url, responseCookies[idx])
+	}
 
 	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;\"")