Last active
October 12, 2015 00:27
-
-
Save meta1203/37e4dbbf6ddfee262bde to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| } | |
| copy_crt( ) | |
| { | |
| echo 'Copying certificate..' | |
| mkdir -p /etc/openvpn | |
| cat << EOF > /etc/openvpn/ca.crt | |
| -----BEGIN CERTIFICATE----- | |
| MIID2jCCA0OgAwIBAgIJAOtqMkR2JSXrMA0GCSqGSIb3DQEBBQUAMIGlMQswCQYD | |
| VQQGEwJVUzELMAkGA1UECBMCT0gxETAPBgNVBAcTCENvbHVtYnVzMSAwHgYDVQQK | |
| ExdQcml2YXRlIEludGVybmV0IEFjY2VzczEjMCEGA1UEAxMaUHJpdmF0ZSBJbnRl | |
| cm5ldCBBY2Nlc3MgQ0Ex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i1jAS9tNP2E+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":"us-california.privateinternetaccess.com","port_forward":false,"ping":"98.143.158.162:8888","openvpn_udp":{"best":"98.143.158.162:8080"},"openvpn_tcp":{"best":"98.143.158.162:500"},"ips":[]},"us2":{"name":"US East","country":"US","dns":"us-east.privateinternetaccess.com","port_forward":false,"ping":"66.55.144.184:8888","openvpn_udp":{"best":"66.55.144.184:8080"},"openvpn_tcp":{"best":"66.55.144.184:500"},"ips":[]},"us1":{"name":"US Midwest","country":"US","dns":"us-midwest.privateinternetaccess.com","port_forward":false,"ping":"108.61.228.156:8888","openvpn_udp":{"best":"108.61.228.156:8080"},"openvpn_tcp":{"best":"108.61.228.156:500"},"ips":[]},"us_south_west":{"name":"US Texas","country":"US","dns":"us-texas.privateinternetaccess.com","port_forward":false,"ping":"162.216.46.85:8888","openvpn_udp":{"best":"162.216.46.85:8080"},"openvpn_tcp":{"best":"162.216.46.85:500"},"ips":[]},"us_florida":{"name":"US Florida","country":"US","dns":"us-florida.privateinternetaccess.com","port_forward":false,"ping":"104.156.240.199:8888","openvpn_udp":{"best":"104.156.240.199:8080"},"openvpn_tcp":{"best":"104.156.240.199:500"},"ips":[]},"us_seattle":{"name":"US Seattle","country":"US","dns":"us-seattle.privateinternetaccess.com","port_forward":false,"ping":"104.200.154.46:8888","openvpn_udp":{"best":"104.200.154.46:8080"},"openvpn_tcp":{"best":"104.200.154.46:500"},"ips":[]},"us3":{"name":"US West","country":"US","dns":"us-west.privateinternetaccess.com","port_forward":false,"ping":"104.200.151.3:8888","openvpn_udp":{"best":"104.200.151.3:8080"},"openvpn_tcp":{"best":"104.200.151.3:500"},"ips":[]},"us_silicon_valley":{"name":"US Silicon Valley","country":"US","dns":"us-siliconvalley.privateinternetaccess.com","port_forward":false,"ping":"104.156.228.150:8888","openvpn_udp":{"best":"104.156.228.150:8080"},"openvpn_tcp":{"best":"104.156.228.150: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.114:8888","openvpn_udp":{"best":"209.95.50.114:8080"},"openvpn_tcp":{"best":"209.95.50.114:500"},"ips":[]},"uk":{"name":"UK London","country":"GB","dns":"uk-london.privateinternetaccess.com","port_forward":false,"ping":"104.238.169.137:8888","openvpn_udp":{"best":"104.238.169.137:8080"},"openvpn_tcp":{"best":"104.238.169.137:500"},"ips":[]},"uk_southampton":{"name":"UK Southampton","country":"GB","dns":"uk-southampton.privateinternetaccess.com","port_forward":false,"ping":"146.185.20.156:8888","openvpn_udp":{"best":"146.185.20.156:8080"},"openvpn_tcp":{"best":"146.185.20.156:500"},"ips":[]},"ca_toronto":{"name":"CA Toronto","country":"CA","dns":"ca-toronto.privateinternetaccess.com","port_forward":true,"ping":"172.98.67.16:8888","openvpn_udp":{"best":"172.98.67.16:8080"},"openvpn_tcp":{"best":"172.98.67.16:500"},"ips":[]},"ca":{"name":"CA North York","country":"CA","dns":"ca.privateinternetaccess.com","port_forward":true,"ping":"173.199.65.11:8888","openvpn_udp":{"best":"173.199.65.11:8080"},"openvpn_tcp":{"best":"173.199.65.11:500"},"ips":[]},"aus":{"name":"AU Sydney","country":"AU","dns":"aus.privateinternetaccess.com","port_forward":false,"ping":"168.1.6.17:8888","openvpn_udp":{"best":"168.1.6.17:8080"},"openvpn_tcp":{"best":"168.1.6.17:500"},"ips":[]},"aus_melbourne":{"name":"AU Melbourne","country":"AU","dns":"aus-melbourne.privateinternetaccess.com","port_forward":false,"ping":"168.1.75.38:8888","openvpn_udp":{"best":"168.1.75.38:8080"},"openvpn_tcp":{"best":"168.1.75.38:500"},"ips":[]},"nl":{"name":"Netherlands","country":"NL","dns":"nl.privateinternetaccess.com","port_forward":true,"ping":"46.166.188.228:8888","openvpn_udp":{"best":"46.166.188.228:8080"},"openvpn_tcp":{"best":"46.166.188.228:500"},"ips":[]},"swiss":{"name":"Switzerland","country":"CH","dns":"swiss.privateinternetaccess.com","port_forward":true,"ping":"179.43.159.194:8888","openvpn_udp":{"best":"179.43.159.194:8080"},"openvpn_tcp":{"best":"179.43.159.194:500"},"ips":[]},"sweden":{"name":"Sweden","country":"SE","dns":"sweden.privateinternetaccess.com","port_forward":true,"ping":"37.203.209.26:8888","openvpn_udp":{"best":"37.203.209.26:8080"},"openvpn_tcp":{"best":"37.203.209.26:500"},"ips":[]},"france":{"name":"France","country":"FR","dns":"france.privateinternetaccess.com","port_forward":true,"ping":"108.61.123.80:8888","openvpn_udp":{"best":"108.61.123.80:8080"},"openvpn_tcp":{"best":"108.61.123.80:500"},"ips":[]},"germany":{"name":"Germany","country":"DE","dns":"germany.privateinternetaccess.com","port_forward":true,"ping":"46.165.210.13:8888","openvpn_udp":{"best":"46.165.210.13:8080"},"openvpn_tcp":{"best":"46.165.210.13:500"},"ips":[]},"ireland":{"name":"Ireland","country":"IE","dns":"ireland.privateinternetaccess.com","port_forward":false,"ping":"185.108.128.11:8888","openvpn_udp":{"best":"185.108.128.11:8080"},"openvpn_tcp":{"best":"185.108.128.11:500"},"ips":[]},"italy":{"name":"Italy","country":"IT","dns":"italy.privateinternetaccess.com","port_forward":false,"ping":"159.122.133.243:8888","openvpn_udp":{"best":"159.122.133.243:8080"},"openvpn_tcp":{"best":"159.122.133.243:500"},"ips":[]},"russia":{"name":"Russia","country":"RU","dns":"russia.privateinternetaccess.com","port_forward":true,"ping":"185.22.183.201:8888","openvpn_udp":{"best":"185.22.183.201:8080"},"openvpn_tcp":{"best":"185.22.183.201:500"},"ips":[]},"ro":{"name":"Romania","country":"RO","dns":"ro.privateinternetaccess.com","port_forward":true,"ping":"93.115.83.244:8888","openvpn_udp":{"best":"93.115.83.244:8080"},"openvpn_tcp":{"best":"93.115.83.244:500"},"ips":[]},"turkey":{"name":"Turkey","country":"TR","dns":"turkey.privateinternetaccess.com","port_forward":false,"ping":"176.53.21.211:8888","openvpn_udp":{"best":"176.53.21.211:8080"},"openvpn_tcp":{"best":"176.53.21.211:500"},"ips":[]},"hk":{"name":"Hong Kong","country":"HK","dns":"hk.privateinternetaccess.com","port_forward":true,"ping":"119.81.253.241:8888","openvpn_udp":{"best":"119.81.253.241:8080"},"openvpn_tcp":{"best":"119.81.253.241:500"},"ips":[]},"sg":{"name":"Singapore","country":"SG","dns":"sg.privateinternetaccess.com","port_forward":false,"ping":"216.185.103.139:8888","openvpn_udp":{"best":"216.185.103.139:8080"},"openvpn_tcp":{"best":"216.185.103.139:500"},"ips":[]},"japan":{"name":"Japan","country":"JP","dns":"japan.privateinternetaccess.com","port_forward":false,"ping":"161.202.72.168:8888","openvpn_udp":{"best":"161.202.72.168:8080"},"openvpn_tcp":{"best":"161.202.72.168:500"},"ips":[]},"israel":{"name":"Israel","country":"IL","dns":"israel.privateinternetaccess.com","port_forward":true,"ping":"31.168.172.147:8888","openvpn_udp":{"best":"31.168.172.147:8080"},"openvpn_tcp":{"best":"31.168.172.147:500"},"ips":[]},"mexico":{"name":"Mexico","country":"MX","dns":"mexico.privateinternetaccess.com","port_forward":false,"ping":"169.57.0.252:8888","openvpn_udp":{"best":"169.57.0.252:8080"},"openvpn_tcp":{"best":"169.57.0.252:500"},"ips":[]},"brazil":{"name":"Brazil","country":"BR","dns":"brazil.privateinternetaccess.com","port_forward":false,"ping":"177.154.145.98:8888","openvpn_udp":{"best":"177.154.145.98:8080"},"openvpn_tcp":{"best":"177.154.145.98: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