fix(manager, stub, util): enhance error handling and logging consistency

This commit is contained in:
Marc Schäfer
2025-10-10 14:16:28 +02:00
parent 77d56596ab
commit b62e18622e
3 changed files with 25 additions and 15 deletions

View File

@@ -20,6 +20,8 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
) )
const errUnsupportedProtoFmt = "unsupported protocol: %s"
// Target represents a proxy target with its address and port // Target represents a proxy target with its address and port
type Target struct { type Target struct {
Address string Address string
@@ -74,13 +76,14 @@ func (cw *countingWriter) Write(p []byte) (int, error) {
n, err := cw.w.Write(p) n, err := cw.w.Write(p)
if n > 0 { if n > 0 {
if cw.pm != nil && cw.pm.asyncBytes && cw.ent != nil { if cw.pm != nil && cw.pm.asyncBytes && cw.ent != nil {
if cw.proto == "tcp" { switch cw.proto {
case "tcp":
if cw.out { if cw.out {
cw.ent.bytesOutTCP.Add(uint64(n)) cw.ent.bytesOutTCP.Add(uint64(n))
} else { } else {
cw.ent.bytesInTCP.Add(uint64(n)) cw.ent.bytesInTCP.Add(uint64(n))
} }
} else if cw.proto == "udp" { case "udp":
if cw.out { if cw.out {
cw.ent.bytesOutUDP.Add(uint64(n)) cw.ent.bytesOutUDP.Add(uint64(n))
} else { } else {
@@ -207,7 +210,7 @@ func (pm *ProxyManager) AddTarget(proto, listenIP string, port int, targetAddr s
} }
pm.udpTargets[listenIP][port] = targetAddr pm.udpTargets[listenIP][port] = targetAddr
default: default:
return fmt.Errorf("unsupported protocol: %s", proto) return fmt.Errorf(errUnsupportedProtoFmt, proto)
} }
if pm.running { if pm.running {
@@ -256,7 +259,7 @@ func (pm *ProxyManager) RemoveTarget(proto, listenIP string, port int) error {
return fmt.Errorf("target not found: %s:%d", listenIP, port) return fmt.Errorf("target not found: %s:%d", listenIP, port)
} }
default: default:
return fmt.Errorf("unsupported protocol: %s", proto) return fmt.Errorf(errUnsupportedProtoFmt, proto)
} }
return nil return nil
} }
@@ -443,7 +446,7 @@ func (pm *ProxyManager) startTarget(proto, listenIP string, port int, targetAddr
go pm.handleUDPProxy(conn, targetAddr) go pm.handleUDPProxy(conn, targetAddr)
default: default:
return fmt.Errorf("unsupported protocol: %s", proto) return fmt.Errorf(errUnsupportedProtoFmt, proto)
} }
logger.Info("Started %s proxy to %s", proto, targetAddr) logger.Info("Started %s proxy to %s", proto, targetAddr)

12
stub.go
View File

@@ -8,25 +8,27 @@ import (
) )
func setupClientsNative(client *websocket.Client, host string) { func setupClientsNative(client *websocket.Client, host string) {
return // This function is not implemented for non-Linux systems. _ = client
_ = host
// No-op for non-Linux systems
} }
func closeWgServiceNative() { func closeWgServiceNative() {
// No-op for non-Linux systems // No-op for non-Linux systems
return
} }
func clientsOnConnectNative() { func clientsOnConnectNative() {
// No-op for non-Linux systems // No-op for non-Linux systems
return
} }
func clientsHandleNewtConnectionNative(publicKey, endpoint string) { func clientsHandleNewtConnectionNative(publicKey, endpoint string) {
_ = publicKey
_ = endpoint
// No-op for non-Linux systems // No-op for non-Linux systems
return
} }
func clientsAddProxyTargetNative(pm *proxy.ProxyManager, tunnelIp string) { func clientsAddProxyTargetNative(pm *proxy.ProxyManager, tunnelIp string) {
_ = pm
_ = tunnelIp
// No-op for non-Linux systems // No-op for non-Linux systems
return
} }

15
util.go
View File

@@ -25,6 +25,8 @@ import (
"golang.zx2c4.com/wireguard/tun/netstack" "golang.zx2c4.com/wireguard/tun/netstack"
) )
const msgHealthFileWriteFailed = "Failed to write health file: %v"
func fixKey(key string) string { func fixKey(key string) string {
// Remove any whitespace // Remove any whitespace
key = strings.TrimSpace(key) key = strings.TrimSpace(key)
@@ -177,7 +179,7 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
if healthFile != "" { if healthFile != "" {
err := os.WriteFile(healthFile, []byte("ok"), 0644) err := os.WriteFile(healthFile, []byte("ok"), 0644)
if err != nil { if err != nil {
logger.Warn("Failed to write health file: %v", err) logger.Warn(msgHealthFileWriteFailed, err)
} }
} }
return stopChan, nil return stopChan, nil
@@ -218,11 +220,11 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
if healthFile != "" { if healthFile != "" {
err := os.WriteFile(healthFile, []byte("ok"), 0644) err := os.WriteFile(healthFile, []byte("ok"), 0644)
if err != nil { if err != nil {
logger.Warn("Failed to write health file: %v", err) logger.Warn(msgHealthFileWriteFailed, err)
} }
} }
return
} }
case <-pingStopChan:
} }
} }
}() }()
@@ -476,7 +478,8 @@ func updateTargets(pm *proxy.ProxyManager, action string, tunnelIP string, proto
continue continue
} }
if action == "add" { switch action {
case "add":
target := parts[1] + ":" + parts[2] target := parts[1] + ":" + parts[2]
// Call updown script if provided // Call updown script if provided
@@ -502,7 +505,7 @@ func updateTargets(pm *proxy.ProxyManager, action string, tunnelIP string, proto
// Add the new target // Add the new target
pm.AddTarget(proto, tunnelIP, port, processedTarget) pm.AddTarget(proto, tunnelIP, port, processedTarget)
} else if action == "remove" { case "remove":
logger.Info("Removing target with port %d", port) logger.Info("Removing target with port %d", port)
target := parts[1] + ":" + parts[2] target := parts[1] + ":" + parts[2]
@@ -520,6 +523,8 @@ func updateTargets(pm *proxy.ProxyManager, action string, tunnelIP string, proto
logger.Error("Failed to remove target: %v", err) logger.Error("Failed to remove target: %v", err)
return err return err
} }
default:
logger.Info("Unknown action: %s", action)
} }
} }