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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
// 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"
"os"
"time"
"0xacab.org/leap/bitmask-systray/bitmask"
"0xacab.org/leap/bitmask-systray/icon"
"0xacab.org/leap/go-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
mHaveDonated *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() {
// TODO: this doesn't get executed :(
log.Println("Finished onExit")
}
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"), printer.Sprintf("Turn RiseupVPN on"))
bt.mTurnOn.Hide()
bt.mTurnOff = systray.AddMenuItem(printer.Sprintf("Turn off"), printer.Sprintf("Turn RiseupVPN off"))
bt.mTurnOff.Hide()
bt.mCancel = systray.AddMenuItem(printer.Sprintf("Cancel"), printer.Sprintf("Cancel connection to RiseupVPN"))
bt.mCancel.Hide()
systray.AddSeparator()
if bt.conf.SelectWateway {
bt.addGateways()
}
mHelp := systray.AddMenuItem(printer.Sprintf("Help ..."), "")
bt.mDonate = systray.AddMenuItem(printer.Sprintf("Donate ..."), "")
bt.mHaveDonated = systray.AddMenuItem(printer.Sprintf("... I have donated"), "")
mAbout := systray.AddMenuItem(printer.Sprintf("About ..."), "")
systray.AddSeparator()
mQuit := systray.AddMenuItem(printer.Sprintf("Quit"), printer.Sprintf("Quit BitmaskVPN"))
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 {
bt.updateDonateMenu()
select {
case status := <-ch:
log.Println("status: " + status)
bt.changeStatus(status)
case <-bt.mTurnOn.ClickedCh:
log.Println("on")
bt.bm.StartVPN(provider)
case <-bt.mTurnOff.ClickedCh:
log.Println("off")
bt.bm.StopVPN()
case <-bt.mCancel.ClickedCh:
log.Println("cancel")
bt.bm.StopVPN()
case <-mHelp.ClickedCh:
open.Run("https://riseup.net/vpn")
case <-bt.mDonate.ClickedCh:
open.Run("https://riseup.net/donate-vpn")
case <-bt.mHaveDonated.ClickedCh:
bt.conf.setDonated()
case <-mAbout.ClickedCh:
open.Run("https://bitmask.net")
case <-mQuit.ClickedCh:
systray.Quit()
// XXX: this a hack as quit doesn't work
// this should be done by defer in the main function
bt.bm.Close()
releasePID()
log.Println("Quit now...")
os.Exit(0)
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, name := range gatewayList {
menuItem := systray.AddMenuItem(name, printer.Sprintf("Use RiseupVPN %v gateway", name))
gateway := gatewayTray{menuItem, name}
if i == 0 {
menuItem.Check()
menuItem.SetTitle("*" + name)
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) {
statusStr := status
bt.mTurnOn.SetTitle(printer.Sprintf("Turn on"))
if bt.waitCh != nil {
bt.waitCh <- true
bt.waitCh = nil
}
switch status {
case "on":
systray.SetIcon(icon.On)
bt.mTurnOn.Hide()
bt.mTurnOff.Show()
bt.mCancel.Hide()
case "off":
systray.SetIcon(icon.Off)
bt.mTurnOn.Show()
bt.mTurnOff.Hide()
bt.mCancel.Hide()
case "starting":
bt.waitCh = make(chan bool)
go bt.waitIcon()
bt.mTurnOn.Hide()
bt.mTurnOff.Hide()
bt.mCancel.Show()
case "stopping":
bt.waitCh = make(chan bool)
go bt.waitIcon()
bt.mTurnOn.Hide()
bt.mTurnOff.Hide()
bt.mCancel.Hide()
case "failed":
systray.SetIcon(icon.Blocked)
bt.mTurnOn.SetTitle(printer.Sprintf("Retry"))
bt.mTurnOn.Show()
bt.mTurnOff.Show()
bt.mCancel.Hide()
statusStr = printer.Sprintf("blocking internet")
}
systray.SetTooltip(printer.Sprintf("RiseupVPN is %v", statusStr))
bt.mStatus.SetTitle(printer.Sprintf("VPN is %v", statusStr))
bt.mStatus.SetTooltip(printer.Sprintf("RiseupVPN is %v", statusStr))
}
func (bt *bmTray) updateDonateMenu() {
if bt.conf.hasDonated() {
bt.mHaveDonated.Hide()
bt.mDonate.Hide()
} else {
bt.mHaveDonated.Show()
bt.mDonate.Show()
}
}
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
}
}
}
|