Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@genbtc
Created March 19, 2021 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genbtc/89c63a7e66ce71a259ceeef9162f7475 to your computer and use it in GitHub Desktop.
Save genbtc/89c63a7e66ce71a259ceeef9162f7475 to your computer and use it in GitHub Desktop.
Mullvad-VPN Wireguard servers,Providers, cities, countries AND ping Test for 1 random = SORTED
import json
import os
import sys
import operator
from icmplib import ping, multiping
import random
relayFile = open('mullvad-relays-3-17-21.json','r')
allrelays=json.load(relayFile)
countries=[]
cities=[]
countriesNames=[]
citiesNames=[]
servers=[]
providers={}
for r in allrelays["countries"]:
#countries.append(r["name"]);
#print (r["name"]);
for c in r["cities"]:
#cities.append(c["name"])
#print ("-",c["name"]);
for q in c["relays"]:
if (q["include_in_country"] and q["active"] == True):
if ("wireguard" in q["hostname"]):
if (cnthead==""):
cnthead=r["name"];
countriesNames.append(r["name"]);
countries.append(r);
print(cnthead)
if (cityhead==""):
cityhead="- "+c["name"];
citiesNames.append(c["name"])
cities.append(c)
print(cityhead)
servers.append(q);
if (q["provider"] not in providers):
providers[q["provider"]] = 1
else:
providers[q["provider"]]+=1;
print("--",q["location"]["city_code"]+"-"+q["hostname"],q["ipv4_addr_in"],q["provider"],("(*Owned*)" if q["owned"] else ".notOwned."))
#q["location"]["country_code"],)
cityhead=""
cnthead=""
print()
print(len(servers),"Servers");
print(len(countries),"Countries");
print(len(cities),"Cities");
print()
print(len(providers),"Providers");
print("-"*16)
#for p in providers:
# print (p,"=",providers[p]);
sortedprovlist = sorted(providers.items(),key=operator.itemgetter(1),reverse=True)
for p in sortedprovlist:
print('{:<12} {:>3}'.format(p[0],p[1]))
#print('{:>3} {:<12}'.format(p[1],p[0]))
print()
cityips=[]
for c in cities:
found=0
#print(len(c["relays"]),"-",c["name"],",",c["relays"][0]["location"]["country"]);
print('{:>2} - {:<30}'.format(len(c["relays"]),c["name"]+" @ "+c["relays"][0]["location"]["country"]));
while (found==0):
r = c["relays"][random.randint(0,len(c["relays"])-1)];
if ("wireguard" in r["hostname"]):
cityips.append(r["ipv4_addr_in"]);
found=1;
#TEST# if (len(cityips)>5): break;
#print(cityips)
print()
NPINGS=2
pingslist=[]
while (len(cityips) > 0):
print('Starting Ping..........')
#print('city @ country, ip address, min_rtt, avg_rtt, max_rtt --- pkt_sent, pkt_rcvd, pkt_loss, is_alive')
pingreturn = multiping(cityips, count=NPINGS, interval=0.34, timeout=1, privileged=False)
for ip in pingreturn:
if (ip.is_alive and ip.packet_loss == 0.0):
c = cities[len(cities)-len(cityips)]
for i in range(len(c["relays"])):
r = c["relays"][i];
ip2 = r["ipv4_addr_in"];
if (ip2 == ip.address):
###print("IP didnt match A:{} vs B:{}".format(ip,p.address));
break;
loc = c["name"]+" @ "+r["location"]["country"];
#srv = r["location"]["city_code"]+"-"+r["hostname"];
srv = r["hostname"];
prv = r["provider"];
own = ("(*Owned*)" if r["owned"] else ".notOwned.");
#print('{:<25} {:>15}\t{:>7.3f}, {:>7.3f}, {:>7.3f} --- {} {} {} {}'.format(loc, ip.address, ip.min_rtt, ip.avg_rtt, ip.max_rtt, ip.packets_sent, ip.packets_received, ip.packet_loss, ip.is_alive));
###print(srv, prv, own)
pingslist.append({"loc":loc, "srv":srv, "ip":ip.address, "min":ip.min_rtt, "avg":ip.avg_rtt, "max":ip.max_rtt, "prv":prv, "own":own});
cityips.pop(0);
#print()
print('location, server, ip address, min_rtt, **avg**, max_rtt, provider, owned? ');
print("-"*116);
sortedpingslist = sorted(pingslist, key = lambda i: i['avg'])
for p in sortedpingslist:
print('{:<25} {:<19} {:>15}\t{:>7.3f}, {:>7.3f}, {:>7.3f} {:<12} {:>10}'.format(p["loc"], p["srv"], p["ip"], p["min"], p["avg"], p["max"], p["prv"], p["own"]))
#def main():
#if __name__ == "__main__":
# main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment