123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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
- }
- }
- /*
- 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 {
- 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}}
- }
- /*
- 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.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.",
- "Msg:": err.Error(),
- })
- return
- }
- req.URL.Path = ctx.Param("ProxiedPath")
- req.Header.Add("User-Agent", c.Config.UserAgent)
- req.Header.Set("Referer", c.Config.AllowedDomain)
- 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")
- }
- 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.",
- "Msg:": err.Error(),
- })
- return
- }
- defer resp.Body.Close()
- b, err := io.ReadAll(resp.Body)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Error creating the request.",
- "Msg:": err.Error(),
- })
- 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;\"")
- 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))
- }
|