Last active
April 2, 2023 23:15
-
-
Save jakzal/d2e19394f02c368c65a0 to your computer and use it in GitHub Desktop.
Run an http/https tunnel with ngrok
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
COMMAND=${1:-"help"} | |
PROJECT_DIR=$(dirname $(readlink -f $0))"/.." | |
BIN_DIR=$PROJECT_DIR"/bin" | |
LOG_PATH=$PROJECT_DIR"/app/logs/ngrok.log" | |
NGROK_PATH=$BIN_DIR"/ngrok" | |
PARAMETERS_PATH=$PROJECT_DIR"/app/config/parameters.yml" | |
download() { | |
echo "Downloading ngrok..." | |
wget $(download_url) -Ongrok.zip | |
unzip ngrok.zip -d $BIN_DIR | |
rm ngrok.zip | |
} | |
download_url() { | |
echo $(wget -q -O- https://ngrok.com/download | grep 'ngrok.zip?os=linux' | sed -e 's/.*href="\([^"]*\)".*/\1/g' | grep 386 | sed -e 's/\&/\&/g') | |
} | |
start() { | |
if [ ! -f $NGROK_PATH ]; then | |
download | |
fi | |
pids=$(pids) | |
if [ "$pids" == "" ]; then | |
screen -S ngrok -d -m $NGROK_PATH -log=$LOG_PATH 80 | |
fi | |
# sleep to wait for ngrok to fully start | |
sleep 2 | |
status | |
} | |
stop() { | |
pids | xargs kill -9 &> /dev/null && echo "ngrok stopped" || echo "ngrok is not running" | |
} | |
restart() { | |
stop | |
start | |
} | |
status() { | |
pids=$(pids) | |
[ "$pids" != "" ] && echo "ngrok is running: "$pids || echo "ngrok is stopped" | |
[ "$pids" != "" ] && echo "To attach the ngrok screen run: screen -r ngrok" | |
urls=$(urls) | |
[ "$urls" != "" ] && echo "Tunnel established at: "$urls | |
} | |
pids() { | |
echo $(ps ax -o'pid command' | grep ngrok | grep -v grep | awk '{print $1}' | tr "\n" ' ') | |
} | |
urls() { | |
[ pids == "" ] || cat $LOG_PATH | grep "Tunnel established at http" | sed -e 's/.*\(https\?:\/\/.*\)/\1/' | |
} | |
update_parameters() { | |
url=$(urls | grep https) | |
if [ "$url" != "" ]; then | |
sed -i $PARAMETERS_PATH -e 's@\(qpush.subscriber.endpoint:\).*$@\1 '$url'/qpush?token=\%authentication_token\%@g' | |
sed -i $PARAMETERS_PATH -e 's@\(qpush.subscriber.protocol:\).*$@\1 https@g' | |
echo $PARAMETERS_PATH" updated" | |
fi | |
} | |
help() { | |
USAGE="$0 "$(compgen -A function | tr "\\n" "|" | sed 's/|$//') | |
echo $USAGE | |
} | |
if [ "$(type -t $COMMAND)" != "function" ]; then | |
help | |
exit 1 | |
fi | |
$COMMAND |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment