include.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. PhpSession *http.Cookie
  17. SsoToken *http.Cookie
  18. }
  19. type Cookie struct {
  20. Name string `json:"name"`
  21. Value string `json:"value"`
  22. MaxAge int `json:"max_age"`
  23. Path string `json:"path"`
  24. Domain string `json:"domain"`
  25. Secure bool `json:"secure"`
  26. IncludeSub bool `json:"include_sub"`
  27. }
  28. /*
  29. Reads the server configuration file, along with the cookie file so that the correlated account can be
  30. accessed through the proxy
  31. :param loc: the location of the config file
  32. */
  33. func ReadConfig(loc string) (*HttpServerConfig, error) {
  34. f, err := os.ReadFile(loc)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var cfg HttpServerConfig
  39. err = json.Unmarshal(f, &cfg)
  40. if err != nil {
  41. return nil, err
  42. }
  43. cf, err := os.ReadFile("./cookies.json")
  44. if err != nil {
  45. return nil, err
  46. }
  47. var cookies []Cookie
  48. err = json.Unmarshal(cf, &cookies)
  49. if err != nil {
  50. return nil, err
  51. }
  52. for idx := range cookies {
  53. httpCookie := &http.Cookie{
  54. Domain: cookies[idx].Domain,
  55. MaxAge: cookies[idx].MaxAge,
  56. Name: cookies[idx].Name,
  57. Value: cookies[idx].Value,
  58. Path: cookies[idx].Path,
  59. Secure: cookies[idx].Secure,
  60. }
  61. cfg.CookieJar = append(cfg.CookieJar, httpCookie)
  62. if httpCookie.Name == "PHPSESSID" {
  63. cfg.PhpSession = httpCookie
  64. }
  65. if httpCookie.Name == "sso_token" {
  66. cfg.SsoToken = httpCookie
  67. }
  68. }
  69. fmt.Printf("%+v\n", cfg)
  70. return &cfg, err
  71. }