controller.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 TokenUpdate struct {
  42. Code string `form:"code"`
  43. Content string `form:"content"`
  44. }
  45. type Controller struct {
  46. Config HttpServerConfig
  47. RouteMaps *RouteMap
  48. Client *http.Client
  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.ProxyDomain)
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. jar.SetCookies(domain, sessCookies)
  69. var resCache *cache.Cache
  70. if cfg.Caching {
  71. fmt.Printf("Starting server with resource caching ENABLED.\n")
  72. resCache = cache.New(24*time.Hour, 10*time.Minute)
  73. } else {
  74. fmt.Printf("Starting server with resource caching DISABLED.\n")
  75. resCache = nil
  76. }
  77. 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}
  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. if c.Config.Caching {
  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. ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
  96. return
  97. } else {
  98. fmt.Printf("Cache MISS! For resource URI: %s\n", incomingPath)
  99. }
  100. }
  101. //dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
  102. //if ok { // below, RequestURI() returns the whole URI with the query
  103. resp := c.RequestGeneric(ctx.Request.Method, c.Config.ProxyDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
  104. if resp.Error != nil {
  105. log.Fatal(resp.Error, " failed to route the request: ", incomingPath, " to the target domain: ", ctx.Request.Host, " Error: ", resp.Error)
  106. }
  107. for k, v := range resp.Headers {
  108. _, ok := NonmutableHeaders[k]
  109. if !ok {
  110. ctx.Header(k, v[0])
  111. }
  112. }
  113. ctx.Header("access-control-allow-origin", c.Config.ServerDomain)
  114. ctx.Data(resp.StatusCode, resp.Headers.Get("content-type"), resp.Data)
  115. return
  116. //}
  117. //c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
  118. }