diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index df1402a4..b8b3579e 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -236,9 +236,7 @@ func _main(background context.Context, buildInfo models.BuildInformation, } tunnelReadyCh := make(chan struct{}) - dnsReadyCh := make(chan struct{}) defer close(tunnelReadyCh) - defer close(dnsReadyCh) if allSettings.Firewall.Enabled { err := firewallConf.SetEnabled(ctx, true) // disabled by default @@ -279,7 +277,7 @@ func _main(background context.Context, buildInfo models.BuildInformation, logger, nonRootUsername, puid, pgid) wg.Add(1) // wait for unboundLooper.Restart or its ticker launched with RunRestartTicker - go unboundLooper.Run(ctx, wg, dnsReadyCh) + go unboundLooper.Run(ctx, wg) publicIPLooper := publicip.NewLooper( httpClient, logger, allSettings.PublicIP, puid, pgid, os) @@ -297,7 +295,7 @@ func _main(background context.Context, buildInfo models.BuildInformation, go shadowsocksLooper.Run(ctx, wg) wg.Add(1) - go routeReadyEvents(ctx, wg, buildInfo, tunnelReadyCh, dnsReadyCh, + go routeReadyEvents(ctx, wg, buildInfo, tunnelReadyCh, unboundLooper, updaterLooper, publicIPLooper, routingConf, logger, httpClient, allSettings.VersionInformation, allSettings.OpenVPN.Provider.PortForwarding.Enabled, openvpnLooper.PortForward, ) @@ -347,7 +345,7 @@ func printVersions(ctx context.Context, logger logging.Logger, } func routeReadyEvents(ctx context.Context, wg *sync.WaitGroup, buildInfo models.BuildInformation, - tunnelReadyCh, dnsReadyCh <-chan struct{}, + tunnelReadyCh <-chan struct{}, unboundLooper dns.Looper, updaterLooper updater.Looper, publicIPLooper publicip.Looper, routing routing.Routing, logger logging.Logger, httpClient *http.Client, versionInformation, portForwardingEnabled bool, startPortForward func(vpnGateway net.IP)) { @@ -356,6 +354,7 @@ func routeReadyEvents(ctx context.Context, wg *sync.WaitGroup, buildInfo models. // for linters only var restartTickerContext context.Context var restartTickerCancel context.CancelFunc = func() {} + first := true for { select { case <-ctx.Done(): @@ -363,22 +362,41 @@ func routeReadyEvents(ctx context.Context, wg *sync.WaitGroup, buildInfo models. tickerWg.Wait() return case <-tunnelReadyCh: // blocks until openvpn is connected - if unboundLooper.GetSettings().Enabled { - _, _ = unboundLooper.SetStatus(constants.Running) - } - restartTickerCancel() // stop previous restart tickers - tickerWg.Wait() - restartTickerContext, restartTickerCancel = context.WithCancel(ctx) - //nolint:gomnd - tickerWg.Add(2) - go unboundLooper.RunRestartTicker(restartTickerContext, tickerWg) - go updaterLooper.RunRestartTicker(restartTickerContext, tickerWg) vpnDestination, err := routing.VPNDestinationIP() if err != nil { logger.Warn(err) } else { logger.Info("VPN routing IP address: %s", vpnDestination) } + + if unboundLooper.GetSettings().Enabled { + _, _ = unboundLooper.SetStatus(constants.Running) + } + + restartTickerCancel() // stop previous restart tickers + tickerWg.Wait() + restartTickerContext, restartTickerCancel = context.WithCancel(ctx) + + // Runs the Public IP getter job once + _, _ = publicIPLooper.SetStatus(constants.Running) + if !versionInformation { + break + } + + if first { + first = false + message, err := versionpkg.GetMessage(ctx, buildInfo, httpClient) + if err != nil { + logger.Error(err) + } else { + logger.Info(message) + } + } + + //nolint:gomnd + tickerWg.Add(2) + go unboundLooper.RunRestartTicker(restartTickerContext, tickerWg) + go updaterLooper.RunRestartTicker(restartTickerContext, tickerWg) if portForwardingEnabled { // vpnGateway required only for PIA vpnGateway, err := routing.VPNLocalGatewayIP() @@ -388,18 +406,6 @@ func routeReadyEvents(ctx context.Context, wg *sync.WaitGroup, buildInfo models. logger.Info("VPN gateway IP address: %s", vpnGateway) startPortForward(vpnGateway) } - case <-dnsReadyCh: - // Runs the Public IP getter job once - _, _ = publicIPLooper.SetStatus(constants.Running) - if !versionInformation { - break - } - message, err := versionpkg.GetMessage(ctx, buildInfo, httpClient) - if err != nil { - logger.Error(err) - break - } - logger.Info(message) } } } diff --git a/internal/dns/loop.go b/internal/dns/loop.go index 37b4748b..ebcc7716 100644 --- a/internal/dns/loop.go +++ b/internal/dns/loop.go @@ -16,7 +16,7 @@ import ( ) type Looper interface { - Run(ctx context.Context, wg *sync.WaitGroup, dnsReadyCh chan<- struct{}) + Run(ctx context.Context, wg *sync.WaitGroup) RunRestartTicker(ctx context.Context, wg *sync.WaitGroup) GetStatus() (status models.LoopStatus) SetStatus(status models.LoopStatus) (outcome string, err error) @@ -83,7 +83,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) { } } -func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup, dnsReadyCh chan<- struct{}) { +func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() const fallback = false @@ -136,8 +136,6 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup, dnsReadyCh chan<- closeStreams = func() {} } - dnsReadyCh <- struct{}{} - stayHere := true for stayHere { select { diff --git a/internal/publicip/loop.go b/internal/publicip/loop.go index 0c9c8e98..0f184c65 100644 --- a/internal/publicip/loop.go +++ b/internal/publicip/loop.go @@ -138,7 +138,6 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) { } return case <-l.start: - l.logger.Info("starting") getCancel() stayHere = false case <-l.stop: