From ea932e4304c17eddb63dbfbd4aa6d1b83b58c0d3 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 26 Oct 2021 18:51:52 +0200 Subject: manager: delay boottime updates and simplify Signed-off-by: Jason A. Donenfeld --- main.go | 10 ++++++++++ manager/updatestate.go | 29 ++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 3fad1a24..9a8a8607 100644 --- a/main.go +++ b/main.go @@ -6,15 +6,18 @@ package main import ( + "crypto/rand" "debug/pe" "errors" "fmt" "io" "log" + unsafeRand "math/rand" "os" "strconv" "strings" "time" + "unsafe" "golang.org/x/sys/windows" @@ -150,12 +153,19 @@ func pipeFromHandleArgument(handleStr string) (*os.File, error) { return os.NewFile(uintptr(handleInt), "pipe"), nil } +func seedUnsafeRng() { + var seed int64 + rand.Read(unsafe.Slice((*byte)(unsafe.Pointer(&seed)), unsafe.Sizeof(seed))) + unsafeRand.Seed(seed) +} + func main() { if windows.SetDllDirectory("") != nil || windows.SetDefaultDllDirectories(windows.LOAD_LIBRARY_SEARCH_SYSTEM32) != nil { panic("failed to restrict dll search path") } setLogFile() + seedUnsafeRng() checkForWow64() if len(os.Args) <= 1 { diff --git a/manager/updatestate.go b/manager/updatestate.go index 069e9b8a..5ea0f661 100644 --- a/manager/updatestate.go +++ b/manager/updatestate.go @@ -7,8 +7,10 @@ package manager import ( "log" + unsafeRand "math/rand" "time" + "golang.zx2c4.com/wireguard/windows/services" "golang.zx2c4.com/wireguard/windows/updater" "golang.zx2c4.com/wireguard/windows/version" ) @@ -23,6 +25,10 @@ const ( var updateState = UpdateStateUnknown +func jitterSleep(min, max time.Duration) { + time.Sleep(min + time.Duration(unsafeRand.Int63n(int64(max-min+1)))) +} + func checkForUpdates() { if !version.IsRunningOfficialVersion() { log.Println("Build is not official, so updates are disabled") @@ -30,26 +36,27 @@ func checkForUpdates() { IPCServerNotifyUpdateFound(updateState) return } - - first := true + if services.StartedAtBoot() { + jitterSleep(time.Minute*2, time.Minute*5) + } + noError, didNotify := true, false for { update, err := updater.CheckForUpdate() - if err == nil && update != nil { + if err == nil && update != nil && !didNotify { log.Println("An update is available") updateState = UpdateStateFoundUpdate IPCServerNotifyUpdateFound(updateState) - return - } - if err != nil { + didNotify = true + } else if err != nil && !didNotify { log.Printf("Update checker: %v", err) - if first { - time.Sleep(time.Minute * 4) - first = false + if noError { + jitterSleep(time.Minute*4, time.Minute*6) + noError = false } else { - time.Sleep(time.Minute * 25) + jitterSleep(time.Minute*25, time.Minute*30) } } else { - time.Sleep(time.Hour) + jitterSleep(time.Hour-time.Minute*3, time.Hour+time.Minute*3) } } } -- cgit v1.2.3-59-g8ed1b