Skip to content

Commit 72e0adc

Browse files
Monitor connection with pings and keep pining
Resolves #24
1 parent b3e8bf7 commit 72e0adc

File tree

1 file changed

+124
-34
lines changed

1 file changed

+124
-34
lines changed

main.go

Lines changed: 124 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ func ping(tnet *netstack.Net, dst string) error {
115115
}
116116

117117
func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}) {
118-
ticker := time.NewTicker(10 * time.Second)
118+
initialInterval := 10 * time.Second
119+
maxInterval := 60 * time.Second
120+
currentInterval := initialInterval
121+
consecutiveFailures := 0
122+
123+
ticker := time.NewTicker(currentInterval)
119124
defer ticker.Stop()
120125

121126
go func() {
@@ -124,8 +129,34 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
124129
case <-ticker.C:
125130
err := ping(tnet, serverIP)
126131
if err != nil {
127-
logger.Warn("Periodic ping failed: %v", err)
132+
consecutiveFailures++
133+
logger.Warn("Periodic ping failed (%d consecutive failures): %v",
134+
consecutiveFailures, err)
128135
logger.Warn("HINT: Do you have UDP port 51820 (or the port in config.yml) open on your Pangolin server?")
136+
137+
// Increase interval if we have consistent failures, with a maximum cap
138+
if consecutiveFailures >= 3 && currentInterval < maxInterval {
139+
// Increase by 50% each time, up to the maximum
140+
currentInterval = time.Duration(float64(currentInterval) * 1.5)
141+
if currentInterval > maxInterval {
142+
currentInterval = maxInterval
143+
}
144+
ticker.Reset(currentInterval)
145+
logger.Info("Increased ping check interval to %v due to consecutive failures",
146+
currentInterval)
147+
}
148+
} else {
149+
// On success, if we've backed off, gradually return to normal interval
150+
if currentInterval > initialInterval {
151+
currentInterval = time.Duration(float64(currentInterval) * 0.8)
152+
if currentInterval < initialInterval {
153+
currentInterval = initialInterval
154+
}
155+
ticker.Reset(currentInterval)
156+
logger.Info("Decreased ping check interval to %v after successful ping",
157+
currentInterval)
158+
}
159+
consecutiveFailures = 0
129160
}
130161
case <-stopChan:
131162
logger.Info("Stopping ping check")
@@ -135,34 +166,97 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
135166
}()
136167
}
137168

