client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "bytes"
  23. "fmt"
  24. "io"
  25. "log"
  26. "net/http"
  27. "strings"
  28. "sync"
  29. )
  30. /*
  31. Generic site call to the semrush site
  32. */
  33. func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ([]byte, *http.Header, int, error) {
  34. reqUrl := fmt.Sprintf("https://%s%s", host, path)
  35. if method == "POST" {
  36. req, err := http.NewRequest(method, reqUrl, c.requestBodyRewrites(body))
  37. if err != nil {
  38. return nil, nil, 500, err
  39. }
  40. c.setHeaders(req, hdrs)
  41. resp, err := c.Client.Do(req)
  42. if err != nil {
  43. return nil, nil, 500, err
  44. }
  45. defer resp.Body.Close()
  46. b, err := io.ReadAll(resp.Body)
  47. if err != nil {
  48. return nil, nil, 500, err
  49. }
  50. return c.pageMod(b), &resp.Header, resp.StatusCode, nil
  51. }
  52. req, err := http.NewRequest(method, reqUrl, body)
  53. if err != nil {
  54. return nil, nil, 500, err
  55. }
  56. c.setHeaders(req, hdrs)
  57. resp, err := c.Client.Do(req)
  58. if err != nil {
  59. return nil, nil, 500, err
  60. }
  61. defer resp.Body.Close()
  62. b, err := io.ReadAll(resp.Body)
  63. if err != nil {
  64. return nil, nil, 500, err
  65. }
  66. var data []byte
  67. _, ok := c.PageMods.Bypass[path]
  68. if ok {
  69. data = b
  70. } else {
  71. data = c.pageMod(b)
  72. }
  73. if !strings.Contains(path, "?") {
  74. if resp.StatusCode == 200 {
  75. if method == "GET" {
  76. c.CacheResource(path, NewCachedResource(data, &resp.Header, resp.StatusCode))
  77. }
  78. }
  79. }
  80. return data, &resp.Header, resp.StatusCode, nil
  81. }
  82. func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, body io.Reader, hosts []string) {
  83. var wg sync.WaitGroup
  84. for idx := range hosts {
  85. wg.Add(1)
  86. go func(method string, host string, path string, hdrs *http.Header, body io.Reader) {
  87. defer wg.Done()
  88. _, _, rcode, err := c.RequestGeneric(method, host, path, hdrs, body)
  89. if err != nil {
  90. log.Fatal("Fatal Error creating request in a RequestGeneric method: ", err)
  91. }
  92. if rcode == 200 {
  93. basePath := strings.Split(path, "?")[0]
  94. c.RouteMaps.MapUriToDomain(basePath, host)
  95. }
  96. }(method, hosts[idx], path, hdrs, body)
  97. }
  98. wg.Wait()
  99. }
  100. /*
  101. perform any request body rewrites as per described in the pagemod config
  102. :param data: a byte array to modify
  103. */
  104. func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
  105. b, err := io.ReadAll(data)
  106. if err != nil {
  107. log.Fatal("couldnt read POST body data: ", err)
  108. }
  109. for idx := range c.PageMods.Content {
  110. if c.PageMods.Content[idx].Target == "body" {
  111. b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  112. }
  113. }
  114. return bytes.NewReader(b)
  115. }
  116. /*
  117. Sets the request headers to whatever is defined in this private method
  118. :param req: a pointer to an HTTP request
  119. */
  120. func (c *Controller) setHeaders(req *http.Request, hdrs *http.Header) {
  121. req.AddCookie(c.Config.PhpSession)
  122. req.AddCookie(c.Config.SsoToken)
  123. req.Header.Set("User-Agent", c.Config.UserAgent)
  124. req.Header.Set("Referer", c.Config.FullDomain)
  125. req.Header.Set("Origin", c.Config.FullDomain)
  126. for k, v := range *hdrs {
  127. _, ok := NonmutableHeaders[k]
  128. if !ok {
  129. req.Header.Add(k, v[0])
  130. }
  131. }
  132. }
  133. /*
  134. Rewrite all occurences of these values into the response body
  135. */
  136. func (c *Controller) pageMod(data []byte) []byte {
  137. for idx := range c.PageMods.Content {
  138. if c.PageMods.Content[idx].Target == "content" {
  139. data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
  140. }
  141. }
  142. return data
  143. }