#!/usr/bin/env bash
# Starts the on-demand remote desktop (idempotent) and mints one connection ticket. Prints one line:
# {"schemaVersion":2,"port":6080,"display":":1","path":"vnc.html","ticket":"<43 chars>","expiresInMs":60000}
# Xvnc (X server + VNC in one) listens on a 0600 unix socket only, with no VNC auth: only this uid
# can open it. orca-desktop-proxy (websockify + a ticket gate) serves noVNC on 127.0.0.1 and
# bridges WebSockets to that socket. Nothing is reachable from outside the VM: the desktop app
# reaches it through Orca's port forward, a localhost port on the user's computer that any local
# process or web page can reach, so each WebSocket needs a ticket from here: 32 random bytes,
# single use, valid for 60 s, stored only as its sha256 (the file name; its content is the expiry).
# The ticket goes to stdout only (the control plane hands it to the desktop app). Never started at boot.
set -euo pipefail
umask 077
DESKTOP=/opt/orca/desktop
STATE="$HOME/.orca-desktop"
PORT="${ORCA_DESKTOP_PORT:-6080}"
DISPLAY_NUM="${ORCA_DESKTOP_DISPLAY:-1}"
GEOMETRY="${ORCA_DESKTOP_GEOMETRY:-1280x800}"
SOCKET="$STATE/vnc.sock"
# The parent's DISPLAY must not leak into the WM/xterm below; they use ours.
export DISPLAY=":$DISPLAY_NUM"

# Exit 3 = this image has no desktop (built before it, or its optional install failed).
fail() { printf 'orca-desktop: %s\n' "$1" >&2; exit "${2:-1}"; }
case "$PORT" in '' | *[!0-9]*) fail 'ORCA_DESKTOP_PORT must be a number' ;; esac
case "$DISPLAY_NUM" in '' | *[!0-9]*) fail 'ORCA_DESKTOP_DISPLAY must be a number' ;; esac
case "$GEOMETRY" in [0-9]*x[0-9]*) ;; *) fail 'ORCA_DESKTOP_GEOMETRY must look like 1280x800' ;; esac
command -v Xvnc >/dev/null || fail 'not installed in this image (Xvnc missing)' 3
[ -s "$DESKTOP/novnc/vnc.html" ] || fail 'not installed in this image (noVNC missing)' 3
PROXY="$(dirname "$(readlink -f "$0")")/orca-desktop-proxy"
[ -x "$PROXY" ] || fail 'orca-desktop-proxy missing next to this script'
TICKETS="$STATE/tickets"
TICKET_TTL_S=60
# Xvnc's mode: a previous version of this script ran it with VncAuth; restart it in this mode.
MODE=ticket-v1

mkdir -p "$STATE"
exec 9>"$STATE/start.lock"
flock -w 30 9 || fail 'another start is in progress'

pid_of() { # never "0": kill 0 targets our own process group
  local pid
  pid="$(cat "$STATE/$1.pid" 2>/dev/null || true)"
  case "$pid" in '' | 0 | *[!0-9]*) return 1 ;; esac
  printf '%s' "$pid"
}
alive() { local pid; pid="$(pid_of "$1")" && kill -0 "$pid" 2>/dev/null; }
mint_ticket() { # prints the ticket; stores only its sha256, with the expiry as content
  local ticket now digest
  mkdir -p "$TICKETS"
  now="$(date +%s)"
  # Expired or spent-but-unswept tickets; keep at most 16 live ones (oldest go first).
  for file in "$TICKETS"/*; do
    [ -f "$file" ] || continue
    [ "$(cat "$file" 2>/dev/null || echo 0)" -ge "$now" ] 2>/dev/null || rm -f "$file"
  done
  # shellcheck disable=SC2012 # names are hex digests
  ls -1t "$TICKETS" 2>/dev/null | tail -n +16 | while read -r old; do rm -f "$TICKETS/$old"; done
  ticket="$(head -c 32 /dev/urandom | base64 | tr '+/' '-_' | tr -d '=\n')"
  [ "${#ticket}" = 43 ] || fail 'could not generate a ticket'
  digest="$(printf '%s' "$ticket" | sha256sum | cut -d' ' -f1)"
  printf '%s' "$((now + TICKET_TTL_S))" >"$TICKETS/.$digest" && mv "$TICKETS/.$digest" "$TICKETS/$digest"
  printf '%s' "$ticket"
}
ready_line() {
  local ticket
  ticket="$(mint_ticket)" || exit 1
  printf '{"schemaVersion":2,"port":%s,"display":":%s","path":"vnc.html","ticket":"%s","expiresInMs":%s}\n' \
    "$PORT" "$DISPLAY_NUM" "$ticket" "$((TICKET_TTL_S * 1000))"
}
listening() { # the proxy answers HTTP on loopback
  curl -fsS -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/vnc.html" 2>/dev/null
}

# Pids from a previous boot (resume = reboot from disk) name dead or unrelated processes.
boot_id="$(cat /proc/sys/kernel/random/boot_id)"
if [ "$(cat "$STATE/boot-id" 2>/dev/null || true)" != "$boot_id" ]; then
  rm -rf "$STATE"/*.pid "$SOCKET" "$STATE/password" "$STATE/passwd" "$STATE/mode" "$TICKETS"
  printf '%s' "$boot_id" >"$STATE/boot-id"
fi

# Same boot, older script: its websockify had no ticket gate and its Xvnc wanted a VNC password.
rm -f "$STATE/password" "$STATE/passwd"
if alive websockify; then kill "$(pid_of websockify)" 2>/dev/null || true; fi
rm -f "$STATE/websockify.pid"
current_mode() { cat "$STATE/mode" 2>/dev/null || true; }

if alive xvnc && alive proxy && listening && [ "$(current_mode)" = "$MODE" ]; then
  ready_line
  exit 0
fi

start() { # name cmd... — detached, logs in $STATE/<name>.log
  local name=$1; shift
  # 9>&-: children must not inherit the start lock, or the next call waits for them to exit.
  setsid nohup "$@" >"$STATE/$name.log" 2>&1 </dev/null 9>&- &
  echo $! >"$STATE/$name.pid"
}

if ! alive xvnc || [ "$(current_mode)" != "$MODE" ]; then
  if alive xvnc; then kill "$(pid_of xvnc)" 2>/dev/null || true; sleep 0.2; fi
  rm -f "$SOCKET"
  # -rfbport -1: no TCP VNC port at all, only a 0600 unix socket (in the 0700 state dir) for the
  # proxy. SecurityTypes None is safe only because of that: the ticket gate is in the proxy.
  # SendPrimary=0: only explicit copies (xterm copies its selection, below) leave the sandbox.
  # The X display itself takes a cookie from ~/.Xauthority (Xlib's default for this user), so only
  # this uid's processes (agents, apps they start on DISPLAY=:1) can open it.
  auth=()
  if command -v xauth >/dev/null && command -v mcookie >/dev/null; then
    xauth -q -f "$HOME/.Xauthority" add ":$DISPLAY_NUM" . "$(mcookie)" 9>&- && auth=(-auth "$HOME/.Xauthority")
  fi
  start xvnc Xvnc ":$DISPLAY_NUM" "${auth[@]}" -geometry "$GEOMETRY" -depth 24 -nolisten tcp \
    -rfbport -1 -rfbunixpath "$SOCKET" -rfbunixmode 0600 \
    -SecurityTypes None -SendPrimary=0 \
    -AlwaysShared -AcceptSetDesktopSize -desktop 'Remote desktop'
  printf '%s' "$MODE" >"$STATE/mode"
  for _ in $(seq 1 100); do
    [ -S "$SOCKET" ] && break
    alive xvnc || { tail -n 20 "$STATE/xvnc.log" >&2; fail 'Xvnc exited'; }
    sleep 0.05
  done
  [ -S "$SOCKET" ] || fail 'Xvnc did not create its socket'
  if command -v xsetroot >/dev/null; then xsetroot -solid '#1e1f22' 9>&- || true; fi
  if command -v openbox >/dev/null; then
    # Dark, flat theme to match Orca; the first terminal stays maximized as the tab resizes the screen.
    printf '%s\n' '<?xml version="1.0"?>' \
      '<openbox_config xmlns="http://openbox.org/3.4/rc"><theme><name>Onyx</name></theme>' \
      '<applications><application class="XTerm"><maximized>yes</maximized></application></applications>' \
      '</openbox_config>' >"$STATE/openbox.xml"
    start wm openbox --config-file "$STATE/openbox.xml"
    # Let it manage the screen before the terminal maps, so its maximize rule applies.
    sleep 0.2
  elif command -v twm >/dev/null; then
    start wm twm
  fi
  if command -v xterm >/dev/null; then
    # selectToClipboard: a selection is a copy (to CLIPBOARD, which reaches this computer).
    start xterm xterm -maximized -bg '#1e1f22' -fg '#d4d4d4' -fa 'Liberation Mono,Monospace' -fs 11 \
      -xrm 'XTerm*selectToClipboard: true'
  fi
fi

if ! alive proxy || ! listening; then
  if alive proxy; then kill "$(pid_of proxy)" 2>/dev/null || true; sleep 0.1; fi
  mkdir -p "$TICKETS"
  PYTHONPATH="$DESKTOP/websockify" start proxy python3 -P -W ignore::UserWarning "$PROXY" \
    --listen "$PORT" --web "$DESKTOP/novnc" --unix-target "$SOCKET" --tickets "$TICKETS"
  for _ in $(seq 1 100); do
    listening && break
    alive proxy || { tail -n 20 "$STATE/proxy.log" >&2; fail 'orca-desktop-proxy exited'; }
    sleep 0.05
  done
  listening || fail "orca-desktop-proxy is not answering on 127.0.0.1:$PORT"
fi
ready_line
