It is impossible that the whisper of a faction should prevail against the voice of a nation.”
~Lord John Russell

nordcheck.py

Google+ Pinterest LinkedIn Tumblr +

This one I’m pretty proud of and use quite often.  NordVPN is a strong VPN provider that has thousands of servers all around the world that are OpenVPN compatible.  Windows. Mac and smartphone users have it easy as they have built-in apps to find the nearest endpoint, but Linux users are on their own.

This multithreaded program will automatically download the latest list of endpoints and find the one nearest you.

nordcheck.py
#!/usr/bin/python3

import os
import shutil
import re
import subprocess
import datetime
import apt
import collections

FNULL = open(os.devnull, 'w')
nordcurrentzip = 'https://nordvpn.com/api/files/zip'
curdate = (datetime.datetime.now().strftime("%m%d%y.%M%S"))
homedir = '/tmp/nordcheck-' + curdate
configzip = homedir + '/config.zip'

norddict = {
    'filename' : ['1.2.3.4','10.000']
    }

windict = {
    '1st' : ['name', '10.000'],
    '2nd' : ['name', '11.000'],
    '3rd' : ['name', '12.000']
    }

def cleanup():
    global homedir
    shutil.rmtree(homedir)

def errout (problem):
    print (problem)
    cleanup()
    exit(1)

def makehome ():
    global homedir
    try:
        os.mkdir(homedir)
    except:
        errout("unable to make dir")

def getnordzip():
    global nordcurrentzip
    cache = apt.Cache()
    cache.open()
    if not cache["wget"].is_installed:
        errout("wget not installed, need to 'apt-get install wget' - exiting")

    print ("Downloading current .ovpn archive..")
    wgetresult = subprocess.call (['wget', nordcurrentzip, '-O', configzip], stdout=FNULL, stderr=subprocess.STDOUT)
    if wgetresult != 0:
        errout("Unable to download " + configzip + ", exiting")
    else:
        print ("Decompressing " + configzip + "...")
        unzipresult = subprocess.call (['unzip', configzip, '-d', homedir], stdout=FNULL, stderr=subprocess.STDOUT)
        if unzipresult != 0 and unzipresult != 2:
            errout("Can't unzip " + configzip + ", exiting")

def loaddict():
    global norddict, homedir

    for parsefile in os.listdir(homedir):
        if parsefile.endswith("tcp443.ovpn"):
            with open(homedir + "/" + parsefile) as ovpnfile:
                for line in ovpnfile:
                    if "remote " in line:
                        line = line.split()
                        ipaddr = line[1]
                        norddict[parsefile]= [ipaddr,'']

def getlatency():
    global norddict
    global windict
    bestscore = 1000.000

    print ("Testing: ")
    for fname, attrib in norddict.items():
        print (fname + " (" + attrib[0] + "): ", end='')
        response = subprocess.Popen (['ping', '-nq', '-c 2', '-W 1', attrib[0]], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, error = response.communicate()
        if "100% packet loss," in str(out):
            norddict[fname]= [attrib[0],int(1000.000)]
            print ("1000.00")
        else:
            output = str(out).split('/')
            norddict[fname]= [attrib[0],output[4]]
            print (output[4])

            if float(output[4]) < bestscore:
                bestscore = float(output[4])
                windict['3rd'] = windict['2nd']
                windict['2nd'] = windict['1st']
                windict['1st'] = [[fname],output[4]]

def showresults():
    global norddict
    global windict

    ordereddict = collections.OrderedDict(sorted(windict.items()))
    print ("")
    for fname, attrib in ordereddict.items():
        print (str(attrib[0]) + ": " + str(attrib[1]))


makehome()
getnordzip()
loaddict()
getlatency()
showresults()

print ('original archive located in ' + str(homedir))
#cleanup()
Share.

Leave A Reply

Secured By miniOrange