Through a Cloudflare quick tunnel
The previous page used Microsoft’s relay, with a sign-in in front of it. Cloudflare’s cloudflared can make a quick tunnel with no account at all: the job connects out to Cloudflare, and you get an https://...trycloudflare.com URL, a new one every time.
On the cluster
#!/bin/bash
#SBATCH --job-name=cloudflare-tunnel
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=01:00:00
# Cluster side. Submit from this directory:
# sbatch launch.sh
# then run connect.sh on your laptop, or read the URL from the job's output.
#
# The app listens on loopback. cloudflared connects out to Cloudflare and serves
# it at a random https://....trycloudflare.com URL, which is PUBLIC: anyone who
# has the URL can reach the app, so the app's own token is all that keeps them out.
set -euo pipefail
CLOUDFLARED="${CLOUDFLARED:-$(command -v cloudflared || echo "$HOME/.local/bin/cloudflared")}"
SESSION="$HOME/.interactive-apps/$SLURM_JOB_ID"
mkdir -p "$SESSION"
chmod 700 "$HOME/.interactive-apps" "$SESSION"
trap 'rm -rf "$SESSION"' EXIT
if [[ ! -x $CLOUDFLARED ]]; then
case "$(uname -m)" in
x86_64) arch=amd64 ;;
aarch64) arch=arm64 ;;
*) echo "no cloudflared build for $(uname -m)" >&2 && exit 1 ;;
esac
mkdir -p "$(dirname "$CLOUDFLARED")"
curl -fsSL "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-$arch" -o "$CLOUDFLARED"
chmod +x "$CLOUDFLARED"
fi
APP_PORT="$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')"
# A strong token. The demo app reads APP_TOKEN; Jupyter makes its own.
APP_TOKEN="$(python3 -c 'import secrets; print(secrets.token_hex(24))')"
export APP_TOKEN
# --- your app, bound to 127.0.0.1:$APP_PORT --------------------------------
python3 "${DEMO_APP:-$SLURM_SUBMIT_DIR/../demo-app/demo_app.py}" --host 127.0.0.1 --port "$APP_PORT" &
# ----------------------------------------------------------------------------
"$CLOUDFLARED" tunnel --no-autoupdate --url "http://127.0.0.1:$APP_PORT" >"$SESSION/cloudflared.log" 2>&1 &
until URL="$(grep -o 'https://[-a-z0-9]*\.trycloudflare\.com' "$SESSION/cloudflared.log")"; do sleep 1; done
# Written aside and moved into place, so connect.sh never reads half of it.
cat >"$SESSION/info.env.tmp" <<INFO
JOBID=$SLURM_JOB_ID
URL=$URL
APP_TOKEN=$APP_TOKEN
INFO
mv "$SESSION/info.env.tmp" "$SESSION/info.env"
echo "the URL is in $SESSION/info.env; connect.sh prints it with the token"
waitThe first time, the job downloads cloudflared from its GitHub releases to ~/.local/bin, for the kind of CPU the compute node has, and later jobs reuse it. Then the app starts on the compute node’s loopback, with a token, and cloudflared tunnel --url publishes it. --no-autoupdate stops cloudflared from replacing itself while the job runs. The job reads the URL from cloudflared’s log, and writes it, with the token, to ~/.interactive-apps/<job ID>.
On your laptop
#!/bin/bash
# Laptop side. After `sbatch launch.sh` on the cluster:
# LOGIN_HOST=<your login node> ./connect.sh <JOBID>
# There is no tunnel to hold open from here; this only looks up the URL, with
# the token, and opens it.
set -euo pipefail
JOBID="${1:?usage: connect.sh JOBID}"
LOGIN_HOST="${LOGIN_HOST:?set LOGIN_HOST to the login node, as you would name it after ssh}"
eval "$(ssh -o BatchMode=yes "$LOGIN_HOST" "cat ~/.interactive-apps/$JOBID/info.env")"
echo "$URL/?token=$APP_TOKEN"
if command -v open >/dev/null; then open "$URL/?token=$APP_TOKEN"; elif command -v xdg-open >/dev/null; then xdg-open "$URL/?token=$APP_TOKEN"; fiLOGIN_HOST=my-cluster ./connect.sh <job ID>reads the URL and the token from the login node, and opens them in your browser.
What it means for security
This URL is public. Nobody has to sign in: anyone who has the URL reaches your app, from anywhere on the internet. The only thing protecting it is the app’s own token, so this is only acceptable for an app that has one, with a strong one. JupyterLab’s own is strong enough, and the launcher makes one for the demo app. Never turn the token off for this, as some recipes do.
And Cloudflare ends the HTTPS and passes the HTTP on, so Cloudflare can read everything that goes through it.1
Limitations
Cloudflare says quick tunnels are for testing and development, with no promise that they keep working.2 It needs the compute nodes to reach the internet, here Cloudflare and, the first time, GitHub. For a quick look, with a strong token, it does the job.
That is the end of this part. Everything in the guide so far has been about getting an analysis to run on a cluster, and the last part is about running it again, and getting the same result.
Footnotes
Cloudflare can put a sign-in in front of a tunnel too, with a named tunnel and Cloudflare Access. But that needs an account and a domain of your own.↩︎
It also limits each quick tunnel to 200 requests at a time, and does not support Server-Sent Events, a way for a web page to receive updates from the server.↩︎