2023-03-16 20:54:23 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
timestamp() {
|
|
|
|
date '+%Y-%m-%d %H:%M:%S'
|
|
|
|
}
|
|
|
|
|
|
|
|
getpublicip() {
|
|
|
|
natpmpc -g ${VPN_GATEWAY} | grep -oP '(?<=Public.IP.address.:.).*'
|
|
|
|
}
|
|
|
|
|
|
|
|
findconfiguredport() {
|
|
|
|
curl -s -i --header "Referer: http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}" --cookie "$1" http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}/api/v2/app/preferences | grep -oP '(?<=\"listen_port\"\:)(\d{1,5})'
|
|
|
|
}
|
|
|
|
|
|
|
|
findactiveport() {
|
2023-03-20 03:22:57 +01:00
|
|
|
natpmpc -g ${VPN_GATEWAY} -a 0 0 udp ${NAT_LEASE_LIFETIME} >/dev/null 2>&1
|
2023-03-16 20:54:23 +01:00
|
|
|
natpmpc -g ${VPN_GATEWAY} -a 0 0 tcp ${NAT_LEASE_LIFETIME} | grep -oP '(?<=Mapped public port.).*(?=.protocol.*)'
|
|
|
|
}
|
|
|
|
|
|
|
|
qbt_login() {
|
2023-04-19 00:52:56 +02:00
|
|
|
curl -s -i --header "Referer: http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}" --data "username=${QBITTORRENT_USER}&password=${QBITTORRENT_PASS}" http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}/api/v2/auth/login | grep -oP '(?!set-cookie:.)SID=.*(?=\;.HttpOnly\;)'
|
2023-03-16 20:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qbt_changeport(){
|
|
|
|
curl -s -i --header "Referer: http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}" --cookie "$1" --data-urlencode "json={\"listen_port\":$2,\"random_port\":false,\"upnp\":false}" http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}/api/v2/app/setPreferences 2>&1 >/dev/null
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:22:57 +01:00
|
|
|
qbt_checksid(){
|
|
|
|
if echo $(curl -s --header "Referer: http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}" --cookie "${qbt_sid}" http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}/api/v2/app/version) | grep -qi forbidden; then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
2023-03-16 20:54:23 +01:00
|
|
|
|
2023-03-22 02:51:27 +01:00
|
|
|
qbt_isreachable(){
|
2023-03-23 23:54:20 +01:00
|
|
|
nc -4 -vw 5 ${QBITTORRENT_SERVER} ${QBITTORRENT_PORT} 2>&1 &>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_delrule(){
|
|
|
|
if (docker exec ${VPN_CT_NAME} /sbin/iptables -L INPUT -n | grep -qP "^ACCEPT.*${configured_port}.*"); then
|
|
|
|
docker exec ${VPN_CT_NAME} /sbin/iptables -D INPUT -i ${VPN_IF_NAME} -p tcp --dport ${configured_port} -j ACCEPT
|
|
|
|
docker exec ${VPN_CT_NAME} /sbin/iptables -D INPUT -i ${VPN_IF_NAME} -p udp --dport ${configured_port} -j ACCEPT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_addrule(){
|
|
|
|
if ! (docker exec ${VPN_CT_NAME} /sbin/iptables -L INPUT -n | grep -qP "^ACCEPT.*${active_port}.*"); then
|
|
|
|
docker exec ${VPN_CT_NAME} /sbin/iptables -A INPUT -i ${VPN_IF_NAME} -p tcp --dport ${active_port} -j ACCEPT
|
|
|
|
docker exec ${VPN_CT_NAME} /sbin/iptables -A INPUT -i ${VPN_IF_NAME} -p udp --dport ${active_port} -j ACCEPT
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
2023-03-22 02:51:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 01:11:22 +01:00
|
|
|
get_portmap() {
|
|
|
|
res=0
|
2023-03-16 20:54:23 +01:00
|
|
|
public_ip=$(getpublicip)
|
2023-03-17 01:11:22 +01:00
|
|
|
|
2023-03-20 03:22:57 +01:00
|
|
|
if ! qbt_checksid; then
|
2023-03-17 01:11:22 +01:00
|
|
|
echo "$(timestamp) | qBittorrent Cookie invalid, getting new SessionID"
|
|
|
|
qbt_sid=$(qbt_login)
|
2023-04-19 00:52:56 +02:00
|
|
|
if [ -z "${qbt_sid}" ]; then
|
|
|
|
echo "$(timestamp) | Failed getting new SessionID from qBittorrent"
|
|
|
|
return 1
|
|
|
|
fi
|
2023-03-20 03:22:57 +01:00
|
|
|
else
|
|
|
|
echo "$(timestamp) | qBittorrent SessionID Ok!"
|
2023-03-17 01:11:22 +01:00
|
|
|
fi
|
|
|
|
|
2023-03-16 20:54:23 +01:00
|
|
|
configured_port=$(findconfiguredport ${qbt_sid})
|
|
|
|
active_port=$(findactiveport)
|
|
|
|
|
|
|
|
echo "$(timestamp) | Public IP: ${public_ip}"
|
|
|
|
echo "$(timestamp) | Configured Port: ${configured_port}"
|
|
|
|
echo "$(timestamp) | Active Port: ${active_port}"
|
|
|
|
|
|
|
|
if [ ${configured_port} != ${active_port} ]; then
|
|
|
|
if qbt_changeport ${qbt_sid} ${active_port}; then
|
2023-03-23 23:54:20 +01:00
|
|
|
if fw_delrule; then
|
|
|
|
echo "$(timestamp) | IPTables rule deleted for port ${configured_port} on ${VPN_CT_NAME} container"
|
2023-03-18 02:19:34 +01:00
|
|
|
fi
|
2023-03-16 20:54:23 +01:00
|
|
|
echo "$(timestamp) | Port Changed to: $(findconfiguredport ${qbt_sid})"
|
|
|
|
else
|
|
|
|
echo "$(timestamp) | Port Change failed."
|
2023-03-17 01:11:22 +01:00
|
|
|
res=1
|
2023-03-16 20:54:23 +01:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "$(timestamp) | Port OK (Act: ${active_port} Cfg: ${configured_port})"
|
|
|
|
fi
|
2023-03-17 01:11:22 +01:00
|
|
|
|
2023-03-23 23:54:20 +01:00
|
|
|
if fw_addrule; then
|
|
|
|
echo "$(timestamp) | IPTables rule added for port ${active_port} on ${VPN_CT_NAME} container"
|
|
|
|
fi
|
|
|
|
|
2023-03-17 01:11:22 +01:00
|
|
|
return $res
|
2023-03-16 20:54:23 +01:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:22:57 +01:00
|
|
|
pre_reqs() {
|
2023-03-18 02:19:34 +01:00
|
|
|
while read var; do
|
|
|
|
[ -z "${!var}" ] && { echo "$(timestamp) | ${var} is empty or not set."; exit 1; }
|
|
|
|
done << EOF
|
|
|
|
QBITTORRENT_SERVER
|
|
|
|
QBITTORRENT_PORT
|
|
|
|
QBITTORRENT_USER
|
|
|
|
QBITTORRENT_PASS
|
|
|
|
VPN_GATEWAY
|
|
|
|
VPN_CT_NAME
|
|
|
|
VPN_IF_NAME
|
|
|
|
CHECK_INTERVAL
|
|
|
|
NAT_LEASE_LIFETIME
|
|
|
|
EOF
|
|
|
|
|
|
|
|
[ ! -S /var/run/docker.sock ] && { echo "$(timestamp) | Docker socket doesn't exist or is inaccessible"; exit 2; }
|
|
|
|
|
2023-03-20 03:22:57 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
load_vals(){
|
|
|
|
public_ip=$(getpublicip)
|
2023-03-22 02:51:27 +01:00
|
|
|
if qbt_isreachable; then
|
|
|
|
qbt_sid=$(qbt_login)
|
|
|
|
configured_port=$(findconfiguredport ${qbt_sid})
|
|
|
|
else
|
|
|
|
echo "$(timestamp) | Unable to reach qBittorrent at ${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}"
|
|
|
|
exit 6
|
|
|
|
fi
|
2023-03-20 03:22:57 +01:00
|
|
|
active_port=''
|
|
|
|
}
|
|
|
|
|
|
|
|
if pre_reqs; then load_vals; fi
|
|
|
|
|
|
|
|
[ -z ${public_ip} ] && { echo "$(timestamp) | Unable to grab VPN Public IP. Please check configuration"; exit 3; }
|
|
|
|
[ -z ${configured_port} ] && { echo "$(timestamp) | qBittorrent configured port value is empty(?). Please check configuration"; exit 4; }
|
|
|
|
[ -z ${qbt_sid} ] && { echo "$(timestamp) | Unable to grab qBittorrent SessionID. Please check configuration"; exit 5; }
|
|
|
|
|
2023-03-16 20:54:23 +01:00
|
|
|
while true;
|
|
|
|
do
|
2023-03-17 01:11:22 +01:00
|
|
|
if get_portmap; then
|
|
|
|
echo "$(timestamp) | NAT-PMP/UPnP Ok!"
|
|
|
|
else
|
|
|
|
echo "$(timestamp) | NAT-PMP/UPnP Failed"
|
|
|
|
fi
|
|
|
|
echo "$(timestamp) | Sleeping for $(echo ${CHECK_INTERVAL}/60 | bc) minutes"
|
2023-03-16 20:54:23 +01:00
|
|
|
sleep ${CHECK_INTERVAL}
|
|
|
|
done
|
|
|
|
|
|
|
|
exit $?
|