golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/manager/updatestate.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package manager 7 8 import ( 9 "log" 10 "time" 11 _ "unsafe" 12 13 "golang.zx2c4.com/wireguard/windows/services" 14 "golang.zx2c4.com/wireguard/windows/updater" 15 "golang.zx2c4.com/wireguard/windows/version" 16 ) 17 18 //go:linkname fastrandn runtime.fastrandn 19 func fastrandn(n uint32) uint32 20 21 type UpdateState uint32 22 23 const ( 24 UpdateStateUnknown UpdateState = iota 25 UpdateStateFoundUpdate 26 UpdateStateUpdatesDisabledUnofficialBuild 27 ) 28 29 var updateState = UpdateStateUnknown 30 31 func jitterSleep(min, max time.Duration) { 32 time.Sleep(min + time.Millisecond*time.Duration(fastrandn(uint32((max-min+1)/time.Millisecond)))) 33 } 34 35 func checkForUpdates() { 36 if !version.IsRunningOfficialVersion() { 37 log.Println("Build is not official, so updates are disabled") 38 updateState = UpdateStateUpdatesDisabledUnofficialBuild 39 IPCServerNotifyUpdateFound(updateState) 40 return 41 } 42 if services.StartedAtBoot() { 43 jitterSleep(time.Minute*2, time.Minute*5) 44 } 45 noError, didNotify := true, false 46 for { 47 update, err := updater.CheckForUpdate() 48 if err == nil && update != nil && !didNotify { 49 log.Println("An update is available") 50 updateState = UpdateStateFoundUpdate 51 IPCServerNotifyUpdateFound(updateState) 52 didNotify = true 53 } else if err != nil && !didNotify { 54 log.Printf("Update checker: %v", err) 55 if noError { 56 jitterSleep(time.Minute*4, time.Minute*6) 57 noError = false 58 } else { 59 jitterSleep(time.Minute*25, time.Minute*30) 60 } 61 } else { 62 jitterSleep(time.Hour-time.Minute*3, time.Hour+time.Minute*3) 63 } 64 } 65 }