feat: 使用 Playwright 实现自动化 cookie 获取和验证

主要变更:
- 新增 Playwright 登录认证服务 (services/playwright-auth.ts)
- 重构 get-activity.ts 使用 Playwright 替代 Axios 登录
- 实现自动 cookie 过期检测和重试机制
- 优化 Docker 配置支持 Playwright 浏览器运行
- 添加启动脚本自动验证和刷新 cookies
- 完善错误处理:区分 4xx(认证失败) 和 5xx(服务器错误)

技术细节:
- 删除旧版 login_template.txt 和 nkcs-engage.cookie.txt
- 添加 startup.sh 启动时自动验证 cookies
- 改进 cookie 验证逻辑,添加指数退避重试
- Dockerfile 安装 Playwright 系统依赖
- docker-compose.yaml 添加 volumes 和 health checks

测试:
- 添加 auth.spec.ts 自动化测试
- 添加 get-cookies.ts 和 test-cookies-validity.ts 工具脚本
- 验证 401/500/000 等错误场景处理正确
This commit is contained in:
JamesFlare1212
2026-04-06 16:05:38 -04:00
parent b18b8a85e0
commit a8f468a497
15 changed files with 679 additions and 289 deletions

View File

@@ -1,15 +1,53 @@
FROM oven/bun:latest
ENV NODE_ENV=production
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /usr/src/app
# Install Playwright system dependencies
RUN apt-get update && apt-get install -y \
libnss3 \
libnspr4 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libcups2 \
libdrm2 \
libxkbcommon0 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libasound2 \
libpango-1.0-0 \
libcairo2 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy dependency files
COPY package.json bun.lock ./
# Install dependencies (including Playwright)
RUN bun install --production
# Install Playwright browsers
RUN bunx playwright install chromium --with-deps || true
# Copy application code
COPY . .
# Make startup script executable
RUN chmod +x startup.sh
# Create non-root user for security
RUN adduser --disabled-password --gecos '' appuser && \
chown -R appuser:appuser /usr/src/app
USER appuser
EXPOSE 3000
CMD ["bun", "start"]
# Use startup script
CMD ["/bin/sh", "startup.sh"]