summaryrefslogtreecommitdiff
path: root/systray.go
blob: e325844f55f097b950a35babf400a4642c04751a (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (C) 2018 LEAP
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"log"
	"time"

	"0xacab.org/leap/bitmask-systray/bitmask"
	"0xacab.org/leap/bitmask-systray/icon"
	"github.com/getlantern/systray"
	"github.com/skratchdot/open-golang/open"
)

type bmTray struct {
	bm            *bitmask.Bitmask
	conf          *systrayConfig
	waitCh        chan bool
	mStatus       *systray.MenuItem
	mTurnOn       *systray.MenuItem
	mTurnOff      *systray.MenuItem
	mDonate       *systray.MenuItem
	mCancel       *systray.MenuItem
	activeGateway *gatewayTray
}

type gatewayTray struct {
	menuItem *systray.MenuItem
	name     string
}

func run(bm *bitmask.Bitmask, conf *systrayConfig) {
	bt := bmTray{bm: bm, conf: conf}
	systray.Run(bt.onReady, bt.onExit)
}

func (bt bmTray) onExit() {
	status, _ := bt.bm.GetStatus()
	if status != "off" {
		ch := bt.bm.GetStatusCh()
		bt.bm.StopVPN()
		status = <-ch
	}
	log.Println("Closing systray")
}

func (bt *bmTray) onReady() {
	systray.SetIcon(icon.Off)

	bt.mStatus = systray.AddMenuItem(printer.Sprintf("Checking status..."), "")
	bt.mStatus.Disable()
	bt.mTurnOn = systray.AddMenuItem(printer.Sprintf("Turn on"), "")
	bt.mTurnOn.Hide()
	bt.mTurnOff = systray.AddMenuItem(printer.Sprintf("Turn off"), "")
	bt.mTurnOff.Hide()
	bt.mCancel = systray.AddMenuItem(printer.Sprintf("Cancel"), printer.Sprintf("Cancel connection to %s", applicationName))
	bt.mCancel.Hide()
	systray.AddSeparator()

	if bt.conf.SelectWateway {
		bt.addGateways()
	}

	mHelp := systray.AddMenuItem(printer.Sprintf("Help ..."), "")
	bt.mDonate = systray.AddMenuItem(printer.Sprintf("Donate ..."), "")
	systray.AddSeparator()

	mQuit := systray.AddMenuItem(printer.Sprintf("Quit"), "")

	go func() {
		ch := bt.bm.GetStatusCh()
		if status, err := bt.bm.GetStatus(); err != nil {
			log.Printf("Error getting status: %v", err)
		} else {
			bt.changeStatus(status)
		}

		for {
			select {
			case status := <-ch:
				log.Println("status: " + status)
				bt.changeStatus(status)

			case <-bt.mTurnOn.ClickedCh:
				log.Println("on")
				bt.bm.StartVPN(provider)
				bt.conf.setUserStoppedVPN(false)
			case <-bt.mTurnOff.ClickedCh:
				log.Println("off")
				bt.bm.StopVPN()
				bt.conf.setUserStoppedVPN(true)
			case <-bt.mCancel.ClickedCh:
				log.Println("cancel")
				bt.bm.StopVPN()
				bt.conf.setUserStoppedVPN(true)

			case <-mHelp.ClickedCh:
				open.Run("https://riseup.net/vpn")
			case <-bt.mDonate.ClickedCh:
				bt.conf.setDonated()
				open.Run("https://riseup.net/donate-vpn")

			case <-mQuit.ClickedCh:
				systray.Quit()

			case <-time.After(time.Minute * 30):
			}
		}
	}()
}

func (bt *bmTray) addGateways() {
	gatewayList, err := bt.bm.ListGateways(provider)
	if err != nil {
		log.Printf("Gateway initialization error: %v", err)
		return
	}

	mGateway := systray.AddMenuItem(printer.Sprintf("Route traffic through"), "")
	mGateway.Disable()
	for i, city := range gatewayList {
		menuItem := systray.AddMenuItem(city, printer.Sprintf("Use %s %v gateway", applicationName, city))
		gateway := gatewayTray{menuItem, city}

		if i == 0 {
			menuItem.Check()
			menuItem.SetTitle("*" + city)
			bt.activeGateway = &gateway
		} else {
			menuItem.Uncheck()
		}

		go func(gateway gatewayTray) {
			for {
				<-menuItem.ClickedCh
				gateway.menuItem.SetTitle("*" + gateway.name)
				gateway.menuItem.Check()

				bt.activeGateway.menuItem.Uncheck()
				bt.activeGateway.menuItem.SetTitle(bt.activeGateway.name)
				bt.activeGateway = &gateway

				bt.bm.UseGateway(gateway.name)
			}
		}(gateway)
	}

	systray.AddSeparator()
}

func (bt *bmTray) changeStatus(status string) {
	bt.mTurnOn.SetTitle(printer.Sprintf("Turn on"))
	if bt.waitCh != nil {
		bt.waitCh <- true
		bt.waitCh = nil
	}

	var statusStr string
	switch status {
	case "on":
		systray.SetIcon(icon.On)
		statusStr = printer.Sprintf("%s on", applicationName)
		bt.mTurnOn.Hide()
		bt.mTurnOff.Show()
		bt.mCancel.Hide()

	case "off":
		systray.SetIcon(icon.Off)
		statusStr = printer.Sprintf("%s off", applicationName)
		bt.mTurnOn.Show()
		bt.mTurnOff.Hide()
		bt.mCancel.Hide()

	case "starting":
		bt.waitCh = make(chan bool)
		go bt.waitIcon()
		statusStr = printer.Sprintf("Connecting to %s", applicationName)
		bt.mTurnOn.Hide()
		bt.mTurnOff.Hide()
		bt.mCancel.Show()

	case "stopping":
		bt.waitCh = make(chan bool)
		go bt.waitIcon()
		statusStr = printer.Sprintf("Stopping %s", applicationName)
		bt.mTurnOn.Hide()
		bt.mTurnOff.Hide()
		bt.mCancel.Hide()

	case "failed":
		systray.SetIcon(icon.Blocked)
		bt.mTurnOn.SetTitle(printer.Sprintf("Retry"))
		statusStr = printer.Sprintf("%s blocking internet", applicationName)
		bt.mTurnOn.Show()
		bt.mTurnOff.Show()
		bt.mCancel.Hide()
	}

	systray.SetTooltip(statusStr)
	bt.mStatus.SetTitle(statusStr)
}

func (bt *bmTray) waitIcon() {
	icons := [][]byte{icon.Wait0, icon.Wait1, icon.Wait2, icon.Wait3}
	for i := 0; true; i = (i + 1) % 4 {
		systray.SetIcon(icons[i])

		select {
		case <-bt.waitCh:
			return
		case <-time.After(time.Millisecond * 500):
			continue
		}
	}
}