include.go 5.7 KB

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