From 2b7d2987536f08ea8bb5e4c81240379cc38585aa Mon Sep 17 00:00:00 2001 From: foxhui Date: Fri, 12 Dec 2025 00:15:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Cookie=20=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 17 +++++++++++++++ README.md | 51 +++++++++++++++++++++++++++++++++++++++++++- lib/browser/utils.js | 11 ++++++++++ server.js | 22 ++++++++++++++++++- 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d0309a..d45e1e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.2.1] - 2025-12-12 + +### Added +- **Cookie获取** + - 增加 Cookie 获取接口,可利用本项目的自动续登功能获取最新的 Cookie 给其他工具使用 + +### Changed +- **自动续登** + - 监听逻辑修改为全局监听,任何时候触发跳转都会执行自动登录(如会话过期自动跳转) + +### Fixed +- **修复杂项** + - 修复自动登录时可能会出现的问题 + - 修复启动VNC服务器时端口可能冲突的问题 + - 修复遗留的浏览器启动参数导致的小问题 + - 修复 zAI 返回错误时无反馈直到请求超时的问题 + ## [2.2.0] - 2025-12-11 ### Added diff --git a/README.md b/README.md index 5fc06ff..878ed72 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,56 @@ curl -X GET http://127.0.0.1:3000/v1/models \ -#### 3. 多模态请求 (图生图/图生文) +#### 3. 获取Cookies + +**功能说明**:可利用本项目的自动续登功能获取最新Cookie给其他工具使用。 + +**请求端点** +``` +GET http://127.0.0.1:3000/v1/cookies +``` + +
+📄 查看API请求示例 + +**请求示例** +```bash +curl -X GET http://127.0.0.1:3000/v1/cookies \ + -H "Authorization: Bearer your-secret-key" +``` + +**响应格式** +```json +{ + "cookies": [ + { + "name": "_GRECAPTCHA", + "value": "09ADxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "domain": "www.google.com", + "path": "/recaptcha", + "expires": 1780000000, + "httpOnly": true, + "secure": true, + "sameSite": "None" + }, + { + "name": "OTZ", + "value": "8888888_24_24__24_", + "domain": "accounts.google.com", + "path": "/", + "expires": 1760000000, + "httpOnly": false, + "secure": true, + "sameSite": "None" + } + .......... more + ] +} +``` + +
+ +#### 4. 多模态请求 (图生图/图生文) **功能说明**:支持在消息中附带图片进行对话或生成。 diff --git a/lib/browser/utils.js b/lib/browser/utils.js index 3734796..3de04e1 100644 --- a/lib/browser/utils.js +++ b/lib/browser/utils.js @@ -434,3 +434,14 @@ export function createPageCloseWatcher(page) { return { promise, cleanup }; } + +/** + * 获取当前页面的所有 Cookies (实时从浏览器获取) + * @param {import('playwright-core').Page} page - Playwright 页面实例 + * @returns {Promise} Cookies 数组 (JSON 格式) + */ +export async function getCookies(page) { + const context = page.context(); + return await context.cookies(); +} + diff --git a/server.js b/server.js index 9432ec3..9624350 100644 --- a/server.js +++ b/server.js @@ -414,7 +414,27 @@ async function startServer() { return; } - // 2. 聊天补全接口 + // 2. 获取 Cookies 接口 + if (req.method === 'GET' && req.url === '/v1/cookies') { + try { + if (!browserContext || !browserContext.page) { + res.writeHead(503, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Browser not initialized' })); + return; + } + const context = browserContext.page.context(); + const cookies = await context.cookies(); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ cookies })); + } catch (err) { + logger.error('服务器', '获取 Cookies 失败', { id, error: err.message }); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: err.message })); + } + return; + } + + // 3. 聊天补全接口 if (req.method === 'POST' && req.url.startsWith('/v1/chat/completions')) { const chunks = []; req.on('data', chunk => chunks.push(chunk));