diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-10-26 18:51:52 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-10-27 19:04:36 +0200 |
commit | ea932e4304c17eddb63dbfbd4aa6d1b83b58c0d3 (patch) | |
tree | e6b7665a5465ad614053f203734f21c6fb5f4752 | |
parent | embeddable-dll-service: update for new wgnt API (diff) | |
download | wireguard-windows-ea932e4304c17eddb63dbfbd4aa6d1b83b58c0d3.tar.xz wireguard-windows-ea932e4304c17eddb63dbfbd4aa6d1b83b58c0d3.zip |
manager: delay boottime updates and simplify
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | main.go | 10 | ||||
-rw-r--r-- | manager/updatestate.go | 29 |
2 files changed, 28 insertions, 11 deletions
@@ -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) } } } |