include.go 1.5 KB

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