feat: 统一服务缓存 + 指纹驱动插件匹配

将 webServiceCache 扩展为通用 serviceCache,所有指纹识别结果
统一缓存,插件匹配时端口不命中则回退到服务名称匹配。

删除多余的 service_cache.go,复用已有的 ServiceInfo 体系。
补充 nil 防御、Explicit 标记、大量单元/集成/回归测试。
This commit is contained in:
ZacharyZcR
2026-06-14 22:23:47 +08:00
parent 2ab7c4d9b2
commit 5ad914a1bb
44 changed files with 1533 additions and 142 deletions
+29 -6
View File
@@ -7,9 +7,11 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
@@ -393,18 +395,39 @@ func (h *ResultHandler) Export(w http.ResponseWriter, r *http.Request) {
// extractPort 从 "ip:port" 中提取端口
func extractPort(target string) string {
if idx := strings.LastIndex(target, ":"); idx != -1 {
return target[idx+1:]
_, port, ok := splitTargetHostPort(target)
if !ok {
return ""
}
return ""
return port
}
// extractHost 从 "ip:port" 中提取主机
func extractHost(target string) string {
if idx := strings.LastIndex(target, ":"); idx != -1 {
return target[:idx]
host, _, ok := splitTargetHostPort(target)
if !ok {
return target
}
return target
return host
}
func splitTargetHostPort(target string) (string, string, bool) {
host, port, err := net.SplitHostPort(target)
if err != nil {
if strings.Count(target, ":") != 1 {
return "", "", false
}
parts := strings.SplitN(target, ":", 2)
host, port = parts[0], parts[1]
}
if host == "" || port == "" {
return "", "", false
}
portNum, err := strconv.Atoi(port)
if err != nil || portNum < 1 || portNum > 65535 {
return "", "", false
}
return host, port, true
}
// extractServiceInfo 从 details 中提取服务信息
+31
View File
@@ -0,0 +1,31 @@
//go:build web
package api
import "testing"
func TestExtractHostPortIPv6(t *testing.T) {
tests := []struct {
name string
target string
wantHost string
wantPort string
}{
{name: "ipv4", target: "192.168.1.1:80", wantHost: "192.168.1.1", wantPort: "80"},
{name: "hostname", target: "example.com:443", wantHost: "example.com", wantPort: "443"},
{name: "bracketed ipv6", target: "[2001:db8::1]:8443", wantHost: "2001:db8::1", wantPort: "8443"},
{name: "bare ipv6 without port", target: "2001:db8::1", wantHost: "2001:db8::1", wantPort: ""},
{name: "invalid port", target: "example.com:abc", wantHost: "example.com:abc", wantPort: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractHost(tt.target); got != tt.wantHost {
t.Fatalf("extractHost(%q) = %q, want %q", tt.target, got, tt.wantHost)
}
if got := extractPort(tt.target); got != tt.wantPort {
t.Fatalf("extractPort(%q) = %q, want %q", tt.target, got, tt.wantPort)
}
})
}
}