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:
@@ -1,13 +1,18 @@
|
||||
// engage-api/get-activity.ts
|
||||
import axios from 'axios';
|
||||
import { readFile,writeFile,unlink } from 'fs/promises';
|
||||
import { resolve } from 'path';
|
||||
import { logger } from '../utils/logger';
|
||||
import {
|
||||
loginWithPlaywright,
|
||||
loadCachedCookies,
|
||||
saveCookiesToCache,
|
||||
clearCookieCache,
|
||||
getCachedCookieString
|
||||
} from '../services/playwright-auth';
|
||||
|
||||
// Define interfaces for our data structures
|
||||
interface ActivityResponse {
|
||||
d: string;
|
||||
isError ? : boolean;
|
||||
isError?: boolean;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@@ -15,71 +20,19 @@ interface ActivityResponse {
|
||||
class AuthenticationError extends Error {
|
||||
status: number;
|
||||
|
||||
constructor(message: string = "Authentication failed, cookie may be invalid.", status ? : number) {
|
||||
constructor(message: string = "Authentication failed, cookie may be invalid.", status?: number) {
|
||||
super(message);
|
||||
this.name = "AuthenticationError";
|
||||
this.status = status || 0;
|
||||
}
|
||||
}
|
||||
|
||||
// In Bun, we can use import.meta.dir instead of the Node.js __dirname approach
|
||||
const COOKIE_FILE_PATH = resolve(import.meta.dir, 'nkcs-engage.cookie.txt');
|
||||
let _inMemoryCookie: string | null = null;
|
||||
|
||||
// Cookie Cache Helper Functions
|
||||
async function loadCachedCookie(): Promise < string | null > {
|
||||
if (_inMemoryCookie) {
|
||||
logger.debug("Using in-memory cached cookie.");
|
||||
return _inMemoryCookie;
|
||||
}
|
||||
try {
|
||||
const cookieFromFile = await readFile(COOKIE_FILE_PATH, 'utf8');
|
||||
if (cookieFromFile) {
|
||||
_inMemoryCookie = cookieFromFile;
|
||||
logger.debug("Loaded cookie from file cache.");
|
||||
return _inMemoryCookie;
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err.code === 'ENOENT') {
|
||||
logger.debug("Cookie cache file not found. No cached cookie loaded.");
|
||||
} else {
|
||||
logger.warn("Error loading cookie from file:", err.message);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function saveCookieToCache(cookieString: string): Promise < void > {
|
||||
if (!cookieString) {
|
||||
logger.warn("Attempted to save an empty or null cookie. Aborting save.");
|
||||
return;
|
||||
}
|
||||
_inMemoryCookie = cookieString;
|
||||
try {
|
||||
await writeFile(COOKIE_FILE_PATH, cookieString, 'utf8');
|
||||
logger.debug("Cookie saved to file cache.");
|
||||
} catch (err: any) {
|
||||
logger.error("Error saving cookie to file:", err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function clearCookieCache(): Promise < void > {
|
||||
_inMemoryCookie = null;
|
||||
try {
|
||||
await unlink(COOKIE_FILE_PATH);
|
||||
logger.debug("Cookie cache file deleted.");
|
||||
} catch (err: any) {
|
||||
if (err.code !== 'ENOENT') {
|
||||
logger.error("Error deleting cookie file:", err.message);
|
||||
} else {
|
||||
logger.debug("Cookie cache file did not exist, no need to delete.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function testCookieValidity(cookieString: string): Promise < boolean > {
|
||||
/**
|
||||
* Test cookie validity by calling API
|
||||
*/
|
||||
async function testCookieValidityWithApi(cookieString: string): Promise<boolean> {
|
||||
if (!cookieString) return false;
|
||||
logger.debug("Testing cookie validity...");
|
||||
logger.debug('Testing cookie validity via API...');
|
||||
|
||||
const MAX_RETRIES = 3;
|
||||
let attempt = 0;
|
||||
@@ -98,123 +51,69 @@ async function testCookieValidity(cookieString: string): Promise < boolean > {
|
||||
};
|
||||
|
||||
logger.debug(`Attempt ${attempt}/${MAX_RETRIES}`);
|
||||
await axios.post(url, payload, {
|
||||
const response = await axios.post(url, payload, {
|
||||
headers,
|
||||
timeout: 20000
|
||||
});
|
||||
|
||||
logger.debug("Cookie test successful (API responded 2xx). Cookie is valid.");
|
||||
// Check for 4xx errors (auth failures)
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
logger.warn(`Cookie test returned ${response.status}, likely invalid`);
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.debug('Cookie test successful (API responded 2xx). Cookie is valid.');
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
logger.warn(`Cookie validity test failed (attempt ${attempt}/${MAX_RETRIES}).`);
|
||||
if (error.response) {
|
||||
logger.warn(`Cookie test API response status: ${error.response.status}.`);
|
||||
// 4xx = auth failure (immediate fail)
|
||||
if (error.response.status >= 400 && error.response.status < 500) {
|
||||
logger.warn(`Cookie test API response status: ${error.response.status} (auth error)`);
|
||||
return false;
|
||||
}
|
||||
// 5xx = server error (retry with delay)
|
||||
logger.warn(`Cookie test API response status: ${error.response.status} (server error, retrying...)`);
|
||||
} else {
|
||||
logger.warn(`Network/other error: ${error.message}`);
|
||||
// No response (000 status, network error, timeout)
|
||||
logger.warn(`Network/timeout error: ${error.message} (retrying...)`);
|
||||
}
|
||||
|
||||
if (attempt >= MAX_RETRIES) {
|
||||
logger.warn("Max retries reached. Cookie is likely invalid or expired.");
|
||||
return false;
|
||||
if (attempt < MAX_RETRIES) {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * (attempt + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn('Max retries reached. Cookie is likely invalid or expired.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Core API Interaction Functions
|
||||
async function getSessionId(): Promise < string | null > {
|
||||
const url = 'https://engage.nkcswx.cn/Login.aspx';
|
||||
try {
|
||||
const response = await axios.get(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)'
|
||||
}
|
||||
});
|
||||
const setCookieHeader = response.headers['set-cookie'];
|
||||
if (setCookieHeader && setCookieHeader.length > 0) {
|
||||
const sessionIdCookie = setCookieHeader.find(cookie => cookie.trim().startsWith('ASP.NET_SessionId='));
|
||||
if (sessionIdCookie) {
|
||||
logger.debug('ASP.NET_SessionId created');
|
||||
return sessionIdCookie.split(';')[0] || null; // Ensure a fallback to `null` if splitting fails
|
||||
}
|
||||
return null; // Explicitly return `null` if no cookie is found
|
||||
}
|
||||
logger.error("No ASP.NET_SessionId cookie found in Set-Cookie header.");
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
logger.error(`Error in getSessionId: ${error.response ? `${error.response.status} - ${error.response.statusText}` : error.message}`);
|
||||
throw error;
|
||||
/**
|
||||
* Get complete cookies using Playwright
|
||||
*/
|
||||
async function getCompleteCookies(userName: string, userPwd: string): Promise<string> {
|
||||
logger.info('Attempting to get complete cookie string using Playwright login...');
|
||||
|
||||
const cookies = await loginWithPlaywright(userName, userPwd);
|
||||
|
||||
if (!cookies || cookies.length === 0) {
|
||||
throw new Error("Login failed: Could not obtain cookies.");
|
||||
}
|
||||
|
||||
const cookieString = cookies.map(c => `${c.name}=${c.value}`).join('; ');
|
||||
return cookieString;
|
||||
}
|
||||
|
||||
async function getMSAUTH(sessionId: string, userName: string, userPwd: string, templateFilePath: string): Promise < string | null > {
|
||||
const url = 'https://engage.nkcswx.cn/Login.aspx';
|
||||
try {
|
||||
let templateData = await readFile(templateFilePath, 'utf8');
|
||||
const postData = templateData
|
||||
.replace('{{USERNAME}}', userName)
|
||||
.replace('{{PASSWORD}}', userPwd);
|
||||
const headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Cookie': sessionId,
|
||||
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)',
|
||||
'Referer': 'https://engage.nkcswx.cn/Login.aspx'
|
||||
};
|
||||
logger.debug('Getting .ASPXFORMSAUTH');
|
||||
const response = await axios.post(url, postData, {
|
||||
headers,
|
||||
maxRedirects: 0,
|
||||
validateStatus: (status) => status >= 200 && status < 400
|
||||
});
|
||||
const setCookieHeader = response.headers['set-cookie'];
|
||||
let formsAuthCookieValue = null;
|
||||
if (setCookieHeader && setCookieHeader.length > 0) {
|
||||
const aspxAuthCookies = setCookieHeader.filter(cookie => cookie.trim().startsWith('.ASPXFORMSAUTH='));
|
||||
if (aspxAuthCookies.length > 0) {
|
||||
for (let i = aspxAuthCookies.length - 1; i >= 0; i--) {
|
||||
const cookieCandidateParts = aspxAuthCookies[i].split(';');
|
||||
if (cookieCandidateParts.length > 0 && cookieCandidateParts[0] !== undefined) { // Explicit check
|
||||
const firstPart = cookieCandidateParts[0].trim();
|
||||
if (firstPart.length > '.ASPXFORMSAUTH='.length && firstPart.substring('.ASPXFORMSAUTH='.length).length > 0) {
|
||||
formsAuthCookieValue = firstPart;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (formsAuthCookieValue) {
|
||||
logger.debug('.ASPXFORMSAUTH cookie obtained.');
|
||||
return formsAuthCookieValue;
|
||||
} else {
|
||||
logger.error("No valid .ASPXFORMSAUTH cookie found. Headers:", setCookieHeader || "none");
|
||||
return null;
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.code === 'ENOENT') logger.error(`Error: Template file '${templateFilePath}' not found.`);
|
||||
else logger.error(`Error in getMSAUTH: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function getCompleteCookies(userName: string, userPwd: string, templateFilePath: string): Promise < string > {
|
||||
logger.debug('Attempting to get complete cookie string (login process).');
|
||||
const sessionId = await getSessionId();
|
||||
if (!sessionId) throw new Error("Login failed: Could not obtain ASP.NET_SessionId.");
|
||||
|
||||
const msAuth = await getMSAUTH(sessionId, userName, userPwd, templateFilePath);
|
||||
if (!msAuth) throw new Error("Login failed: Could not obtain .ASPXFORMSAUTH cookie.");
|
||||
|
||||
return `${sessionId}; ${msAuth}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get activity details from API
|
||||
*/
|
||||
async function getActivityDetailsRaw(
|
||||
activityId: string,
|
||||
cookies: string,
|
||||
maxRetries: number = 3,
|
||||
timeoutMilliseconds: number = 20000
|
||||
): Promise < string | null > {
|
||||
): Promise<string | null> {
|
||||
const url = 'https://engage.nkcswx.cn/Services/ActivitiesService.asmx/GetActivityDetails';
|
||||
const headers = {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
@@ -270,7 +169,6 @@ async function getActivityDetailsRaw(
|
||||
* @param activityId - The ID of the activity to fetch.
|
||||
* @param userName - URL-encoded username.
|
||||
* @param userPwd - URL-encoded password.
|
||||
* @param templateFileName - Name of the login template file.
|
||||
* @param forceLogin - If true, bypasses cached cookie and forces a new login.
|
||||
* @returns The parsed JSON object of activity details, or null on failure.
|
||||
*/
|
||||
@@ -278,10 +176,9 @@ export async function fetchActivityData(
|
||||
activityId: string,
|
||||
userName: string,
|
||||
userPwd: string,
|
||||
templateFileName: string = "login_template.txt",
|
||||
forceLogin: boolean = false
|
||||
): Promise < any | null > {
|
||||
let currentCookie = forceLogin ? null : await loadCachedCookie();
|
||||
): Promise<any | null> {
|
||||
let currentCookie = forceLogin ? null : await getCachedCookieString();
|
||||
|
||||
if (forceLogin && currentCookie) {
|
||||
await clearCookieCache();
|
||||
@@ -289,21 +186,25 @@ export async function fetchActivityData(
|
||||
}
|
||||
|
||||
if (currentCookie) {
|
||||
const isValid = await testCookieValidity(currentCookie);
|
||||
const isValid = await testCookieValidityWithApi(currentCookie);
|
||||
if (!isValid) {
|
||||
logger.info("Cached cookie test failed or cookie expired. Clearing cache.");
|
||||
logger.info('Cached cookie test failed or cookie expired. Clearing cache.');
|
||||
await clearCookieCache();
|
||||
currentCookie = null;
|
||||
} else {
|
||||
logger.info("Using valid cached cookie.");
|
||||
logger.info('Using valid cached cookie.');
|
||||
}
|
||||
}
|
||||
|
||||
if (!currentCookie) {
|
||||
logger.info(forceLogin ? "Forcing new login." : "No valid cached cookie found or cache bypassed. Attempting login...");
|
||||
logger.info(forceLogin ? 'Forcing new login.' : 'No valid cached cookie found or cache bypassed. Attempting login...');
|
||||
try {
|
||||
currentCookie = await getCompleteCookies(userName, userPwd, resolve(import.meta.dir, templateFileName));
|
||||
await saveCookieToCache(currentCookie);
|
||||
currentCookie = await getCompleteCookies(userName, userPwd);
|
||||
|
||||
const cookies = await loadCachedCookies();
|
||||
if (cookies) {
|
||||
await saveCookiesToCache(cookies);
|
||||
}
|
||||
} catch (loginError) {
|
||||
logger.error(`Login process failed: ${(loginError as Error).message}`);
|
||||
return null;
|
||||
@@ -311,7 +212,7 @@ export async function fetchActivityData(
|
||||
}
|
||||
|
||||
if (!currentCookie) {
|
||||
logger.error("Critical: No cookie available after login attempt. Cannot fetch activity data.");
|
||||
logger.error('Critical: No cookie available after login attempt. Cannot fetch activity data.');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -329,11 +230,15 @@ export async function fetchActivityData(
|
||||
await clearCookieCache();
|
||||
|
||||
try {
|
||||
logger.info("Attempting re-login due to authentication failure...");
|
||||
currentCookie = await getCompleteCookies(userName, userPwd, resolve(import.meta.dir, templateFileName));
|
||||
await saveCookieToCache(currentCookie);
|
||||
logger.info('Attempting re-login due to authentication failure...');
|
||||
currentCookie = await getCompleteCookies(userName, userPwd);
|
||||
|
||||
const cookies = await loadCachedCookies();
|
||||
if (cookies) {
|
||||
await saveCookiesToCache(cookies);
|
||||
}
|
||||
|
||||
logger.info("Re-login successful. Retrying request for activity details once...");
|
||||
logger.info('Re-login successful. Retrying request for activity details once...');
|
||||
const rawActivityDetailsStringRetry = await getActivityDetailsRaw(activityId, currentCookie);
|
||||
if (rawActivityDetailsStringRetry) {
|
||||
const parsedOuterRetry = JSON.parse(rawActivityDetailsStringRetry);
|
||||
@@ -351,6 +256,3 @@ export async function fetchActivityData(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optionally
|
||||
//export { clearCookieCache,testCookieValidity };
|
||||
Reference in New Issue
Block a user