123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package httpserver
- import (
- "fmt"
- "io"
- "log"
- "net/http"
- "net/http/cookiejar"
- "net/url"
- "strings"
- "github.com/gin-gonic/gin"
- "golang.org/x/net/publicsuffix"
- )
- var staticRoutes = [...]string{
- "/siteaudit/i18n/messages_en",
- "/siteaudit/index/",
- "/siteaudit/review/",
- "/seo-dashboard/release/",
- "/competitive-list-widget/",
- "/backlink-audit/landing/",
- "/link-building-tool/landing/",
- "/keyword-overview/",
- "/keyword-gap/",
- "/oti/prod/organic_traffic_insights/",
- "/ajst/",
- "/listing-management/landings/",
- "/listing-management/landing-reviews/",
- "/messaging/apps/",
- "/domain-overview/",
- "/traffic-analytics/",
- "/organic-research/",
- "/keyword-magic/kmt_",
- "/keyword-manager-assets/",
- "/position-tracking/landing/",
- }
- var apiRoutes = [...]string{
- "/projects/api/",
- "/siteaudit/api/",
- }
- // Implementing a 'set'
- var NonmutableHeaders = map[string]struct{}{
- "Cookie": struct{}{},
- "User-Agent": struct{}{},
- "Accept-Encoding": struct{}{},
- "Referer": struct{}{},
- "X-Proxy-Url": struct{}{},
- "Host": struct{}{},
- }
- type Controller struct {
- Config *HttpServerConfig
- Client *http.Client
- SiteUrl *url.URL
- }
- type ProxyCookies struct {
- ck map[*url.URL][]*http.Cookie
- }
- /*
- Returns a new Controller struct to register routes to the gin router
- :param cfg: A pointer to an HttpServerConfig struct
- */
- func NewController(cfg *HttpServerConfig) *Controller {
- jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
- if err != nil {
- log.Fatal(err)
- }
- sessCookies := cfg.CookieJar
- domain, err := url.Parse("https://www.semrush.com")
- if err != nil {
- log.Fatal(err)
- }
- jar.SetCookies(domain, sessCookies)
- return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain}
- }
- /*
- This handler will be responsible for proxying out the GET requests that the server recieves
- */
- func (c *Controller) Get(ctx *gin.Context) {
- incomingPath := ctx.Param("ProxiedPath")
- for idx := range staticRoutes {
- if strings.Contains(incomingPath, staticRoutes[idx]) {
- data, ctype, err := c.RetrieveStaticResource(incomingPath)
- if err != nil {
- log.Fatal(err, "failed to get site config")
- }
- ctx.Data(200, ctype, data)
- return
- }
- }
- for idx := range apiRoutes {
- if strings.Contains(incomingPath, apiRoutes[idx]) {
- data, ctype, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body)
- if err != nil {
- log.Fatal(err, "failed to get site config")
- }
- ctx.Data(200, ctype, data)
- return
- }
- }
- reqUrl := fmt.Sprintf("https://%s%s", c.Config.AllowedDomain, incomingPath)
- req, err := http.NewRequest(ctx.Request.Method, reqUrl, ctx.Request.Body)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Error creating the request.",
- "Msg:": err.Error(),
- })
- return
- }
- req.URL.Path = incomingPath
- req.Header.Add("User-Agent", c.Config.UserAgent)
- req.Header.Set("Referer", c.Config.AllowedDomain)
- if incomingPath == "/" {
- ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
- }
- if incomingPath == "/_compatibility/traffic/overview/" {
- ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
- }
- for k, v := range ctx.Request.Header {
- _, ok := NonmutableHeaders[k]
- if !ok {
- req.Header.Add(k, v[0])
- }
- }
- resp, err := c.Client.Do(req)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Error creating the request.",
- "Msg:": err.Error(),
- })
- return
- }
- defer resp.Body.Close()
- b, err := io.ReadAll(resp.Body)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Error creating the request.",
- "Msg:": err.Error(),
- })
- return
- }
- for k, v := range resp.Header {
- _, ok := NonmutableHeaders[k]
- if !ok {
- ctx.Header(k, v[0])
- }
- }
- newBody := strings.ReplaceAll(string(b), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "www.semrush.com", "sem.bunnytools.shop")
- newBody = strings.ReplaceAll(newBody, "static.semrush.com", "sem.bunnytools.shop")
- ctx.Data(resp.StatusCode, resp.Header.Get("Content-Type"), []byte(newBody))
- }
|