|
@@ -3,14 +3,46 @@ package httpserver
|
|
import (
|
|
import (
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
|
|
+ "log"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "net/url"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"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 {
|
|
type Controller struct {
|
|
Config *HttpServerConfig
|
|
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
|
|
:param cfg: A pointer to an HttpServerConfig struct
|
|
*/
|
|
*/
|
|
func NewController(cfg *HttpServerConfig) *Controller {
|
|
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.URL.Path = ctx.Param("ProxiedPath")
|
|
req.Header.Add("User-Agent", c.Config.UserAgent)
|
|
req.Header.Add("User-Agent", c.Config.UserAgent)
|
|
req.Header.Set("Referer", c.Config.AllowedDomain)
|
|
req.Header.Set("Referer", c.Config.AllowedDomain)
|
|
- for idx := range c.Config.CookieJar {
|
|
|
|
- req.AddCookie(c.Config.CookieJar[idx])
|
|
|
|
- }
|
|
|
|
if ctx.Param("ProxiedPath") == "/" {
|
|
if ctx.Param("ProxiedPath") == "/" {
|
|
req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/overview/")
|
|
req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/overview/")
|
|
}
|
|
}
|
|
if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
|
|
if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
|
|
req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
|
|
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 {
|
|
if err != nil {
|
|
ctx.JSON(500, map[string]string{
|
|
ctx.JSON(500, map[string]string{
|
|
"Error": "Error creating the request.",
|
|
"Error": "Error creating the request.",
|
|
@@ -66,6 +111,15 @@ func (c *Controller) Get(ctx *gin.Context) {
|
|
})
|
|
})
|
|
return
|
|
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(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, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
|