controller.go 4.5 KB

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