-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_servers.py
More file actions
241 lines (198 loc) · 7.96 KB
/
Copy pathupdate_servers.py
File metadata and controls
241 lines (198 loc) · 7.96 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
"""
VPN Gate Server List Updater
Downloads and parses VPN Gate API data, tests connectivity, and generates servers.json
Uses OpenVPN configuration from VPN Gate servers
"""
import urllib.request
import json
import base64
import socket
import re
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
VPNGATE_API_URL = "https://www.vpngate.net/api/iphone/"
# Countries that can access Google
GOOGLE_ACCESSIBLE_COUNTRIES = ['JP', 'US', 'KR', 'TW', 'SG', 'HK', 'DE', 'GB', 'FR', 'NL', 'AU', 'CA', 'SE', 'CH', 'NO', 'FI', 'DK', 'BE', 'AT', 'IT', 'ES', 'PT', 'IE', 'NZ', 'PL', 'CZ', 'RO', 'HU', 'TH', 'MY', 'PH', 'VN', 'IN', 'ID']
def download_vpngate_data():
"""Download VPN Gate server list"""
print("Downloading VPN Gate server list...")
try:
req = urllib.request.Request(VPNGATE_API_URL, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
})
with urllib.request.urlopen(req, timeout=30) as response:
data = response.read().decode('utf-8', errors='ignore')
print(f"Downloaded {len(data)} bytes")
return data
except Exception as e:
print(f"Download failed: {e}")
return None
def parse_vpngate_csv(csv_data):
"""Parse VPN Gate CSV data"""
servers = []
lines = csv_data.strip().split('\n')
# Find header line
header_idx = -1
for i, line in enumerate(lines):
if line.startswith('#HostName') or line.startswith('*HostName'):
header_idx = i
break
if header_idx == -1:
print("Could not find header line")
return servers
# Parse data lines
for line in lines[header_idx + 1:]:
if line.startswith('*') or not line.strip():
continue
parts = line.split(',')
if len(parts) < 15:
continue
try:
hostname = parts[0]
ip = parts[1]
score = int(parts[2]) if parts[2] else 0
ping = int(parts[3]) if parts[3] else 0
speed = int(parts[4]) if parts[4] else 0
country_long = parts[5]
country_short = parts[6]
vpn_sessions = int(parts[7]) if parts[7] else 0
config_base64 = parts[14] if len(parts) > 14 else ""
# Only include servers with OpenVPN config
if not config_base64:
continue
servers.append({
'hostname': hostname,
'ip': ip,
'score': score,
'ping': ping,
'speed': speed,
'country_long': country_long,
'country_short': country_short,
'vpn_sessions': vpn_sessions,
'config_base64': config_base64
})
except (ValueError, IndexError) as e:
continue
print(f"Parsed {len(servers)} servers with OpenVPN config")
return servers
def parse_openvpn_config(config_base64):
"""Parse OpenVPN config to extract connection details"""
try:
config = base64.b64decode(config_base64).decode('utf-8', errors='ignore')
# Extract remote server and port
remote_match = re.search(r'remote\s+(\S+)\s+(\d+)', config)
if remote_match:
host = remote_match.group(1)
port = int(remote_match.group(2))
else:
return None, None, None
# Extract protocol (tcp/udp)
proto_match = re.search(r'proto\s+(tcp|udp)', config)
proto = proto_match.group(1) if proto_match else 'udp'
return host, port, proto
except Exception as e:
return None, None, None
def test_server_connectivity(ip, port, timeout=5):
"""Test if server is reachable"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((ip, port))
sock.close()
return result == 0
except Exception:
return False
def test_server(server):
"""Test a single server's connectivity"""
ip = server['ip']
config_base64 = server['config_base64']
# Parse config to get actual connection port
host, port, proto = parse_openvpn_config(config_base64)
if not port:
port = 443 # Default OpenVPN port
# Test connectivity
if test_server_connectivity(ip, port):
return server, port, True
# Try alternative ports
for alt_port in [443, 1194, 992, 995]:
if alt_port != port and test_server_connectivity(ip, alt_port):
return server, alt_port, True
return server, port, False
def convert_to_vpnserver(server, port):
"""Convert parsed server data to VpnServer format"""
return {
"id": str(abs(hash(server['ip']))),
"name": server['hostname'],
"country": server['country_short'],
"city": server['country_long'],
"endpoint": server['ip'],
"port": port,
"protocol": "OpenVPN",
"config": base64.b64decode(server['config_base64']).decode('utf-8', errors='ignore'),
"username": "vpn",
"password": "vpn",
"psk": "vpn",
"pingLatency": server['ping'],
"vpnSessions": server['vpn_sessions'],
"throughput": round(server['speed'] / 1024 / 1024, 2)
}
def main():
# Download data
csv_data = download_vpngate_data()
if not csv_data:
print("Failed to download VPN Gate data")
return
# Parse CSV
servers = parse_vpngate_csv(csv_data)
if not servers:
print("No servers found in data")
return
# Filter by Google-accessible countries and high score
filtered = [s for s in servers
if s['country_short'] in GOOGLE_ACCESSIBLE_COUNTRIES
and s['score'] >= 100000]
print(f"Filtered to {len(filtered)} servers in Google-accessible countries with score >= 100000")
# Sort by score (highest first)
filtered.sort(key=lambda x: x['score'], reverse=True)
# Take top 50 for testing
candidates = filtered[:50]
# Test connectivity in parallel
print(f"Testing connectivity for {len(candidates)} servers...")
working_servers = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(test_server, s): s for s in candidates}
for future in as_completed(futures):
server, port, is_working = future.result()
if is_working:
working_servers.append((server, port))
print(f" ✓ {server['ip']}:{port} ({server['country_short']}) - Score: {server['score']}")
else:
print(f" ✗ {server['ip']} ({server['country_short']}) - Not reachable")
print(f"\nFound {len(working_servers)} working servers")
if not working_servers:
print("No working servers found!")
return
# Sort working servers by score
working_servers.sort(key=lambda x: x[0]['score'], reverse=True)
# Take top 20 working servers
final_servers = working_servers[:20]
# Convert to VpnServer format
vpn_servers = [convert_to_vpnserver(s, p) for s, p in final_servers]
# Create output structure
output = {
"servers": vpn_servers,
"version": "2.0",
"lastUpdated": datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
}
# Write to servers.json
output_path = r"D:\project\AutoGuardVPN\app\src\main\assets\servers.json"
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(output, f, indent=2, ensure_ascii=False)
print(f"\nSaved {len(vpn_servers)} servers to {output_path}")
# Print summary
print("\n=== Server Summary ===")
for i, server in enumerate(vpn_servers[:10], 1):
print(f"{i}. {server['endpoint']}:{server['port']} - {server['city']} ({server['country']}) - {server['throughput']} Mbps")
if __name__ == "__main__":
main()