client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  5. Everyone is permitted to copy and distribute verbatim copies
  6. of this license document, but changing it is not allowed.
  7. http-wokou, An HTTP Proxying framework for bypassing DNS Security
  8. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. package httpserver
  21. import (
  22. "fmt"
  23. "io"
  24. "net/http"
  25. "strings"
  26. "github.com/gin-gonic/gin"
  27. )
  28. /*
  29. Retrieve the site audit config file from Semrush
  30. returns a byte array of the body, the content type of the resp, and an error
  31. */
  32. func (c *Controller) RetrieveStaticResource(method string, path string, ctx *gin.Context) ([]byte, string, int, error) {
  33. cacheResp := c.GetResource(path)
  34. if cacheResp != nil {
  35. return cacheResp.Data, cacheResp.Ctype, cacheResp.Rcode, nil
  36. }
  37. url := fmt.Sprintf("%s%s", c.Config.FullAltAllowedDomain, path)
  38. req, err := http.NewRequest(method, url, nil)
  39. if err != nil {
  40. return nil, "", 500, err
  41. }
  42. c.setHeaders(req, ctx)
  43. resp, err := c.Client.Do(req)
  44. if err != nil {
  45. return nil, "", 500, err
  46. }
  47. defer resp.Body.Close()
  48. b, err := io.ReadAll(resp.Body)
  49. if err != nil {
  50. return nil, "", 500, err
  51. }
  52. if resp.StatusCode == 200 {
  53. c.CacheResource(path, NewCachedResource(b, resp.Header.Get("content-type"), resp.StatusCode))
  54. }
  55. return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
  56. }
  57. /*
  58. Perform a call against the siteaudit api
  59. :param path: the URI path with the query
  60. :param query: the query to add to the request
  61. :param body: an io.Reader to push into the request body
  62. :returns a byte array of the response, the content type and an error
  63. */
  64. func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader, ctx *gin.Context) ([]byte, string, int, error) {
  65. query = strings.ReplaceAll(query, c.Config.FullProxyDomain, c.Config.FullDomain)
  66. url := fmt.Sprintf("%s%s?%s", c.Config.FullDomain, path, query)
  67. req, err := http.NewRequest(method, url, body)
  68. if err != nil {
  69. return nil, "", 500, err
  70. }
  71. c.setHeaders(req, ctx)
  72. resp, err := c.Client.Do(req)
  73. if err != nil {
  74. return nil, "", 500, err
  75. }
  76. defer resp.Body.Close()
  77. b, err := io.ReadAll(resp.Body)
  78. if err != nil {
  79. return nil, "", 500, err
  80. }
  81. return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
  82. }
  83. /*
  84. Generic site call to the semrush site
  85. */
  86. func (c *Controller) SemrushGeneric(ctx *gin.Context) ([]byte, string, int, error) {
  87. path := ctx.Param("ProxiedPath")
  88. method := ctx.Request.Method
  89. query := ctx.Request.URL.RawQuery
  90. body := ctx.Request.Body
  91. var reqUrl string
  92. if query != "" {
  93. reqUrl = fmt.Sprintf("%s%s?%s", c.Config.FullDomain, path, query)
  94. } else {
  95. reqUrl = fmt.Sprintf("%s%s", c.Config.FullDomain, path)
  96. }
  97. cacheResp := c.GetResource(path)
  98. if cacheResp != nil {
  99. return cacheResp.Data, cacheResp.Ctype, cacheResp.Rcode, nil
  100. }
  101. req, err := http.NewRequest(method, reqUrl, body)
  102. if err != nil {
  103. return nil, "", 500, err
  104. }
  105. c.setHeaders(req, ctx)
  106. resp, err := c.Client.Do(req)
  107. if err != nil {
  108. return nil, "", 500, err
  109. }
  110. defer resp.Body.Close()
  111. b, err := io.ReadAll(resp.Body)
  112. if err != nil {
  113. return nil, "", 500, err
  114. }
  115. for k, v := range resp.Header {
  116. _, ok := NonmutableHeaders[k]
  117. if !ok {
  118. ctx.Header(k, v[0])
  119. }
  120. }
  121. if resp.StatusCode == 200 {
  122. if query == "" {
  123. if method == "GET" {
  124. c.CacheResource(path, NewCachedResource(b, resp.Header.Get("content-type"), resp.StatusCode))
  125. }
  126. }
  127. }
  128. return c.pageMod(b), resp.Header.Get("content-type"), resp.StatusCode, nil
  129. }
  130. /*
  131. Sets the request headers to whatever is defined in this private method
  132. :param req: a pointer to an HTTP request
  133. */
  134. func (c *Controller) setHeaders(req *http.Request, ctx *gin.Context) {
  135. req.AddCookie(c.Config.PhpSession)
  136. req.AddCookie(c.Config.SsoToken)
  137. req.Header.Set("User-Agent", c.Config.UserAgent)
  138. req.Header.Set("Referer", c.Config.FullDomain)
  139. req.Header.Set("Origin", c.Config.FullDomain)
  140. for k, v := range ctx.Request.Header {
  141. _, ok := NonmutableHeaders[k]
  142. if !ok {
  143. req.Header.Add(k, v[0])
  144. }
  145. }
  146. }
  147. /*
  148. Rewrite all occurences of these values into the response body
  149. */
  150. func (c *Controller) pageMod(data []byte) []byte {
  151. newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
  152. newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
  153. newBody = strings.ReplaceAll(newBody, "<footer", "<footer style=\"display:none;\"")
  154. newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
  155. newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
  156. newBody = strings.ReplaceAll(newBody, c.Config.AltAllowedDomain, c.Config.ProxyAddr)
  157. return []byte(newBody)
  158. }