controller.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package httpserver
  2. import (
  3. "log"
  4. "net/http"
  5. "net/http/cookiejar"
  6. "net/url"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. "golang.org/x/net/publicsuffix"
  10. )
  11. var staticRoutes = [...]string{
  12. "/siteaudit/i18n/messages_en",
  13. "/siteaudit/index/",
  14. "/siteaudit/review/",
  15. "/seo-dashboard/release/",
  16. "/competitive-list-widget/",
  17. "/backlink-audit/landing/",
  18. "/link-building-tool/landing/",
  19. "/keyword-overview/",
  20. "/keyword-gap/",
  21. "/oti/prod/organic_traffic_insights/",
  22. "/oti/prod/organic-traffic-insights",
  23. "/ajst/",
  24. "/listing-management/landings/",
  25. "/listing-management/landing-reviews/",
  26. "/messaging/apps/",
  27. "/domain-overview/",
  28. "/traffic-analytics/",
  29. "/organic-research/",
  30. "/keyword-magic/kmt_",
  31. "/keyword-manager-assets/",
  32. "/position-tracking/landing/",
  33. }
  34. var apiRoutes = [...]string{
  35. "/projects/api/",
  36. "/siteaudit/api/",
  37. }
  38. // Implementing a 'set'
  39. var NonmutableHeaders = map[string]struct{}{
  40. "Cookie": struct{}{},
  41. "User-Agent": struct{}{},
  42. "Accept-Encoding": struct{}{},
  43. "Referer": struct{}{},
  44. "X-Proxy-Url": struct{}{},
  45. "Host": struct{}{},
  46. }
  47. type Controller struct {
  48. Config *HttpServerConfig
  49. Client *http.Client
  50. SiteUrl *url.URL
  51. }
  52. type ProxyCookies struct {
  53. ck map[*url.URL][]*http.Cookie
  54. }
  55. /*
  56. Returns a new Controller struct to register routes to the gin router
  57. :param cfg: A pointer to an HttpServerConfig struct
  58. */
  59. func NewController(cfg *HttpServerConfig) *Controller {
  60. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. sessCookies := cfg.CookieJar
  65. domain, err := url.Parse(cfg.FullDomain)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. jar.SetCookies(domain, sessCookies)
  70. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain}
  71. }
  72. /*
  73. This handler will be responsible for proxying out the GET requests that the server recieves
  74. */
  75. func (c *Controller) Get(ctx *gin.Context) {
  76. incomingPath := ctx.Param("ProxiedPath")
  77. for idx := range staticRoutes {
  78. if strings.Contains(incomingPath, staticRoutes[idx]) {
  79. data, ctype, rcode, err := c.RetrieveStaticResource(incomingPath, ctx)
  80. if err != nil {
  81. log.Fatal(err, "reroute to the static domain")
  82. }
  83. ctx.Data(rcode, ctype, data)
  84. return
  85. }
  86. }
  87. for idx := range apiRoutes {
  88. if strings.Contains(incomingPath, apiRoutes[idx]) {
  89. data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body, ctx)
  90. if err != nil {
  91. log.Fatal(err, "failed to route to the root domain")
  92. }
  93. ctx.Data(200, ctype, data)
  94. return
  95. }
  96. }
  97. data, ctype, rcode, err := c.SemrushGeneric(ctx.Request.Method, incomingPath, ctx.Request.Body, ctx)
  98. if err != nil {
  99. ctx.JSON(rcode, map[string]string{
  100. "Error": err.Error(),
  101. })
  102. return
  103. }
  104. if incomingPath == "/" {
  105. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
  106. }
  107. if incomingPath == "/_compatibility/traffic/overview/" {
  108. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  109. }
  110. newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  111. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  112. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  113. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  114. newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
  115. newBody = strings.ReplaceAll(newBody, "static.semrush.com", c.Config.ProxyAddr)
  116. ctx.Data(rcode, ctype, []byte(newBody))
  117. }