configuration.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ServerDomain 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) (HttpServerConfig, error) {
  59. f, err := os.ReadFile(loc)
  60. if err != nil {
  61. return HttpServerConfig{}, err
  62. }
  63. var cfg HttpServerConfig
  64. err = json.Unmarshal(f, &cfg)
  65. if err != nil {
  66. return HttpServerConfig{}, err
  67. }
  68. cf, err := os.ReadFile(cfg.CookieFile)
  69. if err != nil {
  70. return HttpServerConfig{}, err
  71. }
  72. var cookies []*http.Cookie
  73. err = json.Unmarshal(cf, &cookies)
  74. if err != nil {
  75. return HttpServerConfig{}, err
  76. }
  77. cfg.CookieJar = cookies
  78. return cfg, err
  79. }
  80. func ReadRouteMap(loc string) *RouteMap {
  81. f, err := os.ReadFile(loc)
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. var mapfile RouteMap
  86. err = json.Unmarshal(f, &mapfile)
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. mapfile.MapCache = cache.New(24*time.Hour, 10*time.Minute)
  91. mapfile.populateRouteMaps()
  92. return &mapfile
  93. }