forked from akazukin5151/ProtonVPN-server-load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn_servers.py
192 lines (153 loc) · 5.76 KB
/
vpn_servers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import json
import requests
import pycountry
response = requests.get('https://vpn-api.proton.me/vpn/logicals')
servers_dict = json.loads(response.text)
# Get rid of the LogicalServers wrapper
servers_list = servers_dict['LogicalServers']
# Compile all server names into single list
server_names = []
for x in range(len(servers_list)):
server_names.append(servers_list[x]['Name'])
# Compile all server hostnames into single list
server_hostnames = []
for x in range(len(servers_list)):
server_hostnames.append(servers_list[x]['Domain'])
# Compile all server hostnames into single list
server_cities = []
for x in range(len(servers_list)):
server_cities.append(servers_list[x]['City'])
# Compile all server load values into single list
server_load = []
for x in range(len(servers_list)):
server_load.append(servers_list[x]['Load'])
# Compile all server tier values into single list
server_tier = []
for x in range(len(servers_list)):
server_tier.append(servers_list[x]['Tier'])
# Compile all server features values into single list
server_features = []
for x in range(len(servers_list)):
server_features.append(servers_list[x]['Features'])
# Compile all servers into single list
server_servers = []
for x in range(len(servers_list)):
server_servers.append(servers_list[x]['Servers'])
def convert_features(features = 1):
error = False
if (features >= 20):
features = features - 20
port_forward = True
else:
port_forward = False
if (features >= 8):
features = features - 8
streaming = True
else:
streaming = False
if (features >= 3):
features = features - 3
netshield = True
else:
netshield = False
if (features >= 1):
features = features - 1
secure_core = True
else:
secure_core = False
# sanity check, features should now equal 0
if (features == 0):
error = False
else:
error = True
return error, secure_core, netshield, streaming, port_forward
def find_a_server(country_code = "US", state_code = "", num_results = 5, max_load = 30):
# decide what string to search for
if state_code == "":
search_for = country_code + "-"
search_for_alt = country_code + "#"
country_only = True
else:
search_for = country_code + "-" + state_code + "#"
country_only = False
draw = 0
# Look for servers in given location
index = []
for i,x in enumerate(server_names):
if search_for in x:
index.append(i)
if country_only and len(index) == 0:
# Look for servers in given location
index = []
for i,x in enumerate(server_names):
if x.startswith(search_for_alt):
index.append(i)
# Look for tier 1 and 2 servers in given location
temp = []
for i in index:
if server_tier[i] == 0:
temp.append(i)
if server_tier[i] == 1:
temp.append(i)
if server_tier[i] == 2:
temp.append(i)
index = temp
# index is list of all tier 1 & 2 servers
#print(index)
# Compile list of server load values
# for tier 1 and 2 servers in given location
load_list = []
for i in index:
load_list.append(server_load[i])
# filter the list to only have servers with loads under max_load
filtered_list = []
for i in index:
if server_load[i] <= max_load:
filtered_list.append(i)
# reorder by server load
filtered_list_max_index = len(filtered_list)
# loop from max_load to 1 stepping by -1
temp = []
for x in range(max_load, 1, -1):
# loop over list checking load
for y in filtered_list:
if server_load[y] == x:
temp.append(y)
filtered_list = temp
# loop through the filtered list and extract values
for i in filtered_list:
# display each server in the list
the_server = server_names[i]
the_hostname = server_hostnames[i]
the_load = server_load[i]
the_tier = server_tier[i]
error, secure_core, netshield, streaming, port_forward = convert_features(server_features[i])
entry_ip = server_servers[i][0]['EntryIP']
exit_ip = server_servers[i][0]['ExitIP']
pub_key = server_servers[i][0]['X25519PublicKey']
the_lat = servers_list[i]['Location']['Lat']
the_long = servers_list[i]['Location']['Long']
# get the country
the_country = pycountry.countries.get(alpha_2=the_server[0:2]).name
# get the state
if the_server.find("-") > 0:
#the_state = the_server[the_server.find("-")+len("-"):the_server.rfind("#")]
the_state = pycountry.subdivisions.get(code=the_server[0:5]).name
else:
the_state = "null"
# moved here to keep it with country/state
the_city = server_cities[i]
if len(server_servers[i]) > 1:
print("WARN: len(server_servers[]) = " + str(len(server_servers[i])) + "! Expected 1!")
addstr = ""
if secure_core:
addstr += " secure_core[True]"
if netshield:
addstr += " netshield[True]"
if streaming:
addstr += " streaming[True]"
if port_forward:
addstr += " port_forward[True]"
if error:
addstr += " error parsing features, features = " + str(server_features[i])
print("Server[" + str(i) + "]: [" + the_server + "] Hostname[" + the_hostname + "] Country[" + str(the_country) + "] State[" + str(the_state) + "] City[" + str(the_city) + "] Load[" + str(the_load) + "%] Tier[" + str(the_tier) + "] Entry[" + entry_ip + "] Exit[" + exit_ip + "] Lat[" + str(the_lat) + "] Long[" + str(the_long) + "] PubKey[" + pub_key + "]" + addstr)