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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
package main
/* a wrapper around bitmask that exposes status to a QtQml gui */
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"reflect"
"sync"
"unsafe"
"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
"0xacab.org/leap/bitmask-vpn/pkg/pickle"
"0xacab.org/leap/bitmask-vpn/pkg/systray2"
"github.com/jmshal/go-locale"
"golang.org/x/text/message"
)
// typedef void (*cb)();
// inline void _do_callback(cb f) {
// f();
// }
import "C"
/* callbacks into C-land */
var mut sync.Mutex
var stmut sync.Mutex
var cbs = make(map[string](*[0]byte))
var initOnce sync.Once
// Events are just a enumeration of all the posible events that C functions can
// be interested in subscribing to. You cannot subscribe to an event that is
// not listed here.
type Events struct {
OnStatusChanged string
}
const OnStatusChanged string = "OnStatusChanged"
// subscribe registers a callback from C-land.
// This callback needs to be passed as a void* C function pointer.
func subscribe(event string, fp unsafe.Pointer) {
mut.Lock()
defer mut.Unlock()
e := &Events{}
v := reflect.Indirect(reflect.ValueOf(&e))
hf := v.Elem().FieldByName(event)
if reflect.ValueOf(hf).IsZero() {
fmt.Println("ERROR: not a valid event:", event)
} else {
cbs[event] = (*[0]byte)(fp)
}
}
// trigger fires a callback from C-land.
func trigger(event string) {
mut.Lock()
defer mut.Unlock()
cb := cbs[event]
if cb != nil {
C._do_callback(cb)
} else {
fmt.Println("ERROR: this event does not have subscribers:", event)
}
}
/* connection status */
const logFile = "systray.log"
const (
offStr = "off"
startingStr = "starting"
onStr = "on"
stoppingStr = "stopping"
failedStr = "failed"
)
// status reflects the current VPN status. Go code is responsible for updating
// it; C-land just watches its changes and pulls its updates via the serialized
// context object.
type status int
const (
off status = iota
starting
on
stopping
failed
unknown
)
func (s status) String() string {
return [...]string{offStr, startingStr, onStr, stoppingStr, failedStr}[s]
}
func (s status) MarshalJSON() ([]byte, error) {
b := bytes.NewBufferString(`"`)
b.WriteString(s.String())
b.WriteString(`"`)
return b.Bytes(), nil
}
func (s status) fromString(st string) status {
switch st {
case offStr:
return off
case startingStr:
return starting
case onStr:
return on
case stoppingStr:
return stopping
case failedStr:
return failed
default:
return unknown
}
}
// FIXME -----------------------------------------------------------------------
// at some moment I thought this was a good idea, but probably is overkill -
// and not used right now. Discuss with meskio in code review, and very likely
// remove it - there are probably better ways of dealing with tracking of user
// actions more towards the ui layer.
// An action is originated in the UI. These represent requests coming from the
// frontend via the C code. VPN code needs to watch them and fullfill their
// requests as soon as possible.
type actions int
const (
switchOn actions = iota
switchOff
unblock
)
func (a actions) String() string {
return [...]string{"switchOn", "switchOff", "unblock"}[a]
}
func (a actions) MarshalJSON() ([]byte, error) {
b := bytes.NewBufferString(`"`)
b.WriteString(a.String())
b.WriteString(`"`)
return b.Bytes(), nil
}
// -----------------------------------------------------------------------------
// The connectionCtx keeps the global state that is passed around to C-land. It
// also serves as the primary way of passing requests from the frontend to the
// Go-core, by letting the UI write some of these variables and processing
// them.
type connectionCtx struct {
AppName string `json:"appName"`
Provider string `json:"provider"`
Status status `json:"status"`
Actions []actions `json:"actions,omitempty"`
bm bitmask.Bitmask
}
func (c connectionCtx) toJson() ([]byte, error) {
stmut.Lock()
defer stmut.Unlock()
b, err := json.Marshal(c)
if err != nil {
log.Println(err)
return nil, err
}
return b, nil
}
func (c connectionCtx) updateStatus() {
if stStr, err := c.bm.GetStatus(); err != nil {
log.Printf("Error getting status: %v", err)
} else {
setStatusFromStr(stStr)
}
statusCh := c.bm.GetStatusCh()
for {
select {
case stStr := <-statusCh:
setStatusFromStr(stStr)
}
}
}
var ctx *connectionCtx
func setStatus(st status) {
stmut.Lock()
defer stmut.Unlock()
ctx.Status = st
go trigger(OnStatusChanged)
}
func setStatusFromStr(stStr string) {
log.Println("status:", stStr)
setStatus(unknown.fromString(stStr))
}
func initPrinter() *message.Printer {
locale, err := go_locale.DetectLocale()
if err != nil {
log.Println("Error detecting the system locale: ", err)
}
return message.NewPrinter(message.MatchLanguage(locale, "en"))
}
// initializeBitmask instantiates a bitmask connection
func initializeBitmask() {
if ctx == nil {
log.Println("error: cannot initialize bitmask, ctx is nil")
os.Exit(1)
}
conf := systray.ParseConfig()
conf.Version = "unknown"
conf.Printer = initPrinter()
b, err := bitmask.Init(conf.Printer)
if err != nil {
log.Fatal(err)
}
ctx.bm = b
}
func startVPN() {
err := ctx.bm.StartVPN(ctx.Provider)
if err != nil {
log.Println(err)
os.Exit(1)
}
}
func stopVPN() {
err := ctx.bm.StopVPN()
if err != nil {
log.Println(err)
}
}
// initializeContext initializes an empty connStatus and assigns it to the
// global ctx holder. This is expected to be called only once, so the public
// api uses the sync.Once primitive to call this.
func initializeContext(provider, appName string) {
var st status = off
ctx = &connectionCtx{
AppName: appName,
Provider: provider,
Status: st,
}
go trigger(OnStatusChanged)
initializeBitmask()
}
/* mock http server: easy way to mocking vpn behavior on ui interaction. This
* should also show a good way of writing functionality tests just for the Qml
* layer */
func mockUIOn(w http.ResponseWriter, r *http.Request) {
log.Println("changing status: on")
setStatus(on)
}
func mockUIOff(w http.ResponseWriter, r *http.Request) {
log.Println("changing status: off")
setStatus(off)
}
func mockUIFailed(w http.ResponseWriter, r *http.Request) {
log.Println("changing status: failed")
setStatus(failed)
}
func mockUI() {
http.HandleFunc("/on", mockUIOn)
http.HandleFunc("/off", mockUIOff)
http.HandleFunc("/failed", mockUIFailed)
http.ListenAndServe(":8080", nil)
}
/*
exported C api
*/
//export SwitchOn
func SwitchOn() {
setStatus(starting)
startVPN()
}
//export SwitchOff
func SwitchOff() {
setStatus(stopping)
stopVPN()
}
//export Quit
func Quit() {
if ctx.Status != off {
setStatus(stopping)
stopVPN()
}
}
//export Unblock
func Unblock() {
fmt.Println("unblock... [not implemented]")
}
//export SubscribeToEvent
func SubscribeToEvent(event string, f unsafe.Pointer) {
subscribe(event, f)
}
//export InitializeBitmaskContext
func InitializeBitmaskContext() {
provider := "black.riseup.net"
appName := "RiseupVPN"
initOnce.Do(func() {
initializeContext(provider, appName)
})
go ctx.updateStatus()
}
//export RefreshContext
func RefreshContext() *C.char {
c, _ := ctx.toJson()
return C.CString(string(c))
}
//export InstallHelpers
func InstallHelpers() {
pickle.InstallHelpers()
}
/* end of the exposed api */
/* we could enable this one optionally for the qt tests */
/* uncomment: export MockUIInteraction */
func MockUIInteraction() {
log.Println("mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.")
go mockUI()
}
func main() {}
|