Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from Chadsr/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
chadsr authored Oct 22, 2017
2 parents b771b3d + c04c166 commit 7e16e02
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ More documentation will be available when this tool is out of Alpha releases.
### Debian/Ubuntu

```
sudo apt update && sudo apt install openvpn network-manager network-manager-gnome network-manager-openvpn-gnome
sudo apt update && sudo apt install network-manager openvpn network-manager-openvpn-gnome
```

### Arch
Expand Down Expand Up @@ -60,7 +60,8 @@ sudo nordnm -uska nl normal tcp

```
usage: nordnm [-h] [-u] [-s] [-a [COUNTRY_CODE] [VPN_CATEGORY] [PROTOCOL]]
[-k] [-p] [--categories] [--credentials] [--settings]
[-k] [-p] [--countries] [--categories] [--credentials]
[--settings]
optional arguments:
-h, --help show this help message and exit
Expand All @@ -76,6 +77,7 @@ optional arguments:
disconnects
-p, --purge Remove all active connections, auto-connect and kill-
switch (if configured)
--countries Display a list of the available countries
--categories Display a list of the available VPN categories
--credentials Change the existing saved credentials
--settings Change the existing saved settings
Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Debian/Ubuntu

::

sudo apt update && sudo apt install openvpn network-manager network-manager-gnome network-manager-openvpn-gnome
sudo apt update && sudo apt install network-manager openvpn network-manager-openvpn-gnome

Arch
~~~~
Expand Down Expand Up @@ -88,7 +88,8 @@ For example:
::

usage: nordnm [-h] [-u] [-s] [-a [COUNTRY_CODE] [VPN_CATEGORY] [PROTOCOL]]
[-k] [-p] [--categories] [--credentials] [--settings]
[-k] [-p] [--countries] [--categories] [--credentials]
[--settings]

optional arguments:
-h, --help show this help message and exit
Expand All @@ -104,6 +105,7 @@ For example:
disconnects
-p, --purge Remove all active connections, auto-connect and kill-
switch (if configured)
--countries Display a list of the available countries
--categories Display a list of the available VPN categories
--credentials Change the existing saved credentials
--settings Change the existing saved settings
Expand Down
2 changes: 1 addition & 1 deletion nordnm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__package__ = "nordnm"
__version__ = "0.0a11"
__version__ = "0.0a12"
__license__ = "GNU General Public License v3 or later (GPLv3+)"
22 changes: 15 additions & 7 deletions nordnm/nordapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
}


def get_server_list(sort_by_load=False):
def get_server_list(sort_by_load=False, sort_by_country=False):
try:
resp = requests.get(API_ADDR + '/server', timeout=TIMEOUT)
server_list = resp.json()

if sort_by_load:
return sorted(server_list, key=itemgetter('load'))
if resp.status_code == requests.codes.ok:
server_list = resp.json()

if sort_by_load:
return sorted(server_list, key=itemgetter('load'))
elif sort_by_country:
return sorted(server_list, key=itemgetter('flag'))
else:
return server_list
else:
return server_list
return None
except Exception as ex:
return None

Expand All @@ -45,6 +50,9 @@ def get_nameservers():
def get_configs():
try:
resp = requests.get(API_ADDR + '/files/zipv2', timeout=TIMEOUT)
return resp.content
if resp.status_code == requests.codes.ok:
return resp.content
else:
return None
except Exception as ex:
return None
38 changes: 31 additions & 7 deletions nordnm/nordnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self):
parser.add_argument('-a', '--auto-connect', nargs=3, metavar=('[COUNTRY_CODE]', '[VPN_CATEGORY]', '[PROTOCOL]'), help='Configure NetworkManager to auto-connect to the chosen server type. Takes country code, category and protocol')
parser.add_argument('-k', '--kill-switch', help='Sets a network kill-switch, to disable the active network interface when an active VPN connection disconnects', action='store_true')
parser.add_argument('-p', '--purge', help='Remove all active connections, auto-connect and kill-switch (if configured)', action='store_true')
parser.add_argument('--countries', help='Display a list of the available countries', action='store_true')
parser.add_argument('--categories', help='Display a list of the available VPN categories', action='store_true')
parser.add_argument('--credentials', help='Change the existing saved credentials', action='store_true')
parser.add_argument('--settings', help='Change the existing saved settings', action='store_true')
Expand All @@ -48,22 +49,28 @@ def __init__(self):
except:
sys.exit(1)

if (args.sync or args.auto_connect or args.kill_switch) and args.purge:
print("Error: The purge argument can not be used with sync, auto-connect or kill-switch")
self.logger = logging.getLogger(__name__)

arg_count = len(sys.argv)

if args.purge and arg_count > 2:
print("Error: --purge should be used on its own.")
sys.exit(1)
elif args.categories and arg_count == 2:
self.print_categories()
elif args.countries and arg_count == 2:
self.print_countries()
elif args.credentials or args.settings or args.update or args.sync or args.purge or args.auto_connect or args.kill_switch:
self.print_splash()

self.run(args.credentials, args.settings, args.update, args.sync, args.purge, args.auto_connect, args.kill_switch)
elif args.categories:
self.print_categories()
else:
parser.print_help()

def print_splash(self):
version_string = __version__

latest_version = utils.get__pypi_package_version(__package__)
latest_version = utils.get_pypi_package_version(__package__)
if latest_version and latest_version != version_string: # There's a new version on PyPi
version_string = version_string + " (v" + latest_version + " available!)"
else:
Expand All @@ -80,9 +87,26 @@ def print_categories(self):
for long_name, short_name in nordapi.VPN_CATEGORIES.items():
print("%-9s (%s)" % (short_name, long_name))

def setup(self):
self.logger = logging.getLogger(__name__)
def print_countries(self):
servers = nordapi.get_server_list(sort_by_country=True)
if servers:
format_string = "| %-14s | %-4s |"
countries = []

print("\n Note: You must use the country code, NOT the country name in this tool.\n")
print(format_string % ("NAME", "CODE"))
print("|----------------+------|")

for server in servers:
country_code = server['flag']
if country_code not in countries:
countries.append(country_code)
country_name = server['country']
print(format_string % (country_name, country_code))
else:
self.logger.error("Could not get available countries from the NordVPN API.")

def setup(self):
self.create_directories()

self.settings = SettingsHandler(paths.SETTINGS)
Expand Down
9 changes: 4 additions & 5 deletions nordnm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import logging
import getpass
import requests
import json

logger = logging.getLogger(__name__)


def get__pypi_package_version(package_name):
req = requests.get("https://pypi.python.org/pypi/" + package_name + "/json")
def get_pypi_package_version(package_name):
resp = requests.get("https://pypi.python.org/pypi/" + package_name + "/json")

if req.status_code == requests.codes.ok:
package = json.loads(req.text.encode(req.encoding))
if resp.status_code == requests.codes.ok:
package = resp.json()

if 'version' in package['info']:
return package['info']['version']
Expand Down

0 comments on commit 7e16e02

Please sign in to comment.