controller.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  5. Everyone is permitted to copy and distribute verbatim copies
  6. of this license document, but changing it is not allowed.
  7. http-wokou, An HTTP Proxying framework for bypassing DNS Security
  8. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. package httpserver
  21. import (
  22. "log"
  23. "net/http"
  24. "net/http/cookiejar"
  25. "net/url"
  26. "strings"
  27. "time"
  28. "github.com/gin-gonic/gin"
  29. "github.com/patrickmn/go-cache"
  30. "golang.org/x/net/publicsuffix"
  31. )
  32. // Implementing a 'set'
  33. var NonmutableHeaders = map[string]struct{}{
  34. "Cookie": struct{}{},
  35. "User-Agent": struct{}{},
  36. "Accept-Encoding": struct{}{},
  37. "Referer": struct{}{},
  38. "X-Proxy-Url": struct{}{},
  39. "Host": struct{}{},
  40. }
  41. type Controller struct {
  42. Config *HttpServerConfig
  43. RouteMaps map[string]*RouteMapping
  44. Client *http.Client
  45. SiteUrl *url.URL
  46. cache *cache.Cache
  47. }
  48. type ProxyCookies struct {
  49. ck map[*url.URL][]*http.Cookie
  50. }
  51. /*
  52. Returns a new Controller struct to register routes to the gin router
  53. :param cfg: A pointer to an HttpServerConfig struct
  54. */
  55. func NewController(cfg *HttpServerConfig, routeMapping map[string]*RouteMapping) *Controller {
  56. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. sessCookies := cfg.CookieJar
  61. domain, err := url.Parse(cfg.FullDomain)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. jar.SetCookies(domain, sessCookies)
  66. cache := cache.New(24*time.Hour, 10*time.Minute)
  67. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain, cache: cache, RouteMaps: routeMapping}
  68. }
  69. /*
  70. This handler will be responsible for proxying out the GET requests that the server recieves
  71. */
  72. func (c *Controller) Get(ctx *gin.Context) {
  73. incomingPath := ctx.Param("ProxiedPath")
  74. routeSplit := strings.Split(incomingPath, "/")
  75. if len(routeSplit) > 1 {
  76. baseRoute := strings.Join(routeSplit[:len(routeSplit)-1], "/")
  77. _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[baseRoute]
  78. if ok {
  79. data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
  80. if err != nil {
  81. log.Fatal(err, " failed to reroute to the static domain")
  82. }
  83. ctx.Data(rcode, ctype, data)
  84. return
  85. }
  86. }
  87. _, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[incomingPath]
  88. if ok {
  89. data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
  90. if err != nil {
  91. log.Fatal(err, " failed to reroute to the static domain")
  92. }
  93. ctx.Data(rcode, ctype, data)
  94. return
  95. }
  96. _, ok = c.RouteMaps[c.Config.AllowedDomain].RouteSet[incomingPath]
  97. if ok {
  98. data, ctype, rcode, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body, ctx)
  99. if err != nil {
  100. log.Fatal(err, " failed to route to the root domain")
  101. }
  102. ctx.Data(rcode, ctype, data)
  103. return
  104. }
  105. data, ctype, rcode, err := c.SemrushGeneric(ctx)
  106. if err != nil {
  107. ctx.JSON(rcode, map[string]string{
  108. "Error": err.Error(),
  109. })
  110. return
  111. }
  112. if incomingPath == "/" {
  113. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
  114. }
  115. if incomingPath == "/_compatibility/traffic/overview/" {
  116. ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  117. }
  118. // TODO: this should also honestly be a configuration thing, so that i can extend page modifications
  119. newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  120. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  121. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  122. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  123. newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
  124. newBody = strings.ReplaceAll(newBody, c.Config.AltAllowedDomain, c.Config.ProxyAddr)
  125. ctx.Data(rcode, ctype, []byte(newBody))
  126. }