From 824972a1e25969d63791c453a495678ce3c86a5f Mon Sep 17 00:00:00 2001 From: Toby Chui Date: Wed, 15 Oct 2025 07:40:48 +0800 Subject: [PATCH] Added custom type for Proxy Protocol Version - Changed enum type for proxy protocol - Added warning for proxy protocol 1 on UDP selection in UI --- src/mod/streamproxy/handler.go | 2 +- src/mod/streamproxy/streamproxy.go | 61 +++++++++++++++++++++++------- src/mod/streamproxy/tcpprox.go | 6 +-- src/mod/streamproxy/udpprox.go | 4 +- src/web/components/streamprox.html | 18 +++++++++ 5 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/mod/streamproxy/handler.go b/src/mod/streamproxy/handler.go index 33faa66..72a593e 100644 --- a/src/mod/streamproxy/handler.go +++ b/src/mod/streamproxy/handler.go @@ -58,7 +58,7 @@ func (m *Manager) HandleAddProxyConfig(w http.ResponseWriter, r *http.Request) { Timeout: timeout, UseTCP: useTCP, UseUDP: useUDP, - ProxyProtocolVersion: ProxyProtocolVersion, + ProxyProtocolVersion: convertIntToProxyProtocolVersion(ProxyProtocolVersion), EnableLogging: enableLogging, }) diff --git a/src/mod/streamproxy/streamproxy.go b/src/mod/streamproxy/streamproxy.go index 428ca54..599c71f 100644 --- a/src/mod/streamproxy/streamproxy.go +++ b/src/mod/streamproxy/streamproxy.go @@ -15,13 +15,22 @@ import ( ) /* - TCP Proxy + Stream Proxy Forward port from one port to another Also accept active connection and passive connection */ +// ProxyProtocolVersion enum type +type ProxyProtocolVersion int + +const ( + ProxyProtocolDisabled ProxyProtocolVersion = 0 + ProxyProtocolV1 ProxyProtocolVersion = 1 + ProxyProtocolV2 ProxyProtocolVersion = 2 +) + type ProxyRelayOptions struct { Name string ListeningAddr string @@ -29,7 +38,7 @@ type ProxyRelayOptions struct { Timeout int UseTCP bool UseUDP bool - ProxyProtocolVersion int + ProxyProtocolVersion ProxyProtocolVersion EnableLogging bool } @@ -48,17 +57,17 @@ type ProxyRuleUpdateConfig struct { type ProxyRelayInstance struct { /* Runtime Config */ - UUID string //A UUIDv4 representing this config - Name string //Name of the config - Running bool //Status, read only - AutoStart bool //If the service suppose to started automatically - ListeningAddress string //Listening Address, usually 127.0.0.1:port - ProxyTargetAddr string //Proxy target address - UseTCP bool //Enable TCP proxy - UseUDP bool //Enable UDP proxy - ProxyProtocolVersion int //Proxy Protocol v1/v2 - EnableLogging bool //Enable logging for ProxyInstance - Timeout int //Timeout for connection in sec + UUID string //A UUIDv4 representing this config + Name string //Name of the config + Running bool //Status, read only + AutoStart bool //If the service suppose to started automatically + ListeningAddress string //Listening Address, usually 127.0.0.1:port + ProxyTargetAddr string //Proxy target address + UseTCP bool //Enable TCP proxy + UseUDP bool //Enable UDP proxy + ProxyProtocolVersion ProxyProtocolVersion //Proxy Protocol v1/v2 + EnableLogging bool //Enable logging for ProxyInstance + Timeout int //Timeout for connection in sec /* Internal */ tcpStopChan chan bool //Stop channel for TCP listener @@ -203,6 +212,30 @@ func (m *Manager) GetConfigByUUID(configUUID string) (*ProxyRelayInstance, error return nil, errors.New("config not found") } +// ConvertIntToProxyProtocolVersion converts an int to ProxyProtocolVersion type +func convertIntToProxyProtocolVersion(v int) ProxyProtocolVersion { + switch v { + case 1: + return ProxyProtocolV1 + case 2: + return ProxyProtocolV2 + default: + return ProxyProtocolDisabled + } +} + +// convertProxyProtocolVersionToInt converts ProxyProtocolVersion type back to int +func convertProxyProtocolVersionToInt(v ProxyProtocolVersion) int { + switch v { + case ProxyProtocolV1: + return 1 + case ProxyProtocolV2: + return 2 + default: + return 0 + } +} + // Edit the config based on config UUID, leave empty for unchange fields func (m *Manager) EditConfig(newConfig *ProxyRuleUpdateConfig) error { // Find the config with the specified UUID @@ -224,7 +257,7 @@ func (m *Manager) EditConfig(newConfig *ProxyRuleUpdateConfig) error { foundConfig.UseTCP = newConfig.UseTCP foundConfig.UseUDP = newConfig.UseUDP - foundConfig.ProxyProtocolVersion = newConfig.ProxyProtocolVersion + foundConfig.ProxyProtocolVersion = convertIntToProxyProtocolVersion(newConfig.ProxyProtocolVersion) foundConfig.EnableLogging = newConfig.EnableLogging if newConfig.NewTimeout != -1 { diff --git a/src/mod/streamproxy/tcpprox.go b/src/mod/streamproxy/tcpprox.go index b102807..07807f0 100644 --- a/src/mod/streamproxy/tcpprox.go +++ b/src/mod/streamproxy/tcpprox.go @@ -46,7 +46,7 @@ func (c *ProxyRelayInstance) connCopy(conn1 net.Conn, conn2 net.Conn, wg *sync.W wg.Done() } -func WriteProxyProtocolHeader(dst net.Conn, src net.Conn, version int) error { +func WriteProxyProtocolHeader(dst net.Conn, src net.Conn, version ProxyProtocolVersion) error { clientAddr, ok1 := src.RemoteAddr().(*net.TCPAddr) proxyAddr, ok2 := src.LocalAddr().(*net.TCPAddr) if !ok1 || !ok2 { @@ -54,7 +54,7 @@ func WriteProxyProtocolHeader(dst net.Conn, src net.Conn, version int) error { } header := proxyproto.Header{ - Version: byte(version), + Version: byte(convertProxyProtocolVersionToInt(version)), Command: proxyproto.PROXY, TransportProtocol: proxyproto.TCPv4, SourceAddr: clientAddr, @@ -165,7 +165,7 @@ func (c *ProxyRelayInstance) Port2host(allowPort string, targetAddress string, s } c.LogMsg("[→] connect target address ["+targetAddress+"] success.", nil) - if c.ProxyProtocolVersion != 0 { + if c.ProxyProtocolVersion != ProxyProtocolDisabled { c.LogMsg("[+] write proxy protocol header to target address ["+targetAddress+"]", nil) err = WriteProxyProtocolHeader(target, conn, c.ProxyProtocolVersion) if err != nil { diff --git a/src/mod/streamproxy/udpprox.go b/src/mod/streamproxy/udpprox.go index 672688c..bf21158 100644 --- a/src/mod/streamproxy/udpprox.go +++ b/src/mod/streamproxy/udpprox.go @@ -88,7 +88,7 @@ func (c *ProxyRelayInstance) CloseAllUDPConnections() { // Write Proxy Protocol v2 header to UDP connection func WriteProxyProtocolHeaderUDP(conn *net.UDPConn, srcAddr, dstAddr *net.UDPAddr) error { header := proxyproto.Header{ - Version: 2, + Version: byte(ProxyProtocolV2), Command: proxyproto.PROXY, TransportProtocol: proxyproto.UDPv4, SourceAddr: srcAddr, @@ -164,7 +164,7 @@ func (c *ProxyRelayInstance) ForwardUDP(address1, address2 string, stopChan chan go c.RunUDPConnectionRelay(conn, lisener) // Send Proxy Protocol header if enabled - if c.ProxyProtocolVersion == 2 { + if c.ProxyProtocolVersion == ProxyProtocolV2 { _ = WriteProxyProtocolHeaderUDP(conn.ServerConn, cliaddr, targetAddr) } } else { diff --git a/src/web/components/streamprox.html b/src/web/components/streamprox.html index 3746a9b..16cf0eb 100644 --- a/src/web/components/streamprox.html +++ b/src/web/components/streamprox.html @@ -90,6 +90,9 @@ Select Proxy Protocol v1 / v2 to use (if any) + @@ -100,6 +103,21 @@