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:
parent
a31fe3f892
commit
76683fb519
@ -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,18 +88,16 @@ 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 = {
|
const result = {
|
||||||
ip: ip,
|
ip: ip,
|
||||||
port: parseInt(port),
|
port: parseInt(port),
|
||||||
isValid: isValid,
|
isValid: true,
|
||||||
responseTime: responseTime,
|
responseTime: responseTime,
|
||||||
error: null,
|
error: null,
|
||||||
testUrl: testUrl
|
testUrl: testUrl
|
||||||
@ -105,7 +106,7 @@ class ProxyValidator {
|
|||||||
// 更新数据库中的验证结果(如果需要)
|
// 更新数据库中的验证结果(如果需要)
|
||||||
if (updateDatabase) {
|
if (updateDatabase) {
|
||||||
try {
|
try {
|
||||||
await ProxyModel.updateValidity(ip, port, isValid ? 1 : 0, responseTime);
|
await ProxyModel.updateValidity(ip, port, 1, responseTime);
|
||||||
} catch (dbError) {
|
} catch (dbError) {
|
||||||
// 如果代理不在数据库中,忽略更新错误
|
// 如果代理不在数据库中,忽略更新错误
|
||||||
if (!dbError.message.includes('not found')) {
|
if (!dbError.message.includes('not found')) {
|
||||||
@ -115,6 +116,27 @@ class ProxyValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 请求成功但验证失败(响应内容不正确),记录错误并继续重试
|
||||||
|
if (logResult) {
|
||||||
|
console.log(`✗ 代理 ${ip}:${port} 第 ${attemptNumber} 次尝试验证失败,响应内容不正确`);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastError = new Error('响应内容验证失败');
|
||||||
|
lastResult = {
|
||||||
|
ip: ip,
|
||||||
|
port: parseInt(port),
|
||||||
|
isValid: false,
|
||||||
|
responseTime: responseTime,
|
||||||
|
error: '响应内容验证失败',
|
||||||
|
testUrl: testUrl
|
||||||
|
};
|
||||||
|
|
||||||
|
// 如果还有重试机会,继续尝试
|
||||||
|
if (attempt < retryCount) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const responseTime = Date.now() - startTime;
|
const responseTime = Date.now() - startTime;
|
||||||
@ -128,20 +150,26 @@ 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) {
|
if (logResult) {
|
||||||
console.log(`✗ 代理 ${ip}:${port} 验证失败: ${error.message}`);
|
console.log(`✗ 代理 ${ip}:${port} 所有 ${retryCount + 1} 次尝试都失败`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新数据库中的验证结果(如果需要)
|
// 更新数据库中的验证结果(如果需要)
|
||||||
if (updateDatabase) {
|
if (updateDatabase && lastResult) {
|
||||||
try {
|
try {
|
||||||
await ProxyModel.updateValidity(ip, port, 0, responseTime);
|
await ProxyModel.updateValidity(ip, port, 0, lastResult.responseTime);
|
||||||
} catch (dbError) {
|
} catch (dbError) {
|
||||||
// 如果代理不在数据库中,忽略更新错误
|
// 如果代理不在数据库中,忽略更新错误
|
||||||
if (!dbError.message.includes('not found')) {
|
if (!dbError.message.includes('not found')) {
|
||||||
@ -150,17 +178,12 @@ class ProxyValidator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastResult;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 所有重试都失败了
|
|
||||||
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
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user