AnyBot/box/browser

87 lines
5.2 KiB
Bash

#!/bin/bash
# The one way to start the box's browser (adapted from XBot's box/box-chrome): the same profile
# and DevTools port every time, so AnyBot's browser tools and the person in the viewer use the
# same window and logins; web pages exposed to accessibility, so their links and buttons get
# numbers on the bot's picture. A second start only hands its address to the running one.
set -euo pipefail
export DISPLAY="${DISPLAY:-:1}" HOME=/home/bot
export GTK_MODULES=atk-bridge GTK_A11Y=atspi GNOME_ACCESSIBILITY=1 NO_AT_BRIDGE=0
# Chinese menus and pages asked for in Chinese first (as on LazyBoy2's box).
export LANG="${LANG:-zh_TW.UTF-8}" LANGUAGE="${LANGUAGE:-zh_TW:zh:en}"
if [[ -z "${DBUS_SESSION_BUS_ADDRESS:-}" && -r /tmp/anybot-dbus.env ]]; then
# shellcheck disable=SC1091
source /tmp/anybot-dbus.env
export DBUS_SESSION_BUS_ADDRESS
fi
# One profile. The box used to have two: Chromium's own (~/.config/chromium, what its menu entry
# opened, where a person logged in by hand) and AnyBot's (~/.config/anybot-browser, what the
# browser tools opened), so a site could be logged in in one and not the other. Chromium's own
# is the one now (the other stays as it was); a box that only has AnyBot's keeps that.
PROFILE=/home/bot/.config/anybot-profile
PORT=9222
LOCK=/tmp/.anybot-browser.lock
LOG=/tmp/anybot-browser.log
# 每個 Bot 一個螢幕 (anybot-screen): ANYBOT_SCREEN=<key> starts that bot's own browser on its own
# screen, with a profile of its own and its own DevTools port. The first time, the profile starts
# as a copy of the shared one, so the sites logged in there are logged in here too (the box keeps
# its cookies with --password-store=basic, readable by any profile of the same user). A 支線's
# temporary screen copies its main bot's instead (the logins that bot has now).
if [[ -n "${ANYBOT_SCREEN:-}" ]]; then
[[ "$ANYBOT_SCREEN" =~ ^[A-Za-z0-9_-]{1,48}$ ]] || { echo "bad screen" >&2; exit 2; }
ENV=/tmp/anybot-screens/$ANYBOT_SCREEN/env
[[ -r "$ENV" ]] || { echo "no screen $ANYBOT_SCREEN: anybot-screen up first" >&2; exit 2; }
# shellcheck disable=SC1090
set -a; source "$ENV"; set +a
PORT=$CDP_PORT
LOCK=/tmp/.anybot-browser-$ANYBOT_SCREEN.lock
LOG=/tmp/anybot-browser-$ANYBOT_SCREEN.log
if [[ ! -d "$PROFILE/Default" ]]; then
SHARED=$(readlink -f /home/bot/.config/anybot-profile 2>/dev/null || true)
# A 支線's temporary screen (<key>-t<n>) starts from its main bot's browser when it has one.
if [[ "$ANYBOT_SCREEN" =~ ^(.+)-t[0-9]+$ && -d "/home/bot/.config/anybot-profiles/${BASH_REMATCH[1]}/Default" ]]; then
SHARED=/home/bot/.config/anybot-profiles/${BASH_REMATCH[1]}
fi
mkdir -p "$PROFILE/Default"
if [[ -n "$SHARED" && -d "$SHARED/Default" ]]; then
cp -a "$SHARED/Local State" "$PROFILE/" 2>/dev/null || true
for f in Cookies Cookies-journal "Login Data" "Login Data-journal" "Web Data" "Web Data-journal" Preferences "Secure Preferences" "Local Storage" "Session Storage" IndexedDB; do
[[ -e "$SHARED/Default/$f" ]] && cp -a "$SHARED/Default/$f" "$PROFILE/Default/" 2>/dev/null || true
done
fi
fi
fi
if [[ -z "${ANYBOT_SCREEN:-}" && ! -e "$PROFILE" ]]; then
if [[ -d /home/bot/.config/chromium/Default || ! -d /home/bot/.config/anybot-browser/Default ]]; then
mkdir -p /home/bot/.config/chromium && ln -s /home/bot/.config/chromium "$PROFILE"
else
ln -s /home/bot/.config/anybot-browser "$PROFILE"
fi
fi
# The box (a container) is the sandbox by default, so Chromium runs --no-sandbox. Under gVisor
# (容器隔離=runsc) the box has a virtualized kernel of its own and AnyBot starts it with
# ANYBOT_BOX_SANDBOX=1 — Chromium's own sandbox works there, so keep it as a second wall.
SANDBOX=(--no-sandbox --test-type)
[ "${ANYBOT_BOX_SANDBOX:-0}" = 1 ] && SANDBOX=()
FLAGS=("${SANDBOX[@]}" --user-data-dir="$PROFILE" --remote-debugging-address=127.0.0.1 --remote-debugging-port=$PORT
--remote-allow-origins=http://127.0.0.1 --force-renderer-accessibility --disable-blink-features=AutomationControlled
--disable-dev-shm-usage --disable-gpu --use-angle=swiftshader --enable-unsafe-swiftshader --password-store=basic
--no-first-run --no-default-browser-check --disable-session-crashed-bubble --hide-crash-restore-bubble
--disable-features=TranslateUI --start-maximized --lang=zh-TW --accept-lang=zh-TW,zh,en-US,en)
# Two starts in the same second (the bot and the person) would both find no browser and fight
# over the profile ("in use"): one at a time until the first is listening.
exec 9>"$LOCK"
flock -w 30 9 || true
if pgrep -u bot -f -- "--user-data-dir=$PROFILE" >/dev/null || pgrep -u bot -f -- "--user-data-dir=$(readlink -f "$PROFILE")" >/dev/null; then
flock -u 9
exec chromium "${FLAGS[@]}" "$@" 9>&-
fi
# A profile from before this box (a new container) still names the old one in its lock.
rm -f "$PROFILE"/SingletonLock "$PROFILE"/SingletonSocket "$PROFILE"/SingletonCookie
# (The lock's fd is not handed to the browser: it would hold the lock for its whole life, and the
# next start would wait on it.)
setsid chromium "${FLAGS[@]}" "${@:-about:blank}" >"$LOG" 2>&1 < /dev/null 9>&- &
for _ in $(seq 1 100); do
python3 -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:$PORT/json/version', timeout=1).close()" 2>/dev/null && break
sleep 0.25
done