Skip to content

Instantly share code, notes, and snippets.

@im-noob
Last active September 22, 2022 03:37
Show Gist options
  • Save im-noob/15c20628c96d3ab4f3e5654ff293cd67 to your computer and use it in GitHub Desktop.
Save im-noob/15c20628c96d3ab4f3e5654ff293cd67 to your computer and use it in GitHub Desktop.
vpngate.py In python 3
#!/usr/bin/env python
# Origal Soruce: https://gist.github.com/Lazza/bbc15561b65c16db8ca8
"""Pick server and start connection with VPNGate (http://www.vpngate.net/en/)"""
import requests, os, sys, tempfile, subprocess, base64, time
__author__ = "Andrea Lazzarotto"
__copyright__ = "Copyright 2014+, Andrea Lazzarotto"
__license__ = "GPLv3"
__version__ = "1.0"
__maintainer__ = "Andrea Lazzarotto"
__email__ = "andrea.lazzarotto@gmail.com"
if len(sys.argv) != 2:
print('usage: ' + sys.argv[0] + ' [country name | country code]')
exit(1)
country = sys.argv[1]
if len(country) == 2:
i = 6 # short name for country
elif len(country) > 2:
i = 5 # long name for country
else:
print('Country is too short!')
exit(1)
try:
vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
servers = [line.split(',') for line in vpn_data.split('\n')]
labels = servers[1]
labels[0] = labels[0][1:]
servers = [s for s in servers[2:] if len(s) > 1]
except:
print('Cannot get VPN servers data')
exit(1)
desired = [s for s in servers if country.lower() in s[i].lower()]
found = len(desired)
print('Found ' + str(found) + ' servers for country ' + country)
if found == 0:
exit(1)
supported = [s for s in desired if len(s[-1]) > 0]
print(str(len(supported)) + ' of these servers support OpenVPN')
# We pick the best servers by score
winner = sorted(supported, key=lambda s: float(s[2].replace(',','.')), reverse=True)[0]
print("\n== Best server ==")
pairs = list(zip(labels, winner))[:-1]
for (l, d) in pairs[:4]:
print(l + ': ' + d)
print(pairs[4][0] + ': ' + str(float(pairs[4][1]) / 10**6) + ' MBps')
print("Country: " + pairs[5][1])
print("\nLaunching VPN...")
_, path = tempfile.mkstemp()
# Creating File here
path = 'tmp/config.conf'
f = open(path, 'w')
f.write(base64.b64decode(winner[-1]).decode('ascii'))
f.write('\nscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf\npull-filter ignore auth-token')
f.close()
x = subprocess.Popen(['sudo', 'openvpn', '--auth-nocache', '--config', path])
try:
while True:
time.sleep(600)
# termination with Ctrl+C
except:
try:
x.kill()
except:
pass
while x.poll() != 0:
time.sleep(1)
print('\nVPN terminated')
@anmolagarwal552
Copy link

anmolagarwal552 commented Aug 4, 2020

not working in windows, i really need it

@im-noob
Copy link
Author

im-noob commented Aug 4, 2020

not working in windows, i really need it

Lemme know what's the issue..?

@paydana
Copy link

paydana commented Dec 24, 2020

not working in windows, i really need it

Lemme know what's the issue..?

C:\Grandes\Desktopp\tools>python vpngateP3.py us
Found 11 servers for country us
11 of these servers support OpenVPN

== Best server ==
HostName: vpn421828382
IP: 98.248.170.144
Score: 1661650
Ping: 40
Speed: 27.848518 MBps
Country: United States

Launching VPN...
Traceback (most recent call last):
File "C:\Grandes\Desktopp\tools\vpngateP3.py", line 73, in
x = subprocess.Popen(["openvpn", "--config", path])
File "C:\Users\bebor\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 947, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\bebor\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1416, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] O sistema não pode encontrar o arquivo especificado

@zohaib3249
Copy link

use (cmd) instead of sudo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment