Skip to content

Commit

Permalink
Merge pull request #18 from jotyGill/test
Browse files Browse the repository at this point in the history
fully adopting api.nordvpn.com which updates every minute
  • Loading branch information
jotyGill authored May 19, 2017
2 parents 5bde206 + 1245bef commit acafb01
Showing 1 changed file with 36 additions and 47 deletions.
83 changes: 36 additions & 47 deletions openpyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import platform

__version__ = "openpyn 1.3.0 (slick)"
__version__ = "openpyn 1.3.1 (slick)"

countryDic = {}
with open("/usr/share/openpyn/country-mappings.json", 'r') as countryMappingsFile:
Expand Down Expand Up @@ -42,13 +42,13 @@ def main(
elif list_servers != 'nope': # means "-l" supplied
if list_servers is None: # no arg given with "-l"
if p2p or dedicated or double_vpn or tor_over_vpn or anti_ddos:
displayServers(
listServers(
list_servers="all", p2p=p2p, dedicated=dedicated, double_vpn=double_vpn,
tor_over_vpn=tor_over_vpn, anti_ddos=anti_ddos) # show the special servers in all countries
else:
listAllCountries()
else: # if a country code is supplied give details about that instead.
displayServers(
listServers(
list_servers=list_servers, p2p=p2p, dedicated=dedicated,
double_vpn=double_vpn, tor_over_vpn=tor_over_vpn, anti_ddos=anti_ddos)

Expand All @@ -59,9 +59,9 @@ def main(
elif force_fw_rules:
clearFWRules()

# if only "-c" used then
# if only positional argument used
if country_code is None and server is None:
country_code = country
country_code = country # consider the positional arg e.g "us" same as "-c us"
# if either "-c" or positional arg f.e "au" is present
if country_code:
country_code = country_code.lower()
Expand All @@ -88,6 +88,7 @@ def main(
parser.print_help()


# Using requests, GETs and returns json from a url.
def getJson(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'}
Expand All @@ -105,6 +106,7 @@ def getJson(url):
return JsonResponse


# Gets json data, older approach, only using now with updateCountryCodes()
def getData(country_code=None, countryName=None):
jsonResList = []
if countryName is not None:
Expand All @@ -120,9 +122,9 @@ def getData(country_code=None, countryName=None):
return jsonResList


# Gets json data, from api.nordvpn.com
def getDataFromApi(
country_code, p2p, dedicated, double_vpn, tor_over_vpn, anti_ddos):
# default "all" overright when findBetterServers use "all" when -l
typeFilteredServers = []
typeNCountryFilterServers = []

Expand Down Expand Up @@ -150,14 +152,15 @@ def getDataFromApi(

# print("Total available servers = ", serverCount)

if country_code != "all":
if country_code != "all": # if "-l" had country code with it. e.g "-l au"
for eachServer in typeFilteredServers:
if eachServer["domain"][:2] == country_code.lower():
typeNCountryFilterServers.append(eachServer)
return typeNCountryFilterServers
return typeFilteredServers


# Filters servers based on the speficied criteria.
def findBetterServers(
country_code, max_load, top_servers, udp, p2p, dedicated,
double_vpn, tor_over_vpn, anti_ddos):
Expand All @@ -167,57 +170,38 @@ def findBetterServers(
else:
usedProtocol = "OPENVPN-TCP"

if p2p or dedicated or double_vpn or tor_over_vpn or anti_ddos:
# use api.nordvpn.com
jsonResList = getDataFromApi(
country_code=country_code, p2p=p2p, dedicated=dedicated,
double_vpn=double_vpn, tor_over_vpn=tor_over_vpn, anti_ddos=anti_ddos)
for res in jsonResList:
# when connecting using UDP only append if it supports OpenVPN-UDP
if udp is True and res["features"]["openvpn_udp"] is True:
serverList.append([res["domain"][:res["domain"].find(".")], res["load"]])
# when connecting using TCP only append if it supports OpenVPN-TCP
elif udp is False and res["features"]["openvpn_tcp"] is True:
serverList.append([res["domain"][:res["domain"].find(".")], res["load"]])
# print("TCP SERVESR :", res["feature"], res["feature"]["openvpn_tcp"])

betterServerList = excludeServers(serverList, max_load, top_servers)
if len(betterServerList) < 1: # if no servers under search criteria
print("There are no servers that satisfy your criteria, please broaden your search.")
sys.exit()
# use api.nordvpn.com
jsonResList = getDataFromApi(
country_code=country_code, p2p=p2p, dedicated=dedicated,
double_vpn=double_vpn, tor_over_vpn=tor_over_vpn, anti_ddos=anti_ddos)
for res in jsonResList:
# when connecting using UDP only append if it supports OpenVPN-UDP
if udp is True and res["features"]["openvpn_udp"] is True:
serverList.append([res["domain"][:res["domain"].find(".")], res["load"]])
# when connecting using TCP only append if it supports OpenVPN-TCP
elif udp is False and res["features"]["openvpn_tcp"] is True:
serverList.append([res["domain"][:res["domain"].find(".")], res["load"]])
# print("TCP SERVESR :", res["feature"], res["feature"]["openvpn_tcp"])

betterServerList = excludeServers(serverList, max_load, top_servers)
if len(betterServerList) < 1: # if no servers under search criteria
print("There are no servers that satisfy your criteria, please broaden your search.")
sys.exit()

if p2p or dedicated or double_vpn or tor_over_vpn or anti_ddos:
print("According to NordVPN, Least Busy " + str(len(betterServerList)) + " Servers, In",
country_code.upper(), "With 'Load' less than", max_load, "Which Support",
usedProtocol, ", p2p = ", p2p, ", dedicated =", dedicated, ", double_vpn =", double_vpn,
", tor_over_vpn =", tor_over_vpn, ", anti_ddos =", anti_ddos, "are :\n", betterServerList)

else: # use nordvpn.com/servers
jsonResList = getData(country_code=country_code)
for res in jsonResList:
# only add if the server is online
if res["exists"] is True:
# when connecting using UDP only append if it supports OpenVPN-UDP
if udp is True and res["feature"]["openvpn_udp"] is True:
serverList.append([res["short"], res["load"]])
# print("UDP SERVESR :", res["feature"], res["feature"]["openvpn_udp"])
# when connecting using TCP only append if it supports OpenVPN-TCP
elif udp is False and res["feature"]["openvpn_tcp"] is True:
serverList.append([res["short"], res["load"]])
# print("TCP SERVESR :", res["feature"], res["feature"]["openvpn_tcp"])

betterServerList = excludeServers(serverList, max_load, top_servers)
if len(betterServerList) < 1: # if no servers under search criteria
print("There are no servers that satisfy your criteria, please broaden your search.")
sys.exit()

else:
print("According to NordVPN, Least Busy " + str(len(betterServerList)) + " Servers, In",
country_code.upper(), "With 'Load' less than", max_load,
"Which Support", usedProtocol, "are :", betterServerList)

return betterServerList


# exclude servers over "max_load" and only keep < "top_servers"
# Exclude servers over "max_load" and only keep < "top_servers"
def excludeServers(serverList, max_load, top_servers):
newServersList = []
# sort list by the server load
Expand All @@ -230,6 +214,7 @@ def excludeServers(serverList, max_load, top_servers):
return newServersList


# Pings servers with the speficied no of "ping", returns a sorted list by Ping Avg and Median Deveation
def pingServers(betterServerList, ping):
pingServerList = []
for i in betterServerList:
Expand Down Expand Up @@ -258,6 +243,7 @@ def pingServers(betterServerList, ping):
return pingServerList


# Returns a final server (randomly) from only the best "toppest_servers" (e.g 3) no. of servers.
def chooseBestServer(pingServerList, toppest_servers):
bestServersList = []
bestServersNameList = []
Expand Down Expand Up @@ -292,6 +278,7 @@ def killVpnProcesses():
return


# Clears Firewall rules, applies basic rules.
def clearFWRules():
verifyRootAccess("Root access needed to modify 'iptables' rules")
print("Flushing iptables INPUT and OUTPUT chains AND Applying defualt Rules")
Expand Down Expand Up @@ -320,7 +307,9 @@ def updateOpenpyn():
print("Exception occured while wgetting zip")


def displayServers(list_servers, p2p, dedicated, double_vpn, tor_over_vpn, anti_ddos):
# Lists information abouts servers under the given criteria.
def listServers(list_servers, p2p, dedicated, double_vpn, tor_over_vpn, anti_ddos):
# if list_servers was not a specific country it would be "all"
jsonResList = getDataFromApi(
country_code=list_servers, p2p=p2p, dedicated=dedicated,
double_vpn=double_vpn, tor_over_vpn=tor_over_vpn, anti_ddos=anti_ddos)
Expand Down

0 comments on commit acafb01

Please sign in to comment.