Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
add0600258

+ 1 - 0
api.go

@@ -60,6 +60,7 @@ func initAPIs() {
 	authRouter.HandleFunc("/api/proxy/vdir/list", ReverseProxyListVdir)
 	authRouter.HandleFunc("/api/proxy/vdir/add", ReverseProxyAddVdir)
 	authRouter.HandleFunc("/api/proxy/vdir/del", ReverseProxyDeleteVdir)
+	authRouter.HandleFunc("/api/proxy/vdir/edit", ReverseProxyEditVdir)
 
 	//Reverse proxy auth related APIs
 	authRouter.HandleFunc("/api/proxy/auth/exceptions/list", ListProxyBasicAuthExceptionPaths)

+ 0 - 2
mod/dynamicproxy/basicAuth.go

@@ -2,7 +2,6 @@ package dynamicproxy
 
 import (
 	"errors"
-	"fmt"
 	"net/http"
 	"strings"
 
@@ -21,7 +20,6 @@ func (h *ProxyHandler) handleBasicAuthRouting(w http.ResponseWriter, r *http.Req
 		//Check if the current path matches the exception rules
 		for _, exceptionRule := range pe.BasicAuthExceptionRules {
 			if strings.HasPrefix(r.RequestURI, exceptionRule.PathPrefix) {
-				fmt.Println(r.RequestURI, exceptionRule.PathPrefix)
 				//This path is excluded from basic auth
 				return nil
 			}

+ 40 - 0
mod/dynamicproxy/endpoints.go

@@ -1,6 +1,7 @@
 package dynamicproxy
 
 import (
+	"encoding/json"
 	"errors"
 	"strings"
 )
@@ -29,6 +30,7 @@ func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath stri
 	return nil
 }
 
+// Delete a vdir rule by its matching path
 func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
 	entryFound := false
 	newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
@@ -47,3 +49,41 @@ func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath s
 	}
 	return errors.New("target virtual directory routing rule not found")
 }
+
+// Delete a vdir rule by its matching path
+func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
+	//Check for matching path duplicate
+	if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
+		return nil, errors.New("rule with same matching path already exists")
+	}
+
+	//Append it to the list of virtual directory
+	ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
+
+	//Prepare to replace the current routing rule
+	parentRouter := ep.parent
+	readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
+	if err != nil {
+		return nil, err
+	}
+
+	if ep.ProxyType == ProxyType_Root {
+		parentRouter.Root = readyRoutingRule
+	} else if ep.ProxyType == ProxyType_Host {
+		ep.Remove()
+		parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
+	} else {
+		return nil, errors.New("unsupported proxy type")
+	}
+
+	return readyRoutingRule, nil
+}
+
+// Create a deep clone object of the proxy endpoint
+// Note the returned object is not activated. Call to prepare function before pushing into runtime
+func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
+	clonedProxyEndpoint := ProxyEndpoint{}
+	js, _ := json.Marshal(ep)
+	json.Unmarshal(js, &clonedProxyEndpoint)
+	return &clonedProxyEndpoint
+}

+ 0 - 2
reverseproxy.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"encoding/json"
-	"fmt"
 	"net/http"
 	"path/filepath"
 	"sort"
@@ -428,7 +427,6 @@ func DeleteProxyEndpoint(w http.ResponseWriter, r *http.Request) {
 	}
 
 	//Remove the config from file
