Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
550f961cc9

+ 1 - 1
main.go

@@ -37,7 +37,7 @@ var (
 	name        = "Zoraxy"
 	version     = "2.2"
 	nodeUUID    = "generic"
-	development = false //Set this to false to use embedded web fs
+	development = true //Set this to false to use embedded web fs
 
 	/*
 		Binary Embedding File System

+ 4 - 1
mod/dynamicproxy/Server.go

@@ -71,8 +71,11 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		h.proxyRequest(w, r, targetProxyEndpoint)
 	} else if !strings.HasSuffix(proxyingPath, "/") {
 		potentialProxtEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath + "/")
+
 		if potentialProxtEndpoint != nil {
-			h.proxyRequest(w, r, potentialProxtEndpoint)
+			//Missing tailing slash. Redirect to target proxy endpoint
+			http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
+			//h.proxyRequest(w, r, potentialProxtEndpoint)
 		} else {
 			h.proxyRequest(w, r, h.Parent.Root)
 		}

+ 20 - 5
mod/dynamicproxy/dpcore/dpcore.go

@@ -7,7 +7,6 @@ import (
 	"net"
 	"net/http"
 	"net/url"
-	"path/filepath"
 	"strings"
 	"sync"
 	"time"
@@ -61,6 +60,10 @@ type ReverseProxy struct {
 	Verbal bool
 }
 
+type ResponseRewriteRuleSet struct {
+	ProxyDomain string
+}
+
 type requestCanceler interface {
 	CancelRequest(req *http.Request)
 }
@@ -229,7 +232,7 @@ func addXForwardedForHeader(req *http.Request) {
 	}
 }
 
-func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) error {
+func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request, rrr *ResponseRewriteRuleSet) error {
 	transport := p.Transport
 	if transport == nil {
 		transport = http.DefaultTransport
@@ -296,8 +299,20 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro
 
 	//Custom header rewriter functions
 	if res.Header.Get("Location") != "" {
+		locationRewrite := res.Header.Get("Location")
+
+		domainWithoutPort := rrr.ProxyDomain
+		if strings.Contains(rrr.ProxyDomain, ":") {
+			//Split the port away
+			domainWithoutPort = strings.Split(rrr.ProxyDomain, ":")[0]
+		}
+
+		//Trim away the source headers if exists
+		locationRewrite = strings.TrimPrefix(locationRewrite, "http://"+rrr.ProxyDomain)
+		locationRewrite = strings.TrimPrefix(locationRewrite, "http://"+domainWithoutPort)
+
 		//Custom redirection to this rproxy relative path
-		res.Header.Set("Location", filepath.ToSlash(filepath.Join(p.Prepender, res.Header.Get("Location"))))
+		res.Header.Set("Location", locationRewrite)
 	}
 	// Copy header from response to client.
 	copyHeader(rw.Header(), res.Header)
@@ -403,12 +418,12 @@ func (p *ReverseProxy) ProxyHTTPS(rw http.ResponseWriter, req *http.Request) err
 	return nil
 }
 
-func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) error {
+func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, rrr *ResponseRewriteRuleSet) error {
 	if req.Method == "CONNECT" {
 		err := p.ProxyHTTPS(rw, req)
 		return err
 	} else {
-		err := p.ProxyHTTP(rw, req)
+		err := p.ProxyHTTP(rw, req, rrr)
 		return err
 	}
 }

+ 6 - 3
mod/dynamicproxy/dynamicproxy.go

@@ -244,13 +244,16 @@ func (router *Router) IsProxiedSubdomain(r *http.Request) bool {
 Add an URL into a custom proxy services
 */
 func (router *Router) AddVirtualDirectoryProxyService(rootname string, domain string, requireTLS bool) error {
+
 	if domain[len(domain)-1:] == "/" {
 		domain = domain[:len(domain)-1]
 	}
 
-	if rootname[len(rootname)-1:] == "/" {
-		rootname = rootname[:len(rootname)-1]
-	}
+	/*
+		if rootname[len(rootname)-1:] == "/" {
+			rootname = rootname[:len(rootname)-1]
+		}
+	*/
 
 	webProxyEndpoint := domain
 	if requireTLS {

+ 9 - 3
mod/dynamicproxy/proxyRequestHandler.go

@@ -8,6 +8,7 @@ import (
 	"net/url"
 	"strings"
 
+	"imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
 	"imuslab.com/zoraxy/mod/geodb"
 	"imuslab.com/zoraxy/mod/statistic"
 	"imuslab.com/zoraxy/mod/websocketproxy"
@@ -44,8 +45,9 @@ func (router *Router) getSubdomainProxyEndpointFromHostname(hostname string) *Su
 }
 
 func (router *Router) rewriteURL(rooturl string, requestURL string) string {
-	if len(requestURL) > len(rooturl) {
-		return requestURL[len(rooturl):]
+	if strings.HasPrefix(requestURL, rooturl) {
+		newURL := strings.TrimPrefix(requestURL, rooturl)
+		return newURL
 	}
 	return ""
 }
@@ -96,6 +98,7 @@ func (h *ProxyHandler) subdomainRequest(w http.ResponseWriter, r *http.Request,
 func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, target *ProxyEndpoint) {
 	rewriteURL := h.Parent.rewriteURL(target.Root, r.RequestURI)
 	r.URL, _ = url.Parse(rewriteURL)
+
 	r.Header.Set("X-Forwarded-Host", r.Host)
 	if r.Header["Upgrade"] != nil && strings.ToLower(r.Header["Upgrade"][0]) == "websocket" {
 		//Handle WebSocket request. Forward the custom Upgrade header and rewrite origin
@@ -115,7 +118,10 @@ func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, targ
 	}
 
 	r.Host = r.URL.Host
-	err := target.Proxy.ServeHTTP(w, r)
+	err := target.Proxy.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
+		ProxyDomain: target.Domain,
+	})
+
 	var dnsError *net.DNSError
 	if err != nil {
 		if errors.As(err, &dnsError) {

+ 3 - 2
reverseproxy.go

@@ -165,6 +165,7 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
+		//Vdir must start with /
 		if !strings.HasPrefix(vdir, "/") {
 			vdir = "/" + vdir
 		}
@@ -302,8 +303,8 @@ func HandleUpdateHttpsRedirect(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-//Handle checking if the current user is accessing via the reverse proxied interface
-//Of the management interface.
+// Handle checking if the current user is accessing via the reverse proxied interface
+// Of the management interface.
 func HandleManagementProxyCheck(w http.ResponseWriter, r *http.Request) {
 	isProxied := dynamicProxyRouter.IsProxiedSubdomain(r)
 	js, _ := json.Marshal(isProxied)

+ 1 - 1
router.go

@@ -79,7 +79,7 @@ func FSHandler(handler http.Handler) http.Handler {
 	})
 }
 
-//Production path fix wrapper. Fix the path on production or development environment
+// Production path fix wrapper. Fix the path on production or development environment
 func ppf(relativeFilepath string) string {
 	if !development {
 		return strings.ReplaceAll(filepath.Join("/web/", relativeFilepath), "\\", "/")