controller.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package httpserver
  2. import (
  3. "log"
  4. "net/http"
  5. "net/http/cookiejar"
  6. "net/url"
  7. "strings"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/patrickmn/go-cache"
  11. "golang.org/x/net/publicsuffix"
  12. )
  13. // TODO: does tihs need to be a configuration thing? How would i handle doing rewrite rules?
  14. // TODO: make these routes cached on the proxy, so that when a client requests them, they arent being tunneled through to the actual site
  15. var staticRoutes = map[string]struct{}{
  16. "/siteaudit/i18n": struct{}{},
  17. "/siteaudit/index": struct{}{},
  18. "/siteaudit/review": struct{}{},
  19. "/seo-dashboard/release": struct{}{},
  20. "/competitive-list-widget": struct{}{},
  21. "/backlink-audit/landing": struct{}{},
  22. "/link-building-tool/landing": struct{}{},
  23. "/keyword-overview": struct{}{},
  24. "/keyword-gap": struct{}{},
  25. "/oti/prod/organic_traffic_insights": struct{}{},
  26. "/oti/prod/organic-traffic-insights": struct{}{},
  27. "/ajst": struct{}{},
  28. "/listing-management/landings": struct{}{},
  29. "/listing-management/landing-reviews": struct{}{},
  30. "/messaging/apps/": struct{}{},
  31. "/domain-overview": struct{}{},
  32. "/traffic-analytics": struct{}{},
  33. "/organic-research": struct{}{},
  34. "/keyword-magic/kmt_": struct{}{},
  35. "/keyword-manager-assets": struct{}{},
  36. "/position-tracking/landing": struct{}{},
  37. }
  38. var apiRoutes = map[string]struct{}{
  39. "/siteaudit/api/campaigns/seolist": struct{}{},
  40. "/siteaudit/api/system-status": struct{}{},
  41. "/siteaudit/api/limits": struct{}{},
  42. "/projects/api/limits": struct{}{},
  43. }
  44. // Implementing a 'set'
  45. var NonmutableHeaders = map[string]struct{}{
  46. "Cookie": struct{}{},
  47. "User-Agent": struct{}{},
  48. "Accept-Encoding": struct{}{},
  49. "Referer": struct{}{},
  50. "X-Proxy-Url": struct{}{},
  51. "Host": struct{}{},
  52. }
  53. type Controller struct {
  54. Config *HttpServerConfig
  55. RouteMaps map[string]*RouteMapping
  56. Client *http.Client
  57. SiteUrl *url.URL
  58. cache *cache.Cache
  59. }
  60. type ProxyCookies struct {
  61. ck map[*url.URL][]*http.Cookie
  62. }
  63. /*
  64. Returns a new Controller struct to register routes to the gin router
  65. :param cfg: A pointer to an HttpServerConfig struct
  66. */
  67. func NewController(cfg *HttpServerConfig, routeMapping map[string]*RouteMapping) *Controller {
  68. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. sessCookies := cfg.CookieJar
  73. domain, err := url.Parse(cfg.FullDomain)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. jar.SetCookies(domain, sessCookies)
  78. cache := cache.New(24*time.Hour, 10*time.Minute)
  79. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain, cache: cache, RouteMaps: routeMapping}
  80. }
  81. /*
  82. This handler will be responsible for proxying out the GET requests that the server recieves
  83. */
  84. func (c *Controller) Get(ctx *gin.Context) {
  85. incomingPath := ctx.Param("ProxiedPath")
  86. routeSplit := strings.Split(incomingPath, "/")
  87. if len(routeSplit) > 2 {
  88. baseRoute := strings.Join(routeSplit[:len(routeSplit)-1], "/")
  89. _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[baseRoute]
  90. if ok {
  91. data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
  92. if err != nil {
  93. log.Fatal(err, " failed to reroute to the static domain")
  94. }
  95. ctx.Data(rcode, ctype, data)
  96. return
  97. }
  98. }
  99. _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[incomingPath]
  100. if ok {
  101. data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
  102. if err != nil {
  103. log.Fatal(err, " failed to reroute to the static domain")
  104. }
  105. ctx.Data(rcode, ctype, data)
  106. return
  107. }
  108. _, ok = c.RouteMaps[c.Config.AllowedDomain].RouteSet[incomingPath]
  109. if ok {
  110. data, ctype, rcode, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body, ctx)
  111. if err != nil {
  112. log.Fatal(err, " failed to route to the root domain")
  113. }
  114. ctx.Data(rcode, ctype, data)
  115. return
  116. }
  117. data, ctype, rcode, err := c.SemrushGeneric(ctx)
  118. if err != nil {
  119. ctx.JSON(rcode, map[string]string{
  120. "Error": err.Error(),
  121. })
  122. return
  123. }
  124. if incomingPath == "/" {
  125. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
  126. }
  127. if incomingPath == "/_compatibility/traffic/overview/" {
  128. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  129. }
  130. // TODO: this should also honestly be a configuration thing, so that i can extend page modifications
  131. newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  132. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  133. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  134. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  135. newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
  136. newBody = strings.ReplaceAll(newBody, c.Config.AltAllowedDomain, c.Config.ProxyAddr)
  137. ctx.Data(rcode, ctype, []byte(newBody))
  138. }