controller.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "log"
  25. "net/http"
  26. "net/http/cookiejar"
  27. "net/url"
  28. "time"
  29. "github.com/gin-gonic/gin"
  30. "github.com/patrickmn/go-cache"
  31. "golang.org/x/net/publicsuffix"
  32. )
  33. // Implementing a 'set'
  34. var NonmutableHeaders = map[string]struct{}{
  35. "Cookie": struct{}{},
  36. "User-Agent": struct{}{},
  37. "Accept-Encoding": struct{}{},
  38. "Referer": struct{}{},
  39. "X-Proxy-Url": struct{}{},
  40. "Host": struct{}{},
  41. }
  42. type Controller struct {
  43. Config HttpServerConfig
  44. ProxyAddr string
  45. TargetAddr string
  46. RouteMaps *RouteMap
  47. Client *http.Client
  48. cache *cache.Cache
  49. }
  50. type ProxyCookies struct {
  51. ck map[*url.URL][]*http.Cookie
  52. }
  53. /*
  54. Returns a new Controller struct to register routes to the gin router
  55. :param cfg: A pointer to an HttpServerConfig struct
  56. */
  57. func NewController(cfg HttpServerConfig, routeMap *RouteMap) *Controller {
  58. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. sessCookies := cfg.CookieJar
  63. domain, err := url.Parse(cfg.ProxyDomain)
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. jar.SetCookies(domain, sessCookies)
  68. var resCache *cache.Cache
  69. if cfg.Caching {
  70. fmt.Printf("Starting server with resource caching ENABLED.\n")
  71. resCache = cache.New(24*time.Hour, 10*time.Minute)
  72. } else {
  73. fmt.Printf("Starting server with resource caching DISABLED.\n")
  74. resCache = nil
  75. }
  76. return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }}, cache: resCache, RouteMaps: routeMap}
  77. }
  78. /*
  79. This handler will be responsible for proxying out the GET requests that the server recieves
  80. */
  81. func (c *Controller) HandleAny(ctx *gin.Context) {
  82. incomingPath := ctx.Param("ProxiedPath")
  83. if c.Config.Caching {
  84. cacheHit := c.GetResource(incomingPath)
  85. if cacheHit != nil {
  86. for k, v := range *cacheHit.Headers {
  87. _, ok := NonmutableHeaders[k]
  88. if !ok {
  89. for i := range v {
  90. ctx.Header(k, v[i])
  91. }
  92. }
  93. }
  94. ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
  95. return
  96. } else {
  97. fmt.Printf("Cache MISS! For resource URI: %s\n", incomingPath)
  98. }
  99. }
  100. resp := c.RequestGeneric(ctx.Request.Method, c.Config.TargetDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
  101. if resp.Error != nil {
  102. log.Fatal(resp.Error, " failed to route the request: ", incomingPath, " to the target domain: ", ctx.Request.Host, " Error: ", resp.Error)
  103. }
  104. for k, v := range resp.Headers {
  105. // _, ok := NonmutableHeaders[k]
  106. // if !ok {
  107. for i := range v {
  108. //ctx.Header(k, strings.ReplaceAll(v[i], c.Config.TargetDomain, c.Config.ProxyDomain))
  109. ctx.Writer.Header().Add(k, v[i])
  110. }
  111. // }
  112. }
  113. fmt.Printf("%+v\n", resp.Headers)
  114. ctx.Header("access-control-allow-origin", c.Config.ProxyDomain)
  115. ctx.Data(resp.StatusCode, resp.Headers.Get("content-type"), resp.Data)
  116. return
  117. }
  118. func (c *Controller) HandleAnyRe(ctx *gin.Context) {
  119. fmt.Printf("%s\n%s\n%s\n", ctx.Request.Method, ctx.Request.Host, ctx.Request.RequestURI)
  120. resp, err := c.Client.Do(ctx.Request)
  121. if err != nil {
  122. ctx.Data(500, "text/html", []byte("Internal server error"))
  123. return
  124. }
  125. defer resp.Body.Close()
  126. b, err := io.ReadAll(resp.Body)
  127. if err != nil {
  128. ctx.Data(500, "text/html", []byte("Internal server error"))
  129. return
  130. }
  131. for k, v := range resp.Header {
  132. for i := range v {
  133. ctx.Header(k, v[i])
  134. }
  135. }
  136. ctx.Data(resp.StatusCode, resp.Header.Get("content-type"), b)
  137. }