|
@@ -27,6 +27,7 @@
|
|
|
package httpserver
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"net/http"
|
|
@@ -41,17 +42,13 @@ Retrieve the site audit config file from Semrush
|
|
|
returns a byte array of the body, the content type of the resp, and an error
|
|
|
*/
|
|
|
func (c *Controller) RetrieveStaticResource(method string, path string, ctx *gin.Context) ([]byte, string, int, error) {
|
|
|
- cacheResp := c.GetResource(path)
|
|
|
- if cacheResp != nil {
|
|
|
- return cacheResp.Data, cacheResp.Ctype, cacheResp.Rcode, nil
|
|
|
- }
|
|
|
url := fmt.Sprintf("%s%s", c.Config.FullAltAllowedDomain, path)
|
|
|
req, err := http.NewRequest(method, url, nil)
|
|
|
if err != nil {
|
|
|
return nil, "", 500, err
|
|
|
}
|
|
|
c.setHeaders(req, ctx)
|
|
|
-
|
|
|
+ fmt.Printf("%+v\n", url)
|
|
|
resp, err := c.Client.Do(req)
|
|
|
if err != nil {
|
|
|
return nil, "", 500, err
|
|
@@ -61,10 +58,11 @@ func (c *Controller) RetrieveStaticResource(method string, path string, ctx *gin
|
|
|
if err != nil {
|
|
|
return nil, "", 500, err
|
|
|
}
|
|
|
+ altPage := c.pageMod(b)
|
|
|
if resp.StatusCode == 200 {
|
|
|
- c.CacheResource(path, NewCachedResource(b, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
+ c.CacheResource(path, NewCachedResource(altPage, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
}
|
|
|
- return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
+ return altPage, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
|
|
}
|
|
|
|
|
@@ -94,7 +92,8 @@ func (c *Controller) SiteauditApiCall(method string, path string, query string,
|
|
|
if err != nil {
|
|
|
return nil, "", 500, err
|
|
|
}
|
|
|
- return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
+ // altPage := c.pageMod(b)
|
|
|
+ return b, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
|
|
}
|
|
|
|
|
@@ -112,12 +111,7 @@ func (c *Controller) SemrushGeneric(ctx *gin.Context) ([]byte, string, int, erro
|
|
|
} else {
|
|
|
reqUrl = fmt.Sprintf("%s%s", c.Config.FullDomain, path)
|
|
|
}
|
|
|
- cacheResp := c.GetResource(path)
|
|
|
-
|
|
|
- if cacheResp != nil {
|
|
|
- return cacheResp.Data, cacheResp.Ctype, cacheResp.Rcode, nil
|
|
|
- }
|
|
|
-
|
|
|
+ fmt.Printf("Request URL: %s\n", reqUrl)
|
|
|
req, err := http.NewRequest(method, reqUrl, body)
|
|
|
if err != nil {
|
|
|
return nil, "", 500, err
|
|
@@ -139,15 +133,16 @@ func (c *Controller) SemrushGeneric(ctx *gin.Context) ([]byte, string, int, erro
|
|
|
ctx.Header(k, v[0])
|
|
|
}
|
|
|
}
|
|
|
+ altPage := c.pageMod(b)
|
|
|
if resp.StatusCode == 200 {
|
|
|
if query == "" {
|
|
|
if method == "GET" {
|
|
|
- c.CacheResource(path, NewCachedResource(b, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
+ c.CacheResource(path, NewCachedResource(altPage, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
+ return altPage, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
|
|
}
|
|
|
|
|
@@ -177,12 +172,12 @@ func (c *Controller) setHeaders(req *http.Request, ctx *gin.Context) {
|
|
|
Rewrite all occurences of these values into the response body
|
|
|
*/
|
|
|
func (c *Controller) pageMod(data []byte) []byte {
|
|
|
- newBody := strings.ReplaceAll(string(data), "\"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, c.Config.AllowedDomain, c.Config.ProxyAddr)
|
|
|
- newBody = strings.ReplaceAll(newBody, c.Config.AltAllowedDomain, c.Config.ProxyAddr)
|
|
|
- return []byte(newBody)
|
|
|
+ for idx := range c.PageMods.Content {
|
|
|
+ data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
|
|
|
+ }
|
|
|
+ data = bytes.ReplaceAll(data, []byte(c.Config.AllowedDomain), []byte(c.Config.ProxyAddr))
|
|
|
+ data = bytes.ReplaceAll(data, []byte(c.Config.AltAllowedDomain), []byte(c.Config.ProxyAddr))
|
|
|
+
|
|
|
+ return data
|
|
|
|
|
|
}
|