123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /*
- GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
- Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
- 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 <https://www.gnu.org/licenses/>.
- */
- package httpserver
- import (
- "encoding/json"
- "fmt"
- "log"
- "net/http"
- "os"
- "strings"
- "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"`
- UseSsl bool `json:"use_ssl"`
- ProxyAddr string `json:"proxy_addr"`
- RouteMapPath string `json:"route_map_path"`
- PageModPath string `json:"page_mod_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"`
- 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{}
- }
- type RouteMap struct {
- Mappings map[string]string `json:"mappings"`
- Shotgun map[string]string `json:"shotgun"`
- MapCache *cache.Cache
- }
- type RouteMapper interface {
- mapUriToDomain(string, string)
- GetMappedDomain(string) (string, bool)
- ExportRouteMaps(string)
- }
- /*
- Set a route to exist for the URI to the specific domain
- :param uri: the URI to set the route for
- :param domain: the domain name to resolve the uri to
- */
- func (r *RouteMap) MapUriToDomain(uri string, domain string) {
- r.MapCache.Set(uri, domain, cache.DefaultExpiration)
- }
- // returns the domain/url that the uri belongs to as defined in the routemap
- func (r *RouteMap) GetMappedDomain(uri string) (string, bool) {
- dname, ok := r.MapCache.Get(uri)
- if ok {
- return fmt.Sprint(dname), true
- }
- for k, v := range r.Shotgun {
- if strings.Contains(uri, k) {
- return v, true
- }
- }
- return "", false
- }
- // This populates the cache in a RouteMap with the data from the config file
- func (r *RouteMap) populateRouteMaps() {
- for k, v := range r.Mappings {
- r.MapUriToDomain(k, v)
- }
- }
- // Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
- func (r *RouteMap) ExportRouteMap(loc string) {
- routeMapOut := &RouteMap{
- Mappings: map[string]string{},
- Shotgun: map[string]string{},
- }
- cachedRoutes := r.MapCache.Items()
- for k, v := range cachedRoutes {
- routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
- }
- for k, v := range r.Shotgun {
- routeMapOut.Shotgun[k] = v
- }
- b, err := json.Marshal(routeMapOut)
- if err != nil {
- log.Fatal("failed to marshal struct: ", err)
- }
- os.WriteFile(loc, b, os.ModePerm)
- }
- /*
- 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.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
- }
|