aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/ui.go
blob: b662f96d47f383c313051bd16df2f54db29dbf6e (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package ui

import (
	"fmt"
	"github.com/lxn/walk"
	"golang.zx2c4.com/wireguard/windows/service"
	"runtime"
	"runtime/debug"
	"time"
)

var shouldQuitManagerWhenExiting = false
var startTime = time.Now()

func RunUI() {
	runtime.LockOSThread()

	defer func() {
		if err := recover(); err != nil {
			walk.MsgBox(nil, "Panic", fmt.Sprint(err, "\n\n", string(debug.Stack())), walk.MsgBoxIconError)
			panic(err)
		}
	}()

	var err error

	var (
		mtw  *ManageTunnelsWindow
		tray *Tray
	)

	for mtw == nil {
		mtw, err = NewManageTunnelsWindow()
		if err != nil {
			time.Sleep(time.Millisecond * 400)
		}
	}

	for tray == nil {
		tray, err = NewTray(mtw)
		if err != nil {
			time.Sleep(time.Millisecond * 400)
		}
	}

	service.IPCClientRegisterManagerStopping(func() {
		mtw.Synchronize(func() {
			walk.App().Exit(0)
		})
	})

	onUpdateNotification := func(updateState service.UpdateState) {
		if updateState == service.UpdateStateUnknown {
			return
		}
		mtw.Synchronize(func() {
			switch updateState {
			case service.UpdateStateFoundUpdate:
				mtw.UpdateFound()
				tray.UpdateFound()
			case service.UpdateStateUpdatesDisabledUnofficialBuild:
				mtw.SetTitle(mtw.Title() + " (unsigned build, no updates)")
			}
		})
	}
	service.IPCClientRegisterUpdateFound(onUpdateNotification)
	go func() {
		updateState, err := service.IPCClientUpdateState()
		if err == nil {
			onUpdateNotification(updateState)
		}
	}()

	mtw.Run()
	tray.Dispose()
	mtw.Dispose()

	if shouldQuitManagerWhenExiting {
		_, err := service.IPCClientQuit(true)
		if err != nil {
			walk.MsgBox(nil, "Error Exiting WireGuard", fmt.Sprintf("Unable to exit service due to: %s. You may want to stop WireGuard from the service manager.", err), walk.MsgBoxIconError)
		}
	}
}

func onQuit() {
	shouldQuitManagerWhenExiting = true
	walk.App().Exit(0)
}