From ef7f2d82c0ba41ed693306b1bc7bfec3d7c9090c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 9 Sep 2022 14:01:59 +0800 Subject: [PATCH] Fix match 4in6 address in ip_cidr --- route/rule_cidr.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/route/rule_cidr.go b/route/rule_cidr.go index b72d1e10..988bfd7e 100644 --- a/route/rule_cidr.go +++ b/route/rule_cidr.go @@ -59,13 +59,13 @@ func NewIPCIDRItem(isSource bool, prefixStrings []string) (*IPCIDRItem, error) { func (r *IPCIDRItem) Match(metadata *adapter.InboundContext) bool { if r.isSource { - return r.ipSet.Contains(metadata.Source.Addr) + return r.match(metadata.Source.Addr) } else { if metadata.Destination.IsIP() { - return r.ipSet.Contains(metadata.Destination.Addr) + return r.match(metadata.Destination.Addr) } else { for _, address := range metadata.DestinationAddresses { - if r.ipSet.Contains(address) { + if r.match(address) { return true } } @@ -74,6 +74,14 @@ func (r *IPCIDRItem) Match(metadata *adapter.InboundContext) bool { return false } +func (r *IPCIDRItem) match(address netip.Addr) bool { + if address.Is4In6() { + return r.ipSet.Contains(netip.AddrFrom4(address.As4())) + } else { + return r.ipSet.Contains(address) + } +} + func (r *IPCIDRItem) String() string { return r.description }