include.go 2.4 KB

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