Skip to content

Instantly share code, notes, and snippets.

@VMuliadi
Last active October 7, 2019 09:08
Show Gist options
  • Save VMuliadi/b2c71278bd1b6a3be389045e59ba15e9 to your computer and use it in GitHub Desktop.
Save VMuliadi/b2c71278bd1b6a3be389045e59ba15e9 to your computer and use it in GitHub Desktop.
Bash script to create a CNAME record that pointing to your ngrok tunnel public address using Cloudflare v4 API
#! /bin/bash
set -e
# Copyright (c) 2019 Vinsen Muliadi
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DOMAIN_NAME={{DOMAIN_NAME}}
APPLICATION_PORT={{APPLICATION_PORT}}
CLOUDFLARE_EMAIL={{CLOUDFLARE_EMAIL}}
NGROK_OAUTH_TOKEN={{NGROK_OAUTH_TOKEN}}
CLOUDFLARE_GLOBAL_ZONE_ID={{CLOUDFLARE_GLOBAL_ZONE_ID}}
CLOUDFLARE_GLOBAL_API_KEY={{CLOUDFLARE_GLOBAL_API_KEY}}
if [[ ! -f ./ngrok ]]; then
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
rm ngrok-stable-linux-amd64.zip
./ngrok authtoken ${NGROK_OAUTH_TOKEN}
fi
if [[ ! -z `pidof ngrok` ]]; then killall ngrok > /dev/null; fi
./ngrok http ${APPLICATION_PORT} --region ap > /dev/null &
sleep 3 # waiting to ensure the ngrok successfully create a tunnel
NGROK_PUBLIC_URL=$(curl -s localhost:4040/api/tunnels | jq .tunnels[0].public_url | tr -d '"')
echo "[INFO] Running ngrok from localhost:${APPLICATION_PORT} to ${NGROK_PUBLIC_URL}"
CNAME_RECORD=`echo ${NGROK_PUBLIC_URL/https:\/\//''}`
echo "[INFO] Set CNAME Record for ${CNAME_RECORD}"
CLOUDFLARE_REQUEST=`curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_GLOBAL_ZONE_ID}/dns_records" \
--data '{"type":"CNAME", "name":"'${DOMAIN_NAME}'", "content":"'${CNAME_RECORD}'"}' \
-H "Content-Type: application/json" \
-H "X-Auth-Key: ${CLOUDFLARE_GLOBAL_API_KEY}" \
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
--silent`
if [[ `echo ${CLOUDFLARE_REQUEST} | jq .success` == "true" ]]; then
echo "[OK] Set CNAME Record for ${CNAME_RECORD} success"
else
echo "[DUPL] Set CNAME Record failed because duplicated. Updating ..."
# Get dns record identifier (required to update DNS record via Cloudflare API)
RECORD_IDENTIFIER=`curl -XGET "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_GLOBAL_ZONE_ID}/dns_records?type=CNAME&name=${DOMAIN_NAME}" \
-H "Content-Type: application/json" \
-H "X-Auth-Key: ${CLOUDFLARE_GLOBAL_API_KEY}" \
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
--silent | jq .result[0].id | tr -d '"'`
curl -XPUT "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_GLOBAL_ZONE_ID}/dns_records/${RECORD_IDENTIFIER}" \
--data '{"type":"CNAME", "name":"'${DOMAIN_NAME}'", "content":"'${CNAME_RECORD}'"}' \
-H "Content-Type: application/json" \
-H "X-Auth-Key: ${CLOUDFLARE_GLOBAL_API_KEY}" \
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
--silent > /dev/null
echo "[OK] Set CNAME Record for ${CNAME_RECORD} success"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment