-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvpnPort.py
More file actions
757 lines (581 loc) · 25 KB
/
nvpnPort.py
File metadata and controls
757 lines (581 loc) · 25 KB
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
#!/usr/bin/env python3
################################
#### nvpnPort.py ####
#### Version 20260113 ####
################################
import sys
import os
import subprocess
import time
import json
import psutil
import requests
from datetime import datetime
# Not used here, so far. from pubsub import pub
import nvpnPort as nvpnT
import myTheme as skin
global glbl_serviceActive
glbl_serviceActive = False
global glbl_loginStatus
glbl_loginStatus = False
global glbl_connectedStatus
glbl_connectedStatus = False
global nordVPNCommandOptions
nordVPNCommandOptions = [ "account",
"cities",
"connect",
"countries",
"disconnect",
"groups",
"login",
"logout",
"rate",
"set",
"settings",
"status",
"version" ]
jsonPath = os.path.join( os.path.abspath( os.path.dirname(__file__) ) , "jsons")
serverURL = "https://api.nordvpn.com/v1/servers/recommendations?limit=0"
global nordVPNGroupList
global nordVPNChosenGroup
nordVPNGroupList = []
nordVPNChosenGroup = ""
jsonGrpFile = os.path.join( jsonPath, "groups.json")
global nordVPNCountryList
global nordVPNChosenCountry
nordVPNCountryList = []
nordVPNChosenCountry = ""
jsonCntFile = os.path.join( jsonPath, "countries.json")
global nordVPNCityList
global nordVPNChosenCity
global jsonCtyFile
nordVPNCityList = []
nordVPNChosenCity = ""
jsonCtyFile = "" # depends on country
global nordVPNServerList
global nordVPNChosenServer
nordVPNServerList = []
nordVPNChosenServer = ""
# Eventually the serverlist will be taken from the server and not stored as json.
jsonSrvrFile = os.path.join( jsonPath, "servers.json")
global connStatusArr
global connStatus
connStatusArr = []
connStatus = False
jsonSttsFile = os.path.join( jsonPath, "status.json")
global allConsArr
allConsArr = []
allConnsPath = os.path.join( jsonPath, "allConns.json" )
def nordProcesses():
"""
Nordprocesses are:
- commandLine: '/usr/bin/norduserd' processName: 'norduserd'
- commandLine: '/usr/sbin/nordvpnd' processName: 'nordvpnd'
If the process 'nordvpnd' isn't running, we'll have to ask the user to start it with admin rights;
'systemctl start nordvpnd.service' . It, in turn, will activate the socket.
Then the app can handle log-in/out, dis-/connect.
"""
global glbl_serviceActive
global glbl_loginStatus
for p in psutil.process_iter( [ "pid", "name", "cmdline", "status" ] ):
if p.info["name"] == "nordvpnd":
glbl_serviceActive = True
print(f"nordProcesses: found NORDVPND { p.info['status'] } as { p.info['pid'] }-{ p.info['cmdline'] } " )
elif p.info["name"] == "norduserd":
glbl_loginStatus = True
print(f"nordProcesses: found NORDUSERD { p.info['status'] } as { p.info['pid'] }-{ p.info['cmdline'] } " )
# as of 6.0 psutil.process_iter.cache_clear()
return ( glbl_serviceActive, glbl_loginStatus )
def nordPort( cmdArgs:[] ):
# print(f"nordPort: --cmdArgs: { str( cmdArgs ) }")
nordPortResp = []
try:
if len(cmdArgs) > 1:
nordPortResp = subprocess.check_output( cmdArgs, stderr= subprocess.STDOUT ).decode('utf-8')
# subprocess.check_output( cmdArgs ).decode('utf-8')
# print(f"nordPort: --nordPortResp(RAW): { str( nordPortResp ) }")
else:
print(f"nordPort| cmdArgs must have at least 2 items ('nordvpn', 'item')")
nordPortResp = ["cmdArgs must have at least 2 items"]
except Exception as nordPortExcptn:
if nordPortExcptn.returncode == 1:
# So far I only get this when I'm already logged in. ? logout ?
if( cmdArgs[1].lower() == "login"):
nordPortResp = ["You are already logged in."]
print(f"nordPort| --login: has nordPortExcptn:{ nordPortExcptn }")
else:
print(f"nordPort| --cmdArgs: {cmdArgs[1]} exit code non zero. nordPortExcptn:{ nordPortExcptn }")
nordPortResp = [""]
# print(f"nordPort: Exception returncode == 1: { nordPortExcptn }\ncmd: { nordPortExcptn.cmd }\nstderr: { nordPortExcptn.stderr }\nreturncode: { nordPortExcptn.returncode }")
else:
print(f"nordPort: Exception returncode Else: { nordPortExcptn }\ncmd: { nordPortExcptn.cmd }\nstderr: { nordPortExcptn.stderr }\nreturncode: { nordPortExcptn.returncode }")
nordPortResp = [""]
finally:
tabCount = nordPortResp.count('\t')
lineCount = nordPortResp.count('\n')
colonCount = nordPortResp.count(':')
# print(f"nordPort- { cmdArgs[1] } finally: counting: --tabs: { tabCount } --lineEnds: { lineCount } --colons: { colonCount }\n{ nordPortResp } ")
if lineCount > 0:
nordPortRetVal = nordPortResp.splitlines()
else:
nordPortRetVal = nordPortResp
#if nordPortRetVal[ (len(nordPortRetVal) -1) ] in [ None, "", {}, [] ] :
#print(f"last line in nordPortRetVal is being popped (should be empty): { nordPortRetVal.pop() }")
return nordPortRetVal
def commandRouter( item, optVars=[] ):
# Here we translate the item into an array of command-strings
# print(f"commandRouter called with item: { item } and optVars: { str( optVars ) }")
try:
itemSupported = (item in nordVPNCommandOptions)
npResponse = []
cmndArr = [ "nordvpn", item ]
if itemSupported:
if item in [ "cities", "connect", "set", "rate" ]:
# These items may/must have extra arguments
cmndArr += optVars
print(f"commandRouter: --item: { item } --supported: { itemSupported } --cmndArr: { str( cmndArr ) }")
else:
print(f"commandRouter: --item: { item } --supported? { itemSupported } --cmndArr: { str( cmndArr ) }")
# print(f"commandRouter: --item: { item } --supported: { itemSupported } --cmndArr: { str( cmndArr ) }")
npResponse = nordPort( cmndArr )
except Exception as commandRouterErr:
print(f"commandRouter- Exception: { commandRouterErr }")
npResponse = [""]
finally:
# Let the json lib clean the output as well as it can.
# print(f"commandRouter: npResponse returns:\n{ json.loads( json.dumps( npResponse ) ) }" )
return json.loads( json.dumps( npResponse ) )
def getNvpnItem( item, optVars=[] ):
global nordVPNGroupList, nordVPNChosenGroup
global nordVPNCountryList, nordVPNChosenCountry
global nordVPNCityList, nordVPNChosenCity, jsonCtyFile
global connStatusArr, connStatus
termRetVal = []
nvpnTnnlResponse = []
try:
itemSupported = ( item in nordVPNCommandOptions )
#==================================================================================================================================
if item == "account" :
rawAccsResponse = commandRouter( item="account" )
nvpnTnnlResponse = handleResponseTuples( rawAccsResponse )
# print(f"getNvpnItem - at End of --ACCOUNT-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "cities" :
# Can only get citites if the country is known
if len( nordVPNChosenCountry ) > 0:
rawCityResponse = commandRouter( item="cities", optVars=[ nordVPNChosenCountry ] )
nvpnTnnlResponse = cleanCityResponse(rawCityResponse)
# saveJson( getCityJsonFlNm(), nvpnTnnlResponse )
else:
if len(optVars) > 0:
for i, argum in enumerate( optVars ):
print(f"getNvpnItem --CITIES-- optVars[{i}] argum: { argum }")
nvpnTnnlResponse = [ "first select a country" ]
# print(f"getNvpnItem - at End of --CITIES-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "connect" :
print(f"getNvpnItem: at Start of --CONNECT-- \n optvars: { str(optVars) }")
rawConnectResponse = commandRouter( item="connect" , optVars=optVars )
nvpnTnnlResponse = handleResponseStrings( rawConnectResponse )
print(f"getNvpnItem: at End of --CONNECT-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "countries":
if hasJson("countries"):
cntJsonRetVal = loadJson( jsonCntFile )
# print(f"getNvpnItem: got countries from json: { str( cntJsonRetVal ) }" )
nvpnTnnlResponse = cntJsonRetVal
else:
rawCntrsResponse = commandRouter( item="countries" )
nvpnTnnlResponse = handleResponseArrays( rawCntrsResponse )
nvpnTnnlResponse.sort()
saveJson( jsonCntFile, nvpnTnnlResponse )
# print(f"getNvpnItem - at End of --COUNTRIES-- nvpnTnnlResponse : { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "disconnect" :
rawDisCnnctResponse = commandRouter( item="disconnect" )
nvpnTnnlResponse = handleResponseStrings( rawDisCnnctResponse )
# print(f"getNvpnItem - at End of --DISCONNECT-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "groups" :
if hasJson("groups"):
grpJsonRetVal = loadJson( jsonGrpFile )
# print(f"getNvpnItem: got groups from json: { str( grpJsonRetVal ) }" )
nvpnTnnlResponse = grpJsonRetVal
else: # hasJson("groups") = False
rawGrpResponse = commandRouter( item="groups" )
# treat as handleResponseArrays
nvpnTnnlResponse = handleResponseArrays( rawGrpResponse )
nvpnTnnlResponse.sort()
saveJson( jsonGrpFile,nvpnTnnlResponse )
# print(f"getNvpnItem - at End of --GROUPS-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "login" :
rawLginResponse = commandRouter( item="login" )
# Either the user is already logged in. The response will be "You are already logged in." or ...
# A html link is returned to be opened in a browser window
# e.g.: "Continue in the browser: https://api.nordvpn.com/v1/users/oauth/login-redirect?attempt=2e7e88d5-1ee5-4ac4-9a47-3ded0f0307a2"
# embedded html because of authentication occurs outside this app.
nvpnTnnlResponse = rawLginResponse
# "https://api.nordvpn.com/v1/users/oauth/login-redirect?"#
# handleResponseStrings( rawLginResponse )
print(f"getNvpnItem - at End of --LOGIN-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "logout" :
rawLgoutResponse = commandRouter( item="logout" )
nvpnTnnlResponse = rawLgoutResponse
print(f"getNvpnItem - at End of --LOGOUT-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "rate" :
print(f"getNvpnItem: at Start of --RATING-- \n optvars: { str(optVars) }")
rawRatingResponse = commandRouter( item="rate", optVars=optVars )
nvpnTnnlResponse = handleResponseStrings( rawRatingResponse )
print(f"getNvpnItem - at End of --RATING-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "set" :
# The output after setting a setting is 1 line of text. treated as splittable string
# To activate a setting we can use: enable, true, True, 1, on, On
# To de-activate a setting we can use: disable, false, False, 0, off, Off
# Using as default: nordvpn set notify enable
if len(optVars) == 0:
# print(f"getNvpnItem: --SET--START: No optvars passed. Using 'notify' 'enable' ")
configItem = "notify"
configValue = "True"
elif len(optVars) == 1:
# print(f"getNvpnItem: --SET--START: Only 1 optvars passed. Assuming the var is 'configItem': { optVars[0] } by default it will be activated" )
configItem = optVars[0]
configValue = "True"
elif len(optVars) == 2:
# print(f"getNvpnItem: --SET--START: 2 optvars passed. Assuming item[0]='configItem': { optVars[0] } and item[1]='configValue': { optVars[1] }")
configItem = optVars[0]
configValue = str(optVars[1])
else:
print(f"getNvpnItem: --SET--START: ELSE ")
for i, argum in enumerate( optVars ):
print(f"getNvpnItem: --SET-- loop optVars: {i} argum: { argum }" )
rawSttngsSetResponse = commandRouter( item="set", optVars=[ configItem, configValue ] )
nvpnTnnlResponse = handleResponseStrings( rawSttngsSetResponse )
# print(f"getNvpnItem - at End of --SET-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "settings" :
rawSttngsResponse = commandRouter( item="settings" )
nvpnTnnlResponse = handleResponseTuples( rawSttngsResponse )
# print(f"getNvpnItem - at End of --SETTINGS-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
elif item == "status":
# print(f"getNvpnItem - at START of --STATUS-- getConnStatus: { getConnStatus() }")
rawSttsResponse = commandRouter( item="status" )
nvpnTnnlResponse = handleResponseTuples( rawSttsResponse )
saveJson( jsonSttsFile, nvpnTnnlResponse )
connStatusArr = nvpnTnnlResponse
connStatus = getConnStatus()
# print(f"getNvpnItem - at End of --STATUS-- connStatusArr : { str( connStatusArr ) }")
# print(f"getNvpnItem - at End of --STATUS-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
# print(f"getNvpnItem - at END of --STATUS-- getConnStatus : { connStatus }")
#==================================================================================================================================
elif item == "version" :
rawVrsResponse = commandRouter( item="version" )
nvpnTnnlResponse = handleResponseStrings( rawVrsResponse )
# print(f"getNvpnItem - at End of --VERSION-- nvpnTnnlResponse: { str( nvpnTnnlResponse ) }")
#==================================================================================================================================
else:
print(f"getNvpnItem: item == Else --unknown item -> { item }")
nvpnTnnlResponse = []
#==================================================================================================================================
#==================================================================================================================================
except Exception as e:
print(f"getNvpnItem Exception= {e}")
finally:
# print(f"getNvpnItem- finally: { str(nvpnTnnlResponse) }")
return nvpnTnnlResponse
def getConnStatus():
global connStatusArr
try:
if connStatusArr[0][1].lower():
if connStatusArr[0][1].lower() == "connected":
return True
except Exception as e:
connStatus = False
return False
def getSetting( setting:str ):
allSettings = getNvpnItem("settings")
for sttng, val in allSettings:
if sttng.upper() == setting.upper():
return val
def getAllItems():
"""
Only used in testing commandrouter results
"""
nordVPNCountryList = []
for nvpnCmd in nordVPNCommandOptions:
if nvpnCmd == "logout":
pass
else:
print(f"getNvpnItem('{nvpnCmd}')\n { json.dumps( getNvpnItem( nvpnCmd ), indent=4 ) } ")
print(f"\n\n")
############################################################################
# For treating account, settings, status, version
def handleResponseTuples( nvpnResponse ):
# print(f"handleResponseTuples nvpnResponse: { str(nvpnResponse) }")
treatedArray = []
try:
for i, entry in enumerate( nvpnResponse ):
key, value = entry.split(":")
if ( value.strip() == "enabled" ):
val = True
elif ( value.strip() == "disabled" ):
val = False
else:
val = value.strip()
treatedArray.append( ( key.replace( " ", "_" ).upper() , val ) )
return treatedArray
except Exception as myErr1:
print(f"handleResponseTuples Exception: { myErr1 }")
return []
# For treating groups, countries, cities
def handleResponseArrays( nvpnResponse ):
# print(f"handleResponseArrays nvpnResponse: { str(nvpnResponse) }")
treatedArray = []
try:
for i, entry in enumerate( nvpnResponse ):
tmpSplitEntry = [ value for value in entry.replace( '\t', ',' ).split(',') if value not in [ None, "", "\t", {}, [] ] ]
treatedArray += tmpSplitEntry
return treatedArray
except Exception as myErr2:
print(f"handleResponseArrays Exception: { myErr2 }")
return []
# For treating set, version, ...
def handleResponseStrings( nvpnResponse ):
return [ datetime.now().isoformat(' ')[0:-7] , " ".join( nvpnResponse ) ]
def cleanCityResponse( nvpnResponse ):
global nordVPNCityList
newCleanArray = []
if type( nvpnResponse ) == type([]):
# print(f"cleanCityResponse: type [] = { nvpnResponse }")
# print(f"cleanCityResponse: len = { len( nvpnResponse ) }")
for i,ln in enumerate(nvpnResponse):
# print(f"newDirtyArray {i} = { ln }")
newDirtyArray = ln.split(" ")
# print(f"newDirtyArray[{i}] = {newDirtyArray}")
for d in newDirtyArray:
if len(d) > 0:
newCleanArray.append(d)
elif( type(nvpnResponse) == type("") ):
print(f"cleanCityResponse: type '' = { nvpnResponse }")
newDirtyArray = str(nvpnResponse).split(" ")
for d in newDirtyArray:
if len(d) > 0:
newCleanArray.append(d)
else:
print(f"cleanCityResponse: type unknown = { type( nvpnResponse ) }")
# print(f"\n\ncleanCityResponse: after clean = { json.dumps( newCleanArray, indent = 4 ) }\n\n")
#nordVPNCityList = newCleanArray
return newCleanArray
############################################################################
# GROUP
def groupListManager( action:str ):
global nordVPNGroupList
chosenGroupManager( "clear" )
if action == "get":
return nordVPNGroupList
elif action == "set":
nordVPNGroupList = getNvpnItem( "groups" )
elif action == "clear":
nordVPNGroupList = []
else:
print(f"groupListManager action==ELSE ")
def chosenGroupManager( action:str, newVal="" ):
global nordVPNChosenGroup
if action == "get":
return nordVPNChosenGroup
elif action == "set":
nordVPNChosenGroup = newVal
elif action == "clear":
nordVPNChosenGroup = ""
else:
print(f"chosenGroupManager action==ELSE ")
############################################################################
# COUNTRIES
def countryListManager( action:str , filterStr="" ):
global nordVPNCountryList
chosenCountryManager( "clear" )
if action == "get":
return nordVPNCountryList
elif action == "set":
nordVPNCountryList = getNvpnItem("countries")
elif action == "clear":
nordVPNCountryList = []
elif action == "filter":
countryArray = nordVPNCountryList
countryArrayFiltered = []
if len(filterStr) > 0:
for i, cntry in enumerate( countryArray ):
if filterStr.lower() in cntry.lower():
countryArrayFiltered.append( cntry )
else:
countryArrayFiltered = countryArray
return countryArrayFiltered
else:
print(f"countryListManager action==ELSE ")
def chosenCountryManager( action:str, newVal="" ):
print(f"chosenCountryManager { newVal }")
global nordVPNChosenCountry
if action == "get":
return nordVPNChosenCountry
elif action == "set":
nordVPNChosenCountry = newVal
cityListManager("set")
elif action == "clear":
nordVPNChosenCountry = ""
cityListManager("clear")
else:
print(f"chosenCountryManager action==ELSE ")
############################################################################
# CITIES
def cityListManager( action:str ):
global nordVPNCityList
chosenCityManager("clear")
if action == "get" :
return nordVPNCityList
elif action == "set" :
nordVPNCityList = getNvpnItem("cities")
elif action == "clear" :
nordVPNCityList = []
else:
print(f"cityListManager action == ELSE ")
def chosenCityManager( action:str, newVal="" ):
global nordVPNChosenCity
if action == "get":
return nordVPNChosenCity
elif action == "set":
nordVPNChosenCity = newVal
elif action == "clear":
nordVPNChosenCity = ""
else:
print(f"chosenCityManager action==ELSE ")
def getCityJsonFlNm():
return os.path.join( jsonPath, "cities", f"{ nordVPNChosenCountry }.json" )
############################################################################
# SERVERS -framework is present but not implemented
def serverListManager( action:str ):
global nordVPNServerList
chosenServerManager("clear")
if action == "get" :
return nordVPNServerList
elif action == "set" :
srvrRaw = requests.get( serverURL )
srvrList = srvrRaw.json()
print(f"serverListManager-SET: { srvrList } ")
# just to check the format do only once
saveJson( jsonSrvrFile, srvrList )
nordVPNServerList = srvrList
elif action == "clear" :
nordVPNServerList = []
else:
print(f"cityListManager action == ELSE ")
def chosenServerManager( action:str, newVal="" ):
global nordVPNChosenServer
if action == "get":
return nordVPNChosenServer
elif action == "set":
nordVPNChosenServer = newVal
elif action == "clear":
nordVPNChosenServer = ""
else:
print(f"chosenServerManager action==ELSE ")
############################################################################
# JSON
def hasJson(item):
content = []
if item in [ "groups", "countries", "cities", "status" ]:
if item == "groups" :
content = loadJson( jsonGrpFile )
elif item == "countries" :
content = loadJson( jsonCntFile )
elif item == "cities" :
content = loadJson( getCityJsonFlNm() )
elif item == "status" :
content = loadJson( jsonSttsFile )
else:
print(f"hasJson ELSE item: {item} ")
return len(content) > 0
def loadJson( fileName ):
try:
if os.path.exists( fileName ):
print(f"loadJson fileName {fileName} exists Yeay!")
fl = open( fileName )
content = json.load( fl )
fl.close()
else:
content = []
except ValueError as e:
# When the file is empty
content = []
finally:
# print(f"loadJson { fileName } returns { json.dumps( content, indent=2 ) } ")
return content
def saveJson( fileName, data ):
#print(f"saving Json to { fileName }")
fl = open( fileName, "w+" )
fl.write( json.dumps( data, indent = 4 ) )
fl.close()
############################################################################
# Connections HIS
def loadAllConns():
global allConsArr
allConsArr = loadJson( allConnsPath )
print(f"loadAllConns. allConsArr has { len( allConsArr ) } items.")
def addToConns(newCon):
global allConsArr
if newCon in allConsArr:
allConsArr.remove(newCon)
allConsArr.insert( 0, newCon )
if len(allConsArr) < 3:
saveJson( allConnsPath, allConsArr )
else:
saveJson( allConnsPath, allConsArr[0:3] )
############################################################################
# App-Levels
def appLevels(step=0):
scrptPth = os.path.abspath( os.path.dirname(__file__) )
# print(f"scrptPth: { scrptPth }")
return scrptPth
"""
--- > INFO
x account Shows account information
x settings Shows current settings
x status Shows connection status
x version Shows the app version
--- > CONNECTIONS
x login Logs you in
x logout Logs you out
x connect, c Connects you to VPN
x disconnect, d Disconnects you from VPN
x cities Shows a list of cities where servers are available
x countries Shows a list of countries where servers are available
x groups Shows a list of available server groups
x rate Rates your last connection quality ( 1-5 )
---> SERVICES
x socket-daemon Shows if the necessary system service "nordvpnd" is running and starts it (admin prompt)
x user Shows if the necessary system service "norduserd" is running
--- > CONFIGURATION
x set, s Sets a configuration option
allowlist, whitelist Adds or removes an option from the allowlist
meshnet, mesh Meshnet is a way to safely access other devices, no matter where in the world they are. Once set up,
Meshnet functions just like a secure local area network (LAN) — it connects devices directly. It also
allows securely sending files to other devices.
Use the "nordvpn set meshnet on" command to enable Meshnet.
Learn more: https://meshnet.nordvpn.com/
fileshare Transfer files of any size between Meshnet peers securely and privately
--- > Not included:
register Registers a new user account
# Command-line examples:
nordvpn login — log in.
nordvpn connect or nordvpn c — connect to VPN. To connect to specific servers, use nordvpn connect <country_code server_number> (eg. nordvpn connect uk715).
nordvpn disconnect or nordvpn d — disconnect from VPN.
nordvpn c double_vpn — connect to the closest Double VPN server.
nordvpn connect --group double_vpn <country_code> — connect to a specific country using Double VPN servers.
nordvpn connect --group p2p <country_code> — Connect to a specific country using P2P servers.
nordvpn connect P2P — connect to a P2P server.
nordvpn connect The_Americas — connect to servers located in the Americas.
nordvpn connect Dedicated_IP — connect to a Dedicated IP server.
"""