123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package httpserver
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- "path"
- )
- 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"`
- RouteMapDir string `json:"routemap_dir"`
- 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"`
- }
- type RouteMapping struct {
- DomainName string `json:"domain_name"`
- UriPaths []string `json:"uri_paths"`
- RouteSet map[string]struct{}
- }
- /*
- 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
- }
- func ReadRouteMap(loc string) *RouteMapping {
- f, err := os.ReadFile(loc)
- if err != nil {
- log.Fatal(err)
- }
- var mapfile RouteMapping
- err = json.Unmarshal(f, &mapfile)
- if err != nil {
- log.Fatal(err)
- }
- rmap := map[string]struct{}{}
- for idx := range mapfile.UriPaths {
- rmap[mapfile.UriPaths[idx]] = struct{}{}
- }
- mapfile.RouteSet = rmap
- return &mapfile
- }
- func PopulateRouteMaps(dir string) map[string]*RouteMapping {
- routeMaps := map[string]*RouteMapping{}
- fnames, err := ioutil.ReadDir(dir)
- if err != nil {
- log.Fatal("Failed to read the routemap directory: ", err)
- }
- for _, f := range fnames {
- mapping := ReadRouteMap(path.Join(dir, f.Name()))
- routeMaps[f.Name()] = mapping
- }
- return routeMaps
- }
|