policyutil: match single-address destinations by text

A node without routes is reached only at its own addresses; skip
parsing. member-member, 5000 nodes: 1471 -> 490 ms per pass.
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 12:32:04 +01:00
1 parent 4b2d227f0e
commit 800392ae89
1 file changed
+20
+20
View File
@@ -3,6 +3,7 @@ package policyutil
import (
"net/netip"
"slices"
"strings"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
@@ -28,6 +29,17 @@ func ReduceFilterRules(
subnetRoutes := node.SubnetRoutes()
exitRoutes := node.ExitRoutes()
// A rule lists every node address it names, so most entries are single
// addresses. Without routes, one reaches the node only as one of its own
// addresses; both are written by [netip.Addr.String], so the text says
// so without parsing every entry for every node.
routed := len(subnetRoutes) > 0 || len(exitRoutes) > 0
ownIPs := make([]string, len(nodeIPs))
for i, ip := range nodeIPs {
ownIPs[i] = ip.String()
}
for _, rule := range rules {
// Handle CapGrant rules separately — they use CapGrant[].Dsts
// instead of DstPorts for destination matching.
@@ -44,6 +56,14 @@ func ReduceFilterRules(
var dests []tailcfg.NetPortRange
for _, dest := range rule.DstPorts {
if !routed && !strings.ContainsAny(dest.IP, "/-*") {
if slices.Contains(ownIPs, dest.IP) {
dests = append(dests, dest)
}
continue
}
if destReachesNode(dest.IP, nodeIPs, subnetRoutes, exitRoutes) {
dests = append(dests, dest)
}