Last active
September 4, 2015 01:46
-
-
Save meta1203/fc954c1e41f32ff9c519 to your computer and use it in GitHub Desktop.
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
#! /bin/sh - | |
# | |
# Install OpenVPN connections for all available | |
# regions to NetworkManager | |
# | |
# Requirements: | |
# should be run as root | |
# python and openvpn (will be installed if not present) | |
# | |
# Usage: | |
# install [--version] | |
IFS=' | |
' | |
SERVER_INFO=/tmp/server_info | |
SPLIT_TOKEN=':' | |
error( ) | |
{ | |
echo "$@" 1>&2 | |
exit 1 | |
} | |
error_and_usage( ) | |
{ | |
echo "$@" 1>&2 | |
usage_and_exit 1 | |
} | |
usage( ) | |
{ | |
echo "Usage: sudo `dirname $0`/$PROGRAM" | |
} | |
usage_and_exit( ) | |
{ | |
usage | |
exit $1 | |
} | |
version( ) | |
{ | |
echo "$PROGRAM version $VERSION" | |
} | |
read_user_login( ) | |
{ | |
echo -n "Please enter your login: " | |
read LOGIN | |
if [ -z $LOGIN ]; then | |
error "A login must be provided for the installation to proceed" | |
fi | |
} | |
verify_running_as_root( ) | |
{ | |
if [ `/usr/bin/id -u` -ne 0 ]; then | |
error_and_usage "$0 must be run as root" | |
fi | |
} | |
install_python_version( ) | |
{ | |
if ! dpkg -l python2.7 | grep '^ii' > /dev/null ; then | |
echo -n 'Package python2.7 required. Install? (y/n): ' | |
read install_python | |
if [ $install_python = 'y' ]; then | |
echo "Installing python2.7.." | |
if ! apt-get install python2.7; then | |
error "Error installing python2.7 Aborting.." | |
fi | |
else | |
error "Package python2.7 is required for installation. Aborting.." | |
fi | |
else | |
echo "Package python2.7 already installed" | |
fi | |
} | |
install_open_vpn( ) | |
{ | |
if ! dpkg -l network-manager-openvpn | grep '^ii' > /dev/null ; then | |
echo -n 'Package network-manager-openvpn required. Install? (y/n): ' | |
read install_openvpn | |
if [ $install_openvpn = 'y' ]; then | |
echo "Installing network-manager-openvpn.." | |
if ! apt-get install network-manager-openvpn; then | |
error "Error installing network-manager-openvpn. Aborting.." | |
fi | |
else | |
error "Package network-manager-openvpn is required for installation. Aborting.." | |
fi | |
else | |
echo "Package network-manager-openvpn already installed" | |
fi | |
} | |
copy_crt( ) | |
{ | |
echo 'Copying certificate..' | |
mkdir -p /etc/openvpn | |
cat << EOF > /etc/openvpn/ca.crt | |
-----BEGIN CERTIFICATE----- | |
MIID2jCCA0OgAwIBAgIJAOtqMkR2JSXrMA0GCSqGSIb3DQEBBQUAMIGlMQswCQYD | |
VQQGEwJVUzELMAkGA1UECBMCT0gxETAPBgNVBAcTCENvbHVtYnVzMSAwHgYDVQQK | |
ExdQcml2YXRlIEludGVybmV0IEFjY2VzczEjMCEGA1UEAxMaUHJpdmF0ZSBJbnRl | |
cm5ldCBBY2Nlc3MgQ0ExLzAtBgkqhkiG9w0BCQEWIHNlY3VyZUBwcml2YXRlaW50 | |
ZXJuZXRhY2Nlc3MuY29tMB4XDTEwMDgyMTE4MjU1NFoXDTIwMDgxODE4MjU1NFow | |
gaUxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJPSDERMA8GA1UEBxMIQ29sdW1idXMx | |
IDAeBgNVBAoTF1ByaXZhdGUgSW50ZXJuZXQgQWNjZXNzMSMwIQYDVQQDExpQcml2 | |
YXRlIEludGVybmV0IEFjY2VzcyBDQTEvMC0GCSqGSIb3DQEJARYgc2VjdXJlQHBy | |
aXZhdGVpbnRlcm5ldGFjY2Vzcy5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ | |
AoGBAOlVlkHcxfN5HAswpryG7AN9CvcvVzcXvSEo91qAl/IE8H0knKZkIAhe/z3m | |
hz0t91dBHh5yfqwrXlGiyilplVB9tfZohvcikGF3G6FFC9j40GKP0/d22JfR2vJt | |
4/5JKRBlQc9wllswHZGmPVidQbU0YgoZl00bAySvkX/u1005AgMBAAGjggEOMIIB | |
CjAdBgNVHQ4EFgQUl8qwY2t+GN0pa/wfq+YODsxgVQkwgdoGA1UdIwSB0jCBz4AU | |
l8qwY2t+GN0pa/wfq+YODsxgVQmhgaukgagwgaUxCzAJBgNVBAYTAlVTMQswCQYD | |
VQQIEwJPSDERMA8GA1UEBxMIQ29sdW1idXMxIDAeBgNVBAoTF1ByaXZhdGUgSW50 | |
ZXJuZXQgQWNjZXNzMSMwIQYDVQQDExpQcml2YXRlIEludGVybmV0IEFjY2VzcyBD | |
QTEvMC0GCSqGSIb3DQEJARYgc2VjdXJlQHByaXZhdGVpbnRlcm5ldGFjY2Vzcy5j | |
b22CCQDrajJEdiUl6zAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAByH | |
atXgZzjFO6qctQWwV31P4qLelZzYndoZ7olY8ANPxl7jlP3YmbE1RzSnWtID9Gge | |
fsKHi1jAS9tNP2E+DCZiWcM/5Y7/XKS/6KvrPQT90nM5klK9LfNvS+kFabMmMBe2 | |
llQlzAzFiIfabACTQn84QLeLOActKhK8hFJy2Gy6 | |
-----END CERTIFICATE----- | |
EOF | |
} | |
parse_server_info( ) | |
{ | |
echo 'Loading servers information..' | |
json='{"us_california":{"name":"US California","country":"US","dns":"198.8.80.183","port_forward":false,"ping":"198.8.80.136:8888","openvpn_udp":{"best":"198.8.80.136:8080"},"openvpn_tcp":{"best":"198.8.80.136:500"},"ips":[]},"us2":{"name":"US East","country":"US","dns":"us-east.privateinternetaccess.com","port_forward":false,"ping":"173.199.127.210:8888","openvpn_udp":{"best":"173.199.127.210:8080"},"openvpn_tcp":{"best":"173.199.127.210:500"},"ips":[]},"us1":{"name":"US Midwest","country":"US","dns":"us-midwest.privateinternetaccess.com","port_forward":false,"ping":"108.61.228.75:8888","openvpn_udp":{"best":"108.61.228.75:8080"},"openvpn_tcp":{"best":"108.61.228.75:500"},"ips":[]},"us_south_west":{"name":"US Texas","country":"US","dns":"us-texas.privateinternetaccess.com","port_forward":false,"ping":"162.216.46.185:8888","openvpn_udp":{"best":"162.216.46.185:8080"},"openvpn_tcp":{"best":"162.216.46.185:500"},"ips":[]},"us_florida":{"name":"US Florida","country":"US","dns":"us-florida.privateinternetaccess.com","port_forward":false,"ping":"104.156.240.150:8888","openvpn_udp":{"best":"104.156.240.150:8080"},"openvpn_tcp":{"best":"104.156.240.150:500"},"ips":[]},"us_seattle":{"name":"US Seattle","country":"US","dns":"us-seattle.privateinternetaccess.com","port_forward":false,"ping":"104.200.154.67:8888","openvpn_udp":{"best":"104.200.154.67:8080"},"openvpn_tcp":{"best":"104.200.154.67:500"},"ips":[]},"us3":{"name":"US West","country":"US","dns":"us-west.privateinternetaccess.com","port_forward":false,"ping":"104.200.151.18:8888","openvpn_udp":{"best":"104.200.151.18:8080"},"openvpn_tcp":{"best":"104.200.151.18:500"},"ips":[]},"us_silicon_valley":{"name":"US Silicon Valley","country":"US","dns":"us-siliconvalley.privateinternetaccess.com","port_forward":false,"ping":"104.156.228.154:8888","openvpn_udp":{"best":"104.156.228.154:8080"},"openvpn_tcp":{"best":"104.156.228.154:500"},"ips":[]},"us_new_york_city":{"name":"US New York City","country":"US","dns":"us-newyorkcity.privateinternetaccess.com","port_forward":false,"ping":"209.95.50.66:8888","openvpn_udp":{"best":"209.95.50.66:8080"},"openvpn_tcp":{"best":"209.95.50.66:500"},"ips":[]},"uk":{"name":"UK London","country":"GB","dns":"uk-london.privateinternetaccess.com","port_forward":false,"ping":"104.238.169.154:8888","openvpn_udp":{"best":"104.238.169.154:8080"},"openvpn_tcp":{"best":"104.238.169.154:500"},"ips":[]},"uk_southampton":{"name":"UK Southampton","country":"GB","dns":"uk-southampton.privateinternetaccess.com","port_forward":false,"ping":"109.123.123.36:8888","openvpn_udp":{"best":"109.123.123.36:8080"},"openvpn_tcp":{"best":"109.123.123.36:500"},"ips":[]},"ca_toronto":{"name":"CA Toronto","country":"CA","dns":"ca-toronto.privateinternetaccess.com","port_forward":true,"ping":"172.98.67.37:8888","openvpn_udp":{"best":"172.98.67.37:8080"},"openvpn_tcp":{"best":"172.98.67.37:500"},"ips":[]},"ca":{"name":"CA North York","country":"CA","dns":"ca.privateinternetaccess.com","port_forward":true,"ping":"173.199.65.58:8888","openvpn_udp":{"best":"173.199.65.58:8080"},"openvpn_tcp":{"best":"173.199.65.58:500"},"ips":[]},"aus":{"name":"AU Sydney","country":"AU","dns":"aus.privateinternetaccess.com","port_forward":false,"ping":"168.1.6.20:8888","openvpn_udp":{"best":"168.1.6.20:8080"},"openvpn_tcp":{"best":"168.1.6.20:500"},"ips":[]},"aus_melbourne":{"name":"AU Melbourne","country":"AU","dns":"aus-melbourne.privateinternetaccess.com","port_forward":false,"ping":"168.1.99.217:8888","openvpn_udp":{"best":"168.1.99.217:8080"},"openvpn_tcp":{"best":"168.1.99.217:500"},"ips":[]},"nl":{"name":"Netherlands","country":"NL","dns":"nl.privateinternetaccess.com","port_forward":true,"ping":"109.201.154.205:8888","openvpn_udp":{"best":"109.201.154.205:8080"},"openvpn_tcp":{"best":"109.201.154.205:500"},"ips":[]},"swiss":{"name":"Switzerland","country":"CH","dns":"swiss.privateinternetaccess.com","port_forward":true,"ping":"179.43.169.162:8888","openvpn_udp":{"best":"179.43.169.162:8080"},"openvpn_tcp":{"best":"179.43.169.162:500"},"ips":[]},"sweden":{"name":"Sweden","country":"SE","dns":"sweden.privateinternetaccess.com","port_forward":true,"ping":"5.153.234.98:8888","openvpn_udp":{"best":"5.153.234.98:8080"},"openvpn_tcp":{"best":"5.153.234.98:500"},"ips":[]},"france":{"name":"France","country":"FR","dns":"france.privateinternetaccess.com","port_forward":true,"ping":"108.61.123.89:8888","openvpn_udp":{"best":"108.61.123.89:8080"},"openvpn_tcp":{"best":"108.61.123.89:500"},"ips":[]},"germany":{"name":"Germany","country":"DE","dns":"germany.privateinternetaccess.com","port_forward":true,"ping":"178.162.201.97:8888","openvpn_udp":{"best":"178.162.201.97:8080"},"openvpn_tcp":{"best":"178.162.201.97:500"},"ips":[]},"ireland":{"name":"Ireland","country":"IE","dns":"ireland.privateinternetaccess.com","port_forward":false,"ping":"185.108.128.21:8888","openvpn_udp":{"best":"185.108.128.21:8080"},"openvpn_tcp":{"best":"185.108.128.21:500"},"ips":[]},"italy":{"name":"Italy","country":"IT","dns":"italy.privateinternetaccess.com","port_forward":false,"ping":"159.122.133.251:8888","openvpn_udp":{"best":"159.122.133.251:8080"},"openvpn_tcp":{"best":"159.122.133.251:500"},"ips":[]},"russia":{"name":"Russia","country":"RU","dns":"russia.privateinternetaccess.com","port_forward":true,"ping":"185.22.183.199:8888","openvpn_udp":{"best":"185.22.183.199:8080"},"openvpn_tcp":{"best":"185.22.183.199:500"},"ips":[]},"ro":{"name":"Romania","country":"RO","dns":"ro.privateinternetaccess.com","port_forward":true,"ping":"93.115.84.125:8888","openvpn_udp":{"best":"93.115.84.125:8080"},"openvpn_tcp":{"best":"93.115.84.125:500"},"ips":[]},"turkey":{"name":"Turkey","country":"TR","dns":"turkey.privateinternetaccess.com","port_forward":false,"ping":"176.53.21.213:8888","openvpn_udp":{"best":"176.53.21.213:8080"},"openvpn_tcp":{"best":"176.53.21.213:500"},"ips":[]},"hk":{"name":"Hong Kong","country":"HK","dns":"hk.privateinternetaccess.com","port_forward":true,"ping":"119.81.249.139:8888","openvpn_udp":{"best":"119.81.249.139:8080"},"openvpn_tcp":{"best":"119.81.249.139:500"},"ips":[]},"sg":{"name":"Singapore","country":"SG","dns":"sg.privateinternetaccess.com","port_forward":false,"ping":"119.81.31.30:8888","openvpn_udp":{"best":"119.81.31.30:8080"},"openvpn_tcp":{"best":"119.81.31.30:500"},"ips":[]},"japan":{"name":"Japan","country":"JP","dns":"japan.privateinternetaccess.com","port_forward":false,"ping":"161.202.72.171:8888","openvpn_udp":{"best":"161.202.72.171:8080"},"openvpn_tcp":{"best":"161.202.72.171:500"},"ips":[]},"israel":{"name":"Israel","country":"IL","dns":"israel.privateinternetaccess.com","port_forward":true,"ping":"31.168.172.145:8888","openvpn_udp":{"best":"31.168.172.145:8080"},"openvpn_tcp":{"best":"31.168.172.145:500"},"ips":[]},"mexico":{"name":"Mexico","country":"MX","dns":"mexico.privateinternetaccess.com","port_forward":false,"ping":"169.57.0.215:8888","openvpn_udp":{"best":"169.57.0.215:8080"},"openvpn_tcp":{"best":"169.57.0.215:500"},"ips":[]},"brazil":{"name":"Brazil","country":"BR","dns":"brazil.privateinternetaccess.com","port_forward":false,"ping":"177.154.145.101:8888","openvpn_udp":{"best":"177.154.145.101:8080"},"openvpn_tcp":{"best":"177.154.145.101:500"},"ips":[]},"info":{"web_ips":["www.privateinternetaccess.com"],"vpn_ports":{"udp":[1194,8080,9201,53],"tcp":[443,110,80]},"latest_version":46,"poll_interval":600,"auto_regions":["us_california","us2","us1","us_south_west","us_florida","us_seattle","us3","us_silicon_valley","us_new_york_city","uk","uk_southampton","ca_toronto","ca","aus","aus_melbourne","nl","swiss","sweden","france","germany","ireland","italy","russia","ro","turkey","hk","sg","japan","israel","mexico","brazil"]}}' | |
python2 > $SERVER_INFO <<EOF | |
payload = '$json' | |
import json | |
d = json.loads(payload) | |
print "\n".join([d[k]['name']+'$SPLIT_TOKEN'+d[k]['dns'] for k in d.keys() if k != 'info']) | |
EOF | |
} | |
write_config_files( ) | |
{ | |
echo 'Removing previous config files if existing..' | |
rm -f /etc/NetworkManager/system-connections/PIA\ -\ * | |
echo 'Creating config files..' | |
IFS=' | |
' | |
while read server_info; do | |
name="PIA - `echo $server_info | awk -F: '{print $1}'`" | |
dns=`echo $server_info | awk -F: '{print $2}'` | |
cat <<EOF > /etc/NetworkManager/system-connections/$name | |
[connection] | |
id=$name | |
uuid=`uuidgen` | |
type=vpn | |
autoconnect=false | |
[vpn] | |
service-type=org.freedesktop.NetworkManager.openvpn | |
username=$LOGIN | |
comp-lzo=yes | |
remote=$dns | |
connection-type=password | |
password-flags=1 | |
ca=/etc/openvpn/ca.crt | |
[ipv4] | |
method=auto | |
EOF | |
chmod 600 /etc/NetworkManager/system-connections/$name | |
done < $SERVER_INFO | |
rm $SERVER_INFO | |
IFS=' | |
' | |
} | |
restart_network_manager( ) | |
{ | |
echo 'Restarting network manager..' | |
systemctl restart NetworkManager | |
} | |
EXITCODE=0 | |
PROGRAM=`basename $0` | |
VERSION=1.0 | |
while test $# -gt 0 | |
do | |
case $1 in | |
--usage | --help | -h ) | |
usage_and_exit 0 | |
;; | |
--version | -v ) | |
version | |
exit 0 | |
;; | |
*) | |
error_and_usage "Unrecognized option: $1" | |
;; | |
esac | |
shift | |
done | |
verify_running_as_root | |
read_user_login | |
copy_crt | |
parse_server_info | |
write_config_files | |
restart_network_manager | |
echo "Install successful!" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment