2020-09-18 17:52:20 +00:00
|
|
|
#!/bin/bash
|
2020-09-18 20:24:40 +00:00
|
|
|
# Copyright (C) 2020 Private Internet Access, Inc.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2021-01-29 03:09:20 +00:00
|
|
|
# absolute path ot this file directory
|
|
|
|
CURRENT_FILE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2020-09-22 21:32:44 +00:00
|
|
|
# This function allows you to check if the required tools have been installed.
|
|
|
|
function check_tool() {
|
|
|
|
cmd=$1
|
|
|
|
if ! command -v $cmd &>/dev/null
|
|
|
|
then
|
|
|
|
echo "$cmd could not be found"
|
2021-01-21 23:12:23 +00:00
|
|
|
echo "Please install $cmd"
|
2020-09-22 21:32:44 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Now we call the function to make sure we can use wg-quick, curl and jq.
|
2021-01-21 23:12:23 +00:00
|
|
|
check_tool wg-quick
|
|
|
|
check_tool curl
|
|
|
|
check_tool jq
|
|
|
|
|
|
|
|
# Check if terminal allows output, if yes, define colors for output
|
|
|
|
if test -t 1; then
|
|
|
|
ncolors=$(tput colors)
|
|
|
|
if test -n "$ncolors" && test $ncolors -ge 8; then
|
|
|
|
GREEN='\033[0;32m'
|
|
|
|
RED='\033[0;31m'
|
|
|
|
NC='\033[0m' # No Color
|
|
|
|
else
|
|
|
|
GREEN=''
|
|
|
|
RED=''
|
|
|
|
NC='' # No Color
|
|
|
|
fi
|
|
|
|
fi
|
2020-09-18 17:52:20 +00:00
|
|
|
|
|
|
|
# PIA currently does not support IPv6. In order to be sure your VPN
|
|
|
|
# connection does not leak, it is best to disabled IPv6 altogether.
|
2020-11-14 21:47:22 +00:00
|
|
|
# IPv6 can also be disabled via kernel commandline param, so we must
|
|
|
|
# first check if this is the case.
|
|
|
|
if [[ -f /proc/net/if_inet6 ]] &&
|
|
|
|
[[ $(sysctl -n net.ipv6.conf.all.disable_ipv6) -ne 1 ||
|
|
|
|
$(sysctl -n net.ipv6.conf.default.disable_ipv6) -ne 1 ]]
|
2020-09-18 17:52:20 +00:00
|
|
|
then
|
2020-09-22 21:17:46 +00:00
|
|
|
echo 'You should consider disabling IPv6 by running:'
|
|
|
|
echo 'sysctl -w net.ipv6.conf.all.disable_ipv6=1'
|
|
|
|
echo 'sysctl -w net.ipv6.conf.default.disable_ipv6=1'
|
2020-09-18 17:52:20 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the mandatory environment variables are set.
|
2020-10-09 20:11:51 +00:00
|
|
|
if [[ ! $WG_SERVER_IP || ! $WG_HOSTNAME || ! $PIA_TOKEN ]]; then
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -e ${RED}This script requires 3 env vars:
|
2020-09-18 17:52:20 +00:00
|
|
|
echo WG_SERVER_IP - IP that you want to connect to
|
|
|
|
echo WG_HOSTNAME - name of the server, required for ssl
|
2020-10-09 20:11:51 +00:00
|
|
|
echo PIA_TOKEN - your authentication token
|
2020-09-18 17:52:20 +00:00
|
|
|
echo
|
|
|
|
echo You can also specify optional env vars:
|
|
|
|
echo "PIA_PF - enable port forwarding"
|
|
|
|
echo "PAYLOAD_AND_SIGNATURE - In case you already have a port."
|
|
|
|
echo
|
|
|
|
echo An easy solution is to just run get_region_and_token.sh
|
2020-10-12 19:49:40 +00:00
|
|
|
echo as it will guide you through getting the best server and
|
2020-09-18 17:52:20 +00:00
|
|
|
echo also a token. Detailed information can be found here:
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -e https://github.com/pia-foss/manual-connections${NC}
|
2020-09-22 21:32:44 +00:00
|
|
|
exit 1
|
2020-09-18 17:52:20 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create ephemeral wireguard keys, that we don't need to save to disk.
|
|
|
|
privKey="$(wg genkey)"
|
|
|
|
export privKey
|
|
|
|
pubKey="$( echo "$privKey" | wg pubkey)"
|
|
|
|
export pubKey
|
|
|
|
|
|
|
|
# Authenticate via the PIA WireGuard RESTful API.
|
|
|
|
# This will return a JSON with data required for authentication.
|
|
|
|
# The certificate is required to verify the identity of the VPN server.
|
|
|
|
# In case you didn't clone the entire repo, get the certificate from:
|
|
|
|
# https://github.com/pia-foss/manual-connections/blob/master/ca.rsa.4096.crt
|
|
|
|
# In case you want to troubleshoot the script, replace -s with -v.
|
|
|
|
echo Trying to connect to the PIA WireGuard API on $WG_SERVER_IP...
|
|
|
|
wireguard_json="$(curl -s -G \
|
|
|
|
--connect-to "$WG_HOSTNAME::$WG_SERVER_IP:" \
|
2021-01-29 03:09:20 +00:00
|
|
|
--cacert "$CURRENT_FILE_DIR/ca.rsa.4096.crt" \
|
2020-10-09 20:11:51 +00:00
|
|
|
--data-urlencode "pt=${PIA_TOKEN}" \
|
2020-09-18 17:52:20 +00:00
|
|
|
--data-urlencode "pubkey=$pubKey" \
|
|
|
|
"https://${WG_HOSTNAME}:1337/addKey" )"
|
|
|
|
export wireguard_json
|
|
|
|
|
|
|
|
# Check if the API returned OK and stop this script if it didn't.
|
|
|
|
if [ "$(echo "$wireguard_json" | jq -r '.status')" != "OK" ]; then
|
2021-01-21 23:12:23 +00:00
|
|
|
>&2 echo -e "${RED}Server did not return OK. Stopping now.${NC}"
|
2020-09-18 17:52:20 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-10-09 20:11:51 +00:00
|
|
|
# Multi-hop is out of the scope of this repo, but you should be able to
|
|
|
|
# get multi-hop running with both WireGuard and OpenVPN by playing with
|
|
|
|
# these scripts. Feel free to fork the project and test it out.
|
|
|
|
echo
|
|
|
|
echo Trying to disable a PIA WG connection in case it exists...
|
2021-01-21 23:12:23 +00:00
|
|
|
wg-quick down pia && echo -e "${GREEN}\nPIA WG connection disabled!${NC}"
|
2020-10-09 20:11:51 +00:00
|
|
|
echo
|
|
|
|
|
2020-09-18 17:52:20 +00:00
|
|
|
# Create the WireGuard config based on the JSON received from the API
|
2020-10-09 20:11:51 +00:00
|
|
|
# In case you want this section to also add the DNS setting, please
|
|
|
|
# start the script with PIA_DNS=true.
|
2020-10-13 15:54:38 +00:00
|
|
|
# This uses a PersistentKeepalive of 25 seconds to keep the NAT active
|
|
|
|
# on firewalls. You can remove that line if your network does not
|
|
|
|
# require it.
|
2020-10-09 20:11:51 +00:00
|
|
|
if [ "$PIA_DNS" == true ]; then
|
|
|
|
dnsServer="$(echo "$wireguard_json" | jq -r '.dns_servers[0]')"
|
|
|
|
echo Trying to set up DNS to $dnsServer. In case you do not have resolvconf,
|
|
|
|
echo this operation will fail and you will not get a VPN. If you have issues,
|
|
|
|
echo start this script without PIA_DNS.
|
2021-01-21 23:12:23 +00:00
|
|
|
echo
|
2020-10-09 20:11:51 +00:00
|
|
|
dnsSettingForVPN="DNS = $dnsServer"
|
|
|
|
fi
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -n "Trying to write /etc/wireguard/pia.conf..."
|
|
|
|
mkdir -p /etc/wireguard
|
2020-09-18 17:52:20 +00:00
|
|
|
echo "
|
|
|
|
[Interface]
|
|
|
|
Address = $(echo "$wireguard_json" | jq -r '.peer_ip')
|
|
|
|
PrivateKey = $privKey
|
2020-10-09 20:11:51 +00:00
|
|
|
$dnsSettingForVPN
|
2020-09-18 17:52:20 +00:00
|
|
|
[Peer]
|
2020-10-13 15:54:38 +00:00
|
|
|
PersistentKeepalive = 25
|
2020-09-18 17:52:20 +00:00
|
|
|
PublicKey = $(echo "$wireguard_json" | jq -r '.server_key')
|
|
|
|
AllowedIPs = 0.0.0.0/0
|
|
|
|
Endpoint = ${WG_SERVER_IP}:$(echo "$wireguard_json" | jq -r '.server_port')
|
|
|
|
" > /etc/wireguard/pia.conf || exit 1
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -e ${GREEN}OK!${NC}
|
2020-09-18 17:52:20 +00:00
|
|
|
|
|
|
|
# Start the WireGuard interface.
|
|
|
|
# If something failed, stop this script.
|
|
|
|
# If you get DNS errors because you miss some packages,
|
2020-10-12 19:49:40 +00:00
|
|
|
# just hardcode /etc/resolv.conf to "nameserver 10.0.0.242".
|
|
|
|
echo
|
2020-09-18 17:52:20 +00:00
|
|
|
echo Trying to create the wireguard interface...
|
|
|
|
wg-quick up pia || exit 1
|
2021-01-21 23:12:23 +00:00
|
|
|
echo
|
|
|
|
echo -e "${GREEN}The WireGuard interface got created.${NC}
|
|
|
|
|
2020-09-18 18:48:44 +00:00
|
|
|
At this point, internet should work via VPN.
|
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
To disconnect the VPN, run:
|
|
|
|
|
|
|
|
--> ${GREEN}wg-quick down pia${NC} <--
|
|
|
|
"
|
2020-09-18 17:52:20 +00:00
|
|
|
|
|
|
|
# This section will stop the script if PIA_PF is not set to "true".
|
|
|
|
if [ "$PIA_PF" != true ]; then
|
2021-01-21 23:12:23 +00:00
|
|
|
echo If you want to also enable port forwarding, you can start the script:
|
|
|
|
echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \
|
|
|
|
PF_GATEWAY=$WG_SERVER_IP \
|
|
|
|
PF_HOSTNAME=$WG_HOSTNAME \
|
|
|
|
./port_forwarding.sh${NC}
|
2020-09-18 17:52:20 +00:00
|
|
|
echo
|
2021-01-21 23:12:23 +00:00
|
|
|
echo The location used must be port forwarding enabled, or this will fail.
|
|
|
|
echo Calling the ./get_region script with PIA_PF=true will provide a filtered list.
|
|
|
|
exit 1
|
2020-09-18 17:52:20 +00:00
|
|
|
fi
|
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -ne "This script got started with ${GREEN}PIA_PF=true${NC}.
|
|
|
|
|
|
|
|
Starting port forwarding in "
|
2020-11-01 23:47:31 +00:00
|
|
|
for i in {5..1}; do
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -n "$i..."
|
2020-11-01 23:47:31 +00:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo
|
2020-09-18 21:08:43 +00:00
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -e "Starting procedure to enable port forwarding by running the following command:
|
|
|
|
$ ${GREEN}PIA_TOKEN=$PIA_TOKEN \\
|
|
|
|
PF_GATEWAY=$WG_SERVER_IP \\
|
|
|
|
PF_HOSTNAME=$WG_HOSTNAME \\
|
|
|
|
./port_forwarding.sh${NC}"
|
|
|
|
|
2020-10-09 20:11:51 +00:00
|
|
|
PIA_TOKEN=$PIA_TOKEN \
|
2021-01-21 23:12:23 +00:00
|
|
|
PF_GATEWAY=$WG_SERVER_IP \
|
|
|
|
PF_HOSTNAME=$WG_HOSTNAME \
|
2020-09-18 21:08:43 +00:00
|
|
|
./port_forwarding.sh
|