2018-04-15 14:15:58 -04:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
2018-09-21 17:00:52 +02:00
|
|
|
printf "\n ========================================="
|
|
|
|
|
printf "\n ========================================="
|
|
|
|
|
printf "\n ============= PIA CONTAINER ============="
|
|
|
|
|
printf "\n ========================================="
|
|
|
|
|
printf "\n ========================================="
|
|
|
|
|
printf "\n == by github.com/qdm12 - Quentin McGaw ==\n"
|
2018-09-20 20:42:37 +02:00
|
|
|
|
2018-09-21 16:39:08 +02:00
|
|
|
cd /openvpn-$PROTOCOL-$ENCRYPTION
|
2018-09-21 11:28:23 +02:00
|
|
|
|
2018-09-21 17:00:52 +02:00
|
|
|
############################################
|
|
|
|
|
# CHECK FOR TUN DEVICE
|
|
|
|
|
############################################
|
|
|
|
|
while [ "$(cat /dev/net/tun 2>&1 /dev/null)" != "cat: read error: File descriptor in bad state" ];
|
|
|
|
|
do
|
|
|
|
|
printf "\nTUN device is not opened, sleeping for 30 seconds..."
|
|
|
|
|
sleep 30
|
|
|
|
|
done
|
|
|
|
|
printf "\nTUN device is opened"
|
|
|
|
|
|
2018-10-04 22:24:43 +02:00
|
|
|
############################################
|
|
|
|
|
# BLOCKING MALICIOUS HOSTS WITH UNBOUND
|
|
|
|
|
############################################
|
|
|
|
|
touch /etc/unbound/blocks-malicious.conf
|
|
|
|
|
printf "\nUnbound malicious hosts blocking is $BLOCK_MALICIOUS"
|
|
|
|
|
if [[ "$BLOCK_MALICIOUS" == "on" ]]; then
|
|
|
|
|
printf "\nExtracting blocks-malicious.conf.bz2..."
|
|
|
|
|
tar -xjf /etc/unbound/blocks-malicious.conf.bz2 -C /etc/unbound/
|
|
|
|
|
rm /etc/unbound/blocks-malicious.conf.bz2
|
|
|
|
|
printf "DONE"
|
|
|
|
|
fi
|
|
|
|
|
|
2018-09-28 19:51:30 +02:00
|
|
|
############################################
|
|
|
|
|
# SETTING DNS OVER TLS TO 1.1.1.1 / 1.0.0.1
|
|
|
|
|
############################################
|
|
|
|
|
printf "\nLaunching Unbound daemon to connect to Cloudflare DNS 1.1.1.1 at its TLS endpoint..."
|
|
|
|
|
unbound
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-28 19:51:30 +02:00
|
|
|
printf "DONE"
|
|
|
|
|
printf "\nChanging DNS to localhost..."
|
|
|
|
|
echo "nameserver 127.0.0.1" > /etc/resolv.conf
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-28 19:51:30 +02:00
|
|
|
echo "options ndots:0" >> /etc/resolv.conf
|
|
|
|
|
printf "DONE"
|
|
|
|
|
|
2018-09-21 11:28:23 +02:00
|
|
|
############################################
|
|
|
|
|
# ORIGINAL IP FOR HEALTHCHECK
|
|
|
|
|
############################################
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\nGetting non VPN public IP address..."
|
2018-05-27 20:38:43 -04:00
|
|
|
export INITIAL_IP=$(wget -qqO- 'https://duckduckgo.com/?q=what+is+my+ip' | grep -ow 'Your IP address is [0-9.]*[0-9]' | grep -ow '[0-9][0-9.]*')
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "$INITIAL_IP"
|
|
|
|
|
|
2018-09-21 11:28:23 +02:00
|
|
|
############################################
|
|
|
|
|
# FIREWALL
|
|
|
|
|
############################################
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\nSetting firewall for killswitch purposes..."
|
|
|
|
|
printf "\n * Detecting local subnet..."
|
2018-06-06 22:44:11 -04:00
|
|
|
SUBNET=$(ip route show default | tail -n 1 | awk '// {print $1}')
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "$SUBNET"
|
2018-09-21 11:28:23 +02:00
|
|
|
printf "\n * Reading parameters to be used for region $REGION, protocol $PROTOCOL and encryption $ENCRYPTION..."
|
2018-06-07 18:33:03 -04:00
|
|
|
CONNECTIONSTRING=$(grep -i "/openvpn-$PROTOCOL-$ENCRYPTION/$REGION.ovpn" -e 'privateinternetaccess.com')
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
PORT=$(echo $CONNECTIONSTRING | cut -d' ' -f3)
|
2018-10-04 22:24:43 +02:00
|
|
|
if [[ "$PORT" == "" ]]; then printf "Port could not be extracted from configuration file\n"; exit 1; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
PIADOMAIN=$(echo $CONNECTIONSTRING | cut -d' ' -f2)
|
2018-10-04 22:24:43 +02:00
|
|
|
if [[ "$PIADOMAIN" == "" ]]; then printf "Port could not be extracted from configuration file\n"; exit 1; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\n * Port: $PORT"
|
|
|
|
|
printf "\n * Domain: $PIADOMAIN"
|
|
|
|
|
printf "\n * Detecting IP addresses corresponding to $PIADOMAIN..."
|
|
|
|
|
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-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-21 16:39:08 +02:00
|
|
|
VPNIPSLENGTH=0
|
2018-06-06 22:44:11 -04:00
|
|
|
for ip in $VPNIPS
|
|
|
|
|
do
|
2018-09-20 20:42:37 +02:00
|
|
|
printf "\n $ip"
|
2018-09-21 16:39:08 +02:00
|
|
|
VPNIPSLENGTH=$((VPNIPSLENGTH+1))
|
2018-06-06 22:44:11 -04:00
|
|
|
done
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\n * Deleting all iptables rules..."
|
2018-06-06 22:44:11 -04:00
|
|
|
iptables --flush
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-06 22:44:11 -04:00
|
|
|
iptables --delete-chain
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-21 16:39:08 +02:00
|
|
|
iptables -t nat --flush
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-21 16:39:08 +02:00
|
|
|
iptables -t nat --delete-chain
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "DONE"
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -F OUTPUT
|
|
|
|
|
iptables -P OUTPUT DROP
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\n * Adding rules to accept local loopback traffic..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
|
iptables -A OUTPUT -o lo -j ACCEPT
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "DONE"
|
|
|
|
|
printf "\n * Adding rules to accept traffic of subnet $SUBNET..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -d $SUBNET -j ACCEPT
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "DONE"
|
2018-06-06 22:44:11 -04:00
|
|
|
for ip in $VPNIPS
|
|
|
|
|
do
|
2018-09-20 20:42:37 +02:00
|
|
|
printf "\n * Adding rules to accept traffic with $ip on port $PROTOCOL $PORT..."
|
|
|
|
|
iptables -A OUTPUT -j ACCEPT -d $ip -o eth0 -p $PROTOCOL -m $PROTOCOL --dport $PORT
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-20 20:42:37 +02:00
|
|
|
printf "DONE"
|
2018-06-06 22:44:11 -04:00
|
|
|
done
|
2018-06-07 18:33:03 -04:00
|
|
|
printf "\n * Adding rules to accept traffic going through the tun device..."
|
2018-09-21 09:33:37 +02:00
|
|
|
iptables -A OUTPUT -o tun0 -j ACCEPT
|
2018-09-21 16:39:08 +02:00
|
|
|
printf "DONE"
|
2018-09-21 11:28:23 +02:00
|
|
|
|
|
|
|
|
############################################
|
2018-09-21 16:39:08 +02:00
|
|
|
# OPENVPN LAUNCH (retry with next VPN IP if fail)
|
2018-09-21 11:28:23 +02:00
|
|
|
############################################
|
2018-09-21 16:39:08 +02:00
|
|
|
failed=1
|
|
|
|
|
i=1
|
|
|
|
|
PREVIOUSIP=$PIADOMAIN
|
|
|
|
|
while [ $failed != 0 ]
|
|
|
|
|
do
|
|
|
|
|
VPNIP=$(echo $VPNIPS | cut -d' ' -f$i)
|
2018-10-04 22:24:43 +02:00
|
|
|
printf "\nChanging server VPN address $PREVIOUSIP to $VPNIP..."
|
2018-09-21 16:39:08 +02:00
|
|
|
sed -i "s/$PREVIOUSIP/$VPNIP/g" $REGION.ovpn
|
2018-10-04 22:24:43 +02:00
|
|
|
status=$?
|
|
|
|
|
if [[ "$status" != 0 ]]; then printf "ERROR with status code $status\nSleeping for 10 seconds before exit...\n"; sleep 10; exit $status; fi
|
2018-09-21 16:39:08 +02:00
|
|
|
PREVIOUSIP=$VPNIP
|
|
|
|
|
printf "\nStarting OpenVPN using the following parameters:"
|
|
|
|
|
printf "\n * Region: $REGION"
|
|
|
|
|
printf "\n * Encryption: $ENCRYPTION"
|
|
|
|
|
printf "\n * Address: $PROTOCOL://$VPNIP:$PORT"
|
|
|
|
|
printf "\n\n"
|
|
|
|
|
openvpn --config "$REGION.ovpn" --auth-user-pass /auth.conf
|
|
|
|
|
failed=$?
|
|
|
|
|
if [[ $failed != 0 ]]; then
|
2018-10-04 22:24:43 +02:00
|
|
|
printf "\n==> Openvpn failed with status code: $failed"
|
2018-09-21 16:39:08 +02:00
|
|
|
i=$((i+1))
|
|
|
|
|
if [[ $i -gt $VPNIPSLENGTH ]]; then
|
|
|
|
|
i=0
|
|
|
|
|
fi
|
|
|
|
|
else
|
2018-10-04 22:24:43 +02:00
|
|
|
printf "\n==> Openvpn stopped gracefully"
|
2018-09-21 16:39:08 +02:00
|
|
|
fi
|
|
|
|
|
done
|