2019-06-26 17:24:10 +02:00
#!/bin/sh
2019-06-29 13:42:44 +02:00
exitOnError( ) {
# $1 must be set to $?
status = $1
message = $2
[ " $message " != "" ] || message = "Undefined error"
if [ $status != 0 ] ; then
printf " [ERROR] $message , with status $status )\n "
exit $status
fi
}
2019-09-10 10:31:44 -04:00
warnOnError( ) {
# $1 must be set to $?
status = $1
message = $2
[ " $message " != "" ] || message = "Undefined error"
if [ $status != 0 ] ; then
printf " [WARNING] $message , with status $status )\n "
fi
}
2019-07-15 22:02:40 +02:00
printf "[INFO] Reading forwarded port\n"
2019-09-10 10:31:44 -04:00
printf " * Generating client ID...\n"
2019-06-26 17:24:10 +02:00
client_id = ` head -n 100 /dev/urandom | sha256sum | tr -d " -" `
2019-07-16 20:44:12 +02:00
exitOnError $? "Unable to generate Client ID"
2019-09-10 10:31:44 -04:00
printf " * Obtaining forward port from PIA server...\n"
2019-07-16 20:44:12 +02:00
json = ` wget -qO- " http://209.222.18.222:2000/?client_id= $client_id " `
2019-09-10 10:31:44 -04:00
exitOnError $? "Could not obtain response from PIA server (does your PIA server support port forwarding?)"
2019-06-26 17:24:10 +02:00
if [ " $json " = = "" ] ; then
2019-09-10 10:31:44 -04:00
printf "[ERROR] Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding\n"
2019-07-16 20:44:12 +02:00
exit 1
2019-06-26 17:24:10 +02:00
fi
2019-09-10 10:31:44 -04:00
printf " * Parsing JSON response...\n"
2019-07-15 22:02:40 +02:00
port = ` echo $json | jq .port`
2019-09-10 10:31:44 -04:00
exitOnError $? "Cannot find port in JSON response"
printf " * Writing forwarded port to file...\n"
2019-09-02 16:38:41 +02:00
port_status_folder = ` dirname " ${ PORT_FORWARDING_STATUS_FILE } " `
2019-09-10 10:31:44 -04:00
warnOnError $? " Cannot find parent directory of ${ PORT_FORWARDING_STATUS_FILE } "
mkdir -p " ${ port_status_folder } "
warnOnError $? " Cannot create containing directory ${ port_status_folder } "
2019-09-02 16:38:41 +02:00
echo " $port " > " ${ PORT_FORWARDING_STATUS_FILE } "
2019-09-10 10:31:44 -04:00
warnOnError $? " Cannot write port to ${ PORT_FORWARDING_STATUS_FILE } "
printf " * Detecting current VPN IP address...\n"
ip = ` wget -qO- https://duckduckgo.com/\? q = ip | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" `
warnOnError $? "Cannot detect remote VPN IP on https://duckduckgo.com"
printf " * Forwarded port accessible at $ip : $port \n "
printf " * Detecting target VPN interface...\n"
vpn_device = $( cat /openvpn/target/config.ovpn | grep 'dev ' | cut -d" " -f 2) 0
exitOnError $? "Unable to find VPN interface in /openvpn/target/config.ovpn"
printf " * Accepting input traffic through $vpn_device to port $port ...\n "
2019-07-15 22:02:40 +02:00
iptables -A INPUT -i $vpn_device -p tcp --dport $port -j ACCEPT
2019-07-16 20:48:24 +02:00
exitOnError $? "Unable to allow the forwarded port in TCP"
2019-07-15 22:02:40 +02:00
iptables -A INPUT -i $vpn_device -p udp --dport $port -j ACCEPT
2019-07-16 20:48:24 +02:00
exitOnError $? "Unable to allow the forwarded port in UDP"
2019-09-10 10:31:44 -04:00
printf "[INFO] Port forwarded successfully\n"