controller.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "net/http/cookiejar"
  8. "net/url"
  9. "strings"
  10. "github.com/gin-gonic/gin"
  11. "golang.org/x/net/publicsuffix"
  12. )
  13. // Implementing a 'set'
  14. var NonmutableHeaders = map[string]struct{}{
  15. "Cookie": struct{}{},
  16. "User-Agent": struct{}{},
  17. "Accept-Encoding": struct{}{},
  18. "Referer": struct{}{},
  19. "X-Proxy-Url": struct{}{},
  20. "Host": struct{}{},
  21. }
  22. type Controller struct {
  23. Config *HttpServerConfig
  24. Client *http.Client
  25. SiteUrl *url.URL
  26. }
  27. type ProxyCookies struct {
  28. ck map[*url.URL][]*http.Cookie
  29. }
  30. /*
  31. Returns a new Controller struct to register routes to the gin router
  32. :param cfg: A pointer to an HttpServerConfig struct
  33. */
  34. func NewController(cfg *HttpServerConfig) *Controller {
  35. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. sessCookies := cfg.CookieJar
  40. domain, err := url.Parse("https://www.semrush.com")
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. jar.SetCookies(domain, sessCookies)
  45. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain}
  46. }
  47. /*
  48. This handler will be responsible for proxying out the GET requests that the server recieves
  49. */
  50. func (c *Controller) Get(ctx *gin.Context) {
  51. var allowedDomain string
  52. // if ctx.Param("ProxiedPath") == "/siteaudit/i18n/messages_en.fd3cadbc.json" {
  53. // allowedDomain = "static.semrush.com"
  54. // }
  55. //if strings.Contains(ctx.Param("ProxiedPath"), "/siteaudit/api") {
  56. // allowedDomain = "static.semrush.com"
  57. // } else {
  58. allowedDomain = c.Config.AllowedDomain
  59. // }
  60. reqUrl := fmt.Sprintf("https://%s%s", allowedDomain, ctx.Param("ProxiedPath"))
  61. req, err := http.NewRequest(ctx.Request.Method, reqUrl, ctx.Request.Body)
  62. if err != nil {
  63. ctx.JSON(500, map[string]string{
  64. "Error": "Error creating the request.",
  65. "Msg:": err.Error(),
  66. })
  67. return
  68. }
  69. req.URL.Path = ctx.Param("ProxiedPath")
  70. cookie, err := ctx.Cookie("csrftoken")
  71. fmt.Printf("------------- INBOUND COOKIE --------\n%+v\n%s", cookie, err)
  72. req.Header.Add("User-Agent", c.Config.UserAgent)
  73. req.AddCookie(&http.Cookie{Name: "csrftoken", Value: cookie, Path: "/siteaudit", MaxAge: 3600, HttpOnly: true, Secure: true, SameSite: http.SameSiteNoneMode})
  74. if strings.Split(ctx.Param("ProxiedPath"), "?")[0] == "/siteaudit/api/campaigns/seolist" {
  75. req.Header.Set("Referer", fmt.Sprintf("https://%s/siteaudit/", c.Config.AllowedDomain))
  76. req.Header.Set("referrer", fmt.Sprintf("https://%s/siteaudit/", c.Config.AllowedDomain))
  77. } else {
  78. req.Header.Set("Referer", c.Config.AllowedDomain)
  79. }
  80. if ctx.Param("ProxiedPath") == "/" {
  81. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
  82. }
  83. if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
  84. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  85. }
  86. for k, v := range ctx.Request.Header {
  87. _, ok := NonmutableHeaders[k]
  88. if !ok {
  89. req.Header.Add(k, v[0])
  90. }
  91. }
  92. resp, err := c.Client.Do(req)
  93. if err != nil {
  94. ctx.JSON(500, map[string]string{
  95. "Error": "Error creating the request.",
  96. "Msg:": err.Error(),
  97. })
  98. return
  99. }
  100. defer resp.Body.Close()
  101. b, err := io.ReadAll(resp.Body)
  102. if err != nil {
  103. ctx.JSON(500, map[string]string{
  104. "Error": "Error creating the request.",
  105. "Msg:": err.Error(),
  106. })
  107. return
  108. }
  109. for k, v := range resp.Header {
  110. _, ok := NonmutableHeaders[k]
  111. if !ok {
  112. ctx.Header(k, v[0])
  113. }
  114. }
  115. fmt.Printf("----------------- HEADERS GOING OUT ---------\n%+v\n", req.Header)
  116. fmt.Printf("-------------- RESPONSE HEADERS ------------\n%+v\n", resp.Header)
  117. fmt.Printf("-------------- COOKIES GOING OUT ------------ \n%+v\n", req.Cookies())
  118. newBody := strings.ReplaceAll(string(b), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  119. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  120. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  121. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  122. newBody = strings.ReplaceAll(newBody, "www.semrush.com", "sem.bunnytools.shop")
  123. // newBody = strings.ReplaceAll(newBody, "static.semrush.com", "sem.bunnytools.shop")
  124. ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
  125. }