From deddb17803e87cb9c2d114e70b7776974ac5afa0 Mon Sep 17 00:00:00 2001 From: Toby Chui Date: Sat, 8 Jun 2024 00:33:29 +0800 Subject: [PATCH] Updated Stream Proxy module - Fixed stream proxy stopping racing condition bug - Merged PR #187 - Updated stream proxy UI --- src/emails.go | 11 ++- src/mod/streamproxy/streamproxy.go | 46 +++++++++---- src/web/components/streamprox.html | 105 ++++++----------------------- src/web/main.css | 18 ++--- 4 files changed, 66 insertions(+), 114 deletions(-) diff --git a/src/emails.go b/src/emails.go index 529d25d..93e3d6f 100644 --- a/src/emails.go +++ b/src/emails.go @@ -272,17 +272,14 @@ func HandleNewPasswordSetup(w http.ResponseWriter, r *http.Request) { return } - //Delete the user account - authAgent.UnregisterUser(username) - - //Ok. Set the new password - err = authAgent.CreateUserAccount(username, newPassword, "") - if err != nil { + // Un register the user account + if err := authAgent.UnregisterUser(username); err != nil { utils.SendErrorResponse(w, err.Error()) return } - if err != nil { + //Ok. Set the new password + if err := authAgent.CreateUserAccount(username, newPassword, ""); err != nil { utils.SendErrorResponse(w, err.Error()) return } diff --git a/src/mod/streamproxy/streamproxy.go b/src/mod/streamproxy/streamproxy.go index 1084379..f6c0f17 100644 --- a/src/mod/streamproxy/streamproxy.go +++ b/src/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,22 +207,17 @@ 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 - c.Running = true - } err := c.ForwardUDP(c.ListeningAddress, c.ProxyTargetAddr, udpStopChan) if err != nil { if !c.UseTCP { c.Running = false + c.parent.SaveConfigToDatabase() } log.Println("[TCP] Error starting stream proxy " + c.Name + "(" + c.UUID + "): " + err.Error()) } @@ -219,41 +225,57 @@ func (c *ProxyRelayConfig) Start() error { } if c.UseTCP { + c.tcpStopChan = tcpStopChan go func() { //Default to transport mode - c.Running = true err := c.Port2host(c.ListeningAddress, c.ProxyTargetAddr, tcpStopChan) if err != nil { c.Running = false + c.parent.SaveConfigToDatabase() log.Println("[TCP] Error starting stream proxy " + c.Name + "(" + c.UUID + "): " + err.Error()) } }() } //Successfully spawned off the proxy routine - + c.Running = true + c.parent.SaveConfigToDatabase() 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 + + //Update the running status + c.parent.SaveConfigToDatabase() } diff --git a/src/web/components/streamprox.html b/src/web/components/streamprox.html index 830f24f..0f38d8d 100644 --- a/src/web/components/streamprox.html +++ b/src/web/components/streamprox.html @@ -5,15 +5,15 @@
-

TCP / UDP Proxy Rules

-

A list of TCP proxy rules created on this host. To enable them, use the toggle button on the right.

-
- +

TCP / UDP Proxy Rules

+

A list of TCP / UDP proxy rules created on this host.

+
+
- - + + @@ -24,12 +24,13 @@
NamePort/Addr APort/Addr BListening AddressTarget Address Mode Timeout (s) Actions
+


-

Add or Edit Stream Proxy

+

Add or Edit Stream Proxy

Create or edit a new stream proxy instance

- + - Port to listen on this host. e.g. :25565 or 127.0.0.1:25565.
- If you are using Docker, you will need to expose this port to host network.
+ Address to listen on this host. e.g. :25565 or 127.0.0.1:25565.
+ If you are using Docker, you will also need to expose this port to host network.
@@ -75,72 +76,6 @@ -
@@ -243,10 +178,10 @@ proxyConfigs.forEach(function(config) { var runningLogo = 'Stopped'; var runningClass = "stopped"; - var startButton = ``; + var startButton = ``; if (config.Running){ runningLogo = 'Running'; - startButton = ``; + startButton = ``; runningClass = "running" } @@ -259,11 +194,11 @@ modeText.push("UDP") } - modeText = modeText.join(" | ") + modeText = modeText.join(" & ") var thisConfig = encodeURIComponent(JSON.stringify(config)); - var row = $(``); + var row = $(``); row.append($('').html(` ${config.Name}
${runningLogo}
`)); @@ -272,11 +207,9 @@ row.append($('').text(modeText)); row.append($('').text(config.Timeout)); row.append($('').html(` -
- ${startButton} - - -
+ ${startButton} + + `)); tableBody.append(row); }); @@ -285,7 +218,7 @@ function getConfigDetailsFromDOM(configUUID){ let thisConfig = null; - $(".tcproxConfig").each(function(){ + $(".streamproxConfig").each(function(){ let uuid = $(this).attr("uuid"); if (configUUID == uuid){ //This is the one we are looking for diff --git a/src/web/main.css b/src/web/main.css index 02f7810..9739502 100644 --- a/src/web/main.css +++ b/src/web/main.css @@ -551,23 +551,23 @@ body{ TCP Proxy */ -.tcproxConfig td:first-child{ +.streamproxConfig td:first-child{ position: relative; } -.tcproxConfig.running td:first-child{ - border-left: 0.6em solid #02cb59 !important; +.streamproxConfig.running td:first-child{ + border-left: 0.6em solid #01cb55 !important; } -.tcproxConfig.stopped td:first-child{ - border-left: 0.6em solid #02032a !important; +.streamproxConfig.stopped td:first-child{ + border-left: 0.6em solid #1b1b1b !important; } -.tcproxConfig td:first-child .statusText{ +.streamproxConfig td:first-child .statusText{ position: absolute; - bottom: 0.3em; - left: 0.2em; - font-size: 1.4em; + bottom: 0.1em; + right: 0.2em; + font-size: 1em; color:rgb(224, 224, 224); opacity: 0.7; pointer-events: none;