Skip to content

Commit

Permalink
Add option to call external command on port change.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marcopaganini committed Nov 20, 2021
1 parent b89d7e0 commit 7bff96a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions port_forwarding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:" \
Expand All @@ -146,6 +148,15 @@ while true; do
echo -e "${RED}The API did not return OK when trying to bind port... Exiting."
exit 1
fi

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}
Expand Down

0 comments on commit 7bff96a

Please sign in to comment.