fix: 简化HTTP错误分析逻辑,消除误报

核心修复:
- 大幅简化analyzeHTTPError逻辑,移除容易误报的指示器
- 不再将SSL/TLS错误误判为HTTP服务(SSH/FTP被误报为HTTPS的根因)
- 统一错误处理:HTTP请求失败一律判定为非HTTP服务
- 协议预检查+HTTP验证的两阶段检测更加可靠

修复的误报:
- SSH端口22不再被误识别为HTTPS服务
- FTP端口21不再被误识别为HTTPS服务
- SMTP端口25不再被误识别为HTTPS服务
- 保持MySQL端口3306的正确识别(协议预检查直接过滤)

技术改进:
- 错误分析逻辑从40+行简化到15行
- 消除硬编码的协议指示器列表
- 基于协议预检查的信任机制:如果预检查通过但HTTP失败,说明不是HTTP
This commit is contained in:
ZacharyZcR 2025-09-02 00:08:17 +00:00
parent 9ad397f58f
commit 46cfa2a64d

View File

@ -261,49 +261,26 @@ func (w *WebPortDetector) tryHTTPConnectionDirect(ctx context.Context, host stri
func (w *WebPortDetector) analyzeHTTPError(err error) bool {
errStr := strings.ToLower(err.Error())
// 先检查明确的非Web服务错误
nonWebErrors := []string{
// 简化逻辑既然我们已经做了协议预检查到这里的都应该是可能的HTTP服务
// 只检查明确的连接错误
connectionErrors := []string{
"connection refused",
"no such host",
"no such host",
"network unreachable",
"timeout",
"deadline exceeded",
"connection reset",
"eof",
"timeout",
}
for _, nonWebErr := range nonWebErrors {
if strings.Contains(errStr, nonWebErr) {
for _, connErr := range connectionErrors {
if strings.Contains(errStr, connErr) {
return false
}
}
// 这些错误表明连接到了HTTP服务器只是协议或其他问题
webIndicators := []string{
"ssl handshake",
"certificate",
"tls",
"bad request",
"method not allowed",
"server gave http response",
}
// 特别处理malformed HTTP response通常表明是非HTTP协议
if strings.Contains(errStr, "malformed http response") {
// 检查是否包含明显的二进制数据如MySQL greeting包
if strings.Contains(errStr, "\\x00") || strings.Contains(errStr, "\\x") {
common.LogDebug(fmt.Sprintf("检测到二进制协议响应非HTTP服务: %s", err.Error()))
return false
}
// 如果是文本形式的malformed response可能仍是HTTP服务的变种
}
for _, indicator := range webIndicators {
if strings.Contains(errStr, indicator) {
return true
}
}
// 所有其他错误包括malformed response、SSL错误等都认为不是HTTP服务
// 因为我们已经通过协议预检查确认目标可能是HTTP服务
// 如果仍然出错说明不是标准的HTTP服务
common.LogDebug(fmt.Sprintf("HTTP请求失败判定为非HTTP服务: %s", err.Error()))
return false
}