add ip range matcher for bypass

This commit is contained in:
ginuerzh
2024-12-23 21:06:55 +08:00
parent be8e0555e5
commit a5d4774682
3 changed files with 92 additions and 1 deletions
+35
View File
@@ -2,6 +2,7 @@ package matcher
import (
"net"
"net/netip"
"strconv"
"strings"
@@ -177,6 +178,7 @@ type wildcardMatcherPattern struct {
glob glob.Glob
pr *xnet.PortRange
}
type wildcardMatcher struct {
patterns []wildcardMatcherPattern
}
@@ -224,3 +226,36 @@ func (m *wildcardMatcher) Match(addr string) bool {
return false
}
type ipRangeMatcher struct {
ranges []xnet.IPRange
}
func IPRangeMatcher(ranges []xnet.IPRange) Matcher {
matcher := &ipRangeMatcher{
ranges: ranges,
}
return matcher
}
func (m *ipRangeMatcher) Match(addr string) bool {
if m == nil || len(m.ranges) == 0 {
return false
}
host, _, _ := net.SplitHostPort(addr)
if host == "" {
host = addr
}
adr, err := netip.ParseAddr(host)
if err != nil {
return false
}
for _, ra := range m.ranges {
if ra.Contains(adr) {
return true
}
}
return false
}