2
0
Fork 0
qbittorrent-natpmp-docker/data/start.sh

157 lines
4.2 KiB
Bash
Raw Normal View History

2023-03-16 20:54:23 +01:00
#!/usr/bin/env bash
timestamp() {
2023-11-08 15:31:49 +01:00
date '+%Y-%m-%d %H:%M:%S'
}
2023-03-16 20:54:23 +01:00
getpublicip() {
2023-11-08 15:31:49 +01:00
# shellcheck disable=SC2086
natpmpc -g ${VPN_GATEWAY} | grep -oP '(?<=Public.IP.address.:.).*'
2023-03-16 20:54:23 +01:00
}
findconfiguredport() {
2023-11-08 15:31:49 +01:00
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})'
2023-03-16 20:54:23 +01:00
}
findactiveport() {
2023-11-08 15:31:49 +01:00
# shellcheck disable=SC2086
natpmpc -g ${VPN_GATEWAY} -a 0 0 udp ${NAT_LEASE_LIFETIME} >/dev/null 2>&1
# shellcheck disable=SC2086
natpmpc -g ${VPN_GATEWAY} -a 0 0 tcp ${NAT_LEASE_LIFETIME} | grep -oP '(?<=Mapped public port.).*(?=.protocol.*)'
2023-03-16 20:54:23 +01:00
}
qbt_login() {
2023-11-08 15:31:49 +01:00
qbt_sid=$(curl -s -i --header "Referer: http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}" --data "username=${QBITTORRENT_USER}" --data-urlencode "password=${QBITTORRENT_PASS}" "http://${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}/api/v2/auth/login" | grep -oP '(?!set-cookie:.)SID=.*(?=\;.HttpOnly\;)')
return $?
2023-03-16 20:54:23 +01:00
}
2023-11-08 15:31:49 +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" >/dev/null 2>&1
return $?
2023-03-16 20:54:23 +01:00
}
2023-11-08 15:31:49 +01:00
qbt_checksid() {
if 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-11-08 15:31:49 +01:00
qbt_isreachable() {
# shellcheck disable=SC2086
nc -4 -vw 5 ${QBITTORRENT_SERVER} ${QBITTORRENT_PORT} &>/dev/null 2>&1
}
get_portmap() {
2023-11-08 15:31:49 +01:00
res=0
public_ip=$(getpublicip)
if ! qbt_checksid; then
echo "$(timestamp) | qBittorrent Cookie invalid, getting new SessionID"
if ! qbt_login; then
echo "$(timestamp) | Failed getting new SessionID from qBittorrent"
return 1
fi
else
echo "$(timestamp) | qBittorrent SessionID Ok!"
fi
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}"
# shellcheck disable=SC2086
if [ ${configured_port} != ${active_port} ]; then
if qbt_changeport "${qbt_sid}" ${active_port}; then
echo "$(timestamp) | Port Changed to: $(findconfiguredport ${qbt_sid})"
else
echo "$(timestamp) | Port Change failed."
res=1
fi
else
echo "$(timestamp) | Port OK (Act: ${active_port} Cfg: ${configured_port})"
fi
return $res
}
pre_reqs() {
2023-11-08 15:31:49 +01:00
while read -r var; do
if [ -z "${!var}" ]; then
echo "$(timestamp) | ${var} is empty or not set."
[ "$var" != "QBITTORRENT_USER" ] && [ "$var" != "QBITTORRENT_PASS" ] && exit 1
fi
done <<EOF
QBITTORRENT_SERVER
QBITTORRENT_PORT
QBITTORRENT_USER
QBITTORRENT_PASS
VPN_GATEWAY
CHECK_INTERVAL
NAT_LEASE_LIFETIME
EOF
2023-11-08 15:31:49 +01:00
[ ! -S /var/run/docker.sock ] && {
echo "$(timestamp) | Docker socket doesn't exist or is inaccessible"
exit 2
}
2023-11-08 15:31:49 +01:00
return 0
}
2023-11-08 15:31:49 +01:00
load_vals() {
public_ip=$(getpublicip)
if qbt_isreachable; then
if qbt_login; then
configured_port=$(findconfiguredport "${qbt_sid}")
else
echo "$(timestamp) | Unable to login to qBittorrent at ${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}"
exit 7
fi
else
echo "$(timestamp) | Unable to reach qBittorrent at ${QBITTORRENT_SERVER}:${QBITTORRENT_PORT}"
exit 6
fi
active_port=''
}
public_ip=
configured_port=
active_port=
qbt_sid=
2023-11-08 15:31:49 +01:00
pre_reqs && load_vals
# shellcheck disable=SC2086
2023-11-08 15:31:49 +01:00
[ -z ${public_ip} ] && {
echo "$(timestamp) | Unable to grab VPN Public IP. Please check configuration"
exit 3
}
# shellcheck disable=SC2086
2023-11-08 15:31:49 +01:00
[ -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
}
while true; do
if get_portmap; then
echo "$(timestamp) | NAT-PMP/UPnP Ok!"
else
echo "$(timestamp) | NAT-PMP/UPnP Failed"
fi
# shellcheck disable=SC2086
echo "$(timestamp) | Sleeping for $(echo ${CHECK_INTERVAL}/60 | bc) minutes"
# shellcheck disable=SC2086
sleep ${CHECK_INTERVAL}
2023-03-16 20:54:23 +01:00
done
exit $?