From 103b4d5e64580c6d4701f0bde23c4af93d410337 Mon Sep 17 00:00:00 2001 From: soxfor <7237915+soxfor@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:22:57 +0000 Subject: [PATCH] update: data/start.sh - reorder some code and add more error checking --- data/start.sh | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/data/start.sh b/data/start.sh index 5e87e10..bf72f93 100755 --- a/data/start.sh +++ b/data/start.sh @@ -13,6 +13,7 @@ findconfiguredport() { } findactiveport() { + natpmpc -g ${VPN_GATEWAY} -a 0 0 udp ${NAT_LEASE_LIFETIME} >/dev/null 2>&1 natpmpc -g ${VPN_GATEWAY} -a 0 0 tcp ${NAT_LEASE_LIFETIME} | grep -oP '(?<=Mapped public port.).*(?=.protocol.*)' } @@ -25,18 +26,23 @@ qbt_changeport(){ return $? } -public_ip=$(getpublicip) -qbt_sid=$(qbt_login) -configured_port=$(findconfiguredport ${qbt_sid}) -active_port='' +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 +} get_portmap() { res=0 public_ip=$(getpublicip) - 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 + if ! qbt_checksid; then echo "$(timestamp) | qBittorrent Cookie invalid, getting new SessionID" qbt_sid=$(qbt_login) + else + echo "$(timestamp) | qBittorrent SessionID Ok!" fi configured_port=$(findconfiguredport ${qbt_sid}) @@ -49,11 +55,14 @@ get_portmap() { if [ ${configured_port} != ${active_port} ]; then if qbt_changeport ${qbt_sid} ${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 -D INPUT -i ${VPN_IF_NAME} -p tcp --dport ${configured_port} -j ACCEPT + docker exec ${VPN_CT_NAME} /sbin/iptables -A INPUT -i ${VPN_IF_NAME} -p udp --dport ${active_port} -j ACCEPT + 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 if docker exec ${VPN_CT_NAME} /sbin/iptables -L INPUT -n | grep -qP "^ACCEPT.*${active_port}.*"; then echo "$(timestamp) | IPTables rule added for port ${active_port} on ${VPN_CT_NAME} container" fi - sleep 3 echo "$(timestamp) | Port Changed to: $(findconfiguredport ${qbt_sid})" else echo "$(timestamp) | Port Change failed." @@ -66,6 +75,7 @@ get_portmap() { return $res } +pre_reqs() { while read var; do [ -z "${!var}" ] && { echo "$(timestamp) | ${var} is empty or not set."; exit 1; } done << EOF @@ -82,6 +92,22 @@ EOF [ ! -S /var/run/docker.sock ] && { echo "$(timestamp) | Docker socket doesn't exist or is inaccessible"; exit 2; } +return 0 +} + +load_vals(){ + public_ip=$(getpublicip) + qbt_sid=$(qbt_login) + configured_port=$(findconfiguredport ${qbt_sid}) + 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; } + while true; do if get_portmap; then