include.go 5.5 KB

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