include.go 2.1 KB

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