include.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package httpserver
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "path"
  10. )
  11. type HttpServerConfig struct {
  12. HttpPort int `json:"http_port"`
  13. HttpsPort int `json:"https_port"`
  14. AllowedDomain string `json:"allowed_domain"`
  15. FullDomain string // The domain name with the protocol before it
  16. AltAllowedDomain string `json:"alt_allowed_domain"` // alternate domain that resources are sourced from
  17. FullAltAllowedDomain string // the alt domain with the protocol
  18. Proto string `json:"proto"` // http/https
  19. UserAgent string `json:"user_agent"`
  20. UseSsl bool `json:"use_ssl"`
  21. ProxyAddr string `json:"proxy_addr"`
  22. RouteMapDir string `json:"routemap_dir"`
  23. FullProxyDomain string // the domain name of the proxied site with the protocol
  24. CookieJar []*http.Cookie
  25. PhpSession *http.Cookie
  26. SsoToken *http.Cookie
  27. }
  28. type Cookie struct {
  29. Name string `json:"name"`
  30. Value string `json:"value"`
  31. MaxAge int `json:"max_age"`
  32. Path string `json:"path"`
  33. Domain string `json:"domain"`
  34. Secure bool `json:"secure"`
  35. IncludeSub bool `json:"include_sub"`
  36. }
  37. type RouteMapping struct {
  38. DomainName string `json:"domain_name"`
  39. UriPaths []string `json:"uri_paths"`
  40. RouteSet map[string]struct{}
  41. }
  42. /*
  43. Reads the server configuration file, along with the cookie file so that the correlated account can be
  44. accessed through the proxy
  45. :param loc: the location of the config file
  46. */
  47. func ReadConfig(loc string) (*HttpServerConfig, error) {
  48. f, err := os.ReadFile(loc)
  49. if err != nil {
  50. return nil, err
  51. }
  52. var cfg HttpServerConfig
  53. err = json.Unmarshal(f, &cfg)
  54. if err != nil {
  55. return nil, err
  56. }
  57. cf, err := os.ReadFile("./cookies.json")
  58. if err != nil {
  59. return nil, err
  60. }
  61. cfg.FullDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AllowedDomain)
  62. cfg.FullProxyDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.ProxyAddr)
  63. cfg.FullAltAllowedDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AltAllowedDomain)
  64. var cookies []Cookie
  65. err = json.Unmarshal(cf, &cookies)
  66. if err != nil {
  67. return nil, err
  68. }
  69. for idx := range cookies {
  70. httpCookie := &http.Cookie{
  71. Domain: cookies[idx].Domain,
  72. MaxAge: cookies[idx].MaxAge,
  73. Name: cookies[idx].Name,
  74. Value: cookies[idx].Value,
  75. Path: cookies[idx].Path,
  76. Secure: cookies[idx].Secure,
  77. }
  78. cfg.CookieJar = append(cfg.CookieJar, httpCookie)
  79. if httpCookie.Name == "PHPSESSID" {
  80. cfg.PhpSession = httpCookie
  81. }
  82. if httpCookie.Name == "sso_token" {
  83. cfg.SsoToken = httpCookie
  84. }
  85. }
  86. fmt.Printf("%+v\n", cfg)
  87. return &cfg, err
  88. }
  89. func ReadRouteMap(loc string) *RouteMapping {
  90. f, err := os.ReadFile(loc)
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. var mapfile RouteMapping
  95. err = json.Unmarshal(f, &mapfile)
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. rmap := map[string]struct{}{}
  100. for idx := range mapfile.UriPaths {
  101. rmap[mapfile.UriPaths[idx]] = struct{}{}
  102. }
  103. mapfile.RouteSet = rmap
  104. return &mapfile
  105. }
  106. func PopulateRouteMaps(dir string) map[string]*RouteMapping {
  107. routeMaps := map[string]*RouteMapping{}
  108. fnames, err := ioutil.ReadDir(dir)
  109. if err != nil {
  110. log.Fatal("Failed to read the routemap directory: ", err)
  111. }
  112. for _, f := range fnames {
  113. mapping := ReadRouteMap(path.Join(dir, f.Name()))
  114. routeMaps[f.Name()] = mapping
  115. }
  116. return routeMaps
  117. }