configuration.go 5.5 KB

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