#!/bin/sh # Kriya One: reach it from outside this machine (docs/kriya-one/INSTALL-ONE.md → "Other devices and webhooks"). # Run on the host, as you (it never uses sudo). It applies what Kriya One settings chose: # sh reach.sh [--name assistant] funnel on|off webhooks through Tailscale Funnel (only /kriya-/hooks) # sh reach.sh [--name assistant] https on|off HTTPS for other devices, as chosen in Kriya One settings → Other # devices: tailscale serve, or Caddy (own domain / local CA) # sh reach.sh [--name assistant] status what is set up now # Kriya One's server can't do these itself: its container has no tailscale and no Docker. reach.sh reads the settings # from the container (docker exec as the server's user) and reports back what it did the same way. # Env: KRIYA_TAILSCALE (the tailscale CLI, default tailscale), KRIYA_DOCKER (docker or podman), KRIYA_FUNNEL_PORT # (443, 8443 or 10000; default 10000, so nothing already served on 443 goes public with it), KRIYA_CADDY_IMAGE (the # Caddy image: default caddy:2.11.7-alpine for the local CA, ghcr.io/trueskillacademy/kriya-caddy (with the # Cloudflare DNS module) for an own domain), KRIYA_HTTPS_OFFSET (10000: the panel's port P is served on P+10000), # KRIYA_REACH_OUT (a file it writes url= to, for install.sh). # tailscale serve: never changes an entry it didn't make. The panel's HTTPS port is P+10000 and the canvas's P+10100 # when both are free; else the first free pair H, H+100 from 18000 (the canvas is always the panel's port + 100). # Its own entries are the ones that proxy to this container's host ports: https off (and uninstall) removes those. set -eu NAME=assistant while [ $# -gt 0 ]; do case "$1" in --name) NAME="$2"; shift 2 ;; -h|--help) sed -n '2,10p' "$0"; exit 0 ;; *) break ;; esac done WHAT="${1:-status}" ARG="${2:-}" TS="${KRIYA_TAILSCALE:-tailscale}" FPORT="${KRIYA_FUNNEL_PORT:-10000}" C="kriya-one-$NAME" say() { printf '\033[1m%s\033[0m\n' "$*"; } die() { printf 'kriya-one reach: %s\n' "$*" >&2; exit 1; } echo "$NAME" | grep -Eq '^[a-z0-9][a-z0-9-]{0,30}$' || die "--name: a-z, 0-9 and -, up to 31 characters" case "$FPORT" in 443|8443|10000) ;; *) die "KRIYA_FUNNEL_PORT: Funnel only serves 443, 8443 or 10000" ;; esac if [ -n "${KRIYA_DOCKER:-}" ]; then DOCKER="$KRIYA_DOCKER" elif command -v docker >/dev/null 2>&1; then DOCKER=docker else DOCKER=podman; fi $DOCKER container inspect "$C" >/dev/null 2>&1 || die "no container $C (install it first: install.sh --name $NAME)" cli() { $DOCKER exec -i -u upa "$C" node /opt/upa/server/onereach-cli.ts "$@"; } # The panel's port on this machine (the container's 7800). PORT=$($DOCKER port "$C" 7800/tcp 2>/dev/null | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -1) [ -n "$PORT" ] || die "$C publishes no panel port" OFFSET="${KRIYA_HTTPS_OFFSET:-10000}" OFFSET_SET="${KRIYA_HTTPS_OFFSET:+1}" CPORT=$((PORT + 100)) HPORT=$((PORT + OFFSET)) HCPORT=$((CPORT + OFFSET)) TPORT=$((PORT + OFFSET + 1)) CC="$C-caddy" # tailscale serve now: the HTTPS ports it serves, and "HTTPS_PORT LOCAL_PORT" for each proxy to this machine. ts_json() { "$TS" serve status --json 2>/dev/null | tr -d ' \n\t\r' || true; } ts_ports() { ts_json | grep -o '[":][0-9]\{1,5\}":{' | tr -cd '0-9\n' | sort -un; } ts_targets() { ts_json | grep -o ':[0-9]\{1,5\}":{"Handlers":{"/":{"Proxy":"http://\(127\.0\.0\.1\|localhost\):[0-9]\{1,5\}"' | sed 's/^:\([0-9]*\)".*:\([0-9]*\)"$/\1 \2/'; } # The serve entries this instance made are recorded on this machine (~/.config/kriya-one/.serve: "PORT HTTPS") # when it makes them: only those are ever reused or removed. An entry made by anyone else, even one that proxies to # this instance's port (a stale one from before), is never touched. No record (made before 0.4.2): its P+10000 and # P+10100 entries, when they proxy to its ports, are its own (what 0.4.1 made). REC="${XDG_CONFIG_HOME:-$HOME/.config}/kriya-one/$NAME.serve" rec_https() { [ -f "$REC" ] && awk -v p="$PORT" '$1 == p { print $2; exit }' "$REC" 2>/dev/null || true; } # target_of H: the local port tailscale serve proxies H to, else nothing. target_of() { ts_targets | awk -v h="$1" '$1 == h { print $2; exit }'; } # own_pair: this instance's panel HTTPS port when both entries (H → PORT, H+100 → CPORT) are its own and there. own_pair() { _r=$(rec_https) if [ -z "$_r" ] && [ ! -f "$REC" ]; then _r=$HPORT; fi [ -n "$_r" ] && [ "$(target_of "$_r")" = "$PORT" ] && [ "$(target_of $((_r + 100)))" = "$CPORT" ] && echo "$_r" } listening() { { command -v ss >/dev/null && ss -ltn 2>/dev/null | grep -q "[:.]$1 "; } || { command -v lsof >/dev/null && lsof -iTCP:"$1" -sTCP:LISTEN >/dev/null 2>&1; }; } # free_https H: nothing serves H on the tailnet and nothing listens on it here. free_https() { case "$TAKEN" in *" $1 "*) return 1 ;; esac ! listening "$1" } # The panel's HTTPS port: the pair this instance has recorded (and still there), else P+OFFSET if free, else the # first free pair from 18000 (KRIYA_HTTPS_OFFSET given: that one or nothing). pick_https() { TAKEN=" $(ts_ports | tr '\n' ' ') " _r=$(rec_https) if [ -n "$_r" ] && [ "$(target_of "$_r")" = "$PORT" ] && [ "$(target_of $((_r + 100)))" = "$CPORT" ]; then echo "$_r"; return 0; fi # Made by 0.4.1 (no record): its pair stays its address. if [ ! -f "$REC" ] && _l=$(own_pair) && [ -n "$_l" ]; then echo "$_l"; return 0; fi if free_https "$HPORT" && free_https "$HCPORT"; then echo "$HPORT"; return 0; fi [ -z "$OFFSET_SET" ] || return 1 _h=18000 while [ "$_h" -le 19899 ]; do if free_https "$_h" && free_https $((_h + 100)); then echo "$_h"; return 0; fi _h=$((_h + 1)) done return 1 } # unserve_ours [KEEP]: remove this instance's own pair (recorded, or the 0.4.1 one), unless it is KEEP; forget it. unserve_ours() { _o=$(own_pair) || true if [ -n "$_o" ] && [ "$_o" != "${1:-}" ]; then for h in "$_o" $((_o + 100)); do "$TS" serve --yes --https="$h" off >/dev/null 2>&1 && echo "removed tailscale serve :$h → 127.0.0.1:$(if [ "$h" = "$_o" ]; then echo "$PORT"; else echo "$CPORT"; fi)" || echo "couldn't remove tailscale serve :$h" >&2 done fi [ -n "${1:-}" ] || rm -f "$REC" } plan_https() { cli plan | sed -n 's/.*"https":{"mode":"\([a-z]*\)","host":"\([^"]*\)".*/\1 \2/p'; } # This machine's tailnet name, or nothing (Tailscale missing, stopped or signed out). ts_host() { out=$("$TS" status --self --json 2>/dev/null) || return 0 printf '%s' "$out" | grep -q '"BackendState": *"Running"' || return 0 printf '%s' "$out" | tr -d '\n' | sed -n 's/.*"Self": *{[^}]*"DNSName": *"\([^"]*\)".*/\1/p' | sed 's/\.$//' } # A JSON string (the few characters a host name or an error can carry). js() { printf '"%s"' "$(printf '%s' "$1" | tr -d '\000-\037' | sed 's/[\\"]/\\&/g' | cut -c1-300)"; } case "$WHAT" in funnel) FPATH="/kriya-$NAME/hooks" case "$ARG" in on) H=$(ts_host) if [ -z "$H" ]; then printf '{"error":%s}' "$(js "Tailscale is not running or not signed in on this machine")" | cli report funnel >/dev/null die "Tailscale is not running or not signed in here (tailscale up), or KRIYA_TAILSCALE doesn't point at its CLI" fi # Only the webhook path, on the Funnel port: the panel itself is never public. if ! out=$("$TS" funnel --yes --bg --https="$FPORT" --set-path="$FPATH" "http://127.0.0.1:$PORT/hooks" 2>&1); then printf '{"error":%s}' "$(js "$out")" | cli report funnel >/dev/null die "tailscale funnel failed: $out" fi URL="https://$H"; [ "$FPORT" = 443 ] || URL="$URL:$FPORT"; URL="$URL$FPATH" printf '{"url":%s,"port":%s}' "$(js "$URL")" "$FPORT" | cli report funnel >/dev/null say "Webhooks are public at $URL/" ;; off) "$TS" funnel --yes --https="$FPORT" --set-path="$FPATH" off >/dev/null 2>&1 || true printf 'null' | cli report funnel >/dev/null say "Webhooks are no longer public through Funnel." ;; *) die "funnel on|off" ;; esac ;; https) # shellcheck disable=SC2046 # plan_https prints "MODE HOST": split on purpose set -- $(plan_https) MODE="${1:-off}" HOST="${2:-}" [ "$ARG" = off ] && MODE=off [ "$ARG" = on ] || [ "$ARG" = off ] || die "https on|off" # Whatever was on before goes first: our own serve entries, our own Caddy. if [ "$MODE" != tailscale ] && command -v "$TS" >/dev/null 2>&1; then unserve_ours; fi if [ "$MODE" != local ] && [ "$MODE" != domain ]; then $DOCKER rm -f "$CC" >/dev/null 2>&1 || true; fi case "$MODE" in off) printf 'null' | cli report tailscale >/dev/null printf 'null' | cli report caddy >/dev/null say "HTTPS for other devices is off: Kriya One is on http://localhost:$PORT only." ;; tailscale) H=$(ts_host) if [ -z "$H" ]; then printf '{"error":%s}' "$(js "Tailscale is not running or not signed in on this machine")" | cli report tailscale >/dev/null die "Tailscale is not running or not signed in here (tailscale up)" fi HP=$(pick_https) || { msg="no free HTTPS port pair for tailscale serve"; [ -z "$OFFSET_SET" ] || msg="port $HPORT or $HCPORT (KRIYA_HTTPS_OFFSET=$OFFSET) is served for something else already" printf '{"error":%s}' "$(js "$msg")" | cli report tailscale >/dev/null die "$msg: nothing was changed (tailscale serve status shows what is there)" } [ "$HP" = "$HPORT" ] || echo "tailscale serve: $HPORT or $HCPORT is in use for something else; this one uses $HP and $((HP + 100))" HPORT=$HP HCPORT=$((HP + 100)) # A pair of ours elsewhere (an older choice) goes first: one address per instance. unserve_ours "$HPORT" for pair in "$HPORT:$PORT" "$HCPORT:$CPORT"; do if ! out=$("$TS" serve --yes --bg --https="${pair%%:*}" "http://127.0.0.1:${pair#*:}" 2>&1); then printf '{"error":%s}' "$(js "$out")" | cli report tailscale >/dev/null die "tailscale serve failed: $out (HTTPS certificates must be on for the tailnet; tailscale set --operator=\$USER may be needed)" fi done mkdir -p "$(dirname "$REC")" && printf '%s %s\n' "$PORT" "$HPORT" >"$REC" printf '{"host":%s,"port":%s}' "$(js "$H")" "$HPORT" | cli report tailscale >/dev/null [ -z "${KRIYA_REACH_OUT:-}" ] || printf 'url=https://%s:%s\n' "$H" "$HPORT" >"$KRIYA_REACH_OUT" say "Kriya One on your tailnet: https://$H:$HPORT" ;; local|domain) [ -n "$HOST" ] || die "no address set: choose one in Kriya One settings → Other devices" if [ "$MODE" = local ]; then IMG="${KRIYA_CADDY_IMAGE:-caddy:2.11.7-alpine}"; else IMG="${KRIYA_CADDY_IMAGE:-ghcr.io/trueskillacademy/kriya-caddy:latest}"; fi DATA=$($DOCKER inspect -f '{{range .Mounts}}{{if eq .Destination "/agent"}}{{.Source}}{{end}}{{end}}' "$C") [ -n "$DATA" ] || die "$C has no /agent folder mounted" # Caddy runs as Kriya One's server user, on the server's own folder: the CA key and the Caddyfile (with any DNS # token) stay readable by that user only, and the root it makes is the one the Trust page can trust. U=$($DOCKER exec "$C" id -u upa) G=$($DOCKER exec "$C" id -g upa) $DOCKER exec -u upa "$C" test -f /agent/.one/https/Caddyfile || die "no Caddyfile yet: save the choice in Kriya One settings → Other devices" for p in "$HPORT" "$HCPORT" "$TPORT"; do if ! $DOCKER container inspect "$CC" >/dev/null 2>&1 && { (command -v ss >/dev/null && ss -ltn 2>/dev/null | grep -q ":$p ") || (command -v lsof >/dev/null && lsof -iTCP:"$p" -sTCP:LISTEN >/dev/null 2>&1); }; then die "port $p is taken (KRIYA_HTTPS_OFFSET picks others)" fi done $DOCKER rm -f "$CC" >/dev/null 2>&1 || true # Host networking: Caddy reaches the panel on 127.0.0.1 and serves the LAN on the HTTPS ports. $DOCKER run -d --name "$CC" --restart unless-stopped --network host --user "$U:$G" \ -e XDG_DATA_HOME=/kriya/data -e XDG_CONFIG_HOME=/kriya/config \ -e KRIYA_PANEL="127.0.0.1:$PORT" -e KRIYA_CANVAS="127.0.0.1:$CPORT" \ -v "$DATA/.one/https:/kriya" --entrypoint caddy "$IMG" run --config /kriya/Caddyfile --adapter caddyfile --watch >/dev/null || die "couldn't start Caddy ($IMG)" # Check it: the HTTPS port answers as this Kriya One, with a chain to its root (local CA) or a public one. tmp=$(mktemp); trap 'rm -f "$tmp"' EXIT ok=0 err="no answer on https port $HPORT" i=0 while [ $i -lt 60 ]; do i=$((i + 1)); sleep 1 if [ "$MODE" = local ]; then $DOCKER exec -u upa "$C" cat /agent/.one/https/data/caddy/pki/authorities/local/root.crt > "$tmp" 2>/dev/null || continue if out=$(curl -fsS --max-time 5 --cacert "$tmp" --resolve "$HOST:$HPORT:127.0.0.1" "https://$HOST:$HPORT/healthz" 2>&1); then ok=1; break; else err="$out"; fi else if out=$(curl -fsS --max-time 5 --resolve "$HOST:$HPORT:127.0.0.1" "https://$HOST:$HPORT/healthz" 2>&1); then ok=1; break; else err="$out"; fi fi done if [ $ok = 1 ]; then printf '{"ok":true,"image":%s}' "$(js "$IMG")" | cli report caddy >/dev/null [ -z "${KRIYA_REACH_OUT:-}" ] || printf 'url=https://%s:%s\n' "$HOST" "$HPORT" >"$KRIYA_REACH_OUT" say "Kriya One on your network: https://$HOST:$HPORT" [ "$MODE" = local ] && say "Each device trusts it once: http://$HOST:$TPORT/trust (also in Kriya One settings → Other devices)" else printf '{"ok":false,"error":%s}' "$(js "$err")" | cli report caddy >/dev/null die "Caddy didn't answer as expected: $err ($DOCKER logs $CC)" fi ;; *) die "unknown HTTPS mode $MODE" ;; esac ;; status) say "$C (panel on 127.0.0.1:$PORT)" cli plan ;; *) die "unknown command $WHAT (funnel on|off, https on|off, status)" ;; esac