configuration.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "log"
  24. "net/http"
  25. "os"
  26. "time"
  27. "github.com/patrickmn/go-cache"
  28. )
  29. type HttpServerConfig struct {
  30. HttpPort int `json:"http_port"`
  31. HttpsPort int `json:"https_port"`
  32. TargetDomain string // This is the domain that will be used by the client
  33. ProxyDomain string // This will be the domain that is being proxied, i.e. the downstream domain
  34. SslPemFile string `json:"ssl_pem_file"`
  35. SslKeyFile string `json:"ssl_key_file"`
  36. ListeningIp string `json:"listening_ip"`
  37. KnownHosts []string `json:"known_hosts"`
  38. RouteMapPath string `json:"route_map_path"` // path to the routemaps
  39. Caching bool
  40. CookieFile string `json:"cookie_file"` // path to a cookie for the outbound client
  41. CookieJar []*http.Cookie
  42. Home string `json:"home"` // the location on the filesystem to save the output config to
  43. }
  44. type Cookie struct {
  45. Name string `json:"name"`
  46. Value string `json:"value"`
  47. MaxAge int `json:"max_age"`
  48. Path string `json:"path"`
  49. Domain string `json:"domain"`
  50. Secure bool `json:"secure"`
  51. IncludeSub bool `json:"include_sub"`
  52. }
  53. /*
  54. Reads the server configuration file, along with the cookie file so that the correlated account can be
  55. accessed through the proxy
  56. :param loc: the location of the config file
  57. */
  58. func ReadConfig(loc string, proxyDomain string, targetDomain string) (HttpServerConfig, error) {
  59. f, err := os.ReadFile(loc)
  60. if err != nil {
  61. return HttpServerConfig{}, err
  62. }
  63. var cfg HttpServerConfig
  64. cfg = HttpServerConfig{ProxyDomain: proxyDomain, TargetDomain: targetDomain}
  65. err = json.Unmarshal(f, &cfg)
  66. if err != nil {
  67. return HttpServerConfig{}, err
  68. }
  69. cf, err := os.ReadFile(cfg.CookieFile)
  70. if err != nil {
  71. return HttpServerConfig{}, err
  72. }
  73. var cookies []*http.Cookie
  74. err = json.Unmarshal(cf, &cookies)
  75. if err != nil {
  76. return HttpServerConfig{}, err
  77. }
  78. cfg.CookieJar = cookies
  79. return cfg, err
  80. }
  81. func ReadRouteMap(loc string) *RouteMap {
  82. f, err := os.ReadFile(loc)
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. var mapfile RouteMap
  87. err = json.Unmarshal(f, &mapfile)
  88. if err != nil {
  89. log.Fatal(err)
  90. }
  91. mapfile.MapCache = cache.New(24*time.Hour, 10*time.Minute)
  92. mapfile.populateRouteMaps()
  93. return &mapfile
  94. }