-	fmt.Println(ep)
 	err = RemoveReverseProxyConfig(ep)
 	if err != nil {
 		utils.SendErrorResponse(w, err.Error())

+ 89 - 16
vdir.go

@@ -132,26 +132,15 @@ func ReverseProxyAddVdir(w http.ResponseWriter, r *http.Request) {
 		SkipCertValidations: skipValid,
 	}
 
-	//Append the virtual directory entry to the endpoint
-	targetProxyEndpoint.VirtualDirectories = append(targetProxyEndpoint.VirtualDirectories, &newVirtualDirectoryRouter)
-
-	//Prepare to replace the current routing rule
-	readyRoutingRule, err := dynamicProxyRouter.PrepareProxyRoute(targetProxyEndpoint)
+	//Add Virtual Directory Rule to this Proxy Endpoint
+	activatedProxyEndpoint, err := targetProxyEndpoint.AddVirtualDirectoryRule(&newVirtualDirectoryRouter)
 	if err != nil {
 		utils.SendErrorResponse(w, err.Error())
 		return
 	}
 
-	if eptype == "root" {
-		//Replace the root router
-		dynamicProxyRouter.Root = readyRoutingRule
-	} else {
-		targetProxyEndpoint.Remove()
-		dynamicProxyRouter.AddProxyRouteToRuntime(readyRoutingRule)
-	}
-
 	//Save it to file
-	SaveReverseProxyConfig(targetProxyEndpoint)
+	SaveReverseProxyConfig(activatedProxyEndpoint)
 
 	// Update uptime monitor
 	UpdateUptimeMonitorTargets()
@@ -194,7 +183,7 @@ func ReverseProxyDeleteVdir(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	//Load the vdir from endpoint
+	//Delete the Vdir from endpoint
 	err = targetEndpoint.RemoveVirtualDirectoryRuleByMatchingPath(vdir)
 	if err != nil {
 		utils.SendErrorResponse(w, err.Error())
@@ -209,9 +198,93 @@ func ReverseProxyDeleteVdir(w http.ResponseWriter, r *http.Request) {
 	}
 
 	utils.SendOK(w)
-
 }
 
+// Handle update of reverse proxy vdir rules
 func ReverseProxyEditVdir(w http.ResponseWriter, r *http.Request) {
+	eptype, err := utils.PostPara(r, "type") //Support root and host
+	if err != nil {
+		utils.SendErrorResponse(w, "type not defined")
+		return
+	}
+
+	vdir, err := utils.PostPara(r, "vdir")
+	if err != nil {
+		utils.SendErrorResponse(w, "vdir matching key not defined")
+		return
+	}
+
+	domain, err := utils.PostPara(r, "domain")
+	if err != nil {
+		utils.SendErrorResponse(w, "target domain not defined")
+		return
+	}
+
+	reqTLSStr, err := utils.PostPara(r, "reqTLS")
+	if err != nil {
+		//Assume false
+		reqTLSStr = "false"
+	}
+	reqTLS := (reqTLSStr == "true")
+
+	skipValidStr, err := utils.PostPara(r, "skipValid")
+	if err != nil {
+		//Assume false
+		skipValidStr = "false"
+	}
+
+	skipValid := (skipValidStr == "true")
+
+	var targetEndpoint *dynamicproxy.ProxyEndpoint
+	if eptype == "root" {
+		targetEndpoint = dynamicProxyRouter.Root
+
+	} else if eptype == "host" {
+		//Proxy rule
+		matchingPath, err := utils.PostPara(r, "path")
+		if err != nil {
+			utils.SendErrorResponse(w, "matching path not defined")
+			return
+		}
+
+		ept, err := dynamicProxyRouter.LoadProxy(matchingPath)
+		if err != nil {
+			utils.SendErrorResponse(w, "target proxy rule not found")
+			return
+		}
 
+		targetEndpoint = ept
+	} else {
+		utils.SendErrorResponse(w, "invalid endpoint type given")
+		return
+	}
+
+	//Check if the target vdir exists
+	if targetEndpoint.GetVirtualDirectoryRuleByMatchingPath(vdir) == nil {
+		utils.SendErrorResponse(w, "target virtual directory rule not exists")
+		return
+	}
+
+	//Overwrite the target endpoint
+	newVdirRule := dynamicproxy.VirtualDirectoryEndpoint{
+		MatchingPath:        vdir,
+		Domain:              domain,
+		RequireTLS:          reqTLS,
+		SkipCertValidations: skipValid,
+		Disabled:            false,
+	}
+
+	targetEndpoint.RemoveVirtualDirectoryRuleByMatchingPath(vdir)
+	activatedProxyEndpoint, err := targetEndpoint.AddVirtualDirectoryRule(&newVdirRule)
+	if err != nil {
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
+
+	//Save changes to file
+	SaveReverseProxyConfig(activatedProxyEndpoint)
+
+	UpdateUptimeMonitorTargets()
+
+	utils.SendOK(w)
 }

+ 1 - 1
web/components/cert.html

@@ -16,7 +16,7 @@
     
     <div class="ui divider"></div>
     <h4>Default Certificates</h4>
-        <small>When there are no matching certificate for the requested server name, reverse proxy router will always fallback to this one.<br>Note that you need both of them uploaded for it to fallback properly</small></p>
+        <p>When there are no matching certificate for the requested server name, reverse proxy router will always fallback to this one.<br>Note that you need both of them uploaded for it to fallback properly</p>
         <table class="ui very basic unstackable celled table">
             <thead>
                 <tr><th class="no-sort">Key Type</th>

+ 0 - 19
web/components/redirection.html

@@ -72,25 +72,10 @@
           <i class="ui green checkmark icon"></i> Redirection Rules Added
         </div>
         <br><br>
-
-        <div class="advancezone ui basic segment">
-          <div class="ui accordion advanceSettings">
-            <div class="title">
-              <i class="dropdown icon"></i>
-              Advance Options
-            </div>
-            <div class="content">
-              <p>If you need custom header, content or status code other than basic redirects, you can use the advance path rules editor.</p>
-              <button class="ui black basic button" onclick="createAdvanceRules();"><i class="ui black external icon"></i> Open Advance Rules Editor</button>
-            </div>
-          </div>
-        </div>
-
     </div>
   </div>
 </div>
 <script>
-    $(".advanceSettings").accordion();
     
   /*
     Redirection functions
@@ -151,10 +136,6 @@
         }
     }
 
-    function createAdvanceRules(){
-      showSideWrapper("snippet/advancePathRules.html?t=" + Date.now(), true);
-    }
-
     function initRedirectionRuleList(){
         $("#redirectionRuleList").html("");
         $.get("/api/redirect/list", function(data){

+ 1 - 0
web/components/rproot.html

@@ -92,6 +92,7 @@
             $("#proxyRoot").parent().addClass("disabled");
             $("#rootReqTLS").parent().checkbox("set unchecked");
             $("#rootReqTLS").parent().addClass("disabled");
+            $("#useRootProxyRouterForVdir").parent().removeClass("disabled");
             currentDefaultSiteOption = 0;
         }else if (selectedDefaultSite == "proxy"){
             $("#defaultSiteProxyOptions").show();

+ 34 - 4
web/components/vdir.html

@@ -289,11 +289,10 @@
     function deleteVdir(matchingPath, endpoint){
         var epType = "host";
         var path = $("#vdirBaseRoutingRule").val().trim();
-        if (endpoint == "root"){
-            eptype = "root";
+        if (endpoint.trim() == "root"){
+            epType = "root";
             path = "";
         }
-
         $.ajax({
             url: "/api/proxy/vdir/del",
             method: "POST",
@@ -368,12 +367,43 @@
     function saveVdirInlineEdit(mathingPath){
         mathingPath = mathingPath.hexDecode();
 
+        var epType = "host";
+        var path = $("#vdirBaseRoutingRule").val().trim();
+        if ($("#useRootProxyRouterForVdir")[0].checked){
+            epType = "root";
+            path = "";
+        }
+
         //Load new setting from inline editor
         let newDomain = $("#vdirList").find(".Domain").val();
         let requireTLS = $("#vdirList").find(".RequireTLS")[0].checked;
         let skipValidation = $("#vdirList").find(".SkipCertValidations")[0].checked;
 
-        console.log(mathingPath, newDomain, requireTLS, skipValidation)
+        //console.log(mathingPath, newDomain, requireTLS, skipValidation);
+
+        $.ajax({
+            url: "/api/proxy/vdir/edit",
+            method: "POST",
+            data: {
+                "type": epType,
+                "vdir": mathingPath,
+                "domain":newDomain,
+                "path":path,
+                "reqTLS":requireTLS,
+                "skipValid": skipValidation
+            },
+            success: function(data){
+                if (data.error != undefined){
+                    msgbox(data.error, false);
+                }else{
+                    msgbox("Virtual Directory rule updated", true);
+                    exitVdirInlineEdit();
+                }
+            },
+            error: function(){
+                msgbox("unknown error occured", false)
+            }
+        })
     }
 
     function exitVdirInlineEdit(){