12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package httpserver
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "os"
- )
- type HttpServerConfig struct {
- HttpPort int `json:"http_port"`
- HttpsPort int `json:"https_port"`
- AllowedDomain string `json:"allowed_domain"`
- FullDomain string // The domain name with the protocol before it
- AltAllowedDomain string `json:"alt_allowed_domain"` // alternate domain that resources are sourced from
- FullAltAllowedDomain string // the alt domain with the protocol
- Proto string `json:"proto"` // http/https
- UserAgent string `json:"user_agent"`
- UseSsl bool `json:"use_ssl"`
- ProxyAddr string `json:"proxy_addr"`
- FullProxyDomain string // the domain name of the proxied site with the protocol
- CookieJar []*http.Cookie
- PhpSession *http.Cookie
- SsoToken *http.Cookie
- }
- type Cookie struct {
- Name string `json:"name"`
- Value string `json:"value"`
- MaxAge int `json:"max_age"`
- Path string `json:"path"`
- Domain string `json:"domain"`
- Secure bool `json:"secure"`
- IncludeSub bool `json:"include_sub"`
- }
- /*
- Reads the server configuration file, along with the cookie file so that the correlated account can be
- accessed through the proxy
- :param loc: the location of the config file
- */
- func ReadConfig(loc string) (*HttpServerConfig, error) {
- f, err := os.ReadFile(loc)
- if err != nil {
- return nil, err
- }
- var cfg HttpServerConfig
- err = json.Unmarshal(f, &cfg)
- if err != nil {
- return nil, err
- }
- cf, err := os.ReadFile("./cookies.json")
- if err != nil {
- return nil, err
- }
- cfg.FullDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AllowedDomain)
- cfg.FullProxyDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.ProxyAddr)
- cfg.FullAltAllowedDomain = fmt.Sprintf("%s://%s", cfg.Proto, cfg.AltAllowedDomain)
- var cookies []Cookie
- err = json.Unmarshal(cf, &cookies)
- if err != nil {
- return nil, err
- }
- for idx := range cookies {
- httpCookie := &http.Cookie{
- Domain: cookies[idx].Domain,
- MaxAge: cookies[idx].MaxAge,
- Name: cookies[idx].Name,
- Value: cookies[idx].Value,
- Path: cookies[idx].Path,
- Secure: cookies[idx].Secure,
- }
- cfg.CookieJar = append(cfg.CookieJar, httpCookie)
- if httpCookie.Name == "PHPSESSID" {
- cfg.PhpSession = httpCookie
- }
- if httpCookie.Name == "sso_token" {
- cfg.SsoToken = httpCookie
- }
- }
- fmt.Printf("%+v\n", cfg)
- return &cfg, err
- }
|