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.

[pastacode lang=”python” manual=”%23!%2Fusr%2Fbin%2Fpython3%0A%0Aimport%20os%0Aimport%20shutil%0Aimport%20re%0Aimport%20subprocess%0Aimport%20datetime%0Aimport%20apt%0Aimport%20collections%0A%0AFNULL%20%3D%20open(os.devnull%2C%20’w’)%0Anordcurrentzip%20%3D%20’https%3A%2F%2Fnordvpn.com%2Fapi%2Ffiles%2Fzip’%0Acurdate%20%3D%20(datetime.datetime.now().strftime(%22%25m%25d%25y.%25M%25S%22))%0Ahomedir%20%3D%20’%2Ftmp%2Fnordcheck-‘%20%2B%20curdate%0Aconfigzip%20%3D%20homedir%20%2B%20’%2Fconfig.zip’%0A%0Anorddict%20%3D%20%7B%0A%20%20%20%20’filename’%20%3A%20%5B’1.2.3.4’%2C’10.000’%5D%0A%20%20%20%20%7D%0A%0Awindict%20%3D%20%7B%0A%20%20%20%20’1st’%20%3A%20%5B’name’%2C%20’10.000’%5D%2C%0A%20%20%20%20’2nd’%20%3A%20%5B’name’%2C%20’11.000’%5D%2C%0A%20%20%20%20’3rd’%20%3A%20%5B’name’%2C%20’12.000’%5D%0A%20%20%20%20%7D%0A%0Adef%20cleanup()%3A%0A%20%20%20%20global%20homedir%0A%20%20%20%20shutil.rmtree(homedir)%0A%0Adef%20errout%20(problem)%3A%0A%20%20%20%20print%20(problem)%0A%20%20%20%20cleanup()%0A%20%20%20%20exit(1)%0A%0Adef%20makehome%20()%3A%0A%20%20%20%20global%20homedir%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20os.mkdir(homedir)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20errout(%22unable%20to%20make%20dir%22)%0A%0Adef%20getnordzip()%3A%0A%20%20%20%20global%20nordcurrentzip%0A%20%20%20%20cache%20%3D%20apt.Cache()%0A%20%20%20%20cache.open()%0A%20%20%20%20if%20not%20cache%5B%22wget%22%5D.is_installed%3A%0A%20%20%20%20%20%20%20%20errout(%22wget%20not%20installed%2C%20need%20to%20’apt-get%20install%20wget’%20-%20exiting%22)%0A%0A%20%20%20%20print%20(%22Downloading%20current%20.ovpn%20archive..%22)%0A%20%20%20%20wgetresult%20%3D%20subprocess.call%20(%5B’wget’%2C%20nordcurrentzip%2C%20′-O’%2C%20configzip%5D%2C%20stdout%3DFNULL%2C%20stderr%3Dsubprocess.STDOUT)%0A%20%20%20%20if%20wgetresult%20!%3D%200%3A%0A%20%20%20%20%20%20%20%20errout(%22Unable%20to%20download%20%22%20%2B%20configzip%20%2B%20%22%2C%20exiting%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print%20(%22Decompressing%20%22%20%2B%20configzip%20%2B%20%22…%22)%0A%20%20%20%20%20%20%20%20unzipresult%20%3D%20subprocess.call%20(%5B’unzip’%2C%20configzip%2C%20′-d’%2C%20homedir%5D%2C%20stdout%3DFNULL%2C%20stderr%3Dsubprocess.STDOUT)%0A%20%20%20%20%20%20%20%20if%20unzipresult%20!%3D%200%20and%20unzipresult%20!%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20errout(%22Can’t%20unzip%20%22%20%2B%20configzip%20%2B%20%22%2C%20exiting%22)%0A%0Adef%20loaddict()%3A%0A%20%20%20%20global%20norddict%2C%20homedir%0A%0A%20%20%20%20for%20parsefile%20in%20os.listdir(homedir)%3A%0A%20%20%20%20%20%20%20%20if%20parsefile.endswith(%22tcp443.ovpn%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20open(homedir%20%2B%20%22%2F%22%20%2B%20parsefile)%20as%20ovpnfile%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20line%20in%20ovpnfile%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20%22remote%20%22%20in%20line%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%20%3D%20line.split()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ipaddr%20%3D%20line%5B1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20norddict%5Bparsefile%5D%20%3D%20%5Bipaddr%2C”%5D%0A%0Adef%20getlatency()%3A%0A%20%20%20%20global%20norddict%0A%20%20%20%20global%20windict%0A%20%20%20%20bestscore%20%3D%201000.000%0A%0A%20%20%20%20print%20(%22Testing%3A%20%22)%0A%20%20%20%20for%20fname%2C%20attrib%20in%20norddict.items()%3A%0A%20%20%20%20%20%20%20%20print%20(fname%20%2B%20%22%20(%22%20%2B%20attrib%5B0%5D%20%2B%20%22)%3A%20%22%2C%20end%3D”)%0A%20%20%20%20%20%20%20%20response%20%3D%20subprocess.Popen%20(%5B’ping’%2C%20′-nq’%2C%20′-c%202’%2C%20’-W%201’%2C%20attrib%5B0%5D%5D%2C%20stdout%3Dsubprocess.PIPE%2C%20stderr%3Dsubprocess.PIPE)%0A%20%20%20%20%20%20%20%20out%2C%20error%20%3D%20response.communicate()%0A%20%20%20%20%20%20%20%20if%20%22100%25%20packet%20loss%2C%22%20in%20str(out)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20norddict%5Bfname%5D%20%3D%20%5Battrib%5B0%5D%2Cint(1000.000)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20(%221000.00%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20output%20%3D%20str(out).split(‘%2F’)%0A%20%20%20%20%20%20%20%20%20%20%20%20norddict%5Bfname%5D%20%3D%20%5Battrib%5B0%5D%2Coutput%5B4%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20(output%5B4%5D)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20float(output%5B4%5D)%20%3C%20bestscore%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bestscore%20%3D%20float(output%5B4%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20windict%5B’3rd’%5D%20%3D%20windict%5B’2nd’%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20windict%5B’2nd’%5D%20%3D%20windict%5B’1st’%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20windict%5B’1st’%5D%20%3D%20%5B%5Bfname%5D%2Coutput%5B4%5D%5D%0A%0Adef%20showresults()%3A%0A%20%20%20%20global%20norddict%0A%20%20%20%20global%20windict%0A%0A%20%20%20%20ordereddict%20%3D%20collections.OrderedDict(sorted(windict.items()))%0A%20%20%20%20print%20(%22%22)%0A%20%20%20%20for%20fname%2C%20attrib%20in%20ordereddict.items()%3A%0A%20%20%20%20%20%20%20%20print%20(str(attrib%5B0%5D)%20%2B%20%22%3A%20%22%20%2B%20str(attrib%5B1%5D))%0A%0A%0Amakehome()%0Agetnordzip()%0Aloaddict()%0Agetlatency()%0Ashowresults()%0A%0Aprint%20(‘original%20archive%20located%20in%20’%20%2B%20str(homedir))%0A%23cleanup()” message=”nordcheck.py” highlight=”” provider=”manual”/]
Leave A Reply