mirror of
https://github.com/fosrl/newt.git
synced 2026-03-09 20:26:45 +00:00
@@ -325,9 +325,11 @@ func (pm *ProxyManager) handleUDPProxy(conn *gonet.UDPConn, targetAddr string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
clientKey := remoteAddr.String()
|
// Use only the client IP as the key, not IP:port
|
||||||
|
// This ensures all packets from the same client reuse the same target connection
|
||||||
|
clientIP := remoteAddr.(*net.UDPAddr).IP.String()
|
||||||
clientsMutex.RLock()
|
clientsMutex.RLock()
|
||||||
targetConn, exists := clientConns[clientKey]
|
targetConn, exists := clientConns[clientIP]
|
||||||
clientsMutex.RUnlock()
|
clientsMutex.RUnlock()
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
@@ -344,15 +346,15 @@ func (pm *ProxyManager) handleUDPProxy(conn *gonet.UDPConn, targetAddr string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clientsMutex.Lock()
|
clientsMutex.Lock()
|
||||||
clientConns[clientKey] = targetConn
|
clientConns[clientIP] = targetConn
|
||||||
clientsMutex.Unlock()
|
clientsMutex.Unlock()
|
||||||
|
|
||||||
go func(clientKey string, targetConn *net.UDPConn, remoteAddr net.Addr) {
|
go func(clientIP string, targetConn *net.UDPConn, remoteAddr net.Addr) {
|
||||||
defer func() {
|
defer func() {
|
||||||
// Always clean up when this goroutine exits
|
// Always clean up when this goroutine exits
|
||||||
clientsMutex.Lock()
|
clientsMutex.Lock()
|
||||||
if storedConn, exists := clientConns[clientKey]; exists && storedConn == targetConn {
|
if storedConn, exists := clientConns[clientIP]; exists && storedConn == targetConn {
|
||||||
delete(clientConns, clientKey)
|
delete(clientConns, clientIP)
|
||||||
targetConn.Close()
|
targetConn.Close()
|
||||||
}
|
}
|
||||||
clientsMutex.Unlock()
|
clientsMutex.Unlock()
|
||||||
@@ -372,7 +374,7 @@ func (pm *ProxyManager) handleUDPProxy(conn *gonet.UDPConn, targetAddr string) {
|
|||||||
return // defer will handle cleanup
|
return // defer will handle cleanup
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(clientKey, targetConn, remoteAddr)
|
}(clientIP, targetConn, remoteAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = targetConn.Write(buffer[:n])
|
_, err = targetConn.Write(buffer[:n])
|
||||||
@@ -380,7 +382,7 @@ func (pm *ProxyManager) handleUDPProxy(conn *gonet.UDPConn, targetAddr string) {
|
|||||||
logger.Error("Error writing to target: %v", err)
|
logger.Error("Error writing to target: %v", err)
|
||||||
targetConn.Close()
|
targetConn.Close()
|
||||||
clientsMutex.Lock()
|
clientsMutex.Lock()
|
||||||
delete(clientConns, clientKey)
|
delete(clientConns, clientIP)
|
||||||
clientsMutex.Unlock()
|
clientsMutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user