Ver Fonte

Optimized auto port check

Toby Chui há 1 mês atrás
pai
commit
91bf1c9be7
4 ficheiros alterados com 44 adições e 10 exclusões
  1. 10 0
      mod/netutils/netutils.go
  2. 22 4
      reverseproxy.go
  3. 4 4
      web/components/access.html
  4. 8 2
      web/darktheme.css

+ 10 - 0
mod/netutils/netutils.go

@@ -157,3 +157,13 @@ func resolveIpFromDomain(targetIpOrDomain string) string {
 
 	return targetIpAddrString
 }
+
+// Check if the given port is already used by another process
+func CheckIfPortOccupied(portNumber int) bool {
+	listener, err := net.Listen("tcp", ":"+strconv.Itoa(portNumber))
+	if err != nil {
+		return true
+	}
+	listener.Close()
+	return false
+}

+ 22 - 4
reverseproxy.go

@@ -14,6 +14,7 @@ import (
 	"imuslab.com/zoraxy/mod/dynamicproxy/loadbalance"
 	"imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
 	"imuslab.com/zoraxy/mod/dynamicproxy/rewrite"
+	"imuslab.com/zoraxy/mod/netutils"
 	"imuslab.com/zoraxy/mod/uptime"
 	"imuslab.com/zoraxy/mod/utils"
 )
@@ -28,10 +29,22 @@ func ReverseProxtInit() {
 		Load Reverse Proxy Global Settings
 	*/
 	inboundPort := 443
+	autoStartReverseProxy := true
 	if sysdb.KeyExists("settings", "inbound") {
+		//Read settings from database
 		sysdb.Read("settings", "inbound", &inboundPort)
-		SystemWideLogger.Println("Serving inbound port ", inboundPort)
+		if netutils.CheckIfPortOccupied(inboundPort) {
+			autoStartReverseProxy = false
+			SystemWideLogger.Println("Inbound port ", inboundPort, " is occupied. Change the listening port in the webmin panel and press \"Start Service\" to start reverse proxy service")
+		} else {
+			SystemWideLogger.Println("Serving inbound port ", inboundPort)
+		}
 	} else {
+		//Default port
+		if netutils.CheckIfPortOccupied(inboundPort) {
+			inboundPort = 8743
+			SystemWideLogger.Println("Port 443 is occupied. Switching to backup port 8743 instead")
+		}
 		SystemWideLogger.Println("Inbound port not set. Using default (443)")
 	}
 
@@ -60,6 +73,9 @@ func ReverseProxtInit() {
 	}
 
 	listenOnPort80 := true
+	if netutils.CheckIfPortOccupied(80) {
+		listenOnPort80 = false
+	}
 	sysdb.Read("settings", "listenP80", &listenOnPort80)
 	if listenOnPort80 {
 		SystemWideLogger.Println("Port 80 listener enabled")
@@ -136,9 +152,11 @@ func ReverseProxtInit() {
 	//Start Service
 	//Not sure why but delay must be added if you have another
 	//reverse proxy server in front of this service
-	time.Sleep(300 * time.Millisecond)
-	dynamicProxyRouter.StartProxyService()
-	SystemWideLogger.Println("Dynamic Reverse Proxy service started")
+	if autoStartReverseProxy {
+		time.Sleep(300 * time.Millisecond)
+		dynamicProxyRouter.StartProxyService()
+		SystemWideLogger.Println("Dynamic Reverse Proxy service started")
+	}
 
 	//Add all proxy services to uptime monitor
 	//Create a uptime monitor service

+ 4 - 4
web/components/access.html

@@ -1174,7 +1174,7 @@
     }
 
     function removeIpBlacklist(ipaddr){
-        if (confirm("Confirm remove blacklist for " + ipaddr + " ?")){
+        //if (confirm("Confirm remove blacklist for " + ipaddr + " ?")){
             $.cjax({
                 url: "/api/blacklist/ip/remove",
                 type: "POST",
@@ -1191,7 +1191,7 @@
                 }
                 
             });
-        }
+        //}
     }
 
     /* 
@@ -1318,7 +1318,7 @@
     }
 
     function removeIpWhitelist(ipaddr){
-        if (confirm("Confirm remove whitelist for " + ipaddr + " ?")){
+        //if (confirm("Confirm remove whitelist for " + ipaddr + " ?")){
             $.cjax({
                 url: "/api/whitelist/ip/remove",
                 type: "POST",
@@ -1335,7 +1335,7 @@
                 }
                 
             });
-        }
+        //}
     }
 
     /*

+ 8 - 2
web/darktheme.css

@@ -26,6 +26,7 @@ body:not(.darkTheme){
     --buttom_toggle_active: #01dc64;
     --buttom_toggle_disabled: #f2f2f2;
     --table_bg_default: transparent;
+    --status_dot_bg: #e8e8e8;
 
     --theme_background: linear-gradient(60deg, rgb(84, 58, 183) 0%, rgb(0, 172, 193) 100%);
     --theme_background_inverted: linear-gradient(215deg, rgba(38,60,71,1) 13%, rgba(2,3,42,1) 84%);
@@ -53,6 +54,7 @@ body.darkTheme{
     --buttom_toggle_active: #01dc64;
     --buttom_toggle_disabled: #2b2b2b;
     --table_bg_default: #121214;
+    --status_dot_bg: #232323;
 
     --theme_background: linear-gradient(23deg, rgba(2,74,106,1) 17%, rgba(46,12,136,1) 86%);
     --theme_background_inverted: linear-gradient(215deg, rgba(38,60,71,1) 13%, rgba(2,3,42,1) 84%);
@@ -207,6 +209,10 @@ body.darkTheme .ui.toggle.checkbox input:checked ~ label::before{
     background-color: var(--buttom_toggle_active) !important;
 }
 
+body.darkTheme .ui.checkbox:not(.toggle) input[type="checkbox"]{
+    opacity: 100% !important;
+}
+
 #sidemenuBtn{
     border: 1px solid var(--button_border_color) !important;
 }
@@ -991,8 +997,8 @@ body.darkTheme #utm .standardContainer {
 }
 
 body.darkTheme #utm .standardContainer .padding.statusDot {
-    background-color: var(--theme_bg) !important;
-    border: 0.2px solid var(--text_color_inverted) !important;
+    background-color: var(--status_dot_bg) !important;
+    
 }
 
 body.darkTheme .ui.utmloading.segment {