controller.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "log"
  23. "net/http"
  24. "net/http/cookiejar"
  25. "net/url"
  26. "time"
  27. "github.com/gin-gonic/gin"
  28. "github.com/patrickmn/go-cache"
  29. "golang.org/x/net/publicsuffix"
  30. )
  31. // Implementing a 'set'
  32. var NonmutableHeaders = map[string]struct{}{
  33. "Cookie": struct{}{},
  34. "User-Agent": struct{}{},
  35. "Accept-Encoding": struct{}{},
  36. "Referer": struct{}{},
  37. "X-Proxy-Url": struct{}{},
  38. "Host": struct{}{},
  39. }
  40. type Controller struct {
  41. Config *HttpServerConfig
  42. RouteMaps *RouteMap
  43. PageMods *AllPageMods
  44. Client *http.Client
  45. SiteUrl *url.URL
  46. cache *cache.Cache
  47. }
  48. type ProxyCookies struct {
  49. ck map[*url.URL][]*http.Cookie
  50. }
  51. /*
  52. Returns a new Controller struct to register routes to the gin router
  53. :param cfg: A pointer to an HttpServerConfig struct
  54. */
  55. func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
  56. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. sessCookies := cfg.CookieJar
  61. domain, err := url.Parse(cfg.FullDomain)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. pgMod := LoadPageMods(cfg.PageModPath)
  66. jar.SetCookies(domain, sessCookies)
  67. cache := cache.New(24*time.Hour, 10*time.Minute)
  68. return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error {
  69. return http.ErrUseLastResponse
  70. }}, SiteUrl: domain, cache: cache, RouteMaps: routeMap, PageMods: pgMod}
  71. }
  72. /*
  73. This handler will be responsible for proxying out the GET requests that the server recieves
  74. */
  75. func (c *Controller) Get(ctx *gin.Context) {
  76. incomingPath := ctx.Param("ProxiedPath")
  77. cacheHit := c.GetResource(incomingPath)
  78. if cacheHit != nil {
  79. for k, v := range *cacheHit.Headers {
  80. _, ok := NonmutableHeaders[k]
  81. if !ok {
  82. for i := range v {
  83. ctx.Header(k, v[i])
  84. }
  85. }
  86. }
  87. ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
  88. return
  89. }
  90. dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
  91. if ok { // below, RequestURI() returns the whole URI with the query
  92. data, headers, rcode, err := c.RequestGeneric(ctx.Request.Method, dname, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
  93. if err != nil {
  94. log.Fatal(err, " failed to route the request: ", incomingPath, " to the target domain: ", dname, " Error: ", err)
  95. }
  96. for k, v := range *headers {
  97. _, ok := NonmutableHeaders[k]
  98. if !ok {
  99. ctx.Header(k, v[0])
  100. }
  101. }
  102. ctx.Header("access-control-allow-origin", c.Config.FullProxyDomain)
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. ctx.Data(rcode, headers.Get("content-type"), data)
  107. return
  108. }
  109. c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
  110. }