/* GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. http-wokou, An HTTP Proxying framework for bypassing DNS Security Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package httpserver import ( "encoding/json" "fmt" "log" "net/http" "os" "time" "github.com/patrickmn/go-cache" ) 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"` SslPemFile string `json:"ssl_pem_file"` SslKeyFile string `json:"ssl_key_file"` ListeningIp string `json:"listening_ip"` ProxyAddr string `json:"proxy_addr"` RouteMapPath string `json:"route_map_path"` PageModPath string `json:"page_mod_path"` CustomFServePath string `json:"custom_fileserve_cfg_path"` CookieFile string `json:"cookie_file"` FullProxyDomain string // the domain name of the proxied site with the protocol KnownHosts []string `json:"known_hosts"` CorsHosts []string `json:"cors_hosts"` Redirects []*RedirectRule `json:"redirects"` Caching bool TkUpdateCode string CustomFserve *CustomFileServer TokenSaveLoc string CookieJar []*http.Cookie PhpSession *http.Cookie SsoToken *http.Cookie } type CustomFile struct { Request string `json:"request"` Serve string `json:"serve"` ContentType string `json:"content-type"` FileData []byte } type CustomFileServer struct { Config []*CustomFile `json:"config"` } type RedirectRule struct { From string `json:"from"` To string `json:"to"` } 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(cfg.CookieFile) if err != nil { return nil, err } cfg.CustomFserve = ReadCustomFiles(cfg.CustomFServePath) 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 []*http.Cookie err = json.Unmarshal(cf, &cookies) if err != nil { return nil, err } for idx := range cookies { if cookies[idx].Name == "PHPSESSID" { cfg.PhpSession = cookies[idx] } if cookies[idx].Name == "sso_token" { cfg.SsoToken = cookies[idx] } } cfg.CookieJar = cookies return &cfg, err } func ReadRouteMap(loc string) *RouteMap { f, err := os.ReadFile(loc) if err != nil { log.Fatal(err) } var mapfile RouteMap err = json.Unmarshal(f, &mapfile) if err != nil { log.Fatal(err) } mapfile.MapCache = cache.New(24*time.Hour, 10*time.Minute) mapfile.populateRouteMaps() return &mapfile } /* Read in the custom file server configuration :param loc: path to the custom file server config */ func ReadCustomFiles(loc string) *CustomFileServer { b, err := os.ReadFile(loc) if err != nil { fmt.Printf("couldnt read custom config file: %s\n", err.Error()) return nil } var fserveCfg CustomFileServer err = json.Unmarshal(b, &fserveCfg) if err != nil { log.Fatal("error loading in the config file: ", err) } for idx := range fserveCfg.Config { _, err = os.Stat(fserveCfg.Config[idx].Serve) if err != nil { log.Fatal("Couldnt verify the existence of file: ", fserveCfg.Config[idx].Serve, " error: ", err) } b, err = os.ReadFile(fserveCfg.Config[idx].Serve) if err != nil { log.Fatal("Error reading in custom fileserver file: ", err) } fserveCfg.Config[idx].FileData = b } return &fserveCfg }