Преглед на файлове

auto update script executed

Toby Chui преди 1 година
родител
ревизия
538881d8d9
променени са 4 файла, в които са добавени 52 реда и са изтрити 1 реда
  1. 1 0
      api.go
  2. 15 0
      mod/dynamicproxy/dynamicproxy.go
  3. 17 0
      reverseproxy.go
  4. 19 1
      web/components/status.html

+ 1 - 0
api.go

@@ -46,6 +46,7 @@ func initAPIs() {
 	authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
 	authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
 	authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
+	authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck)
 
 	//TLS / SSL config
 	authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)

+ 15 - 0
mod/dynamicproxy/dynamicproxy.go

@@ -9,6 +9,7 @@ import (
 	"net/http"
 	"net/url"
 	"strconv"
+	"strings"
 	"sync"
 	"time"
 
@@ -207,6 +208,20 @@ func (router *Router) Restart() error {
 	return err
 }
 
+/*
+	Check if a given request is accessed via a proxied subdomain
+*/
+
+func (router *Router) IsProxiedSubdomain(r *http.Request) bool {
+	hostname := r.Header.Get("X-Forwarded-Host")
+	if hostname == "" {
+		hostname = r.Host
+	}
+	hostname = strings.Split(hostname, ":")[0]
+	subdEndpoint := router.getSubdomainProxyEndpointFromHostname(hostname)
+	return subdEndpoint != nil
+}
+
 /*
 Add an URL into a custom proxy services
 */

+ 17 - 0
reverseproxy.go

@@ -98,6 +98,7 @@ func ReverseProxtInit() {
 }
 
 func ReverseProxyHandleOnOff(w http.ResponseWriter, r *http.Request) {
+
 	enable, _ := utils.PostPara(r, "enable") //Support root, vdir and subd
 	if enable == "true" {
 		err := dynamicProxyRouter.StartProxyService()
@@ -106,6 +107,14 @@ func ReverseProxyHandleOnOff(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 	} else {
+		//Check if it is loopback
+		if dynamicProxyRouter.IsProxiedSubdomain(r) {
+			//Loopback routing. Turning it off will make the user lost control
+			//of the whole system. Do not allow shutdown
+			utils.SendErrorResponse(w, "Unable to shutdown in loopback rp mode. Remove proxy rules for management interface and retry.")
+			return
+		}
+
 		err := dynamicProxyRouter.StopProxyService()
 		if err != nil {
 			utils.SendErrorResponse(w, err.Error())
@@ -267,6 +276,14 @@ 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.
+func HandleManagementProxyCheck(w http.ResponseWriter, r *http.Request) {
+	isProxied := dynamicProxyRouter.IsProxiedSubdomain(r)
+	js, _ := json.Marshal(isProxied)
+	utils.SendJSONResponse(w, string(js))
+}
+
 // Handle incoming port set. Change the current proxy incoming port
 func HandleIncomingPortSet(w http.ResponseWriter, r *http.Request) {
 	newIncomingPort, err := utils.PostPara(r, "incoming")

+ 19 - 1
web/components/status.html

@@ -72,6 +72,10 @@
 <Br>
 <button id="startbtn" class="ui teal button" onclick="startService();">Start Service</button>
 <button id="stopbtn" class="ui red disabled button" onclick="stopService();">Stop Service</button>
+<div id="rploopbackWarning" class="ui segment" style="display:none;">
+    <b><i class="yellow warning icon"></i> Loopback Routing Warning</b><br>
+    <small>This management interface is a loopback proxied service. <br>If you want to shutdown the reverse proxy server, please remove the proxy rule for the management interface and refresh.</small>
+</div>
 <div id="statusErrmsg" class="ui red message" style="display: none;"></div>
 <div class="ui divider"></div>
 <div class="ui two column stackable grid">
@@ -107,12 +111,26 @@
   <br>
   <button class="ui basic green button"><i class="refresh icon"></i> Refresh</button>
 <script>
+    let loopbackProxiedInterface = false;
+    //Initial the start stop button if this is reverse proxied
+    $.get("/api/proxy/requestIsProxied", function(data){
+        if (data == true){
+            //This management interface is reverse proxied by itself
+            //do not allow turning off the proxy
+            $("#stopbtn").addClass("disabled");
+            loopbackProxiedInterface = true;
+            $("#rploopbackWarning").show();
+        }
+    });
+
     //Get the latest server status from proxy server
     function initRPStaste(){
         $.get("/api/proxy/status", function(data){
             if (data.Running == true){
                 $("#startbtn").addClass("disabled");
-                $("#stopbtn").removeClass("disabled");
+                if (!loopbackProxiedInterface){
+                    $("#stopbtn").removeClass("disabled");
+                }
                 $("#serverstatus").addClass("green");
                 $("#statusTitle").text("Online");
                 $("#statusText").text("Serving request on port: " + data.Option.Port);