Forráskód Böngészése

auto update script executed

Toby Chui 1 éve
szülő
commit
91dcda6716

+ 2 - 1
api.go

@@ -56,7 +56,8 @@ func initAPIs() {
 	authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
 	authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
 	authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck)
 	authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck)
 	//Reverse proxy root related APIs
 	//Reverse proxy root related APIs
-	authRouter.HandleFunc("/api/proxy/root/updateOptions", HandleRootOptionsUpdate)
+	authRouter.HandleFunc("/api/proxy/root/listOptions", HandleRootRouteOptionList)
+	authRouter.HandleFunc("/api/proxy/root/updateOptions", HandleRootRouteOptionsUpdate)
 	//Reverse proxy auth related APIs
 	//Reverse proxy auth related APIs
 	authRouter.HandleFunc("/api/proxy/auth/exceptions/list", ListProxyBasicAuthExceptionPaths)
 	authRouter.HandleFunc("/api/proxy/auth/exceptions/list", ListProxyBasicAuthExceptionPaths)
 	authRouter.HandleFunc("/api/proxy/auth/exceptions/add", AddProxyBasicAuthExceptionPaths)
 	authRouter.HandleFunc("/api/proxy/auth/exceptions/add", AddProxyBasicAuthExceptionPaths)

+ 42 - 23
mod/dynamicproxy/Server.go

@@ -3,6 +3,7 @@ package dynamicproxy
 import (
 import (
 	_ "embed"
 	_ "embed"
 	"errors"
 	"errors"
+	"fmt"
 	"log"
 	"log"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
@@ -116,37 +117,55 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 			http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
 			http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
 		} else {
 		} else {
 			//Passthrough the request to root
 			//Passthrough the request to root
-
-			h.proxyRequest(w, r, h.Parent.Root)
+			h.handleRootRouting(w, r)
 		}
 		}
 	} else {
 	} else {
 		//No routing rules found.
 		//No routing rules found.
-		if h.Parent.RootRoutingOptions.EnableRedirectForUnsetRules {
-			//Route to custom domain
-			if h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget == "" {
-				//Not set. Redirect to first level of domain redirectable
-				fld, err := h.getTopLevelRedirectableDomain(domainOnly)
-				if err != nil {
-					//Redirect to proxy root
-					log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
-					h.proxyRequest(w, r, h.Parent.Root)
-				} else {
-					log.Println("[Router] Redirecting request from " + domainOnly + " to " + fld)
-					h.logRequest(r, false, 307, "root-redirect", domainOnly)
-					http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
-				}
-				return
+		h.handleRootRouting(w, r)
+	}
+}
+
+/*
+handleRootRouting
+
+This function handle root routing situations where there are no subdomain
+, vdir or special routing rule matches the requested URI.
+
+Once entered this routing segment, the root routing options will take over
+for the routing logic.
+*/
+func (h *ProxyHandler) handleRootRouting(w http.ResponseWriter, r *http.Request) {
+	domainOnly := r.Host
+	if strings.Contains(r.Host, ":") {
+		hostPath := strings.Split(r.Host, ":")
+		domainOnly = hostPath[0]
+	}
+
+	if h.Parent.RootRoutingOptions.EnableRedirectForUnsetRules {
+		//Route to custom domain
+		fmt.Println("test1", h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget)
+		if h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget == "" {
+			//Not set. Redirect to first level of domain redirectable
+			fld, err := h.getTopLevelRedirectableDomain(domainOnly)
+			if err != nil {
+				//Redirect to proxy root
+				log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
+				h.proxyRequest(w, r, h.Parent.Root)
 			} else {
 			} else {
-				//Redirect to target
+				log.Println("[Router] Redirecting request from " + domainOnly + " to " + fld)
 				h.logRequest(r, false, 307, "root-redirect", domainOnly)
 				h.logRequest(r, false, 307, "root-redirect", domainOnly)
-				http.Redirect(w, r, h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
-				return
+				http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
 			}
 			}
+			return
 		} else {
 		} else {
-			//Route to root
-			h.proxyRequest(w, r, h.Parent.Root)
+			//Redirect to target
+			h.logRequest(r, false, 307, "root-redirect", domainOnly)
+			http.Redirect(w, r, h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
+			return
 		}
 		}
-
+	} else {
+		//Route to root
+		h.proxyRequest(w, r, h.Parent.Root)
 	}
 	}
 }
 }
 
 

