forked from pia-foss/manual-connections
-
Notifications
You must be signed in to change notification settings - Fork 9
/
refresh_pia_port.sh
executable file
·57 lines (47 loc) · 1.98 KB
/
refresh_pia_port.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
#!/usr/local/bin/bash
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin"
printf "
#############################
refresh_pia_port.sh
############################# \n\n"
# Retrieve variables
pf_filepath=/pia-info/pf
PF_HOSTNAME="$( cat $pf_filepath/PF_HOSTNAME )"
PF_GATEWAY="$( cat $pf_filepath/PF_GATEWAY )"
payload="$( cat $pf_filepath/payload )"
signature="$( cat $pf_filepath/signature )"
port="$( cat $pf_filepath/port )"
expires_at="$( cat $pf_filepath/expires_at )"
#variables for authing to transmission fill area between "" with your auth details to your transmission install
transUser=""
transPass=""
echo PF_HOSTNAME: $PF_HOSTNAME
echo PF_GATEWAY: $PF_GATEWAY
echo payload: $payload
echo signature: $signature
echo port: $port
echo expires_at: $expires_at
printf "Sending port# to transmission-remote.\n\n"
transmission-remote --auth "${transUser}":"${transPass}" -p $port
printf "\nTrying to bind the port . . . \n"
# Now we have all required data to create a request to bind the port.
# Set a cron job to run this script every 15 minutes, to keep the port
# alive. The servers have no mechanism to track your activity, so they
# will just delete the port forwarding if you don't send keepalives.
bind_port_response="$(curl -Gs -m 5 \
--connect-to "$PF_HOSTNAME::$PF_GATEWAY:" \
--cacert "/manual-connections/ca.rsa.4096.crt" \
--data-urlencode "payload=${payload}" \
--data-urlencode "signature=${signature}" \
"https://${PF_HOSTNAME}:19999/bindPort")"
echo "$bind_port_response"
# If port did not bind, just exit the script.
# This script will exit in 2 months, since the port will expire.
export bind_port_response
if [ "$(echo "$bind_port_response" | jq -r '.status')" != "OK" ]; then
echo "The API did not return OK when trying to bind port."
echo "Ports expire after two months; maybe that's why. Exiting."
exit 1
fi
echo Port $port refreshed on $(date). \
This port will expire $expires_at