client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "github.com/gin-gonic/gin"
  8. )
  9. /*
  10. Retrieve the site audit config file from Semrush
  11. returns a byte array of the body, the content type of the resp, and an error
  12. */
  13. func (c *Controller) RetrieveStaticResource(path string, ctx *gin.Context) ([]byte, string, int, error) {
  14. url := fmt.Sprintf("https://static.semrush.com%s", path)
  15. req, err := http.NewRequest("GET", url, nil)
  16. if err != nil {
  17. return nil, "", 500, err
  18. }
  19. c.setHeaders(req, ctx)
  20. resp, err := c.Client.Do(req)
  21. if err != nil {
  22. return nil, "", 500, err
  23. }
  24. defer resp.Body.Close()
  25. b, err := io.ReadAll(resp.Body)
  26. if err != nil {
  27. return nil, "", 500, err
  28. }
  29. return b, resp.Header.Get("content-type"), resp.StatusCode, nil
  30. }
  31. /*
  32. Perform a call against the siteaudit api
  33. :param path: the URI path with the query
  34. :param query: the query to add to the request
  35. :param body: an io.Reader to push into the request body
  36. :returns a byte array of the response, the content type and an error
  37. */
  38. func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader, ctx *gin.Context) ([]byte, string, error) {
  39. query = strings.ReplaceAll(query, c.Config.FullProxyDomain, c.Config.FullDomain)
  40. url := fmt.Sprintf("%s%s?%s", c.Config.FullDomain, path, query)
  41. req, err := http.NewRequest(method, url, body)
  42. if err != nil {
  43. return nil, "", err
  44. }
  45. c.setHeaders(req, ctx)
  46. resp, err := c.Client.Do(req)
  47. if err != nil {
  48. return nil, "", err
  49. }
  50. defer resp.Body.Close()
  51. b, err := io.ReadAll(resp.Body)
  52. if err != nil {
  53. return nil, "", err
  54. }
  55. return b, resp.Header.Get("content-type"), nil
  56. }
  57. /*
  58. Generic site call to the semrush site
  59. */
  60. func (c *Controller) SemrushGeneric(method string, path string, body io.Reader, ctx *gin.Context) ([]byte, string, int, error) {
  61. url := fmt.Sprintf("%s%s", c.Config.FullDomain, path)
  62. req, err := http.NewRequest(method, url, body)
  63. if err != nil {
  64. return nil, "", 500, err
  65. }
  66. c.setHeaders(req, ctx)
  67. resp, err := c.Client.Do(req)
  68. if err != nil {
  69. return nil, "", 500, err
  70. }
  71. defer resp.Body.Close()
  72. b, err := io.ReadAll(resp.Body)
  73. if err != nil {
  74. return nil, "", 500, err
  75. }
  76. for k, v := range resp.Header {
  77. _, ok := NonmutableHeaders[k]
  78. if !ok {
  79. ctx.Header(k, v[0])
  80. }
  81. }
  82. return b, resp.Header.Get("content-type"), resp.StatusCode, nil
  83. }
  84. /*
  85. Sets the request headers to whatever is defined in this private method
  86. :param req: a pointer to an HTTP request
  87. */
  88. func (c *Controller) setHeaders(req *http.Request, ctx *gin.Context) {
  89. req.AddCookie(c.Config.PhpSession)
  90. req.AddCookie(c.Config.SsoToken)
  91. req.Header.Set("User-Agent", c.Config.UserAgent)
  92. req.Header.Set("Referer", c.Config.FullDomain)
  93. req.Header.Set("Origin", c.Config.FullDomain)
  94. for k, v := range ctx.Request.Header {
  95. _, ok := NonmutableHeaders[k]
  96. if !ok {
  97. req.Header.Add(k, v[0])
  98. }
  99. }
  100. }