summaryrefslogtreecommitdiff
path: root/pkg/backend/bitmask.go
blob: 07d27ea6a2df61c0b4fcbfc4b16504aeb8300843 (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
package backend

import (
	"log"
	"os"

	"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
)

func initializeBitmask() {
	if ctx == nil {
		log.Println("error: cannot initialize bitmask, ctx is nil")
		os.Exit(1)
	}
	bitmask.InitializeLogger()

	b, err := bitmask.InitializeBitmask()
	if err != nil {
		log.Println("error: cannot initialize bitmask")
	}
	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,
		Donate:   false,
		Status:   st,
	}
	go trigger(OnStatusChanged)
	initializeBitmask()
}