Created
December 4, 2020 19:32
-
-
Save obokaman-com/e1b9a61b34c2ff27cd0e98aadaee7c8f to your computer and use it in GitHub Desktop.
Script to start ngrok, copy the generated URL to clipboard and update a remote Cloudflare worker with the public ngrok url
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 | |
echo "=========== START LOCAL HTTP / HTTPS TUNNEL AND UPDATE YOUR REMOTE DEV DOMAIN TO POINT IT ===========" | |
printf "\n" | |
# Start NGROK in background | |
echo "⚡️ Starting ngrok" | |
ngrok http 8080 > /dev/null & | |
# Wait for ngrok to be available | |
while ! nc -z localhost 4040; do | |
sleep 1/5 # wait Ngrok to be available | |
done | |
# Get NGROK dynamic URL from its own exposed local API | |
NGROK_REMOTE_URL="$(curl -s http://localhost:4040/api/tunnels | jq ".tunnels[0].public_url")" | |
if test -z "${NGROK_REMOTE_URL}" | |
then | |
echo "❌ ERROR: ngrok doesn't seem to return a valid URL (${NGROK_REMOTE_URL})." | |
exit 1 | |
fi | |
# Trim double quotes from variable | |
NGROK_REMOTE_URL=$(echo ${NGROK_REMOTE_URL} | tr -d '"') | |
# If http protocol is returned, replace by https | |
NGROK_REMOTE_URL=${NGROK_REMOTE_URL/http:\/\//https:\/\/} | |
# Get script parent folder to point to .env file and get TELEGRAM_BOT_TOKEN dynamically | |
PARENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P ) | |
# Get TELEGRAM_BOT_TOKEN dynamically from local .env file | |
TELEGRAM_BOT_TOKEN=$(grep TELEGRAM_BOT_TOKEN ${PARENT_PATH}/../.env.local | cut -d '=' -f2) | |
if test -z "${TELEGRAM_BOT_TOKEN}" | |
then | |
echo "❌ ERROR: I haven't been able to recover TELEGRAM_BOT_TOKEN from your local .env.local variables file." | |
exit 1 | |
fi | |
# Get CLOUDFLARE_ACCOUNT_ID dynamically from local .env file | |
CLOUDFLARE_ACCOUNT_ID=$(grep CLOUDFLARE_ACCOUNT_ID ${PARENT_PATH}/../.env.local | cut -d '=' -f2) | |
if test -z "${CLOUDFLARE_ACCOUNT_ID}" | |
then | |
echo "❌ ERROR: I haven't been able to recover CLOUDFLARE_ACCOUNT_ID from your local .env.local variables file." | |
exit 1 | |
fi | |
# Get CLOUDFLARE_WORKERS_AUTH_BEARER dynamically from local .env file | |
CLOUDFLARE_WORKERS_AUTH_BEARER=$(grep CLOUDFLARE_WORKERS_AUTH_BEARER ${PARENT_PATH}/../.env.local | cut -d '=' -f2) | |
if test -z "${CLOUDFLARE_WORKERS_AUTH_BEARER}" | |
then | |
echo "❌ ERROR: I haven't been able to recover CLOUDFLARE_WORKERS_AUTH_BEARER from your local .env.local variables file." | |
exit 1 | |
fi | |
# Get CLOUDFLARE_LOCAL_WORKER dynamically from local .env file | |
CLOUDFLARE_LOCAL_WORKER=$(grep CLOUDFLARE_LOCAL_WORKER ${PARENT_PATH}/../.env.local | cut -d '=' -f2) | |
if test -z "${CLOUDFLARE_LOCAL_WORKER}" | |
then | |
echo "❌ ERROR: I haven't been able to recover CLOUDFLARE_LOCAL_WORKER from your local .env.local variables file." | |
exit 1 | |
fi | |
# Get CLOUDFLARE_LOCAL_URL dynamically from local .env file | |
CLOUDFLARE_LOCAL_URL=$(grep CLOUDFLARE_LOCAL_URL ${PARENT_PATH}/../.env.local | cut -d '=' -f2) | |
if test -z "${CLOUDFLARE_LOCAL_URL}" | |
then | |
echo "❌ ERROR: I haven't been able to recover CLOUDFLARE_LOCAL_URL from your local .env.local variables file." | |
exit 1 | |
fi | |
printf "\n" | |
echo "⚡️ Updating Cloudflare worker to allow dynamic tunneling from" | |
echo " ${CLOUDFLARE_LOCAL_URL} 👉 ${NGROK_REMOTE_URL}" | |
# Update Cloudflare Worker | |
curl -s -X PUT "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/workers/scripts/${CLOUDFLARE_LOCAL_WORKER}" \ | |
-H "Authorization: Bearer ${CLOUDFLARE_WORKERS_AUTH_BEARER}" \ | |
-H "Content-Type: application/javascript" \ | |
--data "addEventListener('fetch', event => { | |
const request = event.request | |
const path = request.url.replace(/^https?:\/\/.*?\//gi, '/'); | |
const modifiedRequest = new Request('${NGROK_REMOTE_URL}'.concat(path), request) | |
return event.respondWith(fetch(modifiedRequest)); | |
})" > /dev/null & | |
echo ${NGROK_REMOTE_URL} | tr -d '\n' | pbcopy | |
printf "\n" | |
echo "📋 I've just copied your ngrok remote URL to your clipboard 😉" | |
printf "\n" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a ton for sharing this!