From 75518b2e04228b9ae920a85639256a8724ed6461 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Tue, 7 Jan 2025 21:12:07 -0500 Subject: [PATCH] Ping interval --- main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/main.go b/main.go index dd04ea9..7fe9026 100644 --- a/main.go +++ b/main.go @@ -112,6 +112,26 @@ func ping(tnet *netstack.Net, dst string) error { return nil } +func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}) { + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + go func() { + for { + select { + case <-ticker.C: + err := ping(tnet, serverIP) + if err != nil { + logger.Warn("Periodic ping failed: %v", err) + } + case <-stopChan: + logger.Info("Stopping ping check") + return + } + } + }() +} + func pingWithRetry(tnet *netstack.Net, dst string) error { const ( maxAttempts = 5 @@ -300,6 +320,9 @@ func main() { client.Close() }) + pingStopChan := make(chan struct{}) + defer close(pingStopChan) + // Register handlers for different message types client.RegisterHandler("newt/wg/connect", func(msg websocket.WSMessage) { logger.Info("Received registration message") @@ -374,6 +397,11 @@ persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey( logger.Error("Failed to ping %s: %v", wgData.ServerIP, err) } + if !connected { + logger.Info("Starting ping check") + startPingCheck(tnet, wgData.ServerIP, pingStopChan) + } + // Create proxy manager pm = proxy.NewProxyManager(tnet)