configuration.go 5.4 KB

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