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