Auto headless + Xvfb for headless servers

Enable Chromium on machines without DISPLAY: Python auto headless
with Xvfb hints, and start.sh installs/starts Xvfb (xvfb-run or
persistent :99) so registration can run headed under a virtual display.
This commit is contained in:
chaos committed 2026-07-12 00:34:15 +08:00
1 parent 4feac70c91
commit cd4a2351ae
8 files changed
+536 -21

No files matched your search

+27 -2
View File
@@ -196,6 +196,13 @@ def resolve_browser_path() -> str | None:
return None
def has_display() -> bool:
"""True if Chromium can open a real GUI window (Windows always; else DISPLAY/WAYLAND)."""
if sys.platform == "win32":
return True
return bool((os.environ.get("DISPLAY") or "").strip() or (os.environ.get("WAYLAND_DISPLAY") or "").strip())
def create_browser_options(headless: bool = False, proxy: str = "") -> ChromiumOptions:
opts = ChromiumOptions()
opts.auto_port()
@@ -217,8 +224,26 @@ def create_browser_options(headless: bool = False, proxy: str = "") -> ChromiumO
print("[!] skip turnstilePatch: Google Chrome disallows --load-extension; use Chromium")
else:
opts.add_extension(EXTENSION_PATH)
if headless:
opts.headless()
# Auto headless when no DISPLAY (unless caller already requested headless)
use_headless = bool(headless) or not has_display()
if use_headless:
try:
opts.headless()
except Exception:
opts.set_argument("--headless=new")
for flag in ("--headless=new", "--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage"):
try:
opts.set_argument(flag)
except Exception:
pass
if not headless and not has_display():
print(
f"[*] 无图形界面 (DISPLAY={os.environ.get('DISPLAY', '')!r}),已自动启用 headless。\n"
" 推荐: xvfb-run -a python auto_register.py ... (过 CF 更稳)",
flush=True,
)
else:
print("[*] browser headless=True", flush=True)
if proxy:
try:
from urllib.parse import urlparse