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