소스 검색

Fixed racing conditions in stream proxy start stop handler

Toby Chui 9 달 전
부모
커밋
ad3e7a11d4
2개의 변경된 파일30개의 추가작업 그리고 9개의 파일을 삭제
  1. 27 6
      mod/streamproxy/streamproxy.go
  2. 3 3
      web/components/streamprox.html

+ 27 - 6
mod/streamproxy/streamproxy.go

@@ -6,6 +6,7 @@ import (
 	"net"
 	"sync"
 	"sync/atomic"
+	"time"
 
 	"github.com/google/uuid"
 	"imuslab.com/zoraxy/mod/database"
@@ -88,6 +89,11 @@ func NewStreamProxy(options *Options) *Manager {
 	//Inject manager into the rules
 	for _, rule := range previousRules {
 		rule.parent = &thisManager
+		if rule.Running {
+			//This was previously running. Start it again
+			log.Println("[Stream Proxy] Resuming stream proxy rule " + rule.Name)
+			rule.Start()
+		}
 	}
 
 	thisManager.Configs = previousRules
@@ -164,6 +170,11 @@ func (m *Manager) EditConfig(configUUID string, newName string, newListeningAddr
 
 	m.SaveConfigToDatabase()
 
+	//Check if config is running. If yes, restart it
+	if foundConfig.IsRunning() {
+		foundConfig.Restart()
+	}
+
 	return nil
 }
 
@@ -196,13 +207,11 @@ func (c *ProxyRelayConfig) Start() error {
 
 	// Create a stopChan to control the loop
 	tcpStopChan := make(chan bool)
-	c.tcpStopChan = tcpStopChan
-
 	udpStopChan := make(chan bool)
-	c.udpStopChan = udpStopChan
 
 	//Start the proxy service
 	if c.UseUDP {
+		c.udpStopChan = udpStopChan
 		go func() {
 			if !c.UseTCP {
 				//By default running state shows TCP proxy. If TCP is not in use, UDP is shown instead
@@ -219,6 +228,7 @@ func (c *ProxyRelayConfig) Start() error {
 	}
 
 	if c.UseTCP {
+		c.tcpStopChan = tcpStopChan
 		go func() {
 			//Default to transport mode
 			c.Running = true
@@ -235,25 +245,36 @@ func (c *ProxyRelayConfig) Start() error {
 	return nil
 }
 
-// Stop a running proxy if running
+// Return if a proxy config is running
 func (c *ProxyRelayConfig) IsRunning() bool {
 	return c.tcpStopChan != nil || c.udpStopChan != nil
 }
 
+// Restart a proxy config
+func (c *ProxyRelayConfig) Restart() {
+	if c.IsRunning() {
+		c.Stop()
+	}
+	time.Sleep(300 * time.Millisecond)
+	c.Start()
+}
+
 // Stop a running proxy if running
 func (c *ProxyRelayConfig) Stop() {
-	log.Println("[PROXY] Stopping Stream Proxy " + c.Name)
+	log.Println("[STREAM PROXY] Stopping Stream Proxy " + c.Name)
 
 	if c.udpStopChan != nil {
+		log.Println("[STREAM PROXY] Stopping UDP for " + c.Name)
 		c.udpStopChan <- true
 		c.udpStopChan = nil
 	}
 
 	if c.tcpStopChan != nil {
+		log.Println("[STREAM PROXY] Stopping TCP for " + c.Name)
 		c.tcpStopChan <- true
 		c.tcpStopChan = nil
 	}
 
-	log.Println("[PROXY] Stopped Stream Proxy " + c.Name)
+	log.Println("[STREAM PROXY] Stopped Stream Proxy " + c.Name)
 	c.Running = false
 }

+ 3 - 3
web/components/streamprox.html

@@ -8,12 +8,12 @@
         <h4>TCP / UDP Proxy Rules</h4>
         <p>A list of TCP proxy rules created on this host. To enable them, use the toggle button on the right.</p>
         <div style="overflow-x: auto; min-height: 400px;">
-            <table id="proxyTable" class="ui celled unstackable table">
+            <table id="proxyTable" class="ui celled basic unstackable table">
                 <thead>
                     <tr>
                         <th>Name</th>
-                        <th>Port/Addr A</th>
-                        <th>Port/Addr B</th>
+                        <th>Listening Address</th>
+                        <th>Target Address</th>
                         <th>Mode</th>
                         <th>Timeout (s)</th>
                         <th>Actions</th>