169+
// Function to track connection status and trigger reconnection as needed
170+
func monitorConnectionStatus(tnet *netstack.Net, serverIP string, client *websocket.Client) {
171+
const checkInterval = 30 * time.Second
172+
connectionLost := false
173+
ticker := time.NewTicker(checkInterval)
174+
defer ticker.Stop()
175+
176+
for {
177+
select {
178+
case <-ticker.C:
179+
// Try a ping to see if connection is alive
180+
err := ping(tnet, serverIP)
181+
182+
if err != nil && !connectionLost {
183+
// We just lost connection
184+
connectionLost = true
185+
logger.Warn("Connection to server lost. Continuous reconnection attempts will be made.")
186+
187+
// Notify the user they might need to check their network
188+
logger.Warn("Please check your internet connection and ensure the Pangolin server is online.")
189+
logger.Warn("Newt will continue reconnection attempts automatically when connectivity is restored.")
190+
} else if err == nil && connectionLost {
191+
// Connection has been restored
192+
connectionLost = false
193+
logger.Info("Connection to server restored!")
194+
195+
// Tell the server we're back
196+
err := client.SendMessage("newt/wg/register", map[string]interface{}{
197+
"publicKey": fmt.Sprintf("%s", privateKey.PublicKey()),
198+
})
199+
200+
if err != nil {
201+
logger.Error("Failed to send registration message after reconnection: %v", err)
202+
} else {
203+
logger.Info("Successfully re-registered with server after reconnection")
204+
}
205+
}
206+
}
207+
}
208+
}
209+
138210
func pingWithRetry(tnet *netstack.Net, dst string) error {
139211
const (
140-
maxAttempts = 15
141-
retryDelay = 2 * time.Second
212+
initialMaxAttempts = 15
213+
initialRetryDelay = 2 * time.Second
214+
maxRetryDelay = 60 * time.Second // Cap the maximum delay
142215
)
143216

144-
var lastErr error
145-
for attempt := 1; attempt <= maxAttempts; attempt++ {
146-
logger.Info("Ping attempt %d of %d", attempt, maxAttempts)
217+
attempt := 1
218+
retryDelay := initialRetryDelay
147219

148-
if err := ping(tnet, dst); err != nil {
149-
lastErr = err
150-
logger.Warn("Ping attempt %d failed: %v", attempt, err)
220+
// First try with the initial parameters
221+
logger.Info("Ping attempt %d", attempt)
222+
if err := ping(tnet, dst); err == nil {
223+
// Successful ping
224+
return nil
225+
} else {
226+
logger.Warn("Ping attempt %d failed: %v", attempt, err)
227+
}
228+
229+
// Start a goroutine that will attempt pings indefinitely with increasing delays
230+
go func() {
231+
attempt = 2 // Continue from attempt 2
232+
233+
for {
234+
logger.Info("Ping attempt %d", attempt)
235+
236+
if err := ping(tnet, dst); err != nil {
237+
logger.Warn("Ping attempt %d failed: %v", attempt, err)
238+
239+
// Increase delay after certain thresholds but cap it
240+
if attempt%5 == 0 && retryDelay < maxRetryDelay {
241+
retryDelay = time.Duration(float64(retryDelay) * 1.5)
242+
if retryDelay > maxRetryDelay {
243+
retryDelay = maxRetryDelay
244+
}
245+
logger.Info("Increasing ping retry delay to %v", retryDelay)
246+
}
151247

152-
if attempt < maxAttempts {
153248
time.Sleep(retryDelay)
154-
continue
249+
attempt++
250+
} else {
251+
// Successful ping
252+
logger.Info("Ping succeeded after %d attempts", attempt)
253+
return
155254
}
156-
return fmt.Errorf("all ping attempts failed after %d tries, last error: %w",
157-
maxAttempts, lastErr)
158255
}
256+
}()
159257

160-
// Successful ping
161-
return nil
162-
}
163-
164-
// This shouldn't be reached due to the return in the loop, but added for completeness
165-
return fmt.Errorf("unexpected error: all ping attempts failed")
258+
// Return an error for the first batch of attempts (to maintain compatibility with existing code)
259+
return fmt.Errorf("initial ping attempts failed, continuing in background")
166260
}
167261

168262
func parseLogLevel(level string) logger.LogLevel {
@@ -353,13 +447,8 @@ func main() {
353447

354448
if connected {
355449
logger.Info("Already connected! But I will send a ping anyway...")
356-
// ping(tnet, wgData.ServerIP)
357-
err = pingWithRetry(tnet, wgData.ServerIP)
358-
if err != nil {
359-
// Handle complete failure after all retries
360-
logger.Warn("Failed to ping %s: %v", wgData.ServerIP, err)
361-
logger.Warn("HINT: Do you have UDP port 51820 (or the port in config.yml) open on your Pangolin server?")
362-
}
450+
// Even if pingWithRetry returns an error, it will continue trying in the background
451+
_ = pingWithRetry(tnet, wgData.ServerIP) // Ignoring initial error as pings will continue
363452
return
364453
}
365454

@@ -414,17 +503,18 @@ persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey(
414503
}
415504

416505
logger.Info("WireGuard device created. Lets ping the server now...")
417-
// Ping to bring the tunnel up on the server side quickly
418-
// ping(tnet, wgData.ServerIP)
419-
err = pingWithRetry(tnet, wgData.ServerIP)
420-
if err != nil {
421-
// Handle complete failure after all retries
422-
logger.Error("Failed to ping %s: %v", wgData.ServerIP, err)
423-
}
424506

507+
// Even if pingWithRetry returns an error, it will continue trying in the background
508+
_ = pingWithRetry(tnet, wgData.ServerIP)
509+
510+
// Always mark as connected and start the proxy manager regardless of initial ping result
511+
// as the pings will continue in the background
425512
if !connected {
426513
logger.Info("Starting ping check")
427514
startPingCheck(tnet, wgData.ServerIP, pingStopChan)
515+
516+
// Start connection monitoring in a separate goroutine
517+
go monitorConnectionStatus(tnet, wgData.ServerIP, client)
428518
}
429519

430520
// Create proxy manager

0 commit comments

Comments
 (0)