include.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "encoding/json"
  23. "fmt"
  24. "log"
  25. "net/http"
  26. "os"
  27. "strings"
  28. "time"
  29. "github.com/patrickmn/go-cache"
  30. )
  31. type HttpServerConfig struct {
  32. HttpPort int `json:"http_port"`
  33. HttpsPort int `json:"https_port"`
  34. AllowedDomain string `json:"allowed_domain"`
  35. FullDomain string // The domain name with the protocol before it
  36. AltAllowedDomain string `json:"alt_allowed_domain"` // alternate domain that resources are sourced from
  37. FullAltAllowedDomain string // the alt domain with the protocol
  38. Proto string `json:"proto"` // http/https
  39. UserAgent string `json:"user_agent"`
  40. UseSsl bool `json:"use_ssl"`
  41. ProxyAddr string `json:"proxy_addr"`
  42. RouteMapPath string `json:"route_map_path"`
  43. PageModPath string `json:"page_mod_path"`
  44. CookieFile string `json:"cookie_file"`
  45. FullProxyDomain string // the domain name of the proxied site with the protocol
  46. KnownHosts []string `json:"known_hosts"`
  47. CorsHosts []string `json:"cors_hosts"`
  48. Redirects []*RedirectRule `json:"redirects"`
  49. CookieJar []*http.Cookie
  50. PhpSession *http.Cookie
  51. SsoToken *http.Cookie
  52. }
  53. type RedirectRule struct {
  54. From string `json:"from"`
  55. To string `json:"to"`
  56. }
  57. type Cookie struct {
  58. Name string `json:"name"`
  59. Value string `json:"value"`
  60. MaxAge int `json:"max_age"`
  61. Path string `json:"path"`
  62. Domain string `json:"domain"`
  63. Secure bool `json:"secure"`
  64. IncludeSub bool `json:"include_sub"`
  65. }
  66. type RouteMapping struct {
  67. DomainName string `json:"domain_name"`
  68. UriPaths []string `json:"uri_paths"`
  69. RouteSet map[string]struct{}
  70. }
  71. type RouteMap struct {
  72. Mappings map[string]string `json:"mappings"`
  73. Shotgun map[string]string `json:"shotgun"`
  74. MapCache *cache.Cache
  75. }
  76. type RouteMapper interface {
  77. mapUriToDomain(string, string)
  78. GetMappedDomain(string) (string, bool)
  79. ExportRouteMaps(string)
  80. }
  81. /*
  82. Set a route to exist for the URI to the specific domain
  83. :param uri: the URI to set the route for
  84. :param domain: the domain name to resolve the uri to
  85. */
  86. func (r *RouteMap) MapUriToDomain(uri string, domain string) {
  87. r.MapCache.Set(uri, domain, cache.DefaultExpiration)
  88. }
  89. // returns the domain/url that the uri belongs to as defined in the routemap
  90. func (r *RouteMap) GetMappedDomain(uri string) (string, bool) {
  91. dname, ok := r.MapCache.Get(uri)
  92. if ok {
  93. return fmt.Sprint(dname), true
  94. }
  95. for k, v := range r.Shotgun {
  96. if strings.Contains(uri, k) {
  97. return v, true
  98. }
  99. }
  100. return "", false
  101. }
  102. // This populates the cache in a RouteMap with the data from the config file
  103. func (r *RouteMap) populateRouteMaps() {
  104. for k, v := range r.Mappings {
  105. r.MapUriToDomain(k, v)
  106. }
  107. }
  108. // Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
  109. func (r *RouteMap) ExportRouteMap(loc string) {
  110. routeMapOut := &RouteMap{
  111. Mappings: map[string]string{},
  112. Shotgun: map[string]string{},
  113. }
  114. cachedRoutes := r.MapCache.Items()
  115. for k, v := range cachedRoutes {
  116. routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
  117. }
  118. for k, v := range r.Shotgun {
  119. routeMapOut.Shotgun[k] = v
  120. }
  121. b, err := json.Marshal(routeMapOut)
  122. if err != nil {
  123. log.Fatal("failed to marshal struct: ", err)
  124. }
  125. os.WriteFile(loc, b, os.ModePerm)
  126. }
  127. /*
  128. Reads the server configuration file, along with the cookie file so that the correlated account can be
  129. accessed through the proxy
  130. :param loc: the location of the config file
  131. */
  132. func ReadConfig(loc string) (*HttpServerConfig, error) {
  133. f, err := os.ReadFile(loc)
  134. if err != nil {
  135. return nil, err
  136. }
  137. var cfg HttpServerConfig
  138. err = json.Unmarshal(f, &cfg)
  139. if err != nil {
  140. return nil, err
  141. }
  142. cf, err := os.ReadFile(cfg.CookieFile)
  143. if err != nil {
  144. return nil, err
  145. }
  146. cfg.FullDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AllowedDomain)
  147. cfg.FullProxyDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.ProxyAddr)
  148. cfg.FullAltAllowedDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AltAllowedDomain)
  149. var cookies []*http.Cookie
  150. err = json.Unmarshal(cf, &cookies)
  151. if err != nil {
  152. return nil, err
  153. }
  154. for idx := range cookies {
  155. if cookies[idx].Name == "PHPSESSID" {
  156. cfg.PhpSession = cookies[idx]
  157. }
  158. if cookies[idx].Name == "sso_token" {
  159. cfg.SsoToken = cookies[idx]
  160. }
  161. }
  162. cfg.CookieJar = cookies
  163. return &cfg, err
  164. }
  165. func ReadRouteMap(loc string) *RouteMap {
  166. f, err := os.ReadFile(loc)
  167. if err != nil {
  168. log.Fatal(err)
  169. }
  170. var mapfile RouteMap
  171. err = json.Unmarshal(f, &mapfile)
  172. if err != nil {
  173. log.Fatal(err)
  174. }
  175. mapfile.MapCache = cache.New(24*time.Hour, 10*time.Minute)
  176. mapfile.populateRouteMaps()
  177. return &mapfile
  178. }