123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- 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 (
- "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, "<footer", "<footer style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, "\"srf-report-sidebar-extras\"", "\"srf-report-sidebar-extra\" style=\"display:none;\"")
- newBody = strings.ReplaceAll(newBody, c.Config.AllowedDomain, c.Config.ProxyAddr)
- newBody = strings.ReplaceAll(newBody, c.Config.AltAllowedDomain, c.Config.ProxyAddr)
- ctx.Data(rcode, ctype, []byte(newBody))
- }
|