Use native Chrome for Reddit login handoff

This commit is contained in:
jjbrunton committed 2026-08-08 16:16:11 +01:00
1 parent 2c533fa52a
commit dbb1842ef4
13 files changed
+324 -60

No files matched your search

+86 -26
View File
@@ -11,6 +11,7 @@ GEOMETRY="${SCREEN_GEOMETRY:-1280x800x24}"
BROWSER_SESSION_DIR="${BROWSER_SESSION_DIR:-/app/data/browser-session}"
CHROME_CDP_PORT="${CHROME_CDP_PORT:-9222}"
GOOGLE_CHROME_BIN="${GOOGLE_CHROME_BIN:-google-chrome}"
NATIVE_LOGIN_EXIT_CODE=75
export CHROME_CDP_URL="${CHROME_CDP_URL:-http://127.0.0.1:${CHROME_CDP_PORT}}"
export DISPLAY=":${DISPLAY_NUM}"
@@ -107,13 +108,46 @@ websockify --web "${NOVNC_WEB_DIR}" "${NOVNC_PORT}" "localhost:${VNC_PORT}" \
>/var/log/websockify.log 2>&1 &
NOVNC_PID=$!
# Start the installed consumer Google Chrome directly. Playwright attaches only
# after Chrome is running, so the human login is not performed in a browser
# process launched with Playwright's automation arguments.
CHROME_PID=""
CHROME_READY=0
if command -v "${GOOGLE_CHROME_BIN}" >/dev/null 2>&1; then
echo "[entrypoint] starting Google Chrome for interactive login"
BOT_PID=""
GATE_PID=""
clear_chrome_locks() {
rm -f \
"${BROWSER_SESSION_DIR}/SingletonLock" \
"${BROWSER_SESSION_DIR}/SingletonSocket" \
"${BROWSER_SESSION_DIR}/SingletonCookie" \
2>/dev/null || true
}
stop_chrome() {
if [ -n "${CHROME_PID}" ]; then
kill "${CHROME_PID}" 2>/dev/null || true
wait "${CHROME_PID}" 2>/dev/null || true
CHROME_PID=""
fi
clear_chrome_locks
}
start_native_login_chrome() {
unset CHROME_CDP_URL
echo "[entrypoint] starting native Google Chrome login with DevTools disabled"
"${GOOGLE_CHROME_BIN}" \
--user-data-dir="${BROWSER_SESSION_DIR}" \
--no-first-run \
--no-default-browser-check \
--disable-dev-shm-usage \
--no-sandbox \
--start-maximized \
https://www.reddit.com/login/ \
>/var/log/google-chrome-native.log 2>&1 &
CHROME_PID=$!
}
start_controlled_chrome() {
export CHROME_CDP_URL="http://127.0.0.1:${CHROME_CDP_PORT}"
local chrome_ready=0
echo "[entrypoint] starting Google Chrome for authenticated bot control"
"${GOOGLE_CHROME_BIN}" \
--user-data-dir="${BROWSER_SESSION_DIR}" \
--remote-debugging-address=127.0.0.1 \
@@ -133,37 +167,63 @@ if command -v "${GOOGLE_CHROME_BIN}" >/dev/null 2>&1; then
fi
if curl -fsS "${CHROME_CDP_URL}/json/version" >/dev/null 2>&1; then
echo "[entrypoint] Google Chrome is ready after ${i} checks"
CHROME_READY=1
chrome_ready=1
break
fi
sleep 0.2
done
fi
if [ "${CHROME_READY}" != "1" ]; then
echo "[entrypoint] WARNING: Google Chrome failed to start; using Playwright fallback" >&2
if [ -n "${CHROME_PID}" ]; then
kill "${CHROME_PID}" 2>/dev/null || true
wait "${CHROME_PID}" 2>/dev/null || true
if [ "${chrome_ready}" != "1" ]; then
echo "[entrypoint] WARNING: Google Chrome failed to start; using Playwright fallback" >&2
stop_chrome
unset CHROME_CDP_URL
fi
rm -f \
"${BROWSER_SESSION_DIR}/SingletonLock" \
"${BROWSER_SESSION_DIR}/SingletonSocket" \
"${BROWSER_SESSION_DIR}/SingletonCookie" \
2>/dev/null || true
CHROME_PID=""
unset CHROME_CDP_URL
fi
}
cleanup() {
echo "[entrypoint] shutting down"
if [ -n "${CHROME_PID}" ]; then
kill "${CHROME_PID}" 2>/dev/null || true
fi
[ -n "${BOT_PID}" ] && kill "${BOT_PID}" 2>/dev/null || true
[ -n "${GATE_PID}" ] && kill "${GATE_PID}" 2>/dev/null || true
stop_chrome
kill "${NOVNC_PID}" "${X11VNC_PID}" "${XVFB_PID}" 2>/dev/null || true
wait || true
}
trap cleanup TERM INT EXIT
echo "[entrypoint] launching bot"
exec node dist/index.js
if ! command -v "${GOOGLE_CHROME_BIN}" >/dev/null 2>&1; then
echo "[entrypoint] WARNING: Google Chrome is unavailable; using Playwright fallback" >&2
unset CHROME_CDP_URL
else
start_controlled_chrome
fi
while true; do
echo "[entrypoint] launching bot"
set +e
node dist/index.js &
BOT_PID=$!
wait "${BOT_PID}"
BOT_EXIT=$?
BOT_PID=""
set -e
if [ "${BOT_EXIT}" -ne "${NATIVE_LOGIN_EXIT_CODE}" ]; then
exit "${BOT_EXIT}"
fi
echo "[entrypoint] bot requested native Reddit login mode"
stop_chrome
start_native_login_chrome
node dist/native-login-gate.js &
GATE_PID=$!
wait "${GATE_PID}"
GATE_EXIT=$?
GATE_PID=""
if [ "${GATE_EXIT}" -ne 0 ]; then
exit "${GATE_EXIT}"
fi
echo "[entrypoint] human completed login handoff; enabling authenticated control"
stop_chrome
start_controlled_chrome
done