Skip to content

Instantly share code, notes, and snippets.

@adnan360
Last active April 19, 2022 07:36
Show Gist options
  • Save adnan360/f5bf854a9278612e0effedbfa202d6fc to your computer and use it in GitHub Desktop.
Save adnan360/f5bf854a9278612e0effedbfa202d6fc to your computer and use it in GitHub Desktop.
Automatically adds the first 6 fastest VPNGate vpn connections to Network Manager

VPNGate Auto Setup Script for OpenVPN on Linux

VPNGate is a an excellent VPN for beginners. But one of the problems is that older ovpn connections stop connecting after a few days. I can easily add them manually, but downloading the ovpn files and adding them every week is a mess! This script easily adds up to date 6 connections from VPNGate records.

Requirements

  • Only supports Linux
  • Requires network-manager-openvpn & network-manager-openvpn-gnome to be installed
  • Python 2 or 3

Instructions

Just download and run:

sudo python autovpngate.py

It will take some time (it will download ~2MB file of records for latest connections) and it will ask for sudo password. Provide the password. Now you should have some connections on your NetworkManager, like vpngateauto_1_JP, vpngateauto_2_HK etc. Some connections may not work. So you can try the first one, if it doesn't work go on to the next one, etc.

It does basically 3 things:

  • Downloads the latest vpn connection data
  • Deletes previously create auto vpn connections (if any)
  • Creates auto vpn connections

Example output:

$> sudo python autovpngate.py
downloading latest vpn connection data...
download finished...
processing...
updating connections...
deleting previously created auto connection vpngateauto_1_JP...
deleting previously created auto connection vpngateauto_2_JP...
deleting previously created auto connection vpngateauto_3_JP...
deleting previously created auto connection vpngateauto_4_KR...
deleting previously created auto connection vpngateauto_5_PE...
deleting previously created auto connection vpngateauto_6_PE...
creating vpngate auto connection vpngateauto_1_JP...
creating vpngate auto connection vpngateauto_2_JP...
creating vpngate auto connection vpngateauto_3_JP...
creating vpngate auto connection vpngateauto_4_KR...
creating vpngate auto connection vpngateauto_5_PE...
creating vpngate auto connection vpngateauto_6_PE...

Previously you had to delete all the previous auto connections in Network Manager. Now it automatically does it for you. So just run and connect!

--- You are welcome to improve it! ---

Have fun!

#! /usr/bin/env python
# License: CC0
# From: https://gist.github.com/adnan360/f5bf854a9278612e0effedbfa202d6fc
# Run: python autovpngate.py
# For CSV parsing and ovpn file handling
import os
import csv
import base64, string
import subprocess
from subprocess import Popen, PIPE
# Set encoding to utf8 for Python 2
# Python 3 has it set by default
import sys
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding("utf-8")
# For regex
import re
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
storagepath = '/tmp/'
# --------------- Functions ---------------
def runme(program):
p = subprocess.Popen([program], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ''
for line in p.stdout.readlines():
#print(line)
output = output+'\n'+line.decode("utf-8")
retval = p.wait()
return output
def deletefile(filename):
if os.path.isfile(filename):
os.remove(filename)
# --------------- Get data from vpngate ---------------
print('downloading latest vpn connection data...')
data = urlopen("http://www.vpngate.net/api/iphone/")
htmlcode = data.read()
print('download finished...')
print('processing...')
deletefile(storagepath+'output.csv')
f = open(storagepath+'output.csv', 'wb')
f.write(htmlcode)
# --------------- Handle CSV and OVPN ---------------
print('updating connections...')
with open(storagepath+'output.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
count = 0
# find previous auto connections
output = runme('sudo /usr/bin/nmcli connection show')
connections = [word for word in output.split() if word.startswith('vpngateauto')]
# remove previous auto connections
for conn in connections:
print('deleting previously created auto connection '+conn+'...')
runme('sudo /usr/bin/nmcli connection delete id '+conn)
for row in reader:
# take first 6 connections
if count < 6:
if len(row) > 13 and row[14] != 'OpenVPN_ConfigData_Base64':
count = count + 1
decoded = base64.b64decode(row[14])
countrycode = row[6]
ovpnname = 'vpngateauto_'+str(count)+'_'+countrycode
deletefile(storagepath+ovpnname+'.ovpn')
with open(storagepath+ovpnname+'.ovpn', 'a') as out:
out.write(decoded.decode('UTF-8') + '\n')
# create connection
print('creating vpngate auto connection '+ovpnname+'...')
runme('sudo /usr/bin/nmcli connection import type openvpn file '+storagepath+ovpnname+'.ovpn')
deletefile(storagepath+ovpnname+'.ovpn')
deletefile(storagepath+'output.csv')
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment