-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverwatch.sh
executable file
·80 lines (69 loc) · 2.08 KB
/
overwatch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /bin/bash
cd "$(dirname "$0")"
. common.sh
# Check to see if OpenVPN and Deluge daemon are running
ps -ef | grep -qi "[o]penvpn --config" && OPENVPN=true
pgrep "deluged" && DELUGE=true
# Profile is chosen randomly by default ... or flip to override
PROFILE='/etc/openvpn/Denmark.ovpn'
PROFILE=$(find /etc/openvpn/*.ovpn | grep -v 'US ' | shuf -n 1)
if [[ -z $DELUGE || -z $OPENVPN ]]; then
log 'Error: deluge or Openvpn not running, killing both and exiting'
sudo pkill deluged || true
sudo pkill openvpn || true
exit 1
fi
# First check what is our public IP
IP=$(curl -m 10 -s ipinfo.io/ip)
STS=$?
if [[ ($STS == 28) || ($STS == 7) ]]; then
# We have lost connectivity, better start over
log "Connectivity lost, killing delug and openvpn"
sudo pkill deluged || true
sudo pkill openvpn || true
exit 1
fi
if [[ -z $OPENVPN ]]; then
OLD_IP=$IP
log "Current IP: $OLD_IP Starting openvpn to $PROFILE"
pushd /etc/openvpn > /dev/null
/usr/sbin/openvpn --config "$PROFILE" >> /var/log/vpn.log 2>&1 &
STS=$!
sleep 20
popd
log "woke up, looking for pid $STS"
ps -ef | grep "$STS" && OPENVPN=true
NEW_IP=$(curl -m 5 -s ipinfo.io/ip)
log "curl command complete."
if [[ "$OLD_IP" == "$NEW_IP" ]]; then
log "ERROR: new IP $NEW_IP is not changing when using VPN, exiting"
pkill deluged
pkill openvpn
exit 1
else
log "SUCCESS: IP is set up"
fi
fi
if [[ -z $DELUGE ]]; then
su - pi -c 'deluged &'
STS=$!
sleep 3
ps -ef | grep -q "$STS" && DELUGE=true
log "Started Deluge"
fi
# Keep checking dummy torrent file to ensure our IP is correct
DLG_TCH=/tmp/deluge_torrent_check
set -e
if [[ ! -e $DLG_TCH ]]; then
log 'Configuring deluge to track public torrent IP'
MAGNET=$(curl -L ipmagnet.services.cbcdn.com -v | grep -o "magnet.*>M" | \
head -c-4 | sed 's@+@ @g;s@%@\\x@g' | xargs -0 printf "%b" | \
sed 's/ /\+/g' | sed 's/\;/\&/g')
log "Using magnet Link: $MAGNET"
su - pi -c "deluge-console add $MAGNET" && touch $DLG_TCH
else
log "$DLG_TCH exists ... deluge is configred?"
fi
set +e
log 'Overwatch complete'
exit 0