Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
d43a943402

+ 18 - 0
mod/dynamicproxy/Server.go

@@ -89,6 +89,24 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 				return
 				return
 			}
 			}
 		}
 		}
+
+		//Check if any virtual directory rules matches
+		proxyingPath := strings.TrimSpace(r.RequestURI)
+		targetProxyEndpoint := sep.GetVirtualDirectoryHandlerFromRequestURI(proxyingPath)
+		if targetProxyEndpoint != nil {
+			//Virtual directory routing rule found. Route via vdir mode
+			h.vdirRequest(w, r, targetProxyEndpoint)
+			return
+		} else if !strings.HasSuffix(proxyingPath, "/") {
+			potentialProxtEndpoint := sep.GetVirtualDirectoryHandlerFromRequestURI(proxyingPath + "/")
+			if potentialProxtEndpoint != nil {
+				//Missing tailing slash. Redirect to target proxy endpoint
+				http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
+				return
+			}
+		}
+
+		//Fallback to handle by the host proxy forwarder
 		h.hostRequest(w, r, sep)
 		h.hostRequest(w, r, sep)
 		return
 		return
 	}
 	}

+ 6 - 86
mod/dynamicproxy/endpoints.go

@@ -1,92 +1,12 @@
 package dynamicproxy
 package dynamicproxy
 
 
-import (
-	"errors"
-	"net/url"
-	"strings"
+import "fmt"
 
 
-	"imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
-)
+/*
+	Endpoint Functions
+*/
 
 
-// Prepare proxy route generate a proxy handler service object for your endpoint
-func (router *Router) PrepareProxyRoute(endpoint *ProxyEndpoint) (*ProxyEndpoint, error) {
-	//Filter the tailing slash if any
-	domain := endpoint.Domain
-	if domain[len(domain)-1:] == "/" {
-		domain = domain[:len(domain)-1]
-	}
-	endpoint.Domain = domain
-
-	//Parse the web proxy endpoint
-	webProxyEndpoint := domain
-	if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
-		//TLS is not hardcoded in proxy target domain
-		if endpoint.RequireTLS {
-			webProxyEndpoint = "https://" + webProxyEndpoint
-		} else {
-			webProxyEndpoint = "http://" + webProxyEndpoint
-		}
-	}
-
-	//Create a new proxy agent for this root
-	path, err := url.Parse(webProxyEndpoint)
-	if err != nil {
-		return nil, err
-	}
-
-	//Create the proxy routing handler
-	proxy := dpcore.NewDynamicProxyCore(path, "", endpoint.SkipCertValidations)
-	endpoint.proxy = proxy
-	endpoint.parent = router
-
-	//Prepare proxy routing hjandler for each of the virtual directories
-	for _, vdir := range endpoint.VirtualDirectories {
-		domain := vdir.Domain
-		if domain[len(domain)-1:] == "/" {
-			domain = domain[:len(domain)-1]
-		}
-
-		//Parse the web proxy endpoint
-		webProxyEndpoint = domain
-		if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
-			//TLS is not hardcoded in proxy target domain
-			if vdir.RequireTLS {
-				webProxyEndpoint = "https://" + webProxyEndpoint
-			} else {
-				webProxyEndpoint = "http://" + webProxyEndpoint
-			}
-		}
-
-		path, err := url.Parse(webProxyEndpoint)
-		if err != nil {
-			return nil, err
-		}
-
-		proxy := dpcore.NewDynamicProxyCore(path, vdir.MatchingPath, vdir.SkipCertValidations)
-		vdir.proxy = proxy
-	}
-
-	return endpoint, nil
-}
-
-// Add Proxy Route to current runtime. Call to PrepareProxyRoute before adding to runtime
-func (router *Router) AddProxyRouteToRuntime(endpoint *ProxyEndpoint) error {
-	if endpoint.proxy == nil {
-		//This endpoint is not prepared
-		return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
-	}
-	// Push record into running subdomain endpoints
-	router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
-	return nil
-}
-
-// Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
-func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
-	if endpoint.proxy == nil {
-		//This endpoint is not prepared
-		return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
-	}
-	// Push record into running root endpoints
-	router.Root = endpoint
+func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
+	fmt.Println(requestURI)
 	return nil
 	return nil
 }
 }

+ 3 - 3
mod/dynamicproxy/proxyRequestHandler.go

@@ -129,8 +129,8 @@ func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, targe
 }
 }
 
 
 // Handle vdir type request
 // Handle vdir type request
-func (h *ProxyHandler) vdirRequest(w http.ResponseWriter, r *http.Request, target *ProxyEndpoint) {
-	rewriteURL := h.Parent.rewriteURL(target.RootOrMatchingDomain, r.RequestURI)
+func (h *ProxyHandler) vdirRequest(w http.ResponseWriter, r *http.Request, target *VirtualDirectoryEndpoint) {
+	rewriteURL := h.Parent.rewriteURL(target.MatchingPath, r.RequestURI)
 	r.URL, _ = url.Parse(rewriteURL)
 	r.URL, _ = url.Parse(rewriteURL)
 
 
 	r.Header.Set("X-Forwarded-Host", r.Host)
 	r.Header.Set("X-Forwarded-Host", r.Host)
@@ -164,7 +164,7 @@ func (h *ProxyHandler) vdirRequest(w http.ResponseWriter, r *http.Request, targe
 		ProxyDomain:  target.Domain,
 		ProxyDomain:  target.Domain,
 		OriginalHost: originalHostHeader,
 		OriginalHost: originalHostHeader,
 		UseTLS:       target.RequireTLS,
 		UseTLS:       target.RequireTLS,
-		PathPrefix:   target.RootOrMatchingDomain,
+		PathPrefix:   target.MatchingPath,
 	})
 	})
 
 
 	var dnsError *net.DNSError
 	var dnsError *net.DNSError

+ 99 - 0
mod/dynamicproxy/router.go

@@ -0,0 +1,99 @@
+package dynamicproxy
+
+import (
+	"errors"
+	"net/url"
+	"strings"
+
+	"imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
+)
+
+/*
+	Dynamic Proxy Router Functions
+
+	This script handle the proxy rules router spawning
+	and preparation
+*/
+
+// Prepare proxy route generate a proxy handler service object for your endpoint
+func (router *Router) PrepareProxyRoute(endpoint *ProxyEndpoint) (*ProxyEndpoint, error) {
+	//Filter the tailing slash if any
+	domain := endpoint.Domain
+	if domain[len(domain)-1:] == "/" {
+		domain = domain[:len(domain)-1]
+	}
+	endpoint.Domain = domain
+
+	//Parse the web proxy endpoint
+	webProxyEndpoint := domain
+	if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
+		//TLS is not hardcoded in proxy target domain
+		if endpoint.RequireTLS {
+			webProxyEndpoint = "https://" + webProxyEndpoint
+		} else {
+			webProxyEndpoint = "http://" + webProxyEndpoint
+		}
+	}
+
+	//Create a new proxy agent for this root
+	path, err := url.Parse(webProxyEndpoint)
+	if err != nil {
+		return nil, err
+	}
+
+	//Create the proxy routing handler
+	proxy := dpcore.NewDynamicProxyCore(path, "", endpoint.SkipCertValidations)
+	endpoint.proxy = proxy
+	endpoint.parent = router
+
+	//Prepare proxy routing hjandler for each of the virtual directories
+	for _, vdir := range endpoint.VirtualDirectories {
+		domain := vdir.Domain
+		if domain[len(domain)-1:] == "/" {
+			domain = domain[:len(domain)-1]
+		}
+
+		//Parse the web proxy endpoint
+		webProxyEndpoint = domain
+		if !strings.HasPrefix("http://", domain) && !strings.HasPrefix("https://", domain) {
+			//TLS is not hardcoded in proxy target domain
+			if vdir.RequireTLS {
+				webProxyEndpoint = "https://" + webProxyEndpoint
+			} else {
+				webProxyEndpoint = "http://" + webProxyEndpoint
+			}
+		}
+
+		path, err := url.Parse(webProxyEndpoint)
+		if err != nil {
+			return nil, err
+		}
+
+		proxy := dpcore.NewDynamicProxyCore(path, vdir.MatchingPath, vdir.SkipCertValidations)
+		vdir.proxy = proxy
+	}
+
+	return endpoint, nil
+}
+
+// Add Proxy Route to current runtime. Call to PrepareProxyRoute before adding to runtime
+func (router *Router) AddProxyRouteToRuntime(endpoint *ProxyEndpoint) error {
+	if endpoint.proxy == nil {
+		//This endpoint is not prepared
+		return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
+	}
+	// Push record into running subdomain endpoints
+	router.ProxyEndpoints.Store(endpoint.RootOrMatchingDomain, endpoint)
+	return nil
+}
+
+// Set given Proxy Route as Root. Call to PrepareProxyRoute before adding to runtime
+func (router *Router) SetProxyRouteAsRoot(endpoint *ProxyEndpoint) error {
+	if endpoint.proxy == nil {
+		//This endpoint is not prepared
+		return errors.New("proxy endpoint not ready. Use PrepareProxyRoute before adding to runtime")
+	}
+	// Push record into running root endpoints
+	router.Root = endpoint
+	return nil
+}

+ 8 - 1
web/components/vdir.html

@@ -160,9 +160,16 @@
                     //List the vdirs
                     //List the vdirs
                     console.log(data);
                     console.log(data);
                     data.forEach(vdir => {
                     data.forEach(vdir => {
+                        if (vdir.RequireTLS){
+                            tlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
+                            if (vdir.SkipCertValidations){
+                                tlsIcon = `<i class="yellow lock icon" title="TLS/SSL mode without verification"></i>`
+                            }
+                        }
+                        
                         $("#vdirList").append(`<tr>
                         $("#vdirList").append(`<tr>
                             <td data-label="" editable="false">${vdir.MatchingPath}</td>
                             <td data-label="" editable="false">${vdir.MatchingPath}</td>
-                            <td data-label="" editable="true" datatype="domain">${vdir.Domain}</td>
+                            <td data-label="" editable="true" datatype="domain">${vdir.Domain} ${tlsIcon}</td>
                             <td class="center aligned" editable="true" datatype="action" data-label="">
                             <td class="center aligned" editable="true" datatype="action" data-label="">
                                 <button class="ui circular mini basic icon button editBtn" onclick='editEndpoint("vdir","${vdir.RootOrMatchingDomain}")'><i class="edit icon"></i></button>
                                 <button class="ui circular mini basic icon button editBtn" onclick='editEndpoint("vdir","${vdir.RootOrMatchingDomain}")'><i class="edit icon"></i></button>
                                 <button class="ui circular mini red basic icon button"  onclick='deleteEndpoint("vdir","${vdir.RootOrMatchingDomain}")'><i class="trash icon"></i></button>
                                 <button class="ui circular mini red basic icon button"  onclick='deleteEndpoint("vdir","${vdir.RootOrMatchingDomain}")'><i class="trash icon"></i></button>