+ 5 - 2
mod/dynamicproxy/rootRoute.go

@@ -43,6 +43,9 @@ func loadRootRoutingOptionsFromFile() (*RootRoutingOptions, error) {
 	return &newRootOption, nil
 	return &newRootOption, nil
 }
 }
 
 
-func (opt *RootRoutingOptions) SaveToFile() {
-
+// Save the new config to file. Note that this will not overwrite the runtime one
+func (opt *RootRoutingOptions) SaveToFile() error {
+	js, _ := json.MarshalIndent(opt, "", " ")
+	err := os.WriteFile(rootConfigFilepath, js, 0775)
+	return err
 }
 }

+ 4 - 1
mod/geodb/geodb_test.go

@@ -41,7 +41,10 @@ func TestTrieConstruct(t *testing.T) {
 
 
 func TestResolveCountryCodeFromIP(t *testing.T) {
 func TestResolveCountryCodeFromIP(t *testing.T) {
 	// Create a new store
 	// Create a new store
-	store, err := geodb.NewGeoDb(nil)
+	store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{
+		false,
+		false,
+	})
 	if err != nil {
 	if err != nil {
 		t.Errorf("error creating store: %v", err)
 		t.Errorf("error creating store: %v", err)
 		return
 		return

+ 14 - 1
reverseproxy.go

@@ -815,7 +815,14 @@ func HandleIncomingPortSet(w http.ResponseWriter, r *http.Request) {
 	utils.SendOK(w)
 	utils.SendOK(w)
 }
 }
 
 
-func HandleRootOptionsUpdate(w http.ResponseWriter, r *http.Request) {
+// Handle list of root route options
+func HandleRootRouteOptionList(w http.ResponseWriter, r *http.Request) {
+	js, _ := json.Marshal(dynamicProxyRouter.RootRoutingOptions)
+	utils.SendJSONResponse(w, string(js))
+}
+
+// Handle update of the root route edge case options. See dynamicproxy/rootRoute.go
+func HandleRootRouteOptionsUpdate(w http.ResponseWriter, r *http.Request) {
 	enableUnsetSubdomainRedirect, err := utils.PostBool(r, "unsetRedirect")
 	enableUnsetSubdomainRedirect, err := utils.PostBool(r, "unsetRedirect")
 	if err != nil {
 	if err != nil {
 		utils.SendErrorResponse(w, err.Error())
 		utils.SendErrorResponse(w, err.Error())
@@ -837,5 +844,11 @@ func HandleRootOptionsUpdate(w http.ResponseWriter, r *http.Request) {
 	}
 	}
 
 
 	dynamicProxyRouter.RootRoutingOptions = &newRootOption
 	dynamicProxyRouter.RootRoutingOptions = &newRootOption
+	err = newRootOption.SaveToFile()
+	if err != nil {
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
 
 
+	utils.SendOK(w)
 }
 }

+ 28 - 17
web/components/rproot.html

@@ -26,7 +26,7 @@
                     <label>Enable redirect for unset subdomains <br><small>Redirect subdomain that is not found to custom domain</small></label>
                     <label>Enable redirect for unset subdomains <br><small>Redirect subdomain that is not found to custom domain</small></label>
                 </div>
                 </div>
             </div>
             </div>
-            <div class="ui basic segment" id="unsetRedirectDomain" style="background-color: #f7f7f7; border-radius: 1em; margin-left: 2em; padding-left: 2em; display:none;">
+            <div class="ui basic segment" id="unsetRedirectDomainWrapper" style="background-color: #f7f7f7; border-radius: 1em; margin-left: 2em; padding-left: 2em; display:none;">
                 <div style="
                 <div style="
                     position: absolute;
                     position: absolute;
                     top:0;
                     top:0;
@@ -42,7 +42,7 @@
                 <div class="field">
                 <div class="field">
                     <label>Redirect target domain</label>
                     <label>Redirect target domain</label>
                     <div class="ui input">
                     <div class="ui input">
-                        <input type="text" placeholder="http://example.com">
+                        <input id="unsetRedirectDomain" type="text" placeholder="http://example.com">
                     </div>
                     </div>
                     <small>Unset subdomain will be redirected to the link above. Remember to include the protocol (e.g. http:// or https://)<br>
                     <small>Unset subdomain will be redirected to the link above. Remember to include the protocol (e.g. http:// or https://)<br>
                     Leave empty for redirecting to upper level domain (e.g. notfound.example.com <i class="right arrow icon"></i> example.com)</small>
                     Leave empty for redirecting to upper level domain (e.g. notfound.example.com <i class="right arrow icon"></i> example.com)</small>
@@ -96,18 +96,26 @@
     //Toggle the display status of the input box for domain setting
     //Toggle the display status of the input box for domain setting
     function updateRedirectionDomainSettingInputBox(useRedirect){
     function updateRedirectionDomainSettingInputBox(useRedirect){
         if(useRedirect){
         if(useRedirect){
-            $("#unsetRedirectDomain").stop().finish().slideDown("fast");
+            $("#unsetRedirectDomainWrapper").stop().finish().slideDown("fast");
         }else{
         }else{
-            $("#unsetRedirectDomain").stop().finish().slideUp("fast");
+            $("#unsetRedirectDomainWrapper").stop().finish().slideUp("fast");
         }
         }
     }
     }
 
 
     function checkCustomRedirectForUnsetSubd(){
     function checkCustomRedirectForUnsetSubd(){
-        $("#unsetRedirect").on("change", function(){
-            let useRedirect = $(this)[0].checked;
-            updateRedirectionDomainSettingInputBox(useRedirect);
-            
+        $.get("/api/proxy/root/listOptions", function(data){
+            $("#unsetRedirect")[0].checked = data.EnableRedirectForUnsetRules || false;
+            $("#disableRootTLS")[0].checked = data.DisableHTTPSOnProxyRoot || false;
+            $("#unsetRedirectDomain").val(data.UnsetRuleRedirectTarget);
+            updateRedirectionDomainSettingInputBox(data.EnableRedirectForUnsetRules);
+
+            //Bind event to the checkbox
+            $("#unsetRedirect").off("change").on("change", function(){
+                let useRedirect = $("#unsetRedirect")[0].checked;
+                updateRedirectionDomainSettingInputBox(useRedirect);
+            });
         })
         })
+       
     }
     }
     checkCustomRedirectForUnsetSubd();
     checkCustomRedirectForUnsetSubd();
 
 
@@ -155,7 +163,7 @@
             data: {"type": "root", tls: rootReqTls, ep: newpr},
             data: {"type": "root", tls: rootReqTls, ep: newpr},
             success: function(data){
             success: function(data){
                 if (data.error != undefined){
                 if (data.error != undefined){
-                    alert(data.error);
+                    msgbox(data.error, false, 5000);
                 }else{
                 }else{
                     //OK
                     //OK
                     initRootInfo();
                     initRootInfo();
@@ -167,17 +175,20 @@
     }
     }
 
 
     function updateRootOptions(){
     function updateRootOptions(){
-        const jsonData = {
-            
-        };
-
         $.ajax({
         $.ajax({
             type: "POST",
             type: "POST",
             url: "/api/proxy/root/updateOptions",
             url: "/api/proxy/root/updateOptions",
-            contentType: "application/json",
-            data: JSON.stringify(jsonData),
-            success: function(response) {
-                console.log("Success:", response);
+            data: {
+                unsetRedirect: $("#unsetRedirect")[0].checked,
+                unsetRedirectTarget: $("#unsetRedirectDomain").val().trim(),
+                disableRootHttps: $("#disableRootTLS")[0].checked,
+            },
+            success: function(data) {
+               if (data.error != undefined){
+                    msgbox(data.error, false);
+               }else{
+                    msgbox("Root Routing Options updated");
+               }
             },
             },
             error: function(error) {
             error: function(error) {
                 console.log("Error:", error);
                 console.log("Error:", error);