controller.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "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. RouteMaps *RouteMap
  45. PageMods *AllPageMods
  46. Client *http.Client
  47. SiteUrl *url.URL
  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.FullDomain)
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. pgMod := LoadPageMods(cfg.PageModPath)
  68. jar.SetCookies(domain, sessCookies)
  69. cache := cache.New(24*time.Hour, 10*time.Minute)
  70. return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, // CheckRedirect: func(req *http.Request, via []*http.Request) error {return http.ErrUseLastResponse}
  71. SiteUrl: domain, cache: cache, RouteMaps: routeMap, PageMods: pgMod}
  72. }
  73. /*
  74. This handler will be responsible for proxying out the GET requests that the server recieves
  75. */
  76. func (c *Controller) Get(ctx *gin.Context) {
  77. incomingPath := ctx.Param("ProxiedPath")
  78. for idx := range c.Config.Redirects {
  79. if incomingPath == c.Config.Redirects[idx].From {
  80. ctx.Header("Location", c.Config.Redirects[idx].To)
  81. ctx.Status(302)
  82. return
  83. }
  84. }
  85. cacheHit := c.GetResource(incomingPath)
  86. if cacheHit != nil {
  87. for k, v := range *cacheHit.Headers {
  88. _, ok := NonmutableHeaders[k]
  89. if !ok {
  90. for i := range v {
  91. ctx.Header(k, v[i])
  92. }
  93. }
  94. }
  95. _, err := io.Copy(ctx.Writer, bytes.NewReader(cacheHit.Data))
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. // ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
  100. return
  101. }
  102. dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
  103. if ok { // below, RequestURI() returns the whole URI with the query
  104. data, headers, rcode, err := c.RequestGeneric(ctx.Request.Method, dname, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
  105. if err != nil {
  106. log.Fatal(err, " failed to route the request: ", incomingPath, " to the target domain: ", dname, " Error: ", err)
  107. }
  108. for k, v := range *headers {
  109. _, ok := NonmutableHeaders[k]
  110. if !ok {
  111. ctx.Header(k, v[0])
  112. }
  113. }
  114. ctx.Header("access-control-allow-origin", c.Config.FullProxyDomain)
  115. if err != nil {
  116. log.Fatal(err)
  117. }
  118. ctx.Data(rcode, headers.Get("content-type"), data)
  119. return
  120. }
  121. c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
  122. }