-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnordvpn.go
229 lines (194 loc) · 4.1 KB
/
nordvpn.go
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
package main
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"regexp"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type (
Status int
NordVPN struct {
sync.Mutex
status Status
connected bool
killswtich bool
server string
}
CountryCodeMap map[string]string
)
const (
STALLED Status = iota
FAILED
NONETWORK
DONE
CONNECTED = "Connected"
DISCONNECTED = "Disconnected"
ENABLED = "enabled"
DISABLED = "disabled"
noNetErrStr = "Please check your internet connection and try again"
)
var (
ErrNoNet = errors.New("no network connection")
reConStatus = regexp.MustCompile("Status: (.*)")
reServer = regexp.MustCompile("Current server: (.*)")
reKillSwitch = regexp.MustCompile("Kill Switch: (.*)")
)
func (n *NordVPN) Update() {
n.Lock()
defer n.Unlock()
out, err := execCmd(2*time.Second, "nordvpn", "status")
if err != nil {
n.parseErr("update on status", err)
return
}
n.status = DONE
n.parseStatus(out)
out, err = execCmd(2*time.Second, "nordvpn", "settings")
if err != nil {
n.parseErr("update", err)
return
}
n.parseSettings(out)
}
func (n *NordVPN) parseStatus(data string) {
var conStatus string
ss := reConStatus.FindStringSubmatch(data)
if len(ss) > 1 {
conStatus = ss[1]
}
switch conStatus {
case CONNECTED:
n.connected = true
n.updateServer(data)
case DISCONNECTED:
n.connected = false
default:
n.status = STALLED
log.Warnf("unrecognized status %s: %s", data, conStatus)
}
}
func (n *NordVPN) parseSettings(data string) {
var ks string
ss := reKillSwitch.FindStringSubmatch(data)
if len(ss) > 1 {
ks = ss[1]
}
switch ks {
case ENABLED:
n.killswtich = true
case DISABLED:
n.killswtich = false
default:
n.status = STALLED
log.Warnf("unrecognized kill swtich %s: %s", data, ks)
}
}
func (n *NordVPN) parseErr(cmd string, err error) {
switch err {
case context.DeadlineExceeded:
log.Errorf("on %s exceeded timeout", cmd)
n.status = STALLED
case ErrNoNet:
log.Debugln("on %s: no network", cmd)
n.status = NONETWORK
default:
log.Errorf("on %s: %s", cmd, err)
n.status = FAILED
}
}
func (n *NordVPN) updateServer(data string) {
ss := reServer.FindStringSubmatch(data)
if len(ss) > 1 {
n.server = ss[1]
}
}
func (n *NordVPN) Status() Status {
return n.status
}
func (n *NordVPN) Connect(country string) {
var err error
// cannot just pass "" as execCmd argument after "c", the executable will think we try to connect to "" instead of whatever country is best
if country == "" {
_, err = execCmd(3*time.Second, "nordvpn", "c")
} else {
_, err = execCmd(3*time.Second, "nordvpn", "c", country)
}
if err != nil {
n.parseErr("connect", err)
return
}
n.status = DONE
}
func (n *NordVPN) Disconnect() {
_, err := execCmd(3*time.Second, "nordvpn", "d")
if err != nil {
n.parseErr("disconnect", err)
return
}
n.status = DONE
}
func (n *NordVPN) Connected() bool {
return n.connected
}
func (n *NordVPN) Server() string {
return n.server
}
func (n *NordVPN) KillSwitch() bool {
return n.killswtich
}
func (n *NordVPN) SetKillSwitch(v bool) {
s := "on"
if !v {
s = "off"
}
_, err := execCmd(3*time.Second, "nordvpn", "set", "killswitch", s)
if err != nil {
n.parseErr("set killswitch", err)
return
}
n.status = DONE
}
func (n *NordVPN) GetCountryCodeMap() CountryCodeMap {
n.Lock()
defer n.Unlock()
var client http.Client
resp, err := client.Get("https://api.nordvpn.com/v1/servers/countries")
if err != nil {
n.parseErr("http get", err)
return nil
}
defer resp.Body.Close()
cm := CountryCodeMap{}
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
n.parseErr("http body read", err)
return nil
}
type (
// list only interesting fields, ignore the rest
Country struct {
Name string `json:"name"`
Code string `json:"code"`
}
CountryList []Country
)
var cl CountryList
err = json.Unmarshal(bodyBytes, &cl)
if err != nil {
n.parseErr("json unmarshal", err)
return nil
}
for _, c := range cl {
cm[c.Name] = strings.ToLower(c.Code)
}
}
n.status = DONE
return cm
}