Enhance ProxyValidator to support three validation attempts with improved logging. Update retryCount default to 2 and clarify retry logic in comments.

This commit is contained in:
theluyuan 2025-10-31 08:50:45 +08:00
parent a31fe3f892
commit 76683fb519

View File

@ -10,6 +10,7 @@ class ProxyValidator {
/** /**
* 核心统一验证代理方法 - 所有验证代理的代码都统一使用此方法 * 核心统一验证代理方法 - 所有验证代理的代码都统一使用此方法
* 验证会尝试3次1次初始 + 2次重试只要有一次成功就算通过
* @param {string} ip - 代理IP地址 * @param {string} ip - 代理IP地址
* @param {number|string} port - 代理端口 * @param {number|string} port - 代理端口
* @param {object} options - 可选配置参数 * @param {object} options - 可选配置参数
@ -18,7 +19,7 @@ class ProxyValidator {
* @param {string} options.userAgent - User-Agent默认使用类中定义的 * @param {string} options.userAgent - User-Agent默认使用类中定义的
* @param {boolean} options.updateDatabase - 是否更新数据库默认true * @param {boolean} options.updateDatabase - 是否更新数据库默认true
* @param {boolean} options.logResult - 是否打印日志默认true * @param {boolean} options.logResult - 是否打印日志默认true
* @param {number} options.retryCount - 重试次数默认0不重试 * @param {number} options.retryCount - 重试次数默认2总共尝试3次
* @param {number} options.retryDelay - 重试延迟毫秒默认500 * @param {number} options.retryDelay - 重试延迟毫秒默认500
* @returns {Promise<object>} 验证结果 {ip, port, isValid, responseTime, error, testUrl} * @returns {Promise<object>} 验证结果 {ip, port, isValid, responseTime, error, testUrl}
*/ */
@ -29,7 +30,7 @@ class ProxyValidator {
userAgent = this.userAgent, userAgent = this.userAgent,
updateDatabase = true, updateDatabase = true,
logResult = true, logResult = true,
retryCount = 0, retryCount = 2, // 默认重试2次总共尝试3次
retryDelay = 500 retryDelay = 500
} = options; } = options;
@ -41,16 +42,18 @@ class ProxyValidator {
}; };
if (logResult) { if (logResult) {
console.log(`正在验证代理 ${ip}:${port}`); console.log(`正在验证代理 ${ip}:${port}(将尝试 ${retryCount + 1} 次)`);
} }
// 支持重试机制 // 支持重试机制:总共尝试 retryCount + 1 次,只要有一次成功就返回成功
let lastError = null; let lastError = null;
let lastResult = null; let lastResult = null;
for (let attempt = 0; attempt <= retryCount; attempt++) { for (let attempt = 0; attempt <= retryCount; attempt++) {
const attemptNumber = attempt + 1;
if (attempt > 0 && logResult) { if (attempt > 0 && logResult) {
console.log(`代理 ${ip}:${port}${attempt + 1} 次重试验证...`); console.log(`代理 ${ip}:${port}${attemptNumber} 次尝试验证...`);
await this.sleep(retryDelay); await this.sleep(retryDelay);
} }
@ -85,37 +88,56 @@ class ProxyValidator {
} }
} }
if (logResult) { // 只要有一次验证成功,立即返回成功结果
if (isValid) { if (isValid) {
console.log(`✓ 代理 ${ip}:${port} 验证成功,响应时间: ${responseTime}ms`); if (logResult) {
} else { console.log(`✓ 代理 ${ip}:${port}${attemptNumber} 次尝试验证成功,响应时间: ${responseTime}ms`);
console.log(`✗ 代理 ${ip}:${port} 验证失败,响应不正确`);
} }
const result = {
ip: ip,
port: parseInt(port),
isValid: true,
responseTime: responseTime,
error: null,
testUrl: testUrl
};
// 更新数据库中的验证结果(如果需要)
if (updateDatabase) {
try {
await ProxyModel.updateValidity(ip, port, 1, responseTime);
} catch (dbError) {
// 如果代理不在数据库中,忽略更新错误
if (!dbError.message.includes('not found')) {
console.warn(`更新数据库失败: ${dbError.message}`);
}
}
}
return result;
} }
const result = { // 请求成功但验证失败(响应内容不正确),记录错误并继续重试
if (logResult) {
console.log(`✗ 代理 ${ip}:${port}${attemptNumber} 次尝试验证失败,响应内容不正确`);
}
lastError = new Error('响应内容验证失败');
lastResult = {
ip: ip, ip: ip,
port: parseInt(port), port: parseInt(port),
isValid: isValid, isValid: false,
responseTime: responseTime, responseTime: responseTime,
error: null, error: '响应内容验证失败',
testUrl: testUrl testUrl: testUrl
}; };
// 更新数据库中的验证结果(如果需要) // 如果还有重试机会,继续尝试
if (updateDatabase) { if (attempt < retryCount) {
try { continue;
await ProxyModel.updateValidity(ip, port, isValid ? 1 : 0, responseTime);
} catch (dbError) {
// 如果代理不在数据库中,忽略更新错误
if (!dbError.message.includes('not found')) {
console.warn(`更新数据库失败: ${dbError.message}`);
}
}
} }
return result;
} catch (error) { } catch (error) {
const responseTime = Date.now() - startTime; const responseTime = Date.now() - startTime;
lastError = error; lastError = error;
@ -128,39 +150,40 @@ class ProxyValidator {
testUrl: testUrl testUrl: testUrl
}; };
// 如果不是最后一次尝试,继续重试 if (logResult) {
console.log(`✗ 代理 ${ip}:${port}${attemptNumber} 次尝试验证失败: ${error.message}`);
}
// 如果还有重试机会,继续尝试
if (attempt < retryCount) { if (attempt < retryCount) {
continue; continue;
} }
// 最后一次尝试失败
if (logResult) {
console.log(`✗ 代理 ${ip}:${port} 验证失败: ${error.message}`);
}
// 更新数据库中的验证结果(如果需要)
if (updateDatabase) {
try {
await ProxyModel.updateValidity(ip, port, 0, responseTime);
} catch (dbError) {
// 如果代理不在数据库中,忽略更新错误
if (!dbError.message.includes('not found')) {
console.warn(`更新数据库失败: ${dbError.message}`);
}
}
}
return lastResult;
} }
} }
// 所有重试都失败了 // 所有尝试都失败了,返回最后一次的结果
if (logResult) {
console.log(`✗ 代理 ${ip}:${port} 所有 ${retryCount + 1} 次尝试都失败`);
}
// 更新数据库中的验证结果(如果需要)
if (updateDatabase && lastResult) {
try {
await ProxyModel.updateValidity(ip, port, 0, lastResult.responseTime);
} catch (dbError) {
// 如果代理不在数据库中,忽略更新错误
if (!dbError.message.includes('not found')) {
console.warn(`更新数据库失败: ${dbError.message}`);
}
}
}
return lastResult || { return lastResult || {
ip: ip, ip: ip,
port: parseInt(port), port: parseInt(port),
isValid: false, isValid: false,
responseTime: Date.now() - startTime, responseTime: Date.now() - startTime,
error: lastError ? lastError.message : '验证失败', error: lastError ? lastError.message : '所有验证尝试都失败',
testUrl: testUrl testUrl: testUrl
}; };
} }