configuration.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "time"
  28. "github.com/patrickmn/go-cache"
  29. )
  30. type HttpServerConfig struct {
  31. HttpPort int `json:"http_port"`
  32. HttpsPort int `json:"https_port"`
  33. AllowedDomain string `json:"allowed_domain"`
  34. FullDomain string // The domain name with the protocol before it
  35. AltAllowedDomain string `json:"alt_allowed_domain"` // alternate domain that resources are sourced from
  36. FullAltAllowedDomain string // the alt domain with the protocol
  37. Proto string `json:"proto"` // http/https
  38. UserAgent string `json:"user_agent"`
  39. UseSsl bool `json:"use_ssl"`
  40. ProxyAddr string `json:"proxy_addr"`
  41. RouteMapPath string `json:"route_map_path"`
  42. PageModPath string `json:"page_mod_path"`
  43. CustomFServePath string `json:"custom_fileserve_cfg_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. Caching bool
  50. CustomFserve *CustomFileServer
  51. CookieJar []*http.Cookie
  52. PhpSession *http.Cookie
  53. SsoToken *http.Cookie
  54. }
  55. type CustomFile struct {
  56. Request string `json:"request"`
  57. Serve string `json:"serve"`
  58. ContentType string `json:"content-type"`
  59. FileData []byte
  60. }
  61. type CustomFileServer struct {
  62. Config []*CustomFile `json:"config"`
  63. }
  64. type RedirectRule struct {
  65. From string `json:"from"`
  66. To string `json:"to"`
  67. }
  68. type Cookie struct {
  69. Name string `json:"name"`
  70. Value string `json:"value"`
  71. MaxAge int `json:"max_age"`
  72. Path string `json:"path"`
  73. Domain string `json:"domain"`
  74. Secure bool `json:"secure"`
  75. IncludeSub bool `json:"include_sub"`
  76. }
  77. /*
  78. Reads the server configuration file, along with the cookie file so that the correlated account can be
  79. accessed through the proxy
  80. :param loc: the location of the config file
  81. */
  82. func ReadConfig(loc string) (*HttpServerConfig, error) {
  83. f, err := os.ReadFile(loc)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var cfg HttpServerConfig
  88. err = json.Unmarshal(f, &cfg)
  89. if err != nil {
  90. return nil, err
  91. }
  92. cf, err := os.ReadFile(cfg.CookieFile)
  93. if err != nil {
  94. return nil, err
  95. }
  96. cfg.CustomFserve = ReadCustomFiles(cfg.CustomFServePath)
  97. cfg.FullDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AllowedDomain)
  98. cfg.FullProxyDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.ProxyAddr)
  99. cfg.FullAltAllowedDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AltAllowedDomain)
  100. var cookies []*http.Cookie
  101. err = json.Unmarshal(cf, &cookies)
  102. if err != nil {
  103. return nil, err
  104. }
  105. for idx := range cookies {
  106. if cookies[idx].Name == "PHPSESSID" {
  107. cfg.PhpSession = cookies[idx]
  108. }
  109. if cookies[idx].Name == "sso_token" {
  110. cfg.SsoToken = cookies[idx]
  111. }
  112. }
  113. cfg.CookieJar = cookies
  114. return &cfg, err
  115. }
  116. func ReadRouteMap(loc string) *RouteMap {
  117. f, err := os.ReadFile(loc)
  118. if err != nil {
  119. log.Fatal(err)
  120. }
  121. var mapfile RouteMap
  122. err = json.Unmarshal(f, &mapfile)
  123. if err != nil {
  124. log.Fatal(err)
  125. }
  126. mapfile.MapCache = cache.New(24*time.Hour, 10*time.Minute)
  127. mapfile.populateRouteMaps()
  128. return &mapfile
  129. }
  130. /*
  131. Read in the custom file server configuration
  132. :param loc: path to the custom file server config
  133. */
  134. func ReadCustomFiles(loc string) *CustomFileServer {
  135. b, err := os.ReadFile(loc)
  136. if err != nil {
  137. log.Fatal("couldnt read custom config file: ", err)
  138. }
  139. var fserveCfg CustomFileServer
  140. err = json.Unmarshal(b, &fserveCfg)
  141. if err != nil {
  142. log.Fatal("error loading in the config file: ", err)
  143. }
  144. for idx := range fserveCfg.Config {
  145. _, err = os.Stat(fserveCfg.Config[idx].Serve)
  146. if err != nil {
  147. log.Fatal("Couldnt verify the existence of file: ", fserveCfg.Config[idx].Serve, " error: ", err)
  148. }
  149. b, err = os.ReadFile(fserveCfg.Config[idx].Serve)
  150. if err != nil {
  151. log.Fatal("Error reading in custom fileserver file: ", err)
  152. }
  153. fserveCfg.Config[idx].FileData = b
  154. }
  155. fmt.Printf("%+v\n", fserveCfg)
  156. return &fserveCfg
  157. }