Skip to content

Commit

Permalink
feat(vpn): run WaitForDNS before querying the public ip address
Browse files Browse the repository at this point in the history
- Fix #2325 better
  • Loading branch information
qdm12 committed Oct 8, 2024
1 parent 5fd0af9 commit 432eaa6
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions internal/vpn/tunnelup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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:
}
}
}

0 comments on commit 432eaa6

Please sign in to comment.