|
@@ -41,7 +41,25 @@ Generic site call to the semrush site
|
|
*/
|
|
*/
|
|
func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ([]byte, *http.Header, int, error) {
|
|
func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ([]byte, *http.Header, int, error) {
|
|
reqUrl := fmt.Sprintf("https://%s%s", host, path)
|
|
reqUrl := fmt.Sprintf("https://%s%s", host, path)
|
|
|
|
+ if method == "POST" {
|
|
|
|
+ req, err := http.NewRequest(method, reqUrl, c.requestBodyRewrites(body))
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, 500, err
|
|
|
|
+ }
|
|
|
|
+ c.setHeaders(req, hdrs)
|
|
|
|
+ resp, err := c.Client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, 500, err
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ b, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, 500, err
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ return c.pageMod(b), &resp.Header, resp.StatusCode, nil
|
|
|
|
+
|
|
|
|
+ }
|
|
req, err := http.NewRequest(method, reqUrl, body)
|
|
req, err := http.NewRequest(method, reqUrl, body)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, 500, err
|
|
return nil, nil, 500, err
|
|
@@ -56,16 +74,16 @@ func (c *Controller) RequestGeneric(method string, host string, path string, hdr
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, 500, err
|
|
return nil, nil, 500, err
|
|
}
|
|
}
|
|
-
|
|
|
|
altPage := c.pageMod(b)
|
|
altPage := c.pageMod(b)
|
|
|
|
+ resp.Header.Set("content-length", string(len(altPage)))
|
|
if !strings.Contains(path, "?") {
|
|
if !strings.Contains(path, "?") {
|
|
if resp.StatusCode == 200 {
|
|
if resp.StatusCode == 200 {
|
|
if method == "GET" {
|
|
if method == "GET" {
|
|
c.CacheResource(path, NewCachedResource(altPage, &resp.Header, resp.StatusCode))
|
|
c.CacheResource(path, NewCachedResource(altPage, &resp.Header, resp.StatusCode))
|
|
|
|
+ fmt.Print("\n")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
return altPage, &resp.Header, resp.StatusCode, nil
|
|
return altPage, &resp.Header, resp.StatusCode, nil
|
|
|
|
|
|
}
|
|
}
|
|
@@ -90,6 +108,24 @@ func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, bod
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/*
|
|
|
|
+perform any request body rewrites as per described in the pagemod config
|
|
|
|
+
|
|
|
|
+ :param data: a byte array to modify
|
|
|
|
+*/
|
|
|
|
+func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
|
|
|
|
+ b, err := io.ReadAll(data)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("couldnt read POST body data: ", err)
|
|
|
|
+ }
|
|
|
|
+ for idx := range c.PageMods.Content {
|
|
|
|
+ if c.PageMods.Content[idx].Target == "body" {
|
|
|
|
+ b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return bytes.NewReader(b)
|
|
|
|
+}
|
|
|
|
+
|
|
/*
|
|
/*
|
|
Sets the request headers to whatever is defined in this private method
|
|
Sets the request headers to whatever is defined in this private method
|
|
|
|
|
|
@@ -116,11 +152,12 @@ Rewrite all occurences of these values into the response body
|
|
*/
|
|
*/
|
|
func (c *Controller) pageMod(data []byte) []byte {
|
|
func (c *Controller) pageMod(data []byte) []byte {
|
|
for idx := range c.PageMods.Content {
|
|
for idx := range c.PageMods.Content {
|
|
- data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
|
|
|
|
|
|
+ if c.PageMods.Content[idx].Target == "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))
|
|
|
|
- data = bytes.ReplaceAll(data, []byte("api-iam.intercom.io"), []byte(c.Config.ProxyAddr))
|
|
|
|
- return data
|
|
|
|
|
|
|
|
|
|
+ return data
|
|
}
|
|
}
|