|
@@ -30,119 +30,63 @@ import (
|
|
"bytes"
|
|
"bytes"
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
|
|
+ "log"
|
|
"net/http"
|
|
"net/http"
|
|
"strings"
|
|
"strings"
|
|
-
|
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
|
|
|
+ "sync"
|
|
)
|
|
)
|
|
|
|
|
|
-/*
|
|
|
|
-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) {
|
|
|
|
- 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
|
|
|
|
- }
|
|
|
|
- defer resp.Body.Close()
|
|
|
|
- b, err := io.ReadAll(resp.Body)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, "", 500, err
|
|
|
|
- }
|
|
|
|
- altPage := c.pageMod(b)
|
|
|
|
- if resp.StatusCode == 200 {
|
|
|
|
- c.CacheResource(path, NewCachedResource(altPage, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
|
- }
|
|
|
|
- return altPage, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/*
|
|
|
|
-Perform a call against the siteaudit api
|
|
|
|
-
|
|
|
|
- :param path: the URI path with the query
|
|
|
|
- :param query: the query to add to the request
|
|
|
|
- :param body: an io.Reader to push into the request body
|
|
|
|
- :returns a byte array of the response, the content type and an error
|
|
|
|
-*/
|
|
|
|
-func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader, ctx *gin.Context) ([]byte, string, int, error) {
|
|
|
|
-
|
|
|
|
- query = strings.ReplaceAll(query, c.Config.FullProxyDomain, c.Config.FullDomain)
|
|
|
|
- url := fmt.Sprintf("%s%s?%s", c.Config.FullDomain, path, query)
|
|
|
|
- req, err := http.NewRequest(method, url, body)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, "", 500, err
|
|
|
|
- }
|
|
|
|
- c.setHeaders(req, ctx)
|
|
|
|
- resp, err := c.Client.Do(req)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, "", 500, err
|
|
|
|
- }
|
|
|
|
- defer resp.Body.Close()
|
|
|
|
- b, err := io.ReadAll(resp.Body)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, "", 500, err
|
|
|
|
- }
|
|
|
|
- // altPage := c.pageMod(b)
|
|
|
|
- return b, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
/*
|
|
/*
|
|
Generic site call to the semrush site
|
|
Generic site call to the semrush site
|
|
*/
|
|
*/
|
|
-func (c *Controller) SemrushGeneric(ctx *gin.Context) ([]byte, string, int, error) {
|
|
|
|
- path := ctx.Param("ProxiedPath")
|
|
|
|
- method := ctx.Request.Method
|
|
|
|
- query := ctx.Request.URL.RawQuery
|
|
|
|
- body := ctx.Request.Body
|
|
|
|
- var reqUrl string
|
|
|
|
- if query != "" {
|
|
|
|
- reqUrl = fmt.Sprintf("%s%s?%s", c.Config.FullDomain, path, query)
|
|
|
|
- } else {
|
|
|
|
- reqUrl = fmt.Sprintf("%s%s", c.Config.FullDomain, path)
|
|
|
|
- }
|
|
|
|
- fmt.Printf("Request URL: %s\n", reqUrl)
|
|
|
|
|
|
+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)
|
|
|
|
+
|
|
req, err := http.NewRequest(method, reqUrl, body)
|
|
req, err := http.NewRequest(method, reqUrl, body)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, "", 500, err
|
|
|
|
|
|
+ return nil, nil, 500, err
|
|
}
|
|
}
|
|
- c.setHeaders(req, ctx)
|
|
|
|
|
|
+ c.setHeaders(req, hdrs)
|
|
resp, err := c.Client.Do(req)
|
|
resp, err := c.Client.Do(req)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, "", 500, err
|
|
|
|
|
|
+ return nil, nil, 500, err
|
|
}
|
|
}
|
|
defer resp.Body.Close()
|
|
defer resp.Body.Close()
|
|
b, err := io.ReadAll(resp.Body)
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, "", 500, err
|
|
|
|
|
|
+ return nil, nil, 500, err
|
|
}
|
|
}
|
|
|
|
|
|
- for k, v := range resp.Header {
|
|
|
|
- _, ok := NonmutableHeaders[k]
|
|
|
|
- if !ok {
|
|
|
|
- ctx.Header(k, v[0])
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
altPage := c.pageMod(b)
|
|
altPage := c.pageMod(b)
|
|
- if resp.StatusCode == 200 {
|
|
|
|
- if query == "" {
|
|
|
|
|
|
+ if !strings.Contains(path, "?") {
|
|
|
|
+ if resp.StatusCode == 200 {
|
|
if method == "GET" {
|
|
if method == "GET" {
|
|
- c.CacheResource(path, NewCachedResource(altPage, resp.Header.Get("content-type"), resp.StatusCode))
|
|
|
|
|
|
+ c.CacheResource(path, NewCachedResource(altPage, &resp.Header, resp.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- return altPage, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
|
|
+ return altPage, &resp.Header, resp.StatusCode, nil
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, body io.Reader, hosts []string) {
|
|
|
|
+ var wg sync.WaitGroup
|
|
|
|
+ for idx := range hosts {
|
|
|
|
+ wg.Add(1)
|
|
|
|
+ go func(method string, host string, path string, hdrs *http.Header, body io.Reader) {
|
|
|
|
+ defer wg.Done()
|
|
|
|
+ _, _, rcode, err := c.RequestGeneric(method, host, path, hdrs, body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("Fatal Error creating request in a RequestGeneric method: ", err)
|
|
|
|
+ }
|
|
|
|
+ if rcode == 200 {
|
|
|
|
+ basePath := strings.Split(path, "?")[0]
|
|
|
|
+ c.RouteMaps.MapUriToDomain(basePath, host)
|
|
|
|
+ }
|
|
|
|
+ }(method, hosts[idx], path, hdrs, body)
|
|
|
|
+ }
|
|
|
|
+ wg.Wait()
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -151,15 +95,14 @@ Sets the request headers to whatever is defined in this private method
|
|
|
|
|
|
:param req: a pointer to an HTTP request
|
|
:param req: a pointer to an HTTP request
|
|
*/
|
|
*/
|
|
-func (c *Controller) setHeaders(req *http.Request, ctx *gin.Context) {
|
|
|
|
|
|
+func (c *Controller) setHeaders(req *http.Request, hdrs *http.Header) {
|
|
|
|
|
|
req.AddCookie(c.Config.PhpSession)
|
|
req.AddCookie(c.Config.PhpSession)
|
|
req.AddCookie(c.Config.SsoToken)
|
|
req.AddCookie(c.Config.SsoToken)
|
|
req.Header.Set("User-Agent", c.Config.UserAgent)
|
|
req.Header.Set("User-Agent", c.Config.UserAgent)
|
|
req.Header.Set("Referer", c.Config.FullDomain)
|
|
req.Header.Set("Referer", c.Config.FullDomain)
|
|
req.Header.Set("Origin", c.Config.FullDomain)
|
|
req.Header.Set("Origin", c.Config.FullDomain)
|
|
-
|
|
|
|
- for k, v := range ctx.Request.Header {
|
|
|
|
|
|
+ for k, v := range *hdrs {
|
|
_, ok := NonmutableHeaders[k]
|
|
_, ok := NonmutableHeaders[k]
|
|
if !ok {
|
|
if !ok {
|
|
req.Header.Add(k, v[0])
|
|
req.Header.Add(k, v[0])
|
|
@@ -177,7 +120,7 @@ func (c *Controller) pageMod(data []byte) []byte {
|
|
}
|
|
}
|
|
data = bytes.ReplaceAll(data, []byte(c.Config.AllowedDomain), []byte(c.Config.ProxyAddr))
|
|
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(c.Config.AltAllowedDomain), []byte(c.Config.ProxyAddr))
|
|
-
|
|
|
|
|
|
+ data = bytes.ReplaceAll(data, []byte("api-iam.intercom.io"), []byte(c.Config.ProxyAddr))
|
|
return data
|
|
return data
|
|
|
|
|
|
}
|
|
}
|