手动登录服务器

This commit is contained in:
ray
2026-04-28 17:52:41 +08:00
parent c7908bf39e
commit fa6e1a0df8
6 changed files with 78 additions and 5 deletions

View File

@@ -20,8 +20,10 @@ export const config = {
rootDir,
baseUrl: process.env.ALIYUN_APS_BASE_URL || 'https://aps.aliyun.com',
headless: toBool(process.env.ALIYUN_APS_HEADLESS, false),
browserMode: (process.env.ALIYUN_APS_BROWSER_MODE || 'launch').trim().toLowerCase(),
browserChannel: (process.env.ALIYUN_APS_BROWSER_CHANNEL || '').trim(),
browserExecutablePath: (process.env.ALIYUN_APS_BROWSER_EXECUTABLE_PATH || '').trim(),
cdpUrl: (process.env.ALIYUN_APS_CDP_URL || 'http://127.0.0.1:9222').trim(),
timezone: process.env.ALIYUN_APS_TIMEZONE || 'Asia/Shanghai',
cron: process.env.ALIYUN_APS_CRON || '0 6 * * *',
orderStartDate: process.env.ALIYUN_APS_ORDER_START_DATE || '2024-01-01',

View File

@@ -33,6 +33,8 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
let _context = null;
let _runtimeController = null;
let _browser = null;
let _isAttachedBrowser = false;
const AUTH_PAGE_KEYWORDS = [
'RAM 用户登录',
@@ -45,6 +47,10 @@ const AUTH_PAGE_KEYWORDS = [
async function closeContextIfNeeded() {
if (!_context) return;
if (_isAttachedBrowser) {
_context = null;
return;
}
await _context.close();
_context = null;
}
@@ -161,6 +167,20 @@ function clearStaleBrowserProfileLocks() {
async function getContext() {
if (_context) return _context;
if (config.browserMode === 'cdp') {
try {
_browser = await chromium.connectOverCDP(config.cdpUrl);
_isAttachedBrowser = true;
const contexts = _browser.contexts();
_context = contexts[0] || await _browser.newContext();
console.log(`[CDP] 已附着到手动浏览器: ${config.cdpUrl}`);
return _context;
} catch (error) {
throw new Error(`无法通过 CDP 连接到手动浏览器(${config.cdpUrl})。请先手动启动 Chrome 并开启远程调试端口。原始错误: ${error.message}`);
}
}
_isAttachedBrowser = false;
clearStaleBrowserProfileLocks();
const launchOptions = {
headless: config.headless,