Last active
May 30, 2018 20:24
-
-
Save S1M0N38/bb6403808c7532d96b6ad6e5d79b6abd to your computer and use it in GitHub Desktop.
CLI tool to connect to https://www.vpnbook.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Help