Skip to content

Commit

Permalink
list: download servers info from nordvpn and display it (fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nstinus committed Jun 10, 2017
1 parent b94c779 commit e8f90d5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
19 changes: 14 additions & 5 deletions nordvpn
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,20 @@ done

case $command in
list)
find /etc/openvpn/client/ -type l -name "nordvpn_*${1}*.conf" \
| xargs -L1 basename \
| cut -d _ -f 2 \
| cut -d . -f 1 \
| sort -g
declare -i force_update=0
case "$1" in
-f|--force)
force_update=1
shift
;;
esac
declare -r servers_filename=/tmp/nordvpn_servers.json
if [ $force_update -eq 1 ] || [ $(find $servers_filename -mmin +15 -writable) ]
then
echo "Updating servers list..."
curl --silent -o $servers_filename https://api.nordvpn.com/server
fi
python /etc/openvpn/client/nordvpn/servers.py $servers_filename
;;
ping)
file="/etc/openvpn/client/nordvpn_${1}.conf"
Expand Down
37 changes: 37 additions & 0 deletions servers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python

if __name__ == "__main__":
from argparse import ArgumentParser
import os
import pandas
import json
import sys
import shlex
from subprocess import Popen, PIPE

parser = ArgumentParser()
parser.add_argument("servers_filename")
args = parser.parse_args()

if not os.access(args.servers_filename, os.R_OK):
print >>sys.stderr, "Can't read {}".format(args.servers_filename)
sys.exit(1)

df = pandas.read_json(args.servers_filename)
df["latitude"] = df.location.apply(lambda x: float(x["lat"]))
df["longitude"] = df.location.apply(lambda x: float(x["long"]))
df["name"] = df.domain.apply(lambda x: x.replace(".nordvpn.com", ""))
features = set()
for feature in df.features:
features |= set(feature)
for feature in features:
df[feature.replace("openvpn_", "")] = df.features.apply(lambda x: x[feature])
df.drop(["categories", "domain", "price", "id", "ip_address", "location", "features"], axis=1, inplace=True)
df = df[df.tcp | df.udp]
df = df[['name', 'country', 'flag', 'load', 'search_keywords', 'latitude', 'longitude', 'udp', 'tcp', 'xor_udp', 'xor_tcp', 'ikev2']]

# Cross servers with the installed files
p1 = Popen(shlex.split('find /etc/openvpn/client/ -type l -name "nordvpn_*.conf"'), stdout=PIPE)
installed = set([os.path.splitext(os.path.basename(i).strip())[0].decode().split("_")[1] for i in p1.stdout.readlines()])

print(df[df.name.apply(lambda x: x in installed)].to_string())

0 comments on commit e8f90d5

Please sign in to comment.