forked from bajcmartinez/nordvpn-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
293 lines (254 loc) · 8.58 KB
/
extension.js
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
// Includes;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const AggregateMenu = Main.panel.statusArea.aggregateMenu;
let vpnStatusIndicator;
const customGroups = [
{
name: "P2P",
label: "P2P"
},
{
name: "Double_VPN",
label: "Double VPN"
},
{
name: "Dedicated_IP",
label: "Dedicated IP"
},
{
name: "Onion_Over_VPN",
label: "Onion"
}
];
class NordVPN {
constructor () {
this._commands = {
connect: "nordvpn c",
disconnect: "nordvpn d",
status: "nordvpn status",
groups: "nordvpn groups"
};
this._states = {
CONNECTED: "CONNECTED"
};
this.groups = [];
this.group = "";
}
/**
* Call NordVPN Command Line Tool to connect to the VPN Service
*
* @param {String} group Group you want to connect to (e.g. nordvpn groups)
* @returns {void}
*/
connect (group = "") {
this.group = group;
GLib.spawn_command_line_async(`${this._commands.connect} ${group}`);
}
/**
* Call NordVPN Command Line Tool to disconnect from the VPN Service
*
* @returns {void}
*/
disconnect () {
this.group = "";
GLib.spawn_command_line_async(this._commands.disconnect);
}
/**
* Call NordVPN Command Line Tool to filter custom groups with VPN groups
*
* @returns {void}
*/
getGroups () {
const data = (GLib.spawn_command_line_sync(this._commands.groups)[1]);
let groups;
if (data instanceof Uint8Array) {
groups = imports.byteArray.toString(data).trim();
} else {
groups = data.toString().trim();
}
const available = groups.replace(/[\-\r,]+/g, " ").split(" ").filter(group => group);
this.groups = customGroups.filter(group => available.indexOf(group.name) >= 0);
}
/**
* Call NordVPN Command Line Tool to get the status of the VPN connection
*
* @returns {{connected: Boolean, country: String, city: String, fullStatus: String, serverNumber: Number, status: String}|{connected: Boolean, fullStatus: String, status: String}}
*/
getStatus () {
const data = (GLib.spawn_command_line_sync(this._commands.status)[1]);
let fullStatus;
if (data instanceof Uint8Array) {
fullStatus = imports.byteArray.toString(data).trim();
} else {
fullStatus = data.toString().trim();
}
const result = fullStatus.split("\n");
const statusLine = result.find((line) => line.includes("Status:"));
const status = statusLine ? statusLine.replace("Status:", "").trim() : "Unknown";
if (status.toUpperCase() === this._states.CONNECTED) {
const serverNumberLine = result.find((line) => line.includes("server:"));
const countryLine = result.find((line) => line.includes("Country:"));
const cityLine = result.find((line) => line.includes("City:"));
const serverNumber = serverNumberLine ? serverNumberLine.match(/\d+/) : "Unknown";
const country = countryLine ? countryLine.replace("Country:", "").trim() : "Unknown";
const city = cityLine ? cityLine.replace("City:", "").trim() : "Unknown";
const group = (customGroups.find(group => group.name === this.group) ? customGroups.find(group => group.name === this.group).name : "Standard");
return {
connected: true,
group,
status,
serverNumber,
country,
city,
fullStatus
};
} else {
return {
connected: false,
group: "Standard",
status: status,
fullStatus
};
}
}
}
class VPNStatusIndicator extends PanelMenu.SystemIndicator {
constructor () {
super();
// Add the indicator to the indicator bar
this._indicator = this._addIndicator();
this._indicator.icon_name = "network-vpn-symbolic";
this._indicator.visible = false;
// Build a menu
// Main item with the header section
this._item = new PopupMenu.PopupSubMenuMenuItem("NordVPN", true);
this._item.icon.icon_name = "network-vpn-symbolic";
this._item.label.clutter_text.x_expand = true;
this.menu.addMenuItem(this._item);
// Content Inside the box
this._item.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._connectionGroup = new PopupMenu.PopupMenuItem("");
this._item.menu.addMenuItem(this._connectionGroup);
this._connectionDetails = new PopupMenu.PopupMenuItem("");
this._item.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._item.menu.addMenuItem(this._connectionDetails);
// Initiate NordVPN handler
this.vpnHandler = new NordVPN();
// Add elements to the UI
AggregateMenu._indicators.insert_child_at_index(this.indicators, 0);
AggregateMenu.menu.addMenuItem(this.menu, 4);
}
enable () {
this.vpnHandler.getGroups();
this.resetTimer();
this._refresh();
}
/**
* Call NordVPN Command Line Tool to connect to the VPN Service
*
* @param {String} group Group to connect width
* @private
*/
_connect (group = "") {
this.stopTimer();
this.vpnHandler.connect(group);
this.resetTimer();
this.startTimer();
}
/**
* Call NordVPN Command Line Tool to disconnect from the VPN Service
*
* @private
*/
_disconnect () {
this.stopTimer();
this.vpnHandler.disconnect();
this.resetTimer();
this.startTimer();
}
/**
* Call NordVPN Command Line Tool to get the current status of the connection
*
* @private
*/
_refresh () {
this.stopTimer();
this._update(this.vpnHandler.getStatus());
this.startTimer();
}
/**
* Updates the widgets based on the vpn status
*
* @param {Object} vpnStatus
* @private
*/
_update (vpnStatus) {
// Update the panel button
this._indicator.visible = vpnStatus.connected;
this._item.label.text = `NordVPN ${vpnStatus.status}`;
if (vpnStatus.connected) {
if (!this._disconnectAction)
this._disconnectAction = this._item.menu.addAction("Disconnect", this._disconnect.bind(this));
if (this._connectAction) {
for (const i in this._connectAction) {
this._connectAction[i].destroy();
}
this._connectAction = null;
}
} else {
if (!this._connectAction) {
this._connectAction = [];
this._connectAction.push(this._item.menu.addAction("Connect", this._connect.bind(this)));
for (const group of this.vpnHandler.groups) {
this._connectAction.push(this._item.menu.addAction(`Connect to ${group.label}`, function () {
this._connect(group.name);
}.bind(this)));
}
}
if (this._disconnectAction) {
this._disconnectAction.destroy();
this._disconnectAction = null;
}
}
this._connectionGroup.label.text = vpnStatus.group;
this._connectionDetails.label.text = vpnStatus.fullStatus;
}
resetTimer () {
this._timerStep = 1;
}
startTimer () {
this._timer = Mainloop.timeout_add_seconds(this._timerStep, Lang.bind(this, this._refresh));
this._timerStep = this._timerStep * 2;
this._timerStep = (this._timerStep > 30) ? 30 : this._timerStep;
}
stopTimer () {
if (this._timer) {
Mainloop.source_remove(this._timer);
delete this._timer;
}
}
destroy () {
this.stopTimer();
// Call destroy on the parent
this.indicators.destroy();
if (typeof this.parent === "function") {
this.parent();
}
}
}
function init () { }
function enable () {
// Init the indicator
vpnStatusIndicator = new VPNStatusIndicator();
vpnStatusIndicator.enable();
}
function disable () {
// Remove the indicator from the panel
vpnStatusIndicator.destroy();
vpnStatusIndicator = null;
}