From 5d15ec8cc75df3baef3b18a6db73e2a462d10e60 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Oct 2021 16:02:59 +0200 Subject: [PATCH] Wait with first update cycle until online --- intel/filterlists/module.go | 2 +- netenv/main.go | 3 ++- netenv/online-status.go | 13 +++++++++---- status/module.go | 2 +- updates/api.go | 3 +-- updates/config.go | 8 ++++---- updates/main.go | 23 ++++++++++------------- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/intel/filterlists/module.go b/intel/filterlists/module.go index 5753d941..d2af5742 100644 --- a/intel/filterlists/module.go +++ b/intel/filterlists/module.go @@ -52,7 +52,7 @@ func prep() error { } if err := module.RegisterEventHook( - "netenv", + netenv.ModuleName, netenv.OnlineStatusChangedEvent, "Check for blocklist updates", func(ctx context.Context, _ interface{}) error { diff --git a/netenv/main.go b/netenv/main.go index e06f0f0d..c4454dca 100644 --- a/netenv/main.go +++ b/netenv/main.go @@ -6,6 +6,7 @@ import ( // Event Names const ( + ModuleName = "netenv" NetworkChangedEvent = "network changed" OnlineStatusChangedEvent = "online status changed" ) @@ -15,7 +16,7 @@ var ( ) func init() { - module = modules.Register("netenv", prep, start, nil) + module = modules.Register(ModuleName, prep, start, nil) module.RegisterEvent(NetworkChangedEvent, true) module.RegisterEvent(OnlineStatusChangedEvent, true) } diff --git a/netenv/online-status.go b/netenv/online-status.go index d0483343..c29ba513 100644 --- a/netenv/online-status.go +++ b/netenv/online-status.go @@ -10,12 +10,12 @@ import ( "sync/atomic" "time" - "github.com/safing/portbase/notifications" + "github.com/tevino/abool" "github.com/safing/portbase/log" + "github.com/safing/portbase/notifications" "github.com/safing/portmaster/network/netutils" - - "github.com/tevino/abool" + "github.com/safing/portmaster/updates" ) // OnlineStatus represent a state of connectivity to the Internet. @@ -200,13 +200,18 @@ func updateOnlineStatus(status OnlineStatus, portalURL *url.URL, comment string) // trigger event if changed { - module.TriggerEvent(OnlineStatusChangedEvent, nil) + module.TriggerEvent(OnlineStatusChangedEvent, status) if status == StatusPortal { log.Infof(`netenv: setting online status to %s at "%s" (%s)`, status, portalURL, comment) } else { log.Infof("netenv: setting online status to %s (%s)", status, comment) } triggerNetworkChangeCheck() + + // Trigger update check when coming (semi) online. + if Online() { + _ = updates.TriggerUpdate(false) + } } } diff --git a/status/module.go b/status/module.go index 61a071da..c9853f39 100644 --- a/status/module.go +++ b/status/module.go @@ -27,7 +27,7 @@ func start() error { triggerAutopilot() err := module.RegisterEventHook( - "netenv", + netenv.ModuleName, netenv.OnlineStatusChangedEvent, "update online status in system status", func(_ context.Context, _ interface{}) error { diff --git a/updates/api.go b/updates/api.go index 44ffed6f..59edc460 100644 --- a/updates/api.go +++ b/updates/api.go @@ -14,8 +14,7 @@ func registerAPIEndpoints() error { Write: api.PermitUser, BelongsTo: module, ActionFunc: func(_ *api.Request) (msg string, err error) { - forceUpdate.Set() - if err := TriggerUpdate(); err != nil { + if err := TriggerUpdate(true); err != nil { return "", err } return "triggered update check", nil diff --git a/updates/config.go b/updates/config.go index 05fb9975..2907b89c 100644 --- a/updates/config.go +++ b/updates/config.go @@ -98,14 +98,14 @@ func registerConfig() error { } func initConfig() { - releaseChannel = config.GetAsString(helper.ReleaseChannelKey, helper.ReleaseChannelStable) + releaseChannel = config.Concurrent.GetAsString(helper.ReleaseChannelKey, helper.ReleaseChannelStable) initialReleaseChannel = releaseChannel() previousReleaseChannel = releaseChannel() - enableUpdates = config.GetAsBool(enableUpdatesKey, true) + enableUpdates = config.Concurrent.GetAsBool(enableUpdatesKey, true) updatesCurrentlyEnabled = enableUpdates() - devMode = config.GetAsBool(cfgDevModeKey, false) + devMode = config.Concurrent.GetAsBool(cfgDevModeKey, false) previousDevMode = devMode() } @@ -150,7 +150,7 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { if updatesCurrentlyEnabled { module.Resolve("") - if err := TriggerUpdate(); err != nil { + if err := TriggerUpdate(false); err != nil { log.Warningf("updates: failed to trigger update: %s", err) } log.Infof("updates: automatic updates are now enabled") diff --git a/updates/main.go b/updates/main.go index 0cc7a61e..b4fb874b 100644 --- a/updates/main.go +++ b/updates/main.go @@ -7,13 +7,12 @@ import ( "runtime" "time" - "github.com/safing/portmaster/updates/helper" - "github.com/safing/portbase/dataroot" "github.com/safing/portbase/log" "github.com/safing/portbase/modules" "github.com/safing/portbase/notifications" "github.com/safing/portbase/updater" + "github.com/safing/portmaster/updates/helper" ) const ( @@ -148,8 +147,7 @@ func start() error { if !disableTaskSchedule { updateTask. Repeat(1 * time.Hour). - MaxDelay(30 * time.Minute). - Schedule(time.Now().Add(10 * time.Second)) + MaxDelay(30 * time.Minute) } if updateASAP { @@ -167,18 +165,19 @@ func start() error { } // TriggerUpdate queues the update task to execute ASAP. -func TriggerUpdate() error { +func TriggerUpdate(force bool) error { switch { case !module.OnlineSoon(): return fmt.Errorf("updates module is disabled") + case !force && !enableUpdates(): + return fmt.Errorf("automatic updating is disabled") + case !module.Online(): updateASAP = true - case forceUpdate.IsNotSet() && !enableUpdates(): - return fmt.Errorf("automatic updating is disabled") - default: + forceUpdate.Set() updateTask.StartASAP() } @@ -200,16 +199,14 @@ func DisableUpdateSchedule() error { } func checkForUpdates(ctx context.Context) (err error) { - if !updatesCurrentlyEnabled && !forceUpdate.IsSet() { - log.Debugf("updates: automatic updates are disabled") + if !forceUpdate.SetToIf(true, false) && !enableUpdates() { + log.Warningf("updates: automatic updates are disabled") return nil } - forceUpdate.UnSet() - - defer log.Debugf("updates: finished checking for updates") defer func() { if err == nil { + log.Infof("updates: successfully checked for updates") module.Resolve(updateFailed) notifications.Notify(¬ifications.Notification{ EventID: updateSuccess,