2021-03-09 16:19:29 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-10-09 20:11:51 +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.
|
|
|
|
|
|
|
|
# This function allows you to check if the required tools have been installed.
|
2021-08-20 14:19:03 +00:00
|
|
|
check_tool() {
|
2020-10-09 20:11:51 +00:00
|
|
|
cmd=$1
|
2021-08-20 14:19:03 +00:00
|
|
|
if ! command -v "$cmd" >/dev/null; then
|
2020-10-09 20:11:51 +00:00
|
|
|
echo "$cmd could not be found"
|
|
|
|
echo "Please install $cmd"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2021-08-20 14:19:03 +00:00
|
|
|
# Now we call the function to make sure we can use openvpn, curl and jq.
|
|
|
|
check_tool openvpn
|
2020-10-09 20:11:51 +00:00
|
|
|
check_tool curl
|
|
|
|
check_tool jq
|
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
# Check if terminal allows output, if yes, define colors for output
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ -t 1 ]]; then
|
2021-01-21 23:12:23 +00:00
|
|
|
ncolors=$(tput colors)
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ -n $ncolors && $ncolors -ge 8 ]]; then
|
2021-10-07 12:30:53 +00:00
|
|
|
red=$(tput setaf 1) # ANSI red
|
|
|
|
green=$(tput setaf 2) # ANSI green
|
|
|
|
nc=$(tput sgr0) # No Color
|
2021-01-21 23:12:23 +00:00
|
|
|
else
|
2021-10-07 12:30:53 +00:00
|
|
|
red=''
|
|
|
|
green=''
|
|
|
|
nc='' # No Color
|
2021-01-21 23:12:23 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-10-12 19:49:40 +00:00
|
|
|
# Check if manual PIA OpenVPN connection is already initialized.
|
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 OpenVPN and WireGuard.
|
2021-08-20 14:19:03 +00:00
|
|
|
adapter_check=$( ip a s tun06 2>&1 )
|
2020-10-09 20:11:51 +00:00
|
|
|
should_read="Device \"tun06\" does not exist"
|
|
|
|
pid_filepath="/opt/piavpn-manual/pia_pid"
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ $adapter_check != *"$should_read"* ]]; then
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}The tun06 adapter already exists, that interface is required"
|
|
|
|
echo -e "for this configuration.${nc}"
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ -f $pid_filepath ]]; then
|
|
|
|
old_pid=$( cat "$pid_filepath" )
|
|
|
|
old_pid_name=$( ps -p "$old_pid" -o comm= )
|
|
|
|
if [[ $old_pid_name == "openvpn" ]]; then
|
2020-10-09 20:11:51 +00:00
|
|
|
echo
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "It seems likely that process ${red}$old_pid${nc} is an OpenVPN connection"
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "that was established by using this script. Unless it is closed"
|
|
|
|
echo "you would not be able to get a new connection."
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -ne "Do you want to run ${red}$ kill $old_pid${nc} (Y/n): "
|
2021-08-04 00:26:29 +00:00
|
|
|
read -r close_connection
|
2020-10-09 20:11:51 +00:00
|
|
|
fi
|
2021-08-20 14:19:03 +00:00
|
|
|
if echo "${close_connection:0:1}" | grep -iq n; then
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}Closing script. Resolve tun06 adapter conflict and run the script again."
|
2020-10-09 20:11:51 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-01-21 23:12:23 +00:00
|
|
|
echo
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${green}Killing the existing OpenVPN process and waiting 5 seconds...${nc}"
|
2021-08-04 00:26:29 +00:00
|
|
|
kill "$old_pid"
|
2021-01-21 23:12:23 +00:00
|
|
|
echo
|
|
|
|
for i in {5..1}; do
|
|
|
|
echo -n "$i..."
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo
|
2020-10-09 20:11:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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-10-09 20:11:51 +00:00
|
|
|
then
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}You should consider disabling IPv6 by running:"
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "sysctl -w net.ipv6.conf.all.disable_ipv6=1"
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "sysctl -w net.ipv6.conf.default.disable_ipv6=1${nc}"
|
2020-10-09 20:11:51 +00:00
|
|
|
fi
|
|
|
|
|
2021-08-20 14:19:03 +00:00
|
|
|
# Check if the mandatory environment variables are set.
|
|
|
|
if [[ -z $OVPN_SERVER_IP ||
|
|
|
|
-z $OVPN_HOSTNAME ||
|
|
|
|
-z $PIA_TOKEN ||
|
|
|
|
-z $CONNECTION_SETTINGS ]]; then
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}This script requires 4 env vars:"
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "PIA_TOKEN - the token used for authentication"
|
|
|
|
echo "OVPN_SERVER_IP - IP that you want to connect to"
|
|
|
|
echo "OVPN_HOSTNAME - name of the server, required for ssl"
|
|
|
|
echo "CONNECTION_SETTINGS - the protocol and encryption specification"
|
|
|
|
echo " - available options for CONNECTION_SETTINGS are:"
|
|
|
|
echo " * openvpn_udp_standard"
|
|
|
|
echo " * openvpn_udp_strong"
|
|
|
|
echo " * openvpn_tcp_standard"
|
|
|
|
echo " * openvpn_tcp_strong"
|
2020-10-09 20:11:51 +00:00
|
|
|
echo
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "You can also specify optional env vars:"
|
2020-10-09 20:11:51 +00:00
|
|
|
echo "PIA_PF - enable port forwarding"
|
|
|
|
echo "PAYLOAD_AND_SIGNATURE - In case you already have a port."
|
|
|
|
echo
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "An easy solution is to just run get_region_and_token.sh"
|
|
|
|
echo "as it will guide you through getting the best server and"
|
|
|
|
echo "also a token. Detailed information can be found here:"
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "https://github.com/pia-foss/manual-connections${nc}"
|
2020-10-09 20:11:51 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create a credentials file with the login token
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -n "Trying to write /opt/piavpn-manual/pia.ovpn..."
|
2020-10-09 20:11:51 +00:00
|
|
|
mkdir -p /opt/piavpn-manual
|
|
|
|
rm -f /opt/piavpn-manual/credentials /opt/piavpn-manual/route_info
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "${PIA_TOKEN:0:62}
|
|
|
|
${PIA_TOKEN:62}" > /opt/piavpn-manual/credentials || exit 1
|
2020-11-13 12:37:58 +00:00
|
|
|
chmod 600 /opt/piavpn-manual/credentials
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${green}OK!${nc}"
|
2020-10-09 20:11:51 +00:00
|
|
|
|
|
|
|
# Translate connection settings variable
|
|
|
|
IFS='_'
|
|
|
|
read -ra connection_settings <<< "$CONNECTION_SETTINGS"
|
|
|
|
IFS=' '
|
2021-08-20 14:19:03 +00:00
|
|
|
protocol=${connection_settings[1]}
|
|
|
|
encryption=${connection_settings[2]}
|
2020-10-09 20:11:51 +00:00
|
|
|
|
|
|
|
prefix_filepath="openvpn_config/standard.ovpn"
|
|
|
|
if [[ $encryption == "strong" ]]; then
|
|
|
|
prefix_filepath="openvpn_config/strong.ovpn"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $protocol == "udp" ]]; then
|
|
|
|
if [[ $encryption == "standard" ]]; then
|
|
|
|
port=1198
|
|
|
|
else
|
|
|
|
port=1197
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ $encryption == "standard" ]]; then
|
|
|
|
port=502
|
|
|
|
else
|
|
|
|
port=501
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create the OpenVPN config based on the settings specified
|
2021-08-20 14:19:03 +00:00
|
|
|
cat "$prefix_filepath" > /opt/piavpn-manual/pia.ovpn || exit 1
|
|
|
|
echo "remote $OVPN_SERVER_IP $port $protocol" >> /opt/piavpn-manual/pia.ovpn
|
2020-10-09 20:11:51 +00:00
|
|
|
|
2020-10-12 19:49:40 +00:00
|
|
|
# Copy the up/down scripts to /opt/piavpn-manual/
|
2020-10-09 20:11:51 +00:00
|
|
|
# based upon use of PIA DNS
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ $PIA_DNS != "true" ]]; then
|
2020-10-09 20:11:51 +00:00
|
|
|
cp openvpn_config/openvpn_up.sh /opt/piavpn-manual/
|
|
|
|
cp openvpn_config/openvpn_down.sh /opt/piavpn-manual/
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}This configuration will not use PIA DNS.${nc}"
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "If you want to also enable PIA DNS, please start the script"
|
|
|
|
echo "with the env var PIA_DNS=true. Example:"
|
2021-08-04 00:26:29 +00:00
|
|
|
echo $ OVPN_SERVER_IP=\""$OVPN_SERVER_IP"\" OVPN_HOSTNAME=\""$OVPN_HOSTNAME"\" \
|
|
|
|
PIA_TOKEN=\""$PIA_TOKEN"\" CONNECTION_SETTINGS=\""$CONNECTION_SETTINGS"\" \
|
2020-10-09 20:11:51 +00:00
|
|
|
PIA_PF=true PIA_DNS=true ./connect_to_openvpn_with_token.sh
|
|
|
|
else
|
|
|
|
cp openvpn_config/openvpn_up_dnsoverwrite.sh /opt/piavpn-manual/openvpn_up.sh
|
|
|
|
cp openvpn_config/openvpn_down_dnsoverwrite.sh /opt/piavpn-manual/openvpn_down.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Start the OpenVPN 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".
|
2020-10-09 20:11:51 +00:00
|
|
|
#rm -f /opt/piavpn-manual/debug_info
|
|
|
|
echo "
|
|
|
|
Trying to start the OpenVPN connection..."
|
|
|
|
openvpn --daemon \
|
|
|
|
--config "/opt/piavpn-manual/pia.ovpn" \
|
|
|
|
--writepid "/opt/piavpn-manual/pia_pid" \
|
|
|
|
--log "/opt/piavpn-manual/debug_info" || exit 1
|
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
echo -n "
|
2020-10-09 20:11:51 +00:00
|
|
|
The OpenVPN connect command was issued.
|
|
|
|
|
2021-01-21 23:12:23 +00:00
|
|
|
Confirming OpenVPN connection state..."
|
2020-10-09 20:11:51 +00:00
|
|
|
|
2020-10-12 19:49:40 +00:00
|
|
|
# Check if manual PIA OpenVPN connection is initialized.
|
|
|
|
# Manually adjust the connection_wait_time if needed
|
2020-10-09 20:11:51 +00:00
|
|
|
connection_wait_time=10
|
|
|
|
confirmation="Initialization Sequence Complete"
|
2021-08-20 14:19:03 +00:00
|
|
|
for (( timeout=0; timeout <= connection_wait_time; timeout++ )); do
|
2020-10-09 20:11:51 +00:00
|
|
|
sleep 1
|
|
|
|
if grep -q "$confirmation" /opt/piavpn-manual/debug_info; then
|
|
|
|
connected=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2021-08-20 14:19:03 +00:00
|
|
|
ovpn_pid=$( cat /opt/piavpn-manual/pia_pid )
|
|
|
|
gateway_ip=$( cat /opt/piavpn-manual/route_info )
|
2020-10-09 20:11:51 +00:00
|
|
|
|
|
|
|
# Report and exit if connection was not initialized within 10 seconds.
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ $connected != "true" ]]; then
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${red}The VPN connection was not established within 10 seconds.${nc}"
|
2021-08-04 00:26:29 +00:00
|
|
|
kill "$ovpn_pid"
|
2020-10-09 20:11:51 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "${green}Initialization Sequence Complete!${nc}
|
2020-10-09 20:11:51 +00:00
|
|
|
|
|
|
|
At this point, internet should work via VPN.
|
|
|
|
"
|
|
|
|
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "OpenVPN Process ID: ${green}$ovpn_pid${nc}
|
|
|
|
VPN route IP: ${green}$gateway_ip${nc}
|
2020-10-09 20:11:51 +00:00
|
|
|
|
2020-10-12 19:49:40 +00:00
|
|
|
To disconnect the VPN, run:
|
2020-10-09 20:11:51 +00:00
|
|
|
|
2021-10-07 12:30:53 +00:00
|
|
|
--> ${green}sudo kill $ovpn_pid${nc} <--
|
2020-10-09 20:11:51 +00:00
|
|
|
"
|
|
|
|
|
|
|
|
# This section will stop the script if PIA_PF is not set to "true".
|
2021-08-20 14:19:03 +00:00
|
|
|
if [[ $PIA_PF != "true" ]]; then
|
2021-09-18 12:08:36 +00:00
|
|
|
echo "If you want to also enable port forwarding, you can start the script:"
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -e "$ ${green}PIA_TOKEN=$PIA_TOKEN" \
|
2021-09-18 12:08:36 +00:00
|
|
|
"PF_GATEWAY=$gateway_ip" \
|
|
|
|
"PF_HOSTNAME=$OVPN_HOSTNAME" \
|
2021-10-07 12:30:53 +00:00
|
|
|
"./port_forwarding.sh${nc}"
|
2020-10-09 20:11:51 +00:00
|
|
|
echo
|
2021-09-18 12:08:36 +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."
|
2021-01-21 23:12:23 +00:00
|
|
|
exit 1
|
2020-10-09 20:11:51 +00:00
|
|
|
fi
|
|
|
|
|
2021-10-07 12:30:53 +00:00
|
|
|
echo -ne "This script got started with ${green}PIA_PF=true${nc}.
|
2021-01-21 23:12:23 +00:00
|
|
|
|
|
|
|
Starting port forwarding in "
|
|
|
|
for i in {5..1}; do
|
|
|
|
echo -n "$i..."
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo
|
|
|
|
|
|
|
|
echo -e "Starting procedure to enable port forwarding by running the following command:
|
2021-10-07 12:30:53 +00:00
|
|
|
$ ${green}PIA_TOKEN=$PIA_TOKEN \\
|
2021-01-21 23:12:23 +00:00
|
|
|
PF_GATEWAY=$gateway_ip \\
|
|
|
|
PF_HOSTNAME=$OVPN_HOSTNAME \\
|
2021-10-07 12:30:53 +00:00
|
|
|
./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=$gateway_ip \
|
|
|
|
PF_HOSTNAME=$OVPN_HOSTNAME \
|
2020-10-09 20:11:51 +00:00
|
|
|
./port_forwarding.sh
|