82 lines
5.2 KiB
Bash
82 lines
5.2 KiB
Bash
#!/bin/bash
|
|
# Boots the desktop: X server, window manager, VNC and the noVNC web viewer.
|
|
set -u
|
|
mkdir -p /home/bot/.fluxbox /home/bot/Desktop && cp -n /etc/anybot/fluxbox-init /home/bot/.fluxbox/init 2>/dev/null
|
|
cp -n /etc/anybot/overlay /home/bot/.fluxbox/overlay 2>/dev/null
|
|
cp -n /etc/anybot/Xresources /home/bot/.Xresources 2>/dev/null
|
|
cp /etc/anybot/menu /home/bot/.fluxbox/menu 2>/dev/null
|
|
# GTK's dialogs (a web page's 上傳檔案 in Chromium, e.g. Threads' 加入相片) with 取消 and 開啟 in a
|
|
# bar at the top, as on GNOME. Without GNOME's settings service GTK put them at the bottom of a
|
|
# file window taller than the screen, under the taskbar: a picture could be found but never sent.
|
|
GTKINI=/home/bot/.config/gtk-3.0/settings.ini
|
|
mkdir -p "${GTKINI%/*}"
|
|
grep -q '^\[Settings\]' "$GTKINI" 2>/dev/null || printf '[Settings]\n' >> "$GTKINI"
|
|
grep -q '^gtk-dialogs-use-header' "$GTKINI" || sed -i '/^\[Settings\]/a gtk-dialogs-use-header=true' "$GTKINI"
|
|
chown -R bot:bot /home/bot
|
|
rm -f /tmp/.X1-lock /tmp/.X11-unix/X1
|
|
# One TigerVNC server is the screen and the VNC server (was Xvfb + x11vnc, which polls the
|
|
# screen for changes). VNC only on 127.0.0.1 inside the box, with the box's VNC password when the
|
|
# app gave one (VNC_PASSWORD), as before.
|
|
GEOM="${SCREEN_SIZE%x*}"
|
|
if [ -n "${VNC_PASSWORD:-}" ]; then
|
|
mkdir -p /home/bot/.vnc
|
|
printf '%s\n' "$VNC_PASSWORD" | vncpasswd -f > /home/bot/.vnc/passwd && chmod 600 /home/bot/.vnc/passwd
|
|
# (The bot user opens the bots' own screens with the same password: anybot-screen.)
|
|
chown bot:bot /home/bot/.vnc /home/bot/.vnc/passwd
|
|
VNC_SEC="-SecurityTypes VncAuth -rfbauth /home/bot/.vnc/passwd"
|
|
else
|
|
VNC_SEC="-SecurityTypes None"
|
|
fi
|
|
Xtigervnc :1 -geometry "$GEOM" -depth 24 -rfbport 5900 -localhost $VNC_SEC -AlwaysShared -AcceptCutText -SendCutText \
|
|
-UseBlacklist 0 -IdleTimeout 0 -MaxIdleTime 0 -MaxDisconnectionTime 0 -MaxConnectionTime 0 -s 0 -dpms -desktop "Bot 的電腦" -ac -noreset \
|
|
>/tmp/xvnc.log 2>&1 &
|
|
XVFB_PID=$!
|
|
for i in $(seq 1 50); do xdpyinfo -display :1 >/dev/null 2>&1 && break; sleep 0.1; done
|
|
# The session bus and the accessibility registry; tools started later over `docker exec`
|
|
# (anybot-a11y, anybot-browser) join this bus through /tmp/anybot-dbus.env.
|
|
su bot -c 'cd /home/bot && dbus-launch --sh-syntax > /tmp/anybot-dbus.sh' && . /tmp/anybot-dbus.sh
|
|
printf 'DBUS_SESSION_BUS_ADDRESS=%s\n' "$DBUS_SESSION_BUS_ADDRESS" > /tmp/anybot-dbus.env
|
|
chown bot:bot /tmp/anybot-dbus.env /tmp/anybot-dbus.sh 2>/dev/null || true
|
|
chmod 600 /tmp/anybot-dbus.env /tmp/anybot-dbus.sh
|
|
export DBUS_SESSION_BUS_ADDRESS
|
|
for c in /usr/libexec/at-spi-bus-launcher /usr/lib/at-spi2-core/at-spi-bus-launcher; do
|
|
[ -x "$c" ] && { su bot -c "DISPLAY=:1 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS $c --launch-immediately >/tmp/atspi-bus.log 2>&1 &"; break; }
|
|
done
|
|
for c in /usr/libexec/at-spi2-registryd /usr/lib/at-spi2-core/at-spi2-registryd; do
|
|
[ -x "$c" ] && { su bot -c "DISPLAY=:1 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS $c >/tmp/atspi-registry.log 2>&1 &"; break; }
|
|
done
|
|
su bot -c "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS gsettings set org.gnome.desktop.interface toolkit-accessibility true" 2>/dev/null || true
|
|
# The file window opens smaller than the screen, whatever size it was left at.
|
|
SW="${GEOM%x*}"; SH="${GEOM#*x}"
|
|
su bot -c "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS gsettings set org.gtk.Settings.FileChooser window-size '($((SW * 7 / 10)), $((SH * 3 / 4)))'" 2>/dev/null || true
|
|
su bot -c "DISPLAY=:1 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS xrdb -nocpp -merge /home/bot/.Xresources"
|
|
su bot -c "DISPLAY=:1 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS fluxbox >/tmp/fluxbox.log 2>&1 &"
|
|
# Copy and paste between the person's computer and this one: Xvnc hands the viewer's clipboard to
|
|
# the X selections (and back) only while vncconfig runs (after LazyBoy2's box).
|
|
su bot -c "DISPLAY=:1 vncconfig -nowin >/tmp/vncconfig.log 2>&1 &"
|
|
(sleep 1.5; xsetroot -display :1 -solid '#2b2f36') &
|
|
xsetroot -display :1 -solid '#2b2f36'
|
|
# One web viewer for every screen: the shared one is "main"; anybot-screen adds each bot's own
|
|
# (每個 Bot 一個螢幕). A viewer picks one with ?path=websockify%3Ftoken%3D<key>.
|
|
rm -rf /tmp/anybot-screens; mkdir -p /tmp/anybot-screens && chmod 1777 /tmp/anybot-screens
|
|
echo "main: localhost:5900" > /tmp/anybot-vnc-tokens && chown bot:bot /tmp/anybot-vnc-tokens
|
|
# (A ping every 30 s: a proxy on the way — Tailscale serve, Caddy — doesn't drop a viewer that is
|
|
# only watching a still screen.)
|
|
websockify --heartbeat=30 --web /usr/share/novnc --token-plugin TokenFile --token-source /tmp/anybot-vnc-tokens 6080 >/tmp/novnc.log 2>&1 &
|
|
WEBSOCKIFY_PID=$!
|
|
# Say ready only once the viewer's port listens: the app opens noVNC on this flag, and a too
|
|
# early file meant the first viewer load 404'd.
|
|
(
|
|
for i in $(seq 1 100); do
|
|
python3 -c "import socket; socket.create_connection(('127.0.0.1', 6080), 0.5)" 2>/dev/null && break
|
|
sleep 0.2
|
|
done
|
|
echo "anybot-box ready" > /tmp/anybot-box.ready
|
|
) &
|
|
# When the desktop's heart stops, the container ends too (it used to sit there as Running
|
|
# with a dead X server, and every screen action failed forever).
|
|
wait $XVFB_PID
|
|
kill $WEBSOCKIFY_PID 2>/dev/null
|
|
pkill -u bot fluxbox 2>/dev/null
|
|
exit 1
|