From a5334fe853955c24597b615b3b0ad9c188a2a97d Mon Sep 17 00:00:00 2001 From: Marco Paganini Date: Sat, 20 Nov 2021 12:13:34 -0800 Subject: [PATCH] Backport option to call external command on port change. Add support for the PF_POST_COMMAND environment variable in port_forwarding.sh. This will cause an external command to be called with the new forwarding port and the original forwarding port (as command-line arguments) every time the forwarding port changes. Use cases: - Update firewall configuration. - Update configuration of anything that needs to bind to the correct port. --- port_forwarding.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/port_forwarding.sh b/port_forwarding.sh index d5e1966..e4a223d 100755 --- a/port_forwarding.sh +++ b/port_forwarding.sh @@ -130,6 +130,8 @@ Trying to bind the port... " # We will repeat this request every 15 minutes, in order 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. + +old_port=0 while true; do bind_port_response="$(curl -Gs -m 5 \ --connect-to "$PF_HOSTNAME::$PF_GATEWAY:" \ @@ -151,6 +153,19 @@ while true; do echo -e Expires on'\t'"${red}$(date --date="$expires_at")${nc}" echo -e "\n${green}This script will need to remain active to use port forwarding, and will refresh every 15 minutes.${nc}\n" + if [[ "${PF_POST_COMMAND}" ]] && [[ "${port}" != "${old_port}" ]]; then + echo -e "${green}Note: Port has changed from ${old_port} to ${port}. Running PF_POST_COMMAND.${nc}" + if ! "${PF_POST_COMMAND}" "${port}" "${old_port}"; then + echo -e "${red}Warning: "${PF_POST_COMMAND}" returned non-zero.${nc}" + fi + old_port="${port}" + fi + + echo -e Forwarded port'\t'${green}$port${nc} + echo -e Refreshed on'\t'${green}$(date)${nc} + echo -e Expires on'\t'${red}$(date --date="$expires_at")${nc} + echo -e "\n${green}This script will need to remain active to use port forwarding, and will refresh every 15 minutes.${nc}\n" + # sleep 15 minutes sleep 900 done