client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. type ResponseCarrier struct {
  31. Data []byte
  32. Headers http.Header
  33. StatusCode int
  34. Error error
  35. }
  36. /*
  37. Generic site call to an upstream server
  38. */
  39. func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ResponseCarrier {
  40. reqUrl := fmt.Sprintf("%s://%s%s", "http", host, path)
  41. req, err := http.NewRequest(method, reqUrl, body)
  42. if err != nil {
  43. return ResponseCarrier{Data: nil, Headers: nil, StatusCode: 500, Error: err}
  44. }
  45. resp, err := c.Client.Do(req)
  46. if err != nil {
  47. return ResponseCarrier{Data: nil, Headers: nil, StatusCode: 500, Error: err}
  48. }
  49. defer resp.Body.Close()
  50. b, err := io.ReadAll(resp.Body)
  51. if err != nil {
  52. return ResponseCarrier{Data: nil, Headers: resp.Header, StatusCode: resp.StatusCode, Error: err}
  53. }
  54. bytes.ReplaceAll(b, []byte(c.Config.TargetDomain), []byte(c.Config.ProxyDomain))
  55. if c.Config.Caching {
  56. if !strings.Contains(path, "?") {
  57. if resp.StatusCode == 200 {
  58. if method == "GET" {
  59. c.CacheResource(path, NewCachedResource(b, &resp.Header, resp.StatusCode))
  60. }
  61. }
  62. }
  63. }
  64. return ResponseCarrier{Data: b, Headers: resp.Header, StatusCode: resp.StatusCode, Error: nil}
  65. }
  66. func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, body io.Reader, hosts []string) {
  67. var wg sync.WaitGroup
  68. for idx := range hosts {
  69. wg.Add(1)
  70. go func(method string, host string, path string, hdrs *http.Header, body io.Reader) {
  71. defer wg.Done()
  72. resp := c.RequestGeneric(method, host, path, hdrs, body)
  73. if resp.Error != nil {
  74. log.Fatal("Fatal Error creating request in a RequestGeneric method: ", resp.Error)
  75. }
  76. if resp.StatusCode == 200 {
  77. basePath := strings.Split(path, "?")[0]
  78. c.RouteMaps.MapUriToDomain(basePath, host)
  79. }
  80. }(method, hosts[idx], path, hdrs, body)
  81. }
  82. wg.Wait()
  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, hdrs *http.Header) {
  89. //req.AddCookie(c.Config.PhpSession)
  90. //req.Header.Set("User-Agent", c.Config.UserAgent)
  91. //req.Header.Set("Referer", c.Config.FullDomain)
  92. //req.Header.Set("Origin", c.Config.FullDomain)
  93. for k, v := range *hdrs {
  94. _, ok := NonmutableHeaders[k]
  95. if !ok {
  96. req.Header.Add(k, v[0])
  97. }
  98. }
  99. }