Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save diyism/d00c6bbc08b1a55208c6731da61a6277 to your computer and use it in GitHub Desktop.

Select an option

Save diyism/d00c6bbc08b1a55208c6731da61a6277 to your computer and use it in GitHub Desktop.
muse ai container_sshdog_cloudflared access tcp_terminal browser
$ ssh -t -p 2222 any@localhost bash -i
root@htch-runtime:/root# cat ./terminal-browser.sh
#!/bin/bash
set -u
APP=/home/hatch/.local/share/terminal-browser/app
CLI=/home/hatch/.local/bin/terminal-browser
LOG=/tmp/terminal-browser-daemon.log
PIDFILE=/tmp/terminal-browser-daemon.pid
URL="${1:-https://myip.ipip.net}"
# The manually started daemon and the CLI wrapper must use the same
# distribution root, otherwise they compute different Unix socket paths.
export TERMINAL_BROWSER_DIST_ROOT="$APP"
export ELECTRON_DISABLE_SANDBOX=1
export TERMINAL_BROWSER_DISABLE_GPU=1
export PATH=/home/hatch/.local/bin:$PATH
# The SSHDog connection is a PTY, but the background CLI process is no longer
# detected as interactive. Force terminal-browser's direct embedded-session
# path and preserve the actual PTY instead of asking it to split a WezTerm pane.
if [ -z "${PIXEL_TTY:-}" ]; then
current_tty=$(tty 2>/dev/null || true)
case "$current_tty" in
/dev/*) export PIXEL_TTY=/dev/tty ;;
esac
fi
if [ "$URL" = "stop" ]; then
"$CLI" shutdown 2>/dev/null || true
if [ -f "$PIDFILE" ]; then
daemon_pid=$(cat "$PIDFILE" 2>/dev/null || true)
[ -n "$daemon_pid" ] && kill "$daemon_pid" 2>/dev/null || true
rm -f "$PIDFILE"
fi
echo "terminal-browser daemon stopped"
exit 0
fi
pid=""
if [ -f "$PIDFILE" ]; then
candidate=$(cat "$PIDFILE" 2>/dev/null || true)
if [ -n "$candidate" ] && kill -0 "$candidate" 2>/dev/null; then
pid="$candidate"
fi
fi
if [ -z "$pid" ]; then
nohup env \
TERMINAL_BROWSER_DIST_ROOT="$APP" \
ELECTRON_DISABLE_SANDBOX=1 \
TERMINAL_BROWSER_DISABLE_GPU=1 \
"$APP/electron/pixel" "$APP/browser/dist/main.js" \
--daemon --ozone-platform=headless '--screen-info={8192x8192}' \
>"$LOG" 2>&1 &
pid=$!
echo "$pid" >"$PIDFILE"
sleep 2
fi
if ! kill -0 "$pid" 2>/dev/null; then
echo "daemon failed to start; log follows:" >&2
sed -n '1,160p' "$LOG" 2>/dev/null || true
exit 1
fi
echo "daemon pid: $pid"
echo "daemon log: $LOG"
echo "initial browser state:"
"$CLI" ls --json 2>&1 || true
echo "opening: $URL"
# Stay attached to the SSH PTY. The browser session needs this terminal for
# its native renderer; a detached/background process loses that renderer.
exec "$CLI" open "$URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment