controller.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type Controller struct {
  10. Config *HttpServerConfig
  11. }
  12. /*
  13. Returns a new Controller struct to register routes to the gin router
  14. :param cfg: A pointer to an HttpServerConfig struct
  15. */
  16. func NewController(cfg *HttpServerConfig) *Controller {
  17. return &Controller{Config: cfg}
  18. }
  19. /*
  20. This handler will be responsible for proxying out the GET requests that the server recieves
  21. */
  22. func (c *Controller) Get(ctx *gin.Context) {
  23. url := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, ctx.Param("ProxiedPath"))
  24. req, err := http.NewRequest(ctx.Request.Method, url, ctx.Request.Body)
  25. if err != nil {
  26. ctx.JSON(500, map[string]string{
  27. "Error": "Error creating the request.",
  28. "Msg:": err.Error(),
  29. })
  30. return
  31. }
  32. req.URL.Path = ctx.Param("ProxiedPath")
  33. req.Header.Add("User-Agent", c.Config.UserAgent)
  34. req.Header.Set("Referer", c.Config.AllowedDomain)
  35. for idx := range c.Config.CookieJar {
  36. req.AddCookie(c.Config.CookieJar[idx])
  37. }
  38. if ctx.Param("ProxiedPath") == "/" {
  39. req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/overview/")
  40. }
  41. if ctx.Param("ProxiedPath") == "/_compatibility/traffic/overview/" {
  42. req.Header.Add("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
  43. }
  44. client := &http.Client{}
  45. resp, err := client.Do(req)
  46. if err != nil {
  47. ctx.JSON(500, map[string]string{
  48. "Error": "Error creating the request.",
  49. "Msg:": err.Error(),
  50. })
  51. return
  52. }
  53. defer resp.Body.Close()
  54. b, err := io.ReadAll(resp.Body)
  55. if err != nil {
  56. ctx.JSON(500, map[string]string{
  57. "Error": "Error creating the request.",
  58. "Msg:": err.Error(),
  59. })
  60. return
  61. }
  62. newBody := strings.ReplaceAll(string(b), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  63. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  64. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  65. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  66. newBody = strings.ReplaceAll(newBody, "www.semrush.com", "sem.bunnytools.shop")
  67. ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
  68. }