StarrySkyWorld commited on
Commit
c94202e
·
1 Parent(s): 20cef5e

refactor: 替换 undetected-chromedriver 为 seleniumbase,并简化 VNC 相关配置

Browse files

移除了对 undetected-chromedriver 的依赖,改为使用 seleniumbase 库,同时更新了 pyproject.toml 和 requirements.txt 文件中的依赖版本。
- 将 `import undetected_chromedriver as uc` 替换为 `from seleniumbase import Driver`。
- 移除了 `build_chrome_options` 和 `get_chromedriver_path` 函数的实现,改用 `seleniumbase.Driver` 的构造函数来配置 Chrome 浏览器。
- 在 `create_driver` 函数中,使用 `Driver` 类来创建 Chrome 浏览器实例,并传递相应的配置参数。
- 简化了 startup.sh 脚本中与 VNC 相关的配置,移除了 fluxbox 和 x11vnc 的启动逻辑,只保留了 Xvfb 的启动部分,并简化了 DISPLAY 的配置方式。

Files changed (5) hide show
  1. Dockerfile +2 -3
  2. main.py +16 -29
  3. pyproject.toml +1 -1
  4. requirements.txt +1 -1
  5. startup.sh +3 -38
Dockerfile CHANGED
@@ -8,12 +8,11 @@ COPY . .
8
  COPY example.config.json config.json
9
 
10
  RUN apt-get update -y \
11
- && apt-get install -y chromium chromium-driver xvfb x11vnc fluxbox dbus-x11 \
12
- fontconfig fonts-dejavu fonts-liberation fonts-noto fonts-noto-color-emoji \
13
  && rm -rf /var/lib/apt/lists/*
14
 
15
  RUN pip install --no-cache-dir -r requirements.txt
16
  RUN chmod +x /app/startup.sh
17
 
18
- EXPOSE 7860 5900
19
  CMD ["/app/startup.sh"]
 
8
  COPY example.config.json config.json
9
 
10
  RUN apt-get update -y \
11
+ && apt-get install -y chromium chromium-driver xvfb \
 
12
  && rm -rf /var/lib/apt/lists/*
13
 
14
  RUN pip install --no-cache-dir -r requirements.txt
15
  RUN chmod +x /app/startup.sh
16
 
17
+ EXPOSE 7860
18
  CMD ["/app/startup.sh"]
main.py CHANGED
@@ -1,4 +1,4 @@
1
- import undetected_chromedriver as uc
2
  from selenium.webdriver.common.by import By
3
  from selenium.webdriver.support.ui import WebDriverWait
4
  from selenium.webdriver.support import expected_conditions as EC
@@ -231,35 +231,22 @@ def register(driver):
231
  log(f"注册成功: {email}")
232
  return email, True, config
233
 
234
- def build_chrome_options():
235
- options = uc.ChromeOptions()
236
- options.add_argument("--no-sandbox")
237
- options.add_argument("--disable-dev-shm-usage")
238
- options.add_argument("--disable-gpu")
239
- options.add_argument("--disable-software-rasterizer")
240
- options.add_argument("--window-size=1280,720")
241
- if HEADLESS:
242
- options.add_argument("--headless=new")
243
- chrome_bin = os.getenv("CHROME_BIN")
244
- if chrome_bin:
245
- options.binary_location = chrome_bin
246
- return options
247
-
248
- def get_chromedriver_path():
249
- candidate = os.getenv("CHROMEDRIVER_PATH")
250
- if candidate and os.path.exists(candidate):
251
- return candidate
252
- for candidate in ("/usr/bin/chromedriver", "/usr/lib/chromium/chromedriver"):
253
- if os.path.exists(candidate):
254
- return candidate
255
- return None
256
-
257
  def create_driver():
258
- options = build_chrome_options()
259
- driver_path = get_chromedriver_path()
260
- if driver_path:
261
- return uc.Chrome(options=options, use_subprocess=True, driver_executable_path=driver_path)
262
- return uc.Chrome(options=options, use_subprocess=True)
 
 
 
 
 
 
 
 
 
 
263
 
264
  def register_one_account():
265
  driver = create_driver()
 
1
+ from seleniumbase import Driver
2
  from selenium.webdriver.common.by import By
3
  from selenium.webdriver.support.ui import WebDriverWait
4
  from selenium.webdriver.support import expected_conditions as EC
 
231
  log(f"注册成功: {email}")
232
  return email, True, config
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  def create_driver():
235
+ chrome_bin = os.getenv("CHROME_BIN")
236
+ chromium_args = [
237
+ "--no-sandbox",
238
+ "--disable-dev-shm-usage",
239
+ "--disable-gpu",
240
+ "--disable-software-rasterizer",
241
+ "--window-size=1280,720",
242
+ ]
243
+ return Driver(
244
+ uc=True,
245
+ headless=HEADLESS,
246
+ browser="chrome",
247
+ binary_location=chrome_bin,
248
+ chromium_arg=chromium_args,
249
+ )
250
 
251
  def register_one_account():
252
  driver = create_driver()
pyproject.toml CHANGED
@@ -9,7 +9,7 @@ dependencies = [
9
  "fastapi>=0.115.0",
10
  "requests>=2.32.5",
11
  "selenium>=4.39.0",
 
12
  "setuptools>=80.9.0",
13
- "undetected-chromedriver>=3.5.5",
14
  "uvicorn>=0.30.0",
15
  ]
 
9
  "fastapi>=0.115.0",
10
  "requests>=2.32.5",
11
  "selenium>=4.39.0",
12
+ "seleniumbase>=4.30.5",
13
  "setuptools>=80.9.0",
 
14
  "uvicorn>=0.30.0",
15
  ]
requirements.txt CHANGED
@@ -18,6 +18,7 @@ pydantic-core==2.41.5
18
  pysocks==1.7.1
19
  requests==2.32.5
20
  selenium==4.39.0
 
21
  setuptools==80.9.0
22
  sniffio==1.3.1
23
  sortedcontainers==2.4.0
@@ -27,7 +28,6 @@ trio==0.32.0
27
  trio-websocket==0.12.2
28
  typing-extensions==4.15.0
29
  typing-inspection==0.4.2
30
- undetected-chromedriver==3.5.5
31
  urllib3==2.6.3
32
  uvicorn==0.40.0
33
  websocket-client==1.9.0
 
18
  pysocks==1.7.1
19
  requests==2.32.5
20
  selenium==4.39.0
21
+ seleniumbase==4.30.5
22
  setuptools==80.9.0
23
  sniffio==1.3.1
24
  sortedcontainers==2.4.0
 
28
  trio-websocket==0.12.2
29
  typing-extensions==4.15.0
30
  typing-inspection==0.4.2
 
31
  urllib3==2.6.3
32
  uvicorn==0.40.0
33
  websocket-client==1.9.0
startup.sh CHANGED
@@ -15,45 +15,10 @@ if [ -z "${CHROME_BIN:-}" ]; then
15
  fi
16
  fi
17
 
18
- if [ -n "${ENABLE_VNC:-}" ]; then
19
- export DISPLAY="${DISPLAY:-${VNC_DISPLAY:-:99}}"
20
- export SCREEN_WIDTH="${SCREEN_WIDTH:-1366}"
21
- export SCREEN_HEIGHT="${SCREEN_HEIGHT:-768}"
22
- export SCREEN_DPI="${SCREEN_DPI:-96}"
23
- export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/runtime-root}"
24
- mkdir -p "${XDG_RUNTIME_DIR}" && chmod 700 "${XDG_RUNTIME_DIR}"
25
- if command -v Xvfb >/dev/null 2>&1; then
26
- echo "Starting Xvfb for VNC on DISPLAY=${DISPLAY}."
27
- Xvfb "${DISPLAY}" -screen 0 "${SCREEN_WIDTH}x${SCREEN_HEIGHT}x24" -dpi "${SCREEN_DPI}" >/tmp/xvfb.log 2>&1 &
28
- sleep 1
29
- else
30
- echo "Xvfb not available; VNC requires a display server."
31
- fi
32
-
33
- if command -v fluxbox >/dev/null 2>&1; then
34
- echo "Starting fluxbox window manager."
35
- fluxbox >/tmp/fluxbox.log 2>&1 &
36
- fi
37
-
38
- if command -v x11vnc >/dev/null 2>&1; then
39
- echo "Starting x11vnc server."
40
- if [ -n "${VNC_PASSWORD:-}" ]; then
41
- x11vnc -display "${DISPLAY}" -passwd "${VNC_PASSWORD}" -forever -shared -rfbport "${VNC_PORT:-5900}" >/tmp/x11vnc.log 2>&1 &
42
- else
43
- x11vnc -display "${DISPLAY}" -nopw -forever -shared -rfbport "${VNC_PORT:-5900}" >/tmp/x11vnc.log 2>&1 &
44
- fi
45
- else
46
- echo "x11vnc not available; skipping VNC server."
47
- fi
48
- elif [ -z "${DISABLE_XVFB:-}" ] && command -v Xvfb >/dev/null 2>&1; then
49
- export DISPLAY="${DISPLAY:-:99}"
50
- export SCREEN_WIDTH="${SCREEN_WIDTH:-1366}"
51
- export SCREEN_HEIGHT="${SCREEN_HEIGHT:-768}"
52
- export SCREEN_DPI="${SCREEN_DPI:-96}"
53
- export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/runtime-root}"
54
- mkdir -p "${XDG_RUNTIME_DIR}" && chmod 700 "${XDG_RUNTIME_DIR}"
55
  echo "Starting Xvfb for virtual display."
56
- Xvfb "${DISPLAY}" -screen 0 "${SCREEN_WIDTH}x${SCREEN_HEIGHT}x24" -dpi "${SCREEN_DPI}" >/tmp/xvfb.log 2>&1 &
 
57
  sleep 1
58
  if ps | grep -q "[X]vfb"; then
59
  echo "Xvfb started on DISPLAY=${DISPLAY}."
 
15
  fi
16
  fi
17
 
18
+ if [ -z "${DISABLE_XVFB:-}" ] && command -v Xvfb >/dev/null 2>&1; then
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  echo "Starting Xvfb for virtual display."
20
+ Xvfb :99 -screen 0 1280x720x24 >/tmp/xvfb.log 2>&1 &
21
+ export DISPLAY=:99
22
  sleep 1
23
  if ps | grep -q "[X]vfb"; then
24
  echo "Xvfb started on DISPLAY=${DISPLAY}."