From 612a9ddb1598baa6e5a18ca6328b01a81291dcb8 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 28 Jul 2025 11:55:03 -0700 Subject: [PATCH] Dont kick off the process again on the ws Former-commit-id: f1b3abdffc393247a5066e067c902a8b262c2d66 --- main.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index ebb76af..6c17388 100644 --- a/main.go +++ b/main.go @@ -180,6 +180,7 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { pingInterval time.Duration pingTimeout time.Duration doHolepunch bool + connected bool ) stopHolepunch = make(chan struct{}) @@ -433,18 +434,15 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { go keepSendingUDPHolePunch(holePunchData.Endpoint, id, sourcePort) }) - connectTimes := 0 // Register handlers for different message types olm.RegisterHandler("olm/wg/connect", func(msg websocket.WSMessage) { logger.Debug("Received message: %v", msg.Data) - if connectTimes > 0 { + if connected { logger.Info("Already connected. Ignoring new connection request.") return } - connectTimes++ - if stopRegister != nil { stopRegister() stopRegister = nil @@ -606,6 +604,8 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { peerMonitor.Start() + connected = true + logger.Info("WireGuard device created.") }) @@ -828,6 +828,17 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { }) olm.OnConnect(func() error { + logger.Info("Websocket Connected") + + if httpServer != nil { + httpServer.SetConnectionStatus(true) + } + + if connected { + logger.Debug("Already connected, skipping registration") + return nil + } + publicKey := privateKey.PublicKey() logger.Debug("Sending registration message to server with public key: %s and relay: %v", publicKey, !doHolepunch) @@ -839,10 +850,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { go keepSendingPing(olm) - if httpServer != nil { - httpServer.SetConnectionStatus(true) - } - logger.Info("Sent registration message") return nil })