-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathextension.js
1911 lines (1622 loc) · 66 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const St = imports.gi.St;
const Main = imports.ui.main;
const Panel = imports.ui.panel;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Gio = imports.gi.Gio;
const GObject = imports.gi.GObject;
const Mainloop = imports.mainloop;
const ByteArray = imports.byteArray;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Unescape= Me.imports.unescape;
const MyUtils= Convenience.MyUtils;
const Util = imports.misc.util;
const GLib = imports.gi.GLib;
const Gettext = imports.gettext.domain('gnome-shell-extensions-nvpnconnect');
const _ = Gettext.gettext;
const SubMenus= Me.imports.subMenus;
const BoxPointer = imports.ui.boxpointer;
const NORDVPN_TOOL_EXPECTED_VERSION= "3.12";
/**
* Calls for a given shell command in a synchronous way
* @function
* @param {string} cmd - the shell command to execute
* @param {string} shell - shell that will execute the command (default: "/bin/bash")
* if null, undefined or empty, acts as default call (system dependant)
* @param {number} descriptor - 1 (default) for the function to return the stdout output,
* 2 for stderr.
* @returns {string} the stddout of the command's exectuion as a string
*/
function COMMAND_LINE_SYNC(cmd, shell="/bin/bash", descriptor=1, locale="en_US.UTF-8"){
let _cmd= (Boolean(shell) && Boolean(locale))? "LANG="+locale+"; "+cmd : cmd;
let command= (Boolean(shell))? (shell + " -c \""+ _cmd + "\"") : _cmd;
return ByteArray.toString(GLib.spawn_command_line_sync(command)[(descriptor>=2)?2:1]);
}
/**
* Calls for a given shell command in an asynchronous way
* @function
* @param {string} cmd - the shell command to execute
* @param {string} shell - shell that will execute the command (default: "/bin/bash")
* if null, undefined or empty, acts as default call (system dependant)
*/
function COMMAND_LINE_ASYNC(cmd, shell="/bin/bash"){
let command= (shell)? (shell + " -c \""+ cmd + "\"") : cmd;
GLib.spawn_command_line_async(command);
}
/**
* Dictionnary that pair up country from their country code
* (seems similar to the ISO norm except for the uk ('uk' instead
* of 'gb' )
*/
const Country_Dict= {
al: "Albania", de: "Germany", pl: "Poland",
ar: "Argentina", gr: "Greece", pt: "Portugal",
au: "Australia", hk: "Hong_Kong", ro: "Romania",
at: "Austria", hu: "Hungary", //ru: "Russia", (russia no longer availabe due to governmental reasons)
az: "Azerbaijan", is: "Iceland", rs: "Serbia",
be: "Belgium", in: "India", sg: "Singapore",
ba: "Bosnia_And_Herzegovina", id: "Indonesia", sk: "Slovakia",
br: "Brazil", ie: "Ireland", si: "Slovenia",
bg: "Bulgaria", il: "Israel", za: "South_Africa",
ca: "Canada", it: "Italy", kp: "South_Korea",
cl: "Chile", jp: "Japan", es: "Spain",
cr: "Costa_Rica", lv: "Latvia", se: "Sweden",
hr: "Croatia", lu: "Luxembourg", ch: "Switzerland",
cy: "Cyprus", mk: "Macedonia", tw: "Taiwan",
cz: "Czech_Republic", my: "Malaysia", th: "Thailand",
dk: "Denmark", mx: "Mexico", tr: "Turkey",
ee: "Estonia", md: "Moldova", ua: "Ukraine",
fi: "Finland", nl: "Netherlands", uk: "United_Kingdom",//gb: "United_Kingdom",
fr: "France", nz: "New_Zealand", us: "United_States",
ge: "Georgia", no: "Norway", vn: "Vietnam"
};
/**
* The list of all the 'groups' that the CLI tool can connect to
*/
const Group_List=[
"Africa,_The_Middle_East_And_India",
"Asia_Pacific",
"Europe",
"The_Americas",
"Dedicated_IP",
"P2P",
"Double_VPN",
//"Onion_Over_VPN", //this group seems to have disappeared from the CLI tool
];
/**
* Class that allows to store and extract infos about the currently
* connected server from the 'status' textual output of the CLI tool.
*/
class ServerInfos{
constructor(){
this.reset();
}
/**
* Method that (re)initializes the data
*/
reset(){
this._connected= false;
this._current_serv= undefined;
this._country= undefined;
this._city= undefined;
this._ip= [undefined, undefined, undefined, undefined];
this._protocol= false; //false=UDP, true= TCP
this._transfer= {recv: {data: 0, unit: 'B'}, sent: {data: 0, unit: 'B'}};
this._uptime= "unknown";
this._technology= false; //false=OpenVPN, true=NordLynx
}
isConnected(){ return this._connected; }
get serverName(){return (this._current_serv)?this._current_serv:"";}
get country(){return this._country;}
get city(){return this._city;}
get ip(){return this._ip;}
isUDP(){return !(this._protocol);}
isOpenVPN(){return (!this._technology);}
get transferData(){return this._transfer;}
get uptimeInfoString(){return this._uptime;}
/**
* Method that extracts information about the server and stores it.
* The text information is expected to be (at least partially) matching
* the format of the output of the 'nordvpn status' command
*
* @param {string} txt text containing matching the 'nordvpn status' output
* from which to extract the server informations
*/
process(txt){
this.reset();
let lines= txt.split('\n');
lines.forEach( (line)=>{
var r= null;
if( (r=/[Ss]tatus:\s*(.*)$/.exec(line)) && r.length>1 ){
this._connected= r[1].match(/[Cc]onnected/)!=null;
}
else if( (r=/^[Cc]urrent\s*[Ss]erver:\s*(.*)$/.exec(line)) && r.length>1 ){
r= r[1].match(/^([a-z]{2}(\-[a-z]*)?[0-9]+)(\.nordvpn\.com)?$/);
this._current_serv= (r && r.length>0)? r[0] : undefined;
}
else if( (r=/^[Cc]ountry:\s*(.*)$/.exec(line)) && r.length>1 ){
this._country= (r[1])?r[1]:'';
}
else if( (r=/^[Cc]ity:\s*(.*)$/.exec(line)) && r.length>1 ){
this._city= (r[1])?r[1]:'';
}
else if( (r=/^.*IP:\s*(.*)$/.exec(line)) && r.length>1 ){
if( (r=r[1].match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)) ){
var i=0;
r[0].split('.').forEach( (strn) => {
this._ip[i]= parseInt(strn);
++i;
});
}
}
else if( (r=/^.*[Pp]rotocol:\s*(.*)$/.exec(line)) && r.length>1 ){
this._protocol= ( r[1] && r[1]==='TCP' );
}
else if( (r=/^.*[Tt]ransfer:\s*(.*)$/.exec(line)) && r.length>1 ){
if( (r=/^([0-9]+(\.[0-9]*)?)\s([PTGMK]i)?B\sreceived,\s*([0-9]+(\.[0-9]*)?)\s([PTGMK]i)?B\ssent.*$/.exec(r[1])) && r.length>6){
if(r[1] && r[3] && r[4] && r[6]){
let r_data= parseFloat(r[1]);
let s_data= parseFloat(r[4]);
if(r_data && s_data){
this._transfer.recv.data= r_data;
this._transfer.recv.unit= r[3]+'B';
this._transfer.sent.data= s_data;
this._transfer.sent.unit= r[6]+'B';
}
}
}
}
else if( (r=/^.*[Uu]ptime:\s*(.*)$/.exec(line)) && r.length>1 ){
this._uptime= r[1];
}
else if( (r=/^.*[Tt]echnology:\s*(.*)$/.exec(line)) && r.length>1 ){
this._technology= ( r[1] && r[1]==='NordLynx' );
}
});
}
}
/** Object that will be the access holder to this extension's gSettings */
var SETTINGS;
/**
* Class that loads the core commands of this extension that are stored in the gSettings
* It connects signals to allow any exterior change on them to take effect.
**/
class Core_CMDs{
/**
*
* @param {*} parent Parent is use to directly connect this class with the instance
* of NVPNMenu using this instance of Core_CMDs (direct callbacks)
*/
constructor(parent=null){
// Store the id of the signal connections for disarding
this.SETT_SIGS= [];
this._parent= parent;
}
/**
* Initiate all the values from the gSettings, and connect signals
*
* 'SETTINGS' variable must be correctly intiated
*/
init(){
let txt= "";
/** Here the 'Unescape.convert()' method is used since, in string read from gSettings',
* special characters don't seem to be interpreted. This helps sets things right (hopefully)
* if need be.
*/
this.command_shell= (txt=Unescape.convert(SETTINGS.get_string("cmd-shell")))?
txt : "/bin/bash";
this.tool_available= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-available")))?
txt : "hash nordvpn";
this.tool_connected_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-connected-check")))?
txt : "nordvpn status | grep -Po ' [cC]onnected'";
this.tool_transition_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-transition-check")))?
txt : "nordvpn status | grep -Po '[cC]onnecting'";
this.daemon_unreachable_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-daemon-unreachable-check")))?
txt : "nordvpn status | grep -Po 'TransientFailure.*nordvpn.sock'";
this.tool_logged_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-logged-check")))?
txt : "NVPNLOG_=$( nordvpn login --nordaccount ); ( echo $NVPNLOG_ | grep -Po 'logged' ) || ( echo $NVPNLOG_ | grep -Po '(https?:\/\/\S*login\S*)' )";
this.current_server_get= (txt=Unescape.convert(SETTINGS.get_string("cmd-current-server-get")))?
txt : "nordvpn status";
this.server_place_connect= (txt=Unescape.convert(SETTINGS.get_string("cmd-server-place-connect")))?
txt : "nordvpn c _%target%_";
this.server_disconnect= (txt=Unescape.convert(SETTINGS.get_string("cmd-server-disconnect")))?
txt : "nordvpn d";
this.daemon_online_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-daemon-online-check")))?
txt : "echo \";`systemctl --user is-active nordvpnud`;`systemctl is-active nordvpnsd`;`systemctl is-active nordvpnd`\" | grep -Po \";active$\"";
this.vpn_online_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-vpn-online-check")))?
txt : "ifconfig -a | grep tun0";
this.option_set= (txt=Unescape.convert(SETTINGS.get_string("cmd-option-set")))?
txt : "nordvpn set _%option%_ _%value%_";
this.get_options= (txt=Unescape.convert(SETTINGS.get_string("cmd-get-options")))?
txt : "nordvpn settings | sed -e :a -e N -e '$!ba' -e 's/\\n/;/g' | sed -e 's/: /:/g' | sed -e 's/ //g' | sed -e 's/-//g' | tr '[:upper:]' '[:lower:]'";
this.get_version= (txt=Unescape.convert(SETTINGS.get_string("cmd-get-version")))?
txt : "nordvpn --version | grep -Po \"([0-9]\\.?)+[0-9]\"";
this.get_groups_countries= (txt=Unescape.convert(SETTINGS.get_string("cmd-get-groups-countries")))?
txt : "echo `nordvpn groups | sed -e :a -e N -e '$!ba' -e 's/\\n/;/g'`; echo `nordvpn countries | sed -e :a -e N -e '$!ba' -e 's/\\n/;/g'`";
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-shell', () => {
this.command_shell= Unescape.convert(SETTINGS.get_string("cmd-shell"));
this._shell_valid= this.command_shell_found();
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-tool-available', () => {
this.tool_available= Unescape.convert(SETTINGS.get_string("cmd-tool-available"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-tool-connected-check', () => {
this.tool_connected_check= Unescape.convert(SETTINGS.get_string("cmd-tool-connected-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-tool-transition-check', () => {
this.tool_transition_check= Unescape.convert(SETTINGS.get_string("cmd-tool-transition-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-daemon-unreachable-check', () => {
this.daemon_unreachable_check= Unescape.convert(SETTINGS.get_string("cmd-daemon-unreachable-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-tool-logged-check', () => {
this.tool_logged_check= Unescape.convert(SETTINGS.get_string("cmd-tool-logged-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-current-server-get', () => {
this.current_server_get= Unescape.convert(SETTINGS.get_string("cmd-current-server-get"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-server-place-connect', () => {
this.server_place_connect= Unescape.convert(SETTINGS.get_string("cmd-server-place-connect"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-server-disconnect', () => {
this.server_disconnect= Unescape.convert(SETTINGS.get_string("cmd-server-disconnect"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-daemon-online-check', () => {
this.daemon_online_check= Unescape.convert(SETTINGS.get_string("cmd-daemon-online-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-vpn-online-check', () => {
this.vpn_online_check= Unescape.convert(SETTINGS.get_string("cmd-vpn-online-check"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-option-set', () => {
this.option_set= Unescape.convert(SETTINGS.get_string("cmd-option-set"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-get-options', () => {
this.get_options= Unescape.convert(SETTINGS.get_string("cmd-get-options"));
if(this._parent){
this._parent._update_status_and_ui();
}
}));
this._shell_valid= this.command_shell_found();
}
/**
* Destructor; discard connected signals
*/
destroy(){
for(var i= 0; i<this.SETT_SIGS.length; ++i){
if(this.SETT_SIGS[i])
SETTINGS.disconnect(this.SETT_SIGS[i]);
}
}
exec_sync(cmdKey, params={}, descriptor= 1){
let command= this[cmdKey];
if(!command || !this._shell_valid) return undefined;
for(var k in params){
command= command.replace("_%"+k+"%_", params[k]);
}
return COMMAND_LINE_SYNC(command, this.command_shell, descriptor);
}
exec_async(cmdKey, params={}){
let command= this[cmdKey];
if(!command || !this._shell_valid) return undefined;
for(var k in params){
command= command.replace("_%"+k+"%_", params[k]);
}
COMMAND_LINE_ASYNC(command, this.command_shell);
return true;
}
/**
* Method that checks if set command shell is accessible
* @method
* @return {boolean}
*/
command_shell_found(){
let t= COMMAND_LINE_SYNC("which "+this.command_shell, "", 2);
return !(t.includes("which: no ") || t.includes("not found"));
}
}
/**
* Since gnome-shell 3.32, this is needed on class that extends certain UI objects,
* including PanelMenu.Button
*/
let NVPNMenu = GObject.registerClass(
/** Class that implements the dedicated status area of the extension
* and contains its main menu
*/
class NVPNMenu extends PanelMenu.Button{
/** Enumerator for the values of the "main states", this extension can be found in
* @readonly
* @enum {number}
*/
static get STATUS() {
return {
/** If the 'nordvpn' tool isn't avaiblable for the extension **/
NOT_FOUND: 0,
/** The 'nordvpnd' systemd deamon isn't available or down **/
DAEMON_DOWN: 1,
/** The user hasn't set his logins through the nordvpn tool yet **/
LOGGED_OUT: 2,
/** The 'nordvpn' is processing and (dis)connection and his in transition **/
TRANSITION: 3,
/** Disconnected from any server **/
DISCONNECTED: 4,
/** connected to a server **/
CONNECTED: 5
};
}
/**
* Initiate the UI element and creates the object.
* @method
*/
_init(){
super._init(0.0, _("NordVPN"), false);
/** Create and init the gSettings's core commands manager*/
this._cmd= new Core_CMDs(this);
this._cmd.init();
/** storing signal connectio ids for later discards */
this.SETT_SIGS= [];
/** 'groups' and 'countries' dynamic storages */
this.targets= {'groups': [],'countries':[]};
this.server_info= new ServerInfos();
/** @member {boolean} nvpn_monitor
* whether or not the extension monitors the state of the connection to
* nordvpn servers
* (contrary is experimental) */
this.nvpn_monitor= true;
//unused
this._transition_time_out= 0;
this.style_class+=' panel-root-button'
/** should the status be colored according to the goption 'colored-status' */
this._b_colored_status= SETTINGS.get_boolean('colored-status');
this.SETT_SIGS[4]= SETTINGS.connect('changed::colored-status', () => {
this._b_colored_status= SETTINGS.get_boolean('colored-status');
log("nordvpn this._b_colored_status: "+this._b_colored_status);
var add=''
this._clearTransitionStateStyleClass()
switch(this.currentStatus){
case NVPNMenu.STATUS.DAEMON_DOWN:
case NVPNMenu.STATUS.LOGGED_OUT:
case NVPNMenu.STATUS.NOT_FOUND:
add= (this._b_colored_status)?" state-problem":''
break;
case NVPNMenu.STATUS.CONNECTED:
add= (this._b_colored_status)?" state-connected":''
break;
case NVPNMenu.STATUS.DISCONNECTED:
break;
case NVPNMenu.STATUS.TRANSITION:
default:
add= (this._b_colored_status)?" state-transition":''
break;
}
this.style_class+= add
});
/** this private member is the horyzontal layout box contaning the server indicator
* in the panel area*/
this._panel_hbox= new St.BoxLayout({style_class: 'panel-status-menu-hbox'});
/** the icon in the top panel area (may change according to current status)*/
this._panel_icon = new St.Icon({ icon_name: 'action-unavailable-symbolic',
style_class: 'system-status-icon nvpn-status-icon' });
this._panel_hbox.add(this._panel_icon);
/** 'NVPN' panel text label*/
this.label_nvpn= new St.Label({style_class: 'label-nvpn-panel', y_align: St.Align.END});
this.label_nvpn.text= (SETTINGS.get_boolean('compact-icon')) ? ' ' : 'NVPN ';
this.SETT_SIGS[0]= SETTINGS.connect('changed::compact-icon', () => {
this.label_nvpn.text= (SETTINGS.get_boolean('compact-icon')) ? ' ' : 'NVPN ';
});
this.label_nvpn.y_fill= false;
this._panel_hbox.add(this.label_nvpn)
this.add_child(this._panel_hbox);
/** saving this idea for later disconnection of the signal during object's destruction */
this._id_c_click1= this.connect('button-press-event',
function(){
/** only usefull if menu is opening */
if(this.menu.isOpen){
this._update_displayed_server_infos(true);
if(/*(!this.nvpn_monitor) &&*/ this.currentStatus<NVPNMenu.STATUS.CONNECTED){
this._update_status_and_ui();
}
/** if the locations menu update is still pending
* i.e.: the locations menus haven't been updated yet
* i.e.: these menu are filled only on first click*/
if(this._location_update_pending){
/** fetchs the current value of 'displayMode' option */
let displayMode= SETTINGS.get_int('target-display-mode');
/** calls for refreshing and updating the locations display
* dynamically (according to CLI) */
this._updateGroupsAndCountries();
this._fill_country_submenu_b(displayMode);
this._update_recent_location_submenu(displayMode);
/** flag that means that the udpate is still pending, is discarded */
this._location_update_pending= false;
let country= this._getCountyFromServerName(this.server_info.serverName);
if(country && this._submenuPlaces){
this._submenuPlaces.select_from_name(country);
}
}
}
}.bind(this)
);
/** this private member implements the menu that appears when user clicks on the top
* panel's indicator */
this._main_menu = new PopupMenu.PopupBaseMenuItem({
/** elements will not be interacive by default */
reactive: false
});
/** vertical box layout, the first item of our menu, that will contain all
* server information ui elements */
let vbox= new St.BoxLayout({style_class: 'nvpn-menu-vbox'});
vbox.set_vertical(true);
let hbox2= new St.BoxLayout({style_class: 'nvpn-menu-hbox'});
hbox2.x_expand= true;
hbox2.x_fill= true;
hbox2.x_align= St.Align.END
let label1= new St.Label({style_class: 'label-nvpn-menu', text: _("NordVPN")});
hbox2.add_child(label1);
/** this private member is the part of the server info that is an adaptable
* text according to status */
this._label_status= new St.Label({style_class: 'label-nvpn-status'});
/** this private member is the text label that will display the current nordvpn connected
* server name */
this.label_connection= new St.Label({style_class: 'label-nvpn-connection', text: '--'});
this.label_connection.x_expand= true;
this.label_connection.x_fill= true;
this.label_connection.x_align= St.Align.END
hbox2.add_child(this._label_status);
vbox.add_child(hbox2);
vbox.add_child(this.label_connection);
vbox.x_expand= true;
this._main_menu.actor.add(vbox)
/**
* Adding the text elements that will display the infos
* about the currently connected server
*/
let vbox3= new St.BoxLayout({style_class: 'nvpn-menu-vbox3'});
vbox3.set_vertical(true);
this._location_label= new St.Label({style_class: 'label-server-info', text: '*,*', x_align: St.Align.END});
vbox3.add_child(this._location_label);
this._ip_label= new St.Label({style_class: 'label-server-info', text: "Shown IP: ....", x_align: St.Align.END});
vbox3.add_child(this._ip_label);
this._tech_label= new St.Label({style_class: 'label-server-info', text: "Technology: ....", x_align: St.Align.END});
vbox3.add_child(this._tech_label);
this._transfer_label= new St.Label({style_class: 'label-server-info', text: "↑ - ; ↓ - ", x_align: St.Align.END});
vbox3.add_child(this._transfer_label);
this._uptime_label= new St.Label({style_class: 'label-server-info', text: "uptime: ", x_align: St.Align.END});
vbox3.add_child(this._uptime_label);
this._serverInfosItem= new PopupMenu.PopupBaseMenuItem({
reactive: false
});
vbox3.x_expand= true;
vbox3.x_fill= true;
this._serverInfosItem.actor.add(vbox3);
this.menu.addMenuItem(this._serverInfosItem);
this._serverInfosItem.actor.hide();
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
/** Adding the buttons that will respectively show/hide the submenus
* corresponding to the 'Location/group connection picker', the
* 'server specifier' and the 'option toggles'
*/
let hbox3= new St.BoxLayout();
let ic0= new St.Icon({icon_name:'mark-location-symbolic'});
this.v3_button0= new St.Button({
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action sub-menu-btn',
child:ic0});
hbox3.add_child(this.v3_button0);
let ic1= new St.Icon({icon_name:'network-server-symbolic'});
this.v3_button1= new St.Button({
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action sub-menu-btn',
child:ic1});
hbox3.add_child(this.v3_button1);
let ic2= new St.Icon({icon_name:'view-more-symbolic'});
this.v3_button2= new St.Button({
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action sub-menu-btn',
child:ic2});
hbox3.add_child(this.v3_button2);
this._itemSubmenusButtons= new PopupMenu.PopupBaseMenuItem({
reactive: false
});
hbox3.x_expand= true
hbox3.x_fill= false
hbox3.x_align= St.Align.END;
this._itemSubmenusButtons.actor.add(hbox3)
this.menu.addMenuItem(this._itemSubmenusButtons);
this._id_c_btn2= this.v3_button1.connect('clicked', this.cb_serverManagement.bind(this));
this._id_c_btn3= this.v3_button0.connect('clicked', this.cb_locationPick.bind(this));
this._id_c_btn4= this.v3_button2.connect('clicked', this.cb_options.bind(this));;
/** this private member is the implementation of the submenu that allows to select
* a nordvpn server by clicking on the country */
this._submenuPlaces= new SubMenus.LocationsMenu();
this.menu.addMenuItem(this._submenuPlaces);
/** when an item of this submenu (i.e. a place name) is selected,
* the '_place_menu_new_selection()' method will be called (no argument). */
this._submenuPlaces.select_callback(this._place_menu_new_selection.bind(this));
/** the locations menu is pending
* will be only filled on first click/opening*/
this._location_update_pending= true;
/** this private member is the implementation of the submenu that allows to select
* to input a server name to connect to it */
this._submenuServer= new SubMenus.ServerSubMenu();
/** when a server name is entered,
* the 'server_entry()' method will be called (server name as argument). */
this._submenuServer.newServerEntry_callback(this.server_entry.bind(this));
this.menu.addMenuItem(this._submenuServer);
/** this private member is the implementation of the submenu that allows to select
* to toggle different option of the nordvpn tool */
this._submenuOptions= new SubMenus.OptionsSubMenu();
/** when an option is toggled,
* the 'option_changed()' method will be called
* (the option name (string) and its new value (string) as arguments).*/
this._submenuOptions.set_optionChangeCallBack(this.option_changed.bind(this));
this.menu.addMenuItem(this._submenuOptions);
/** when a fav'd server has been clicked,
* a call to the '_serv_fav_cliked()' method
*/
this._id_sm_1= this._submenuServer.connect('server-fav-connect', this._serv_fav_cliked.bind(this));
this._id_sm_2= this._submenuServer.connect('location-connect', this._serv_fav_cliked.bind(this));
this.menu.addMenuItem(this._main_menu, 0);
/** adding a sperator int his menu to separate the 'information display' part
* from the 'connection interface' part*/
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
/** creating the menu item that contains the 'connection' menu button */
let _itemCurrent2 = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false
});
let vbox2= new St.BoxLayout({style_class: 'nvpn-menu-vbox2'});
vbox2.set_vertical(true);
this.action_button= new St.Button({style_class: 'nvpn-action-button', label: _("Quick Connect")});
/** saving this id for later disconnection of the signal during object's destruction */
this._id_c_btn1= this.action_button.connect('clicked', this._button_clicked.bind(this));
vbox2.add_child(this.action_button);
vbox2.x_expand= true;
_itemCurrent2.actor.add(vbox2);
this.menu.addMenuItem(_itemCurrent2);
/** Adding the menu item that displays a message when the CLI tool version
* doesn't match the expected version number */
let cur_ver= this._getCliToolCurrentVersion();
this._versionChecker= new SubMenus.VersionChecker(NORDVPN_TOOL_EXPECTED_VERSION, cur_ver);
this.menu.addMenuItem(this._versionChecker);
/** Choosing to show the message or not according to the Gnome Settings
* current or changing state, and if the the nordvpn CLI tool is
* effectively installed */
if(!SETTINGS.get_boolean('version-check') || !this._is_NVPN_found()){
this._versionChecker.actor.hide();
}
this.SETT_SIGS[2]= SETTINGS.connect('changed::version-check', () => {
if(SETTINGS.get_boolean('version-check')
&& (this._versionChecker.compareResult()<0)
&& (this._is_NVPN_found()))
{
this._versionChecker.actor.show();
}
else{
this._versionChecker.actor.hide();
}
});
/** update the 'locations menu' and 'recent locations' display in case the option
* value changes*/
this.SETT_SIGS[3]= SETTINGS.connect('changed::target-display-mode', () =>{
this._udpate_location_submenu();
});
this._shell_checker= new SubMenus.MessageItem(
"Extension can't use the given shell \""+this._cmd.command_shell
+ "\".\nTry setting another shell (or change its path) within this "
+ "extension's settings page"
);
this.menu.addMenuItem(this._shell_checker);
/** @member {enum} currentStatus
* member that stored the current status designating the current state of the interaction
* with the 'nordvpn' tool */
this.currentStatus= NVPNMenu.STATUS.DISCONNECTED;
/** call to the private '_update_status_and_ui()' method that updates the ui and the currentStatus
* according to the current state provided of the 'nordvpn tool' */
this._update_status_and_ui()
this._update_displayed_server_infos(true);
/**
* Access the 'refresh-delay' gSettings and connects any change to ensure it will take effect
* within this extension
*/
this._refresh_delay= SETTINGS.get_int('refresh-delay');
this.SETT_SIGS[1]= SETTINGS.connect('changed::refresh-delay', () => {
this._refresh_delay= SETTINGS.get_int('refresh-delay');
});
/** this private member is a boolean that is used (when 'true') to keep the ui from updating during
* a connection transition, for instance */
this._vpn_lock= false;
/** call to the private method '_vpn_survey()' to start "the monitoring loop "
* that update the ui in case of a 'norvdpn' tool state change */
this._vpn_survey();
var max= Math.max(this.menu.actor.width,
this._submenuServer.menu.actor.width,
this._submenuPlaces.menu.actor.width,
this._submenuOptions.menu.actor.width
);
// this.menu.actor.width=hbox3.actor.width + max;
this.menu.actor.width=hbox3.width + max
/** flag used to inform if a connexion should be registered or not as a "recent connexion"*/
this._unregister_next_connexion= false;
this._vpn_check_stop= false;
}
/**
* Disconnect the ui signals before the object's destruction
* @method
*/
_onDestroy(){
this._vpn_check_stop= true;
if(this._vpn_timeout){
Mainloop.source_remove(this._vpn_timeout);
this._vpn_timeout= null;
}
this.disconnect(this._id_c_click1);
this._id_c_click1= 0;
this.action_button.disconnect(this._id_c_btn1);
this._id_c_btn1= 0;
this.action_button.disconnect(this._id_c_btn2);
this._id_c_btn2= 0;
this.action_button.disconnect(this._id_c_btn3);
this._id_c_btn3= 0;
this.action_button.disconnect(this._id_c_btn4);
this._id_c_btn4= 0;
this._submenuServer.disconnect(this._id_sm_1);
this._id_sm_1= 0;
this._submenuServer.disconnect(this._id_sm_2);
this._id_sm_2= 0;
for(var i= 0; i<this.SETT_SIGS.length; ++i){
if(this.SETT_SIGS[i])
SETTINGS.disconnect(this.SETT_SIGS[i]);
}
if(this._cmd){
this._cmd.destroy();
}
this._submenuPlaces.destroy();
this._submenuServer.destroy();
this._submenuOptions.destroy();
super.destroy();
}
_clearTransitionStateStyleClass(){
this.style_class=
this.style_class.replaceAll(' state-transition','')
.replaceAll(' state-connected','')
.replaceAll(' state-problem','')
}
/** Private method that fetchs the current version of the NordVPN CLI tool
* by invoking the appropriate command
* @method
* @returns {string} the string that matches the found version ("0.0" if not found)
*/
_getCliToolCurrentVersion(){
let t= this._cmd.exec_sync('get_version');
let txt= (t!==undefined && t!==null && this._is_NVPN_found)? t
:"0.0";
return txt;
}
/** Private method that fetchs the 'groups' and 'country' lists given by the CLI
* @method
* @returns {object} a couple that contains the 2 lists as fields 'groups' and 'countries',
* object can be null (if call failed), or fields can be null (if results unreadable)
*/
_getGroupsAndCountries(){
/** anonymous function to suppress empty entries or invalid of lsit */
let _clearEmpty= (t) => {
var i=0;
while(i<t.length){
if(t[i]) ++i;
else t.splice(i,1);
}
}
/** calling command, initiating objects… */
let t= this._cmd.exec_sync('get_groups_countries');
let r= (t===undefined || t===null || t==='')? null : {'groups':null,'countries':null};
var tmp= [];
/** processing the command results and storing result */
if(r){
tmp= t.split('\n');
r.groups= tmp[0].split(',');
_clearEmpty(r.groups);
if(tmp.length>1){
r.countries= tmp[1].split(',');
_clearEmpty(r.countries);
}
}
return r;
}
/** Private commands that updates dynamically local attribute object that stores the 'countries'
* and 'groups' list given by the CLI
* @method
*/
_updateGroupsAndCountries(){
/** dynamically fetchs the lists */
let gac= this._getGroupsAndCountries();
/** update local attribute */
this.targets.groups= ( gac && gac.groups )? gac.groups : [];
this.targets.countries= ( gac && gac.countries )? gac.countries : [];
}
/** Private method used to hide or show the submenus and the associated buttons
* when need be.
* @method
* @param {boolean} b - Whether to show or not the submenus
*/
_submenusVisible(b){
if(b){
this._submenuPlaces.actor.show();
this._submenuServer.actor.show();
this._submenuOptions.actor.show();
this._itemSubmenusButtons.actor.show();
}
else{
this._submenuPlaces.actor.hide();
this._submenuServer.actor.hide();
this._submenuOptions.actor.hide();
this._itemSubmenusButtons.actor.hide();
}
}
/**
* Private method that determine whether or not the 'nordvpn' command tool is available
* @method
* @return {boolean}
*/
_is_NVPN_found(){
// return (COMMAND_LINE_SYNC('hash nordvpn',2).length === 0);
let t= this._cmd.exec_sync('command_shell', {}, 2);
return (t !== undefined && t !== null)? (t.length === 0) : false;
}
/**
* Private method that determine whether or not the 'nordvpn' command tool has the 'Connected' status
* @method
* @return {boolean}
*/
_is_NVPN_connected(){
// return !(COMMAND_LINE_SYNC("nordvpn status | grep -Po ' [cC]onnected'").length===0);
let t= this._cmd.exec_sync('tool_connected_check');
return (t !== undefined && t !== null)? (t.length!==0) : false;
}
/**
* Private method that determine whether or not the 'nordvpn' command tool in connexion transition
* (i.e. connecting or disconnecting from a server)
* @method
* @return {boolean}
*/
_is_in_transition(){
// return (COMMAND_LINE_SYNC("nordvpn status | grep -Po '[cC]onnecting'").length!==0);
let t= this._cmd.exec_sync('tool_transition_check');
return (t !== undefined && t !== null)? (t.length!==0) : false;
}
/**
* Private method that determine whether or not the 'nordvpnd' systemd daemon is availabe to the
* 'nordvpn' command line tool
* @method
* @return {boolean}
*/
_is_daemon_unreachable(){
// return !(COMMAND_LINE_SYNC("nordvpn status | grep -Po 'TransientFailure.*nordvpn.sock'").length===0);
let t= this._cmd.exec_sync('daemon_unreachable_check');
return (t !== undefined && t !== null)? !(t.length===0) : false;
}
/**
* Private method that determine whether or not the user is logged in to use the 'nordvpn' command line tool
*
* [ NordVPN CLI >= 3.12 ] is the login check return a 'login url', said url is stored in private attribute
* '_lastLoginUrl' (null if no link fetched)
* @method
* @return {boolean}
*/