From 2dec016201d22bd17c84173d344ca34942870e9c Mon Sep 17 00:00:00 2001 From: Zoltan Papp Date: Fri, 11 Aug 2023 11:51:39 +0200 Subject: [PATCH] Fix/always on boot (#1062) In case of 'always-on' feature has switched on, after the reboot the service do not start properly in all cases. If the device is in offline state (no internet connection) the auth login steps will fail and the service will stop. For the auth steps make no sense in this case because if the OS start the service we do not have option for the user interaction. --- client/android/client.go | 26 ++++++++++++++++++++++++-- client/internal/connect.go | 3 +-- client/internal/peer/notifier.go | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/client/android/client.go b/client/android/client.go index d8f561e18..bb15268eb 100644 --- a/client/android/client.go +++ b/client/android/client.go @@ -55,7 +55,6 @@ type Client struct { ctxCancelLock *sync.Mutex deviceName string routeListener routemanager.RouteListener - onHostDnsFn func([]string) } // NewClient instantiate a new Client @@ -97,7 +96,30 @@ func (c *Client) Run(urlOpener URLOpener, dns *DNSList, dnsReadyListener DnsRead // todo do not throw error in case of cancelled context ctx = internal.CtxInitState(ctx) - c.onHostDnsFn = func([]string) {} + return internal.RunClientMobile(ctx, cfg, c.recorder, c.tunAdapter, c.iFaceDiscover, c.routeListener, dns.items, dnsReadyListener) +} + +// RunWithoutLogin we apply this type of run function when the backed has been started without UI (i.e. after reboot). +// In this case make no sense handle registration steps. +func (c *Client) RunWithoutLogin(dns *DNSList, dnsReadyListener DnsReadyListener) error { + cfg, err := internal.UpdateOrCreateConfig(internal.ConfigInput{ + ConfigPath: c.cfgFile, + }) + if err != nil { + return err + } + c.recorder.UpdateManagementAddress(cfg.ManagementURL.String()) + + var ctx context.Context + //nolint + ctxWithValues := context.WithValue(context.Background(), system.DeviceNameCtxKey, c.deviceName) + c.ctxCancelLock.Lock() + ctx, c.ctxCancel = context.WithCancel(ctxWithValues) + defer c.ctxCancel() + c.ctxCancelLock.Unlock() + + // todo do not throw error in case of cancelled context + ctx = internal.CtxInitState(ctx) return internal.RunClientMobile(ctx, cfg, c.recorder, c.tunAdapter, c.iFaceDiscover, c.routeListener, dns.items, dnsReadyListener) } diff --git a/client/internal/connect.go b/client/internal/connect.go index 87aab0b54..6eecf4207 100644 --- a/client/internal/connect.go +++ b/client/internal/connect.go @@ -179,8 +179,6 @@ func runClient(ctx context.Context, config *Config, statusRecorder *peer.Status, log.Print("Netbird engine started, my IP is: ", peerConfig.Address) state.Set(StatusConnected) - statusRecorder.ClientStart() - <-engineCtx.Done() statusRecorder.ClientTeardown() @@ -201,6 +199,7 @@ func runClient(ctx context.Context, config *Config, statusRecorder *peer.Status, return nil } + statusRecorder.ClientStart() err = backoff.Retry(operation, backOff) if err != nil { log.Debugf("exiting client retry loop due to unrecoverable error: %s", err) diff --git a/client/internal/peer/notifier.go b/client/internal/peer/notifier.go index eb15bdeeb..f1175c2c4 100644 --- a/client/internal/peer/notifier.go +++ b/client/internal/peer/notifier.go @@ -61,7 +61,7 @@ func (n *notifier) clientStart() { n.serverStateLock.Lock() defer n.serverStateLock.Unlock() n.currentClientState = true - n.lastNotification = stateConnected + n.lastNotification = stateConnecting n.notify(n.lastNotification) } @@ -114,7 +114,7 @@ func (n *notifier) calculateState(managementConn, signalConn bool) int { return stateConnected } - if !managementConn && !signalConn { + if !managementConn && !signalConn && !n.currentClientState { return stateDisconnected }