发布网络质量探测 1.1.2 #1

Merged
YanJiu merged 12 commits from release/1.1.2 into main 2026-11-20 19:04:06 +08:00
Showing only changes of commit 420eed64f7 - Show all commits
+12 -3
View File
@@ -25,8 +25,17 @@ var metadataIP = net.ParseIP("169.254.169.254")
// Loopback, private, link-local and cloud metadata are rejected unless // Loopback, private, link-local and cloud metadata are rejected unless
// allowPrivate is set. Metadata addresses stay blocked either way. // allowPrivate is set. Metadata addresses stay blocked either way.
// Every resolved address is checked so a later DNS answer cannot rebind // Every resolved address is checked so a later DNS answer cannot rebind
// the dial onto a forbidden IP; the caller dials only the returned IP. // the dial onto a forbidden IP. targetOK returns the first allowed address;
// resolveTargets returns every allowed address in resolver order.
func targetOK(ctx context.Context, resolver resolver, host string, allowPrivate bool, extra []*net.IPNet) (net.IP, error) { func targetOK(ctx context.Context, resolver resolver, host string, allowPrivate bool, extra []*net.IPNet) (net.IP, error) {
ips, err := resolveTargets(ctx, resolver, host, allowPrivate, extra)
if err != nil {
return nil, err
}
return ips[0], nil
}
func resolveTargets(ctx context.Context, resolver resolver, host string, allowPrivate bool, extra []*net.IPNet) ([]net.IP, error) {
host = strings.TrimSpace(host) host = strings.TrimSpace(host)
host = strings.Trim(host, "[]") host = strings.Trim(host, "[]")
if host == "" { if host == "" {
@@ -39,7 +48,7 @@ func targetOK(ctx context.Context, resolver resolver, host string, allowPrivate
if !ipAllowed(ip, allowPrivate, extra) { if !ipAllowed(ip, allowPrivate, extra) {
return nil, fmt.Errorf("%w: %s", errForbiddenTarget, ip.String()) return nil, fmt.Errorf("%w: %s", errForbiddenTarget, ip.String())
} }
return ip, nil return []net.IP{ip}, nil
} }
if resolver == nil { if resolver == nil {
resolver = net.DefaultResolver resolver = net.DefaultResolver
@@ -65,7 +74,7 @@ func targetOK(ctx context.Context, resolver resolver, host string, allowPrivate
if len(allowed) != len(addrs) { if len(allowed) != len(addrs) {
return nil, fmt.Errorf("%w: %s resolved to a mix of public and blocked addresses", errForbiddenTarget, host) return nil, fmt.Errorf("%w: %s resolved to a mix of public and blocked addresses", errForbiddenTarget, host)
} }
return allowed[0], nil return allowed, nil
} }
func ipAllowed(ip net.IP, allowPrivate bool, extra []*net.IPNet) bool { func ipAllowed(ip net.IP, allowPrivate bool, extra []*net.IPNet) bool {