controller.go 4.7 KB

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