/* 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 ( "fmt" "log" "net/http" "net/http/cookiejar" "net/url" "time" "github.com/gin-gonic/gin" "github.com/patrickmn/go-cache" "golang.org/x/net/publicsuffix" ) // Implementing a 'set' var NonmutableHeaders = map[string]struct{}{ "Cookie": struct{}{}, "User-Agent": struct{}{}, "Accept-Encoding": struct{}{}, "Referer": struct{}{}, "X-Proxy-Url": struct{}{}, "Host": struct{}{}, } type Controller struct { Config HttpServerConfig ProxyAddr string TargetAddr string RouteMaps *RouteMap Client *http.Client cache *cache.Cache } type ProxyCookies struct { ck map[*url.URL][]*http.Cookie } /* Returns a new Controller struct to register routes to the gin router :param cfg: A pointer to an HttpServerConfig struct */ func NewController(cfg HttpServerConfig, routeMap *RouteMap) *Controller { jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) if err != nil { log.Fatal(err) } sessCookies := cfg.CookieJar domain, err := url.Parse(cfg.ProxyDomain) if err != nil { log.Fatal(err) } jar.SetCookies(domain, sessCookies) var resCache *cache.Cache if cfg.Caching { fmt.Printf("Starting server with resource caching ENABLED.\n") resCache = cache.New(24*time.Hour, 10*time.Minute) } else { fmt.Printf("Starting server with resource caching DISABLED.\n") resCache = nil } return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }}, cache: resCache, RouteMaps: routeMap} } /* This handler will be responsible for proxying out the GET requests that the server recieves */ func (c *Controller) HandleAny(ctx *gin.Context) { incomingPath := ctx.Param("ProxiedPath") if c.Config.Caching { cacheHit := c.GetResource(incomingPath) if cacheHit != nil { for k, v := range *cacheHit.Headers { _, ok := NonmutableHeaders[k] if !ok { for i := range v { ctx.Header(k, v[i]) } } } ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data) return } else { fmt.Printf("Cache MISS! For resource URI: %s\n", incomingPath) } } resp := c.RequestGeneric(ctx.Request.Method, c.Config.TargetDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body) if resp.Error != nil { log.Fatal(resp.Error, " failed to route the request: ", incomingPath, " to the target domain: ", ctx.Request.Host, " Error: ", resp.Error) } for k, v := range resp.Headers { _, ok := NonmutableHeaders[k] if !ok { // ctx.Header(k, strings.ReplaceAll(v[0], c.Config.TargetDomain, c.Config.ProxyDomain)) ctx.Header(k, v[0]) } } ctx.Header("access-control-allow-origin", c.Config.ProxyDomain) ctx.Data(resp.StatusCode, resp.Headers.Get("content-type"), resp.Data) return }