Skip to content

Instantly share code, notes, and snippets.

@S1M0N38
Last active May 30, 2018 20:24
Show Gist options
  • Save S1M0N38/bb6403808c7532d96b6ad6e5d79b6abd to your computer and use it in GitHub Desktop.
Save S1M0N38/bb6403808c7532d96b6ad6e5d79b6abd to your computer and use it in GitHub Desktop.
CLI tool to connect to https://www.vpnbook.com
import argparse
import glob
import os
import subprocess
from bs4 import BeautifulSoup
import requests
import zipfile
BASE_URL = 'https://www.vpnbook.com/freevpn'
SERVER_URL = 'https://www.vpnbook.com/free-openvpn-account/'
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
VPN_DIR = 'vpn'
LOGIN_FILE = os.path.join(VPN_DIR, 'credential.conf')
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'-s', '--server', type=str, default='DE',
choices=['Euro1', 'Euro2', 'US1', 'US2', 'CA', 'DE', 'FR'],
help='choose server location')
parser.add_argument(
'-d', '--download', action='store_true',
help='download all configuration files')
parser.add_argument(
'-c', '--connection', type=str, default='tcp', choices=['tcp', 'udp'],
help='select connection protocol')
parser.add_argument(
'-p', '--port', type=int, default=80, choices=[80, 443, 53, 25000],
help='select connection port')
return parser.parse_args()
def get_data_openvpn():
r = requests.get(BASE_URL)
soup = BeautifulSoup(r.text, 'html.parser')
data = soup.find_all('ul')[-1]
servers_names = [l.text.split(' ')[0] for l in data.find_all('a')]
servers_links = [l.get('href').split('/')[-1] for l in data.find_all('a')]
servers = dict(zip(servers_names, servers_links))
username = data.find_all('li')[-2].find('strong').text
password = data.find_all('li')[-1].find('strong').text
return servers, username, password
def download_conf(server_link, server_name):
os.chdir(VPN_DIR)
zip_file = server_link.split('/')[-1]
r = requests.get(server_link, allow_redirects=True)
with open(zip_file, 'wb') as f:
f.write(r.content)
if not os.path.isdir(server_name):
os.mkdir(server_name)
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
zip_ref.extractall(server_name)
[os.remove(f) for f in glob.glob('VPN*.zip')]
os.chdir('..')
def run_openvpn(config_file):
subprocess.run(['openvpn', '--config', config_file, '--auth-user-pass', LOGIN_FILE])
def main():
args = argument_parser()
# download data from vpnbook
servers, username, password = get_data_openvpn()
server_link = SERVER_URL + servers[args.server]
# check if VPN dir exists, otherwise mkdir and dowload all files
if not os.path.isdir(VPN_DIR):
os.mkdir(VPN_DIR)
# download all file config
if args.download:
for server_name in servers:
server_link = SERVER_URL + servers[server_name]
download_conf(server_link, server_name)
# create credential.conf
with open(os.path.join(VPN_DIR, 'credential.conf'), 'w') as f:
f.write('{}\n{}'.format(username, password))
# check if .ovpn exists
config_file = glob.glob(os.path.join(
VPN_DIR, args.server, '*-{}{}.ovpn'
.format(args.connection, args.port)))
if not config_file:
download_conf(server_link, args.server)
config_file = glob.glob(os.path.join(
VPN_DIR, args.server, '*-{}{}.ovpn'
.format(args.connection, args.port)))
if not config_file:
raise FileNotFoundError(
'No file found for port: {} and connection: {}'
.format(args.connection, args.port))
subprocess.run(['sudo', 'openvpn', '--config', config_file[0],
'--auth-user-pass', LOGIN_FILE])
if __name__ == '__main__':
# This file must bu run as sudo
main()
@S1M0N38
Copy link
Author

S1M0N38 commented May 30, 2018

Usage

sudo python vpn.py 

Help

sudo python vpn.py -h
usage: vpn.py [-h] [-s {Euro1,Euro2,US1,US2,CA,DE,FR}] [-d] [-c {tcp,udp}]
              [-p {80,443,53,25000}]

optional arguments:
  -h, --help            show this help message and exit
  -s {Euro1,Euro2,US1,US2,CA,DE,FR}, --server {Euro1,Euro2,US1,US2,CA,DE,FR}
                        choose server location
  -d, --download        download all configuration files
  -c {tcp,udp}, --connection {tcp,udp}
                        select connection protocol
  -p {80,443,53,25000}, --port {80,443,53,25000}
                        select connection port

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