aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/service/updatestate.go
blob: c046edb2548795bc70d6757b406ac810f679e9d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package service

import (
	"golang.zx2c4.com/wireguard/windows/updater"
	"golang.zx2c4.com/wireguard/windows/version"
	"log"
	"time"
)

type UpdateState uint32

const (
	UpdateStateUnknown UpdateState = iota
	UpdateStateFoundUpdate
	UpdateStateUpdatesDisabledUnofficialBuild
)

var updateState = UpdateStateUnknown

func checkForUpdates() {
	if !version.IsRunningOfficialVersion() {
		log.Println("Build is not official, so updates are disabled")
		updateState = UpdateStateUpdatesDisabledUnofficialBuild
		IPCServerNotifyUpdateFound(updateState)
		return
	}

	time.Sleep(time.Second * 10)

	first := true
	for {
		update, err := updater.CheckForUpdate()
		if err == nil && update != nil {
			log.Println("An update is available")
			updateState = UpdateStateFoundUpdate
			IPCServerNotifyUpdateFound(updateState)
			return
		}
		if err != nil {
			log.Printf("Update checker: %v", err)
			if first {
				time.Sleep(time.Minute * 4)
				first = false
			} else {
				time.Sleep(time.Minute * 25)
			}
		} else {
			time.Sleep(time.Hour)
		}
	}
}