2018-04-15 14:15:58 -04:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError(){
|
2018-11-14 14:38:10 +02:00
|
|
|
# $1 must be set to $?
|
|
|
|
|
status=$1
|
|
|
|
|
message=$2
|
|
|
|
|
[ "$message" != "" ] || message="Error!"
|
|
|
|
|
if [ $status != 0 ]; then
|
|
|
|
|
printf "$message (status $status)\n"
|
|
|
|
|
exit $status
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exitIfUnset(){
|
|
|
|
|
# $1 is the name of the variable to check - not the variable itself
|
|
|
|
|
var="$(eval echo "\$$1")"
|
|
|
|
|
if [ -z "$var" ]; then
|
|
|
|
|
printf "Environment variable $1 is not set\n"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exitIfNotIn(){
|
|
|
|
|
# $1 is the name of the variable to check - not the variable itself
|
|
|
|
|
# $2 is a string of comma separated possible values
|
|
|
|
|
var="$(eval echo "\$$1")"
|
|
|
|
|
for value in ${2//,/ }
|
|
|
|
|
do
|
|
|
|
|
if [ "$var" = "$value" ]; then
|
|
|
|
|
return 0
|
2018-11-06 14:55:11 +01:00
|
|
|
fi
|
2018-11-14 14:38:10 +02:00
|
|
|
done
|
|
|
|
|
printf "Environment variable $1=$var must be one of the following: "
|
|
|
|
|
for value in ${2//,/ }
|
|
|
|
|
do
|
|
|
|
|
printf "$value "
|
|
|
|
|
done
|
|
|
|
|
printf "\n"
|
|
|
|
|
exit 1
|
2018-11-06 14:55:11 +01:00
|
|
|
}
|
2018-09-20 20:42:37 +02:00
|
|
|
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " =========================================\n"
|
|
|
|
|
printf " =========================================\n"
|
|
|
|
|
printf " ============= PIA CONTAINER =============\n"
|
|
|
|
|
printf " =========================================\n"
|
|
|
|
|
printf " =========================================\n"
|
|
|
|
|
printf " == by github.com/qdm12 - Quentin McGaw ==\n\n"
|
2018-11-06 14:55:11 +01:00
|
|
|
|
|
|
|
|
printf "OpenVPN version: $(openvpn --version | head -n 1 | grep -oE "OpenVPN [0-9\.]* " | cut -d" " -f2)\n"
|
|
|
|
|
printf "Unbound version: $(unbound -h | grep "Version" | cut -d" " -f2)\n"
|
|
|
|
|
printf "Iptables version: $(iptables --version | cut -d" " -f2)\n"
|
2018-10-28 14:08:14 +01:00
|
|
|
|
2018-10-08 08:45:02 +02:00
|
|
|
############################################
|
|
|
|
|
# CHECK PARAMETERS
|
|
|
|
|
############################################
|
2018-11-14 14:38:10 +02:00
|
|
|
exitIfUnset USER
|
|
|
|
|
exitIfUnset PASSWORD
|
|
|
|
|
exitIfNotIn ENCRYPTION "normal,strong"
|
|
|
|
|
exitIfNotIn PROTOCOL "tcp,udp"
|
|
|
|
|
exitIfNotIn BLOCK_MALICIOUS "on,off"
|
2018-11-17 14:44:17 +02:00
|
|
|
cat "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn" &> /dev/null
|
|
|
|
|
exitOnError $? "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn is not accessible"
|
2018-11-14 14:38:10 +02:00
|
|
|
for SUBNET in ${EXTRA_SUBNETS//,/ }; do
|
|
|
|
|
if [ $(echo "$SUBNET" | grep -Eo '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(/([0-2]?[0-9])|([3]?[0-1]))?$') = "" ]; then
|
|
|
|
|
printf "Subnet $SUBNET is not a valid IPv4 subnet of the form 255.255.255.255/31 or 255.255.255.255\n"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
#####################################################
|
|
|
|
|
# Writes to protected file and remove USER, PASSWORD
|
|
|
|
|
#####################################################
|
|
|
|
|
printf "Writing USER and PASSWORD to protected file /auth.conf..."
|
|
|
|
|
echo "$USER" > /auth.conf
|
|
|
|
|
exitOnError $?
|
|
|
|
|
echo "$PASSWORD" >> /auth.conf
|
|
|
|
|
exitOnError $?
|
|
|
|
|
chown nonrootuser /auth.conf
|
|
|
|
|
exitOnError $?
|
|
|
|
|
chmod 400 /auth.conf
|
|
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
|
|
|
|
printf "Clearing environment variables USER and PASSWORD..."
|
|
|
|
|
unset -v USER
|
|
|
|
|
unset -v PASSWORD
|
|
|
|
|
printf "DONE\n"
|
2018-10-08 08:45:02 +02:00
|
|
|
|
2018-09-21 17:00:52 +02:00
|
|
|
############################################
|
|
|
|
|
# CHECK FOR TUN DEVICE
|
|
|
|
|
############################################
|
2018-11-06 14:55:11 +01:00
|
|
|
while [ "$(cat /dev/net/tun 2>&1 /dev/null)" != "cat: read error: File descriptor in bad state" ]; do
|
|
|
|
|
printf "TUN device is not available, sleeping for 30 seconds...\n"
|
|
|
|
|
sleep 30
|
|
|
|
|
done
|
|
|
|
|
printf "TUN device OK\n"
|
2018-09-21 17:00:52 +02:00
|
|
|
|
2018-10-04 22:24:43 +02:00
|
|
|
############################################
|
2018-10-28 14:08:14 +01:00
|
|
|
# BLOCKING MALICIOUS HOSTNAMES AND IPs WITH UNBOUND
|
2018-10-04 22:24:43 +02:00
|
|
|
############################################
|
2018-11-14 14:38:10 +02:00
|
|
|
printf "Malicious hostnames and ips blocking is $BLOCK_MALICIOUS\n"
|
|
|
|
|
if [ "$BLOCK_MALICIOUS" = "on" ]; then
|
|
|
|
|
tar -xjf /etc/unbound/blocks-malicious.bz2 -C /etc/unbound/
|
|
|
|
|
printf "$(cat /etc/unbound/blocks-malicious.conf | grep "local-zone" | wc -l ) malicious hostnames and $(cat /etc/unbound/blocks-malicious.conf | grep "private-address" | wc -l) malicious IP addresses blacklisted\n"
|
2018-10-28 14:08:14 +01:00
|
|
|
else
|
2018-11-14 14:38:10 +02:00
|
|
|
echo "" > /etc/unbound/blocks-malicious.conf
|
2018-10-04 22:24:43 +02:00
|
|
|
fi
|
|
|
|
|
|
2018-09-28 19:51:30 +02:00
|
|
|
############################################
|
|
|
|
|
# SETTING DNS OVER TLS TO 1.1.1.1 / 1.0.0.1
|
|
|
|
|
############################################
|
2018-11-14 14:38:10 +02:00
|
|
|
printf "Launching Unbound daemon to connect to Cloudflare DNS 1.1.1.1 at its TLS endpoint..."
|
2018-09-28 19:51:30 +02:00
|
|
|
unbound
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
|
|
|
|
printf "Changing DNS to localhost..."
|
2018-09-28 19:51:30 +02:00
|
|
|
echo "nameserver 127.0.0.1" > /etc/resolv.conf
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-09-28 19:51:30 +02:00
|
|
|
echo "options ndots:0" >> /etc/resolv.conf
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-09-28 19:51:30 +02:00
|
|
|
|
2018-09-21 11:28:23 +02:00
|
|
|
############################################
|
|
|
|
|
# FIREWALL
|
|
|
|
|
############################################
|
2018-11-06 14:55:11 +01:00
|
|
|
printf "Setting firewall for killswitch purposes...\n"
|
|
|
|
|
printf " * Detecting local subnet..."
|
2018-11-14 14:38:10 +02:00
|
|
|
SUBNET=$(ip route show default | tail -n 1 | cut -d" " -f 1)
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "$SUBNET\n"
|
|
|
|
|
printf " * Reading parameters to be used for region $REGION, protocol $PROTOCOL and encryption $ENCRYPTION..."
|
2018-11-17 14:44:17 +02:00
|
|
|
CONNECTIONSTRING=$(grep -i "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn" -e 'privateinternetaccess.com')
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-06-07 18:33:03 -04:00
|
|
|
PORT=$(echo $CONNECTIONSTRING | cut -d' ' -f3)
|
2018-11-06 14:55:11 +01:00
|
|
|
if [ "$PORT" = "" ]; then
|
2018-11-17 14:44:17 +02:00
|
|
|
printf "Port not found in /openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn\n"
|
2018-11-06 14:55:11 +01:00
|
|
|
exit 1
|
|
|
|
|
fi
|
2018-06-07 18:33:03 -04:00
|
|
|
PIADOMAIN=$(echo $CONNECTIONSTRING | cut -d' ' -f2)
|
2018-11-06 14:55:11 +01:00
|
|
|
if [ "$PIADOMAIN" = "" ]; then
|
2018-11-17 14:44:17 +02:00
|
|
|
printf "Domain not found in /openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn\n"
|
2018-11-06 14:55:11 +01:00
|
|
|
exit 1
|
|
|
|
|
fi
|
2018-11-17 14:44:17 +02:00
|
|
|
sed -i "/$CONNECTIONSTRING/d" "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn"
|
|
|
|
|
exitOnError $? "Can't delete remote connection string in /openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn"
|
2018-11-06 14:55:11 +01:00
|
|
|
printf "DONE\n"
|
|
|
|
|
printf " * Port: $PORT\n"
|
|
|
|
|
printf " * Domain: $PIADOMAIN\n"
|
|
|
|
|
printf " * Detecting IP addresses corresponding to $PIADOMAIN..."
|
2018-06-07 18:33:03 -04:00
|
|
|
VPNIPS=$(nslookup $PIADOMAIN localhost | tail -n +5 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
|
|
|
|
for ip in $VPNIPS; do printf " $ip\n"; done
|
2018-11-17 14:44:17 +02:00
|
|
|
printf " * Adding IP addresses of $PIADOMAIN to /openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn...\n"
|
2018-11-06 14:55:11 +01:00
|
|
|
for ip in $VPNIPS; do
|
2018-11-17 14:44:17 +02:00
|
|
|
if [ "$(grep "remote $ip $PORT" "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn")" != "" ]; then
|
2018-11-06 14:55:11 +01:00
|
|
|
printf " remote $ip $PORT (already present)\n"
|
|
|
|
|
else
|
|
|
|
|
printf " remote $ip $PORT\n"
|
2018-11-17 14:44:17 +02:00
|
|
|
echo "remote $ip $PORT" >> "/openvpn/$PROTOCOL-$ENCRYPTION/$REGION.ovpn"
|
2018-11-06 14:55:11 +01:00
|
|
|
fi
|
2018-06-06 22:44:11 -04:00
|
|
|
done
|
2018-11-06 14:55:11 +01:00
|
|
|
printf " * Deleting all iptables rules..."
|
2018-06-06 22:44:11 -04:00
|
|
|
iptables --flush
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-06-06 22:44:11 -04:00
|
|
|
iptables --delete-chain
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-09-21 16:39:08 +02:00
|
|
|
iptables -t nat --flush
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-09-21 16:39:08 +02:00
|
|
|
iptables -t nat --delete-chain
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Block output traffic..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -F OUTPUT
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -P OUTPUT DROP
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Accept established and related output traffic..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
2018-11-14 14:38:10 +02:00
|
|
|
printf "DONE\n"
|
|
|
|
|
printf " * Accept local loopback output traffic..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -o lo -j ACCEPT
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Accept output traffic with local subnet $SUBNET..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -d $SUBNET -j ACCEPT
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-11-14 14:38:10 +02:00
|
|
|
for EXTRASUBNET in ${EXTRA_SUBNETS//,/ }
|
|
|
|
|
do
|
|
|
|
|
printf " * Accept output traffic with extra subnet $EXTRASUBNET..."
|
|
|
|
|
iptables -A OUTPUT -d $EXTRASUBNET -j ACCEPT
|
|
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
|
|
|
|
done
|
2018-11-06 14:55:11 +01:00
|
|
|
for ip in $VPNIPS; do
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Accept output traffic to $ip on interface eth0, port $PROTOCOL $PORT..."
|
|
|
|
|
iptables -A OUTPUT -j ACCEPT -d $ip -o eth0 -p $PROTOCOL -m $PROTOCOL --dport $PORT
|
|
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-06-06 22:44:11 -04:00
|
|
|
done
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Accept all output traffic on tun0 interface..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -o tun0 -j ACCEPT
|
2018-11-06 14:55:11 +01:00
|
|
|
exitOnError $?
|
|
|
|
|
printf "DONE\n"
|
2018-09-21 11:28:23 +02:00
|
|
|
|
2018-11-20 09:28:48 +02:00
|
|
|
############################################
|
|
|
|
|
# Additional OpenVPN settings
|
|
|
|
|
############################################
|
|
|
|
|
cd "/openvpn/$PROTOCOL-$ENCRYPTION"
|
|
|
|
|
# Uses the username/password from this file to get the token from PIA
|
|
|
|
|
[ "$(grep "auth-user-pass /auth.conf" "$REGION.ovpn")" != "" ] || echo "auth-user-pass /auth.conf" >> "$REGION.ovpn"
|
|
|
|
|
# Reconnects automatically on failure
|
|
|
|
|
[ "$(grep "auth-retry nointeract" "$REGION.ovpn")" != "" ] || echo "auth-retry nointeract" >> "$REGION.ovpn"
|
|
|
|
|
# Prevents auth_failed infinite loops - make it interact? Remove persist-tun? nobind?
|
|
|
|
|
[ "$(grep "pull-filter ignore \"auth-token\"" "$REGION.ovpn")" != "" ] || echo "pull-filter ignore \"auth-token\"" >> "$REGION.ovpn"
|
|
|
|
|
# Runs openvpn without root, as nonrootuser
|
|
|
|
|
[ "$(grep "user nonrootuser" "$REGION.ovpn")" != "" ] || echo "user nonrootuser" >> "$REGION.ovpn"
|
|
|
|
|
|
2018-10-05 12:43:16 +02:00
|
|
|
############################################
|
|
|
|
|
# OPENVPN LAUNCH
|
|
|
|
|
############################################
|
2018-11-06 14:55:11 +01:00
|
|
|
printf "Starting OpenVPN using the following parameters:\n"
|
|
|
|
|
printf " * Region: $REGION\n"
|
|
|
|
|
printf " * Encryption: $ENCRYPTION\n"
|
|
|
|
|
printf " * Protocol: $PROTOCOL\n"
|
|
|
|
|
printf " * Port: $PORT\n"
|
2018-11-14 14:38:10 +02:00
|
|
|
printf " * Initial VPN IP address: $(echo "$VPNIPS" | head -n 1)\n\n"
|
2018-11-20 09:28:48 +02:00
|
|
|
|
|
|
|
|
openvpn --config "$REGION.ovpn"
|
2018-11-14 14:38:10 +02:00
|
|
|
status=$?
|
|
|
|
|
printf "\n =========================================\n"
|
|
|
|
|
printf " OpenVPN exit with status $status\n"
|
|
|
|
|
printf " =========================================\n"
|