feat(vpn): run WaitForDNS before querying the public ip address
- Fix #2325 better
This commit is contained in:
@@ -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:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user