controller.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. var staticRoutes = [...]string{
  14. "/siteaudit/i18n/messages_en",
  15. "/siteaudit/index/",
  16. "/siteaudit/review/",
  17. "/seo-dashboard/release/",
  18. "/competitive-list-widget/",
  19. "/backlink-audit/landing/",
  20. "/link-building-tool/landing/",
  21. "/keyword-overview/",
  22. "/keyword-gap/",
  23. "/oti/prod/organic_traffic_insights/",
  24. "/ajst/",
  25. "/listing-management/landings/",
  26. "/listing-management/landing-reviews/",
  27. "/messaging/apps/",
  28. "/domain-overview/",
  29. "/traffic-analytics/",
  30. "/organic-research/",
  31. "/keyword-magic/kmt_",
  32. "/keyword-manager-assets/",
  33. "/position-tracking/landing/",
  34. }
  35. var apiRoutes = [...]string{
  36. "/projects/api/",
  37. "/siteaudit/api/",
  38. }
  39. // Implementing a 'set'
  40. var NonmutableHeaders = map[string]struct{}{
  41. "Cookie": struct{}{},
  42. "User-Agent": struct{}{},
  43. "Accept-Encoding": struct{}{},
  44. "Referer": struct{}{},
  45. "X-Proxy-Url": struct{}{},
  46. "Host": struct{}{},
  47. }
  48. type Controller struct {
  49. Config *HttpServerConfig
  50. Client *http.Client
  51. SiteUrl *url.URL
  52. }
  53. type ProxyCookies struct {
  54. ck map[*url.URL][]*http.Cookie
  55. }
  56. /*
  57. Returns a new Controller struct to register routes to the gin router
  58. :param cfg: A pointer to an HttpServerConfig struct
  59. */
  60. func NewController(cfg *HttpServerConfig) *Controller {
  61. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. sessCookies := cfg.CookieJar
  66. domain, err := url.Parse("https://www.semrush.com")
  67. if err != nil {
  68. log.Fatal(err)
  69. }
  70. jar.SetCookies(domain, sessCookies)
  71. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain}
  72. }
  73. /*
  74. This handler will be responsible for proxying out the GET requests that the server recieves
  75. */
  76. func (c *Controller) Get(ctx *gin.Context) {
  77. incomingPath := ctx.Param("ProxiedPath")
  78. for idx := range staticRoutes {
  79. if strings.Contains(incomingPath, staticRoutes[idx]) {
  80. data, ctype, err := c.RetrieveStaticResource(incomingPath)
  81. if err != nil {
  82. log.Fatal(err, "failed to get site config")
  83. }
  84. ctx.Data(200, ctype, data)
  85. return
  86. }
  87. }
  88. for idx := range apiRoutes {
  89. if strings.Contains(incomingPath, apiRoutes[idx]) {
  90. data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body)
  91. if err != nil {
  92. log.Fatal(err, "failed to get site config")
  93. }
  94. ctx.Data(200, ctype, data)
  95. return
  96. }
  97. }
  98. reqUrl := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, incomingPath)
  99. req, err := http.NewRequest(ctx.Request.Method, reqUrl, ctx.Request.Body)
  100. if err != nil {
  101. ctx.JSON(500, map[string]string{
  102. "Error": "Error creating the request.",
  103. "Msg:": err.Error(),
  104. })
  105. return
  106. }
  107. req.URL.Path = incomingPath
  108. req.Header.Add("User-Agent", c.Config.UserAgent)
  109. req.Header.Set("Referer", c.Config.AllowedDomain)
  110. if incomingPath == "/" {
  111. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
  112. }
  113. if incomingPath == "/_compatibility/traffic/overview/" {
  114. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  115. }
  116. for k, v := range ctx.Request.Header {
  117. _, ok := NonmutableHeaders[k]
  118. if !ok {
  119. req.Header.Add(k, v[0])
  120. }
  121. }
  122. resp, err := c.Client.Do(req)
  123. if err != nil {
  124. ctx.JSON(500, map[string]string{
  125. "Error": "Error creating the request.",
  126. "Msg:": err.Error(),
  127. })
  128. return
  129. }
  130. defer resp.Body.Close()
  131. b, err := io.ReadAll(resp.Body)
  132. if err != nil {
  133. ctx.JSON(500, map[string]string{
  134. "Error": "Error creating the request.",
  135. "Msg:": err.Error(),
  136. })
  137. return
  138. }
  139. for k, v := range resp.Header {
  140. _, ok := NonmutableHeaders[k]
  141. if !ok {
  142. ctx.Header(k, v[0])
  143. }
  144. }
  145. newBody := strings.ReplaceAll(string(b), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  146. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  147. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  148. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  149. newBody = strings.ReplaceAll(newBody, "www.semrush.com", "sem.bunnytools.shop")
  150. newBody = strings.ReplaceAll(newBody, "static.semrush.com", "sem.bunnytools.shop")
  151. ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
  152. }