Skip to content

Instantly share code, notes, and snippets.

@phongaster
Forked from rjz/ngrok_hostname.sh
Created June 4, 2021 07:54
Show Gist options
  • Save phongaster/cb2877ddccda11e9c354d770bf2581f9 to your computer and use it in GitHub Desktop.
Save phongaster/cb2877ddccda11e9c354d770bf2581f9 to your computer and use it in GitHub Desktop.

Revisions

  1. @rjz rjz created this gist Aug 9, 2016.
    35 changes: 35 additions & 0 deletions ngrok_hostname.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/sh

    # ngrok's web interface is HTML, but configuration is bootstrapped as a JSON
    # string. We can hack out the forwarded hostname by extracting the next
    # `*.ngrok.io` string from the JSON
    #
    # Brittle as all get out--YMMV. If you're still reading, usage is:
    #
    # $ ./ngrok_hostname.sh <proto> <addr>
    #
    # To retrieve the ngrok'd URL of an HTTP service running locally on :3332, use:
    #
    # $ ./ngrok_hostname.sh http localhost:3332
    #

    # The protocol (http, https, etc) of the forwarded service
    PROTO=$1

    # The address of the forwarded service
    ADDR=$2

    # Hack JSON out of the web interface bootstrap
    json=$(curl -s localhost:4040/inspect/http \
    | grep -oP 'window.common[^;]+' \
    | sed 's/^[^\(]*("//' \
    | sed 's/")\s*$//' \
    | sed 's/\\"/"/g')

    # Parse JSON for the URLs matching the configured `$ADDR`
    hosts=$(echo $json \
    | jq -r ".Session.Tunnels \
    | values \
    | map(select(.Config.addr == \"$ADDR\") | .URL) | .[]")

    echo "$hosts" | grep "^${PROTO}:"