fix: Cookie管理优化及刷新间隔补充

- monitor-server.js: Cookie存储改为Map合并机制,避免丢失CDN Cookie导致403
- checkActivity.js: 补上Cookie两步请求逻辑,修复无Cookie直接请求失败的问题
- public/index.html: 新增20秒刷新间隔选项
This commit is contained in:
ikmkj
2026-03-19 15:21:01 +08:00
parent b0afdbd9a0
commit 7907716c9f
3 changed files with 96 additions and 38 deletions

View File

@@ -17,30 +17,43 @@ const REQUEST_HEADERS = {
'Upgrade-Insecure-Requests': '1',
};
// 存储 Cookie
let cachedCookies = '';
// 存储 Cookiekey-value Map支持合并更新
let cookieJar = new Map();
let cookieExpiry = 0;
// 从 set-cookie 响应头提取 Cookie
function extractCookies(response) {
// 从 set-cookie 响应头提取 Cookie,合并到 cookieJar
function mergeCookies(response) {
const setCookieHeaders = response.headers.getSetCookie?.() || [];
const cookies = setCookieHeaders.map(c => c.split(';')[0]).join('; ');
return cookies;
for (const header of setCookieHeaders) {
const pair = header.split(';')[0]; // 取 name=value 部分
const eqIdx = pair.indexOf('=');
if (eqIdx > 0) {
const name = pair.substring(0, eqIdx).trim();
const value = pair.substring(eqIdx + 1).trim();
cookieJar.set(name, value);
}
}
}
// 将 cookieJar 序列化为请求头格式
function getCookieString() {
return Array.from(cookieJar.entries()).map(([k, v]) => `${k}=${v}`).join('; ');
}
// 获取有效 Cookie过期或为空时重新获取
async function getValidCookies() {
if (cachedCookies && Date.now() < cookieExpiry) {
return cachedCookies;
if (cookieJar.size > 0 && Date.now() < cookieExpiry) {
return getCookieString();
}
console.log('[Cookie] 正在获取新的 Cookie...');
cookieJar.clear();
const response = await fetch(ACTIVITY_URL, { headers: REQUEST_HEADERS, redirect: 'manual' });
cachedCookies = extractCookies(response);
// Cookie 有效期设为 1 小时(服务器 max-age=86400,保守取 1 小时
mergeCookies(response);
// CDN Cookie 有效期 2 小时,保守取 1 小时刷新
cookieExpiry = Date.now() + 3600 * 1000;
console.log('[Cookie] 获取成功:', cachedCookies);
return cachedCookies;
console.log('[Cookie] 获取成功:', getCookieString());
return getCookieString();
}
app.get('/api/activity', async (req, res) => {
@@ -54,11 +67,13 @@ app.get('/api/activity', async (req, res) => {
});
const html = await response.text();
// 合并第二次请求返回的 Cookie如 PHPSESSID
mergeCookies(response);
// 如果返回内容过短,可能 Cookie 失效,清除缓存并重试一次
if (html.length < 1000) {
console.log('[Cookie] 响应内容过短,尝试刷新 Cookie 重试...');
cachedCookies = '';
cookieJar.clear();
cookieExpiry = 0;
const newCookies = await getValidCookies();
const retryResponse = await fetch(ACTIVITY_URL, {
@@ -68,22 +83,10 @@ app.get('/api/activity', async (req, res) => {
}
});
const retryHtml = await retryResponse.text();
// 更新重试后的 Cookie
const retryCookies = extractCookies(retryResponse);
if (retryCookies) {
cachedCookies = retryCookies;
cookieExpiry = Date.now() + 3600 * 1000;
}
mergeCookies(retryResponse);
return res.json({ success: true, html: retryHtml });
}
// 更新响应中的新 Cookie
const newCookies = extractCookies(response);
if (newCookies) {
cachedCookies = newCookies;
cookieExpiry = Date.now() + 3600 * 1000;
}
res.json({ success: true, html });
} catch (error) {
res.status(500).json({ success: false, error: error.message });