From 432eaa6c0433fee93e8f1d582efe2cd8241d62de Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 7 Oct 2024 20:11:23 +0000 Subject: [PATCH] feat(vpn): run `WaitForDNS` before querying the public ip address - Fix #2325 better --- internal/vpn/tunnelup.go | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/internal/vpn/tunnelup.go b/internal/vpn/tunnelup.go index c55c76e1..103d65dd 100644 --- a/internal/vpn/tunnelup.go +++ b/internal/vpn/tunnelup.go @@ -2,9 +2,8 @@ package vpn import ( "context" - "strings" - "time" + "github.com/qdm12/dns/v2/pkg/check" "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/version" ) @@ -31,9 +30,17 @@ func (l *Loop) onTunnelUp(ctx context.Context, data tunnelUpData) { if *l.dnsLooper.GetSettings().DoT.Enabled { _, _ = l.dnsLooper.ApplyStatus(ctx, constants.Running) + } else { + err := check.WaitForDNS(ctx, check.Settings{}) + if err != nil { + l.logger.Error("waiting for DNS to be ready: " + err.Error()) + } } - l.fetchPublicIPWithRetry(ctx) + err := l.publicip.RunOnce(ctx) + if err != nil { + l.logger.Error("getting public IP address information: " + err.Error()) + } if l.versionInfo { l.versionInfo = false // only get the version information once @@ -45,31 +52,8 @@ func (l *Loop) onTunnelUp(ctx context.Context, data tunnelUpData) { } } - err := l.startPortForwarding(data) + err = l.startPortForwarding(data) if err != nil { l.logger.Error(err.Error()) } } - -func (l *Loop) fetchPublicIPWithRetry(ctx context.Context) { - for { - err := l.publicip.RunOnce(ctx) - if err == nil { - return - } - - l.logger.Error("getting public IP address information: " + err.Error()) - if !strings.HasSuffix(err.Error(), "read: connection refused") { - return - } - - // Retry mechanism asked in https://github.com/qdm12/gluetun/issues/2325 - const retryPeriod = 2 * time.Second - l.logger.Infof("retrying public IP address information fetch in %s", retryPeriod) - timer := time.NewTimer(retryPeriod) - select { - case <-ctx.Done(): - case <-timer.C: - } - } -}