|
@@ -5,33 +5,33 @@ import (
|
|
|
"io"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
)
|
|
|
|
|
|
/*
|
|
|
- Retrieve the site audit config file from Semrush
|
|
|
- returns a byte array of the body, the content type of the resp, and an error
|
|
|
+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(path string) ([]byte, string, error) {
|
|
|
+func (c *Controller) RetrieveStaticResource(path string, ctx *gin.Context) ([]byte, string, int, error) {
|
|
|
url := fmt.Sprintf("https://static.semrush.com%s", path)
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
if err != nil {
|
|
|
- return nil, "", err
|
|
|
+ return nil, "", 500, err
|
|
|
}
|
|
|
- req.AddCookie(c.Config.PhpSession)
|
|
|
- req.AddCookie(c.Config.SsoToken)
|
|
|
- req.Header.Set("User-Agent", c.Config.UserAgent)
|
|
|
- req.Header.Set("Referer", "https://www.semrush.com")
|
|
|
- req.Header.Set("Origin", "https://www.semrush.com")
|
|
|
+ c.setHeaders(req, ctx)
|
|
|
+
|
|
|
resp, err := c.Client.Do(req)
|
|
|
if err != nil {
|
|
|
- return nil, "", err
|
|
|
+ return nil, "", 500, err
|
|
|
}
|
|
|
defer resp.Body.Close()
|
|
|
b, err := io.ReadAll(resp.Body)
|
|
|
if err != nil {
|
|
|
- return nil, "", err
|
|
|
+ return nil, "", 500, err
|
|
|
}
|
|
|
- return b, resp.Header.Get("content-type"), nil
|
|
|
+ return b, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
|
|
|
}
|
|
|
|
|
@@ -40,20 +40,17 @@ 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) ([]byte, string, error) {
|
|
|
- query = strings.ReplaceAll(query, "https://sem.bunnytools.shop", "https://www.semrush.com")
|
|
|
- url := fmt.Sprintf("https://www.semrush.com%s?%s", path, query)
|
|
|
+func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader, ctx *gin.Context) ([]byte, string, 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, "", err
|
|
|
}
|
|
|
- req.AddCookie(c.Config.PhpSession)
|
|
|
- req.AddCookie(c.Config.SsoToken)
|
|
|
- req.Header.Set("User-Agent", c.Config.UserAgent)
|
|
|
- req.Header.Set("Referer", "https://www.semrush.com")
|
|
|
- req.Header.Set("Origin", "https://www.semrush.com")
|
|
|
+ c.setHeaders(req, ctx)
|
|
|
resp, err := c.Client.Do(req)
|
|
|
if err != nil {
|
|
|
return nil, "", err
|
|
@@ -70,6 +67,53 @@ func (c *Controller) SiteauditApiCall(method string, path string, query string,
|
|
|
/*
|
|
|
Generic site call to the semrush site
|
|
|
*/
|
|
|
-func (c *Controller) SemrushGeneric() ([]byte, string, error) {
|
|
|
- // TODO: make this working lol
|
|
|
+func (c *Controller) SemrushGeneric(method string, path string, body io.Reader, ctx *gin.Context) ([]byte, string, int, error) {
|
|
|
+
|
|
|
+ url := fmt.Sprintf("%s%s", c.Config.FullDomain, path)
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v := range resp.Header {
|
|
|
+ _, ok := NonmutableHeaders[k]
|
|
|
+ if !ok {
|
|
|
+ ctx.Header(k, v[0])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return b, resp.Header.Get("content-type"), resp.StatusCode, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+Sets the request headers to whatever is defined in this private method
|
|
|
+
|
|
|
+ :param req: a pointer to an HTTP request
|
|
|
+*/
|
|
|
+func (c *Controller) setHeaders(req *http.Request, ctx *gin.Context) {
|
|
|
+
|
|
|
+ req.AddCookie(c.Config.PhpSession)
|
|
|
+ req.AddCookie(c.Config.SsoToken)
|
|
|
+ req.Header.Set("User-Agent", c.Config.UserAgent)
|
|
|
+ req.Header.Set("Referer", c.Config.FullDomain)
|
|
|
+ req.Header.Set("Origin", c.Config.FullDomain)
|
|
|
+
|
|
|
+ for k, v := range ctx.Request.Header {
|
|
|
+ _, ok := NonmutableHeaders[k]
|
|
|
+ if !ok {
|
|
|
+ req.Header.Add(k, v[0])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|