learn-spider/bin/start_browser.sh

82 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
TARGET_URL="https://www.baidu.com" # 直接打开百度
VNC_PWD=$1
VNC_PORT=$2
DISPLAY_NUM=$3
CDP_PORT=30010
PROFILE_DIR="/tmp/chrome-profile-$$"
LOCK_FILE="/tmp/cdp.lock"
GREEN='\033[0;32m'; RED='\033[0;31m'; NC='\033[0m'
log() { echo -e "${GREEN}[$(date '+%H:%M:%S')]${NC} $*"; }
err() { echo -e "${RED}[$(date '+%H:%M:%S')]${NC} $*" >&2; }
exec 9>"$LOCK_FILE"
flock -n 9 || { err "Another instance running"; exit 1; }
cleanup() {
log "Cleaning up..."
pkill -P $$ 2>/dev/null || true
pkill -f "remote-debugging-port=${CDP_PORT}" 2>/dev/null || true
pkill -f "Xvfb :${DISPLAY_NUM}" 2>/dev/null || true
pkill -f "x11vnc.*${DISPLAY_NUM}" 2>/dev/null || true
rm -rf "$PROFILE_DIR" 2>/dev/null || true
rm -f /tmp/login_done 2>/dev/null || true
}
trap cleanup EXIT INT TERM
log "Starting Xvfb :${DISPLAY_NUM}"
Xvfb :${DISPLAY_NUM} -screen 0 1280x1024x24 -nolisten tcp &
sleep 2
export DISPLAY=:${DISPLAY_NUM}
log "Starting Chromium"
mkdir -p "$PROFILE_DIR"
CHROME_BIN=$(ls $HOME/.cache/ms-playwright/chromium-*/chrome-linux/chrome 2>/dev/null | head -1)
if [ -z "$CHROME_BIN" ]; then
CHROME_BIN=$(which chromium-browser 2>/dev/null || which google-chrome-stable 2>/dev/null || true)
fi
[ -z "$CHROME_BIN" ] && { err "Chromium not found"; exit 1; }
# 去掉 --headless=new这样才能看到界面
"$CHROME_BIN" \
--no-sandbox \
--disable-gpu \
--disable-dev-shm-usage \
--remote-debugging-port=${CDP_PORT} \
--remote-debugging-address=127.0.0.1 \
--remote-allow-origins=* \
--user-data-dir=${PROFILE_DIR} \
--window-size=1280,1024 \
"${TARGET_URL}" &
CHROME_PID=$!
# 启动 x11vnc将 Xvfb 转为 VNC 服务)
log "Starting x11vnc on port ${VNC_PORT}..."
x11vnc -display :${DISPLAY_NUM} -forever -shared -passwd ${VNC_PWD} -rfbport ${VNC_PORT} -bg &
log "Waiting for CDP..."
for i in {1..30}; do
curl -sf "http://127.0.0.1:${CDP_PORT}/json/version" >/dev/null 2>&1 && { log "CDP ready"; break; }
sleep 1
[ $i -eq 30 ] && { err "CDP timeout"; exit 1; }
done
PUBLIC_HOST=$(curl -sf ifconfig.me 2>/dev/null || hostname -I | awk '{print $1}')
log "=========================================="
log "✅ Browser is running!"
log "📝 CDP: http://${PUBLIC_HOST}:${CDP_PORT}"
log "🖥️ VNC: ${PUBLIC_HOST}:${VNC_PORT} (密码: ${VNC_PWD})"
log "=========================================="
rm -f /tmp/login_done
log "Waiting for /tmp/login_done ..."
while [ ! -f /tmp/login_done ]; do sleep 1; done
log "Login detected, automation can start"
wait $CHROME_PID 2>/dev/null || true