Sfoglia il codice sorgente

pushing before going to dinner

svcs 1 anno fa
parent
commit
f912a15cae
7 ha cambiato i file con 60 aggiunte e 26 eliminazioni
  1. 2 1
      .gitignore
  2. 17 0
      Makefile
  3. 0 0
      cmd/http-wokou/http-wokou.go
  4. 0 0
      cmd/wokou-cmd/wokou-cmd.go
  5. 3 5
      pkg/client.go
  6. 16 4
      pkg/controller.go
  7. 22 16
      pkg/include.go

+ 2 - 1
.gitignore

@@ -40,7 +40,8 @@ config/routemaps/*
 config/pagemod/*
 # cookies.json file
 cookies.json
-
+./build/linux/http-wokou/*
+./build/linux/wokou-cmd/*
 # anything .pem
 *.pem
 

+ 17 - 0
Makefile

@@ -0,0 +1,17 @@
+.PHONY: build-proxy format build-proxy-cmd
+
+
+PROXY_BIN = http-wokou
+PROXY_CMD = wokou-cmd 
+
+build-proxy:
+	mkdir -p ./build/linux/$(PROXY_BIN) && go build -o ./build/linux/$(PROXY_BIN)/$(PROXY_BIN) ./cmd/$(PROXY_BIN)/$(PROXY_BIN).go
+build-proxy-cmd:
+	mkdir -p ./build/linux/$(PROXY_CMD) && go build -o ./build/linux/$(PROXY_CMD)/$(PROXY_CMD) ./cmd/$(PROXY_CMD)/$(PROXY_CMD).go
+
+format:
+	go fmt ./...
+
+
+
+

+ 0 - 0
cmd/http-wokou.go → cmd/http-wokou/http-wokou.go


+ 0 - 0
cmd/wokou-cmd.go → cmd/wokou-cmd/wokou-cmd.go


+ 3 - 5
pkg/client.go

@@ -32,6 +32,7 @@ import (
 	"io"
 	"log"
 	"net/http"
+	"net/textproto"
 	"strings"
 	"sync"
 )
@@ -75,12 +76,12 @@ func (c *Controller) RequestGeneric(method string, host string, path string, hdr
 		return nil, nil, 500, err
 	}
 	altPage := c.pageMod(b)
-	resp.Header.Set("content-length", string(len(altPage)))
+	resp.Header.Set(textproto.CanonicalMIMEHeaderKey("content-length"), string(len(altPage)))
+
 	if !strings.Contains(path, "?") {
 		if resp.StatusCode == 200 {
 			if method == "GET" {
 				c.CacheResource(path, NewCachedResource(altPage, &resp.Header, resp.StatusCode))
-				fmt.Print("\n")
 			}
 		}
 	}
@@ -154,10 +155,7 @@ func (c *Controller) pageMod(data []byte) []byte {
 	for idx := range c.PageMods.Content {
 		if c.PageMods.Content[idx].Target == "content" {
 			data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
-
 		}
-
 	}
-
 	return data
 }

+ 16 - 4
pkg/controller.go

@@ -27,6 +27,8 @@
 package httpserver
 
 import (
+	"bytes"
+	"io"
 	"log"
 	"net/http"
 	"net/http/cookiejar"
@@ -82,9 +84,8 @@ func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
 	jar.SetCookies(domain, sessCookies)
 	cache := cache.New(24*time.Hour, 10*time.Minute)
 
-	return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error {
-		return http.ErrUseLastResponse
-	}}, SiteUrl: domain, cache: cache, RouteMaps: routeMap, PageMods: pgMod}
+	return &Controller{Config: cfg, Client: &http.Client{Jar: jar}, //	CheckRedirect: func(req *http.Request, via []*http.Request) error {return http.ErrUseLastResponse}
+		SiteUrl: domain, cache: cache, RouteMaps: routeMap, PageMods: pgMod}
 }
 
 /*
@@ -92,6 +93,13 @@ This handler will be responsible for proxying out the GET requests that the serv
 */
 func (c *Controller) Get(ctx *gin.Context) {
 	incomingPath := ctx.Param("ProxiedPath")
+	for idx := range c.Config.Redirects {
+		if incomingPath == c.Config.Redirects[idx].From {
+			ctx.Header("Location", c.Config.Redirects[idx].To)
+			ctx.Status(302)
+			return
+		}
+	}
 	cacheHit := c.GetResource(incomingPath)
 	if cacheHit != nil {
 		for k, v := range *cacheHit.Headers {
@@ -102,7 +110,11 @@ func (c *Controller) Get(ctx *gin.Context) {
 				}
 			}
 		}
-		ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
+		_, err := io.Copy(ctx.Writer, bytes.NewReader(cacheHit.Data))
+		if err != nil {
+			log.Fatal(err)
+		}
+		//		ctx.Data(cacheHit.Rcode, cacheHit.Headers.Get("content-type"), cacheHit.Data)
 		return
 	}
 

+ 22 - 16
pkg/include.go

@@ -39,27 +39,33 @@ import (
 )
 
 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"`
+	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"`
+	Redirects            []*RedirectRule `json:"redirects"`
 	CookieJar            []*http.Cookie
 	PhpSession           *http.Cookie
 	SsoToken             *http.Cookie
 }
 
+type RedirectRule struct {
+	From string `json:"from"`
+	To   string `json:"to"`
+}
+
 type Cookie struct {
 	Name       string `json:"name"`
 	Value      string `json:"value"`