Skip to content

Instantly share code, notes, and snippets.

@Fusion86
Created April 23, 2019 16:52
Show Gist options
  • Save Fusion86/0a073ea6f68c00793913fa1b59c2c6be to your computer and use it in GitHub Desktop.
Save Fusion86/0a073ea6f68c00793913fa1b59c2c6be to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import json
import time
import subprocess
if __name__ == "__main__":
try:
with open(os.devnull, "w") as devnull:
output = subprocess.check_output(
"sudo su deluge -c 'curl ipinfo.io -m 5'",
shell=True,
timeout=5,
stderr=devnull,
)
ipinfo = json.loads(output)
print("IP: " + ipinfo["ip"])
print("City: " + ipinfo["city"])
print("Region: " + ipinfo["region"])
print("Country: " + ipinfo["country"])
print("The OpenVPN connection looks fine :)")
except (TimeoutError, subprocess.CalledProcessError):
print("Timeout :(")
print("This means that we'll restart the OpenVPN client")
subprocess.run("systemctl restart openvpn@openvpn", shell=True)
print("Sleeping for 5 seconds")
time.sleep(5)
print("Check if we need to change our Deluge port")
subprocess.run("/etc/openvpn/portforward.sh")
#!/usr/bin/env bash
# Source: http://www.htpcguides.com
# Adapted from https://github.com/blindpet/piavpn-portforward/
# Author: Mike
# Based on https://github.com/crapos/piavpn-portforward
# Set path for root Cron Job
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
USERNAME=p0602059
PASSWORD=Eiiry7XDRA
VPNINTERFACE=tun0
VPNLOCALIP=$(ifconfig $VPNINTERFACE | awk '/inet / {print $2}' | awk 'BEGIN { FS = ":" } {print $(NF)}')
CURL_TIMEOUT=5
CLIENT_ID=$(uname -v | sha1sum | awk '{ print $1 }')
# set to 1 if using VPN Split Tunnel
SPLITVPN="1"
DELUGEUSER=portforward
DELUGEPASS=***
DELUGEHOST=localhost
#get VPNIP
VPNIP=$(curl -m $CURL_TIMEOUT --interface $VPNINTERFACE "http://ipinfo.io/ip" --silent --stderr -)
echo $VPNIP
#request new port
PORTFORWARDJSON=$(curl -m $CURL_TIMEOUT --silent --interface $VPNINTERFACE 'https://www.privateinternetaccess.com/vpninfo/port_forward_assignment' -d "user=$USERNAME&pass=$PASSWORD&clie$
#trim VPN forwarded port from JSON
PORT=$(echo $PORTFORWARDJSON | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
echo $PORT
#change firewall rules if SPLITVPN is set to 1
if [ "$SPLITVPN" -eq "1" ]; then
#change firewall rules if necessary
IPTABLERULETWO=$(iptables -L INPUT -n --line-numbers | grep -E "2.*reject-with icmp-port-unreachable" | awk '{ print $8 }')
if [ -z $IPTABLERULETWO ]; then
sudo iptables -D INPUT 2
sudo iptables -I INPUT 2 -i $VPNINTERFACE -p tcp --dport $PORT -j ACCEPT
else
sudo iptables -I INPUT 2 -i $VPNINTERFACE -p tcp --dport $PORT -j ACCEPT
fi
fi
#change deluge port on the fly
deluge-console "connect $DELUGEHOST:58846 $DELUGEUSER $DELUGEPASS; config --set listen_ports ($PORT,$PORT)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment