diff --git a/src/services/validator.js b/src/services/validator.js index a1e7656..07c3a7a 100644 --- a/src/services/validator.js +++ b/src/services/validator.js @@ -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} 验证结果 {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 }; }