include.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package httpserver
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. )
  8. type HttpServerConfig struct {
  9. HttpPort int `json:"http_port"`
  10. HttpsPort int `json:"https_port"`
  11. AllowedDomain string `json:"allowed_domain"`
  12. UserAgent string `json:"user_agent"`
  13. UseSsl bool `json:"use_ssl"`
  14. ProxyAddr string `json:"proxy_addr"`
  15. CookieJar []*http.Cookie
  16. }
  17. type Cookie struct {
  18. Name string `json:"name"`
  19. Value string `json:"value"`
  20. MaxAge int `json:"max_age"`
  21. Path string `json:"path"`
  22. Domain string `json:"domain"`
  23. Secure bool `json:"secure"`
  24. IncludeSub bool `json:"include_sub"`
  25. }
  26. /*
  27. Reads the server configuration file, along with the cookie file so that the correlated account can be
  28. accessed through the proxy
  29. :param loc: the location of the config file
  30. */
  31. func ReadConfig(loc string) (*HttpServerConfig, error) {
  32. f, err := os.ReadFile(loc)
  33. if err != nil {
  34. return nil, err
  35. }
  36. var cfg HttpServerConfig
  37. err = json.Unmarshal(f, &cfg)
  38. if err != nil {
  39. return nil, err
  40. }
  41. cf, err := os.ReadFile("./cookies.json")
  42. if err != nil {
  43. return nil, err
  44. }
  45. var cookies []Cookie
  46. err = json.Unmarshal(cf, &cookies)
  47. if err != nil {
  48. return nil, err
  49. }
  50. for idx := range cookies {
  51. httpCookie := &http.Cookie{
  52. Domain: cookies[idx].Domain,
  53. MaxAge: cookies[idx].MaxAge,
  54. Name: cookies[idx].Name,
  55. Value: cookies[idx].Value,
  56. Path: cookies[idx].Path,
  57. Secure: cookies[idx].Secure,
  58. }
  59. cfg.CookieJar = append(cfg.CookieJar, httpCookie)
  60. }
  61. fmt.Printf("%+v\n", cfg)
  62. return &cfg, err
  63. }