/*
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 (
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"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
RouteMaps map[string]*RouteMapping
Client *http.Client
SiteUrl *url.URL
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, routeMapping map[string]*RouteMapping) *Controller {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
}
sessCookies := cfg.CookieJar
domain, err := url.Parse(cfg.FullDomain)
if err != nil {
log.Fatal(err)
}
jar.SetCookies(domain, sessCookies)
cache := cache.New(24*time.Hour, 10*time.Minute)
return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, SiteUrl: domain, cache: cache, RouteMaps: routeMapping}
}
/*
This handler will be responsible for proxying out the GET requests that the server recieves
*/
func (c *Controller) Get(ctx *gin.Context) {
incomingPath := ctx.Param("ProxiedPath")
routeSplit := strings.Split(incomingPath, "/")
if len(routeSplit) > 1 {
baseRoute := strings.Join(routeSplit[:len(routeSplit)-1], "/")
_, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[baseRoute]
if ok {
data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
if err != nil {
log.Fatal(err, " failed to reroute to the static domain")
}
ctx.Data(rcode, ctype, data)
return
}
}
_, ok := c.RouteMaps[c.Config.AltAllowedDomain].RouteSet[incomingPath]
if ok {
data, ctype, rcode, err := c.RetrieveStaticResource(ctx.Request.Method, incomingPath, ctx)
if err != nil {
log.Fatal(err, " failed to reroute to the static domain")
}
ctx.Data(rcode, ctype, data)
return
}
_, ok = c.RouteMaps[c.Config.AllowedDomain].RouteSet[incomingPath]
if ok {
data, ctype, rcode, err := c.SiteauditApiCall(ctx.Request.Method, incomingPath, ctx.Request.URL.RawQuery, ctx.Request.Body, ctx)
if err != nil {
log.Fatal(err, " failed to route to the root domain")
}
ctx.Data(rcode, ctype, data)
return
}
data, ctype, rcode, err := c.SemrushGeneric(ctx)
if err != nil {
ctx.JSON(rcode, map[string]string{
"Error": err.Error(),
})
return
}
if incomingPath == "/" {
ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
}
if incomingPath == "/_compatibility/traffic/overview/" {
ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
}
// TODO: this should also honestly be a configuration thing, so that i can extend page modifications
newBody := strings.ReplaceAll(string(data), "\"srf-browser-unhappy\"", "\"srf-browser-unhappy\" style=\"display:none;\"")
newBody = strings.ReplaceAll(newBody, "\"srf-navbar__right\"", "\"srf-navbar__right\" style=\"display:none;\"")
newBody = strings.ReplaceAll(newBody, "