controller.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // Implementing a 'set'
  12. var NonmutableHeaders = map[string]struct{}{
  13. "Cookie": struct{}{},
  14. "User-Agent": struct{}{},
  15. "Accept-Encoding": struct{}{},
  16. "Referer": struct{}{},
  17. "X-Proxy-Url": struct{}{},
  18. "Host": struct{}{},
  19. }
  20. type Controller struct {
  21. Config *HttpServerConfig
  22. Client *http.Client
  23. }
  24. type ProxyCookies struct {
  25. ck map[*url.URL][]*http.Cookie
  26. }
  27. func (p *ProxyCookies) SetCookies(u *url.URL, cookies []*http.Cookie) {
  28. p.ck[u] = cookies
  29. }
  30. func (p *ProxyCookies) Cookies(u *url.URL) []*http.Cookie {
  31. cookies, ok := p.ck[u]
  32. if !ok {
  33. return []*http.Cookie{
  34. nil,
  35. }
  36. } else {
  37. return cookies
  38. }
  39. }
  40. /*
  41. Returns a new Controller struct to register routes to the gin router
  42. :param cfg: A pointer to an HttpServerConfig struct
  43. */
  44. func NewController(cfg *HttpServerConfig) *Controller {
  45. ckJar := &ProxyCookies{}
  46. for idx := range cfg.CookieJar {
  47. ck := cfg.CookieJar[idx]
  48. url, err := url.Parse(ck.Domain)
  49. if err != nil {
  50. log.Fatal("required cookie could not be patsed: ", ck, err)
  51. }
  52. ckJar.SetCookies(url, ck)
  53. }
  54. return &Controller{Config: cfg, Client: &http.Client{Jar: ckJar}}
  55. }
  56. /*
  57. This handler will be responsible for proxying out the GET requests that the server recieves
  58. */
  59. func (c *Controller) Get(ctx *gin.Context) {
  60. url := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, ctx.Param("ProxiedPath"))
  61. req, err := http.NewRequest(ctx.Request.Method, url, 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. req.Header.Add("User-Agent", c.Config.UserAgent)
  71. req.Header.Set("Referer", c.Config.AllowedDomain)
  72. if ctx.Param("ProxiedPath") == "/" {
  73. req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/overview/")
  74. }
  75. if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
  76. req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  77. }
  78. for k, v := range ctx.Request.Header {
  79. _, ok := NonmutableHeaders[k]
  80. if !ok {
  81. req.Header.Add(k, v[0])
  82. }
  83. }
  84. fmt.Printf("%+v\n", req.Header)
  85. resp, err := c.Client.Do(req)
  86. if err != nil {
  87. ctx.JSON(500, map[string]string{
  88. "Error": "Error creating the request.",
  89. "Msg:": err.Error(),
  90. })
  91. return
  92. }
  93. defer resp.Body.Close()
  94. b, err := io.ReadAll(resp.Body)
  95. if err != nil {
  96. ctx.JSON(500, map[string]string{
  97. "Error": "Error creating the request.",
  98. "Msg:": err.Error(),
  99. })
  100. return
  101. }
  102. responseCookies := resp.Cookies()
  103. for idx := range responseCookies {
  104. url, err := url.Parse(ck.Domain)
  105. if err != nil {
  106. fmt.Printf("required cookie could not be patsed: %+v\n, error: %s\n", ck, err)
  107. }
  108. c.Client.Jar.SetCookies(url, responseCookies[idx])
  109. }
  110. newBody := strings.ReplaceAll(string(b), "\"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, "www.semrush.com", "sem.bunnytools.shop")
  115. ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
  116. }