fix: process IPv6 Link-Local address (#1657)
Some checks failed
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 8s

This commit is contained in:
wwqgtxx 2024-11-18 10:34:43 +08:00
parent 25b3c86d31
commit 80e4eaad14
4 changed files with 25 additions and 26 deletions

View File

@ -34,12 +34,5 @@ func SkipAuthRemoteAddress(addr string) bool {
} }
func skipAuth(addr netip.Addr) bool { func skipAuth(addr netip.Addr) bool {
if addr.IsValid() { return prefixesContains(skipAuthPrefixes, addr)
for _, prefix := range skipAuthPrefixes {
if prefix.Contains(addr.Unmap()) {
return true
}
}
}
return false
} }

View File

@ -31,27 +31,17 @@ func IsRemoteAddrDisAllowed(addr net.Addr) bool {
if err := m.SetRemoteAddr(addr); err != nil { if err := m.SetRemoteAddr(addr); err != nil {
return false return false
} }
return isAllowed(m.AddrPort().Addr().Unmap()) && !isDisAllowed(m.AddrPort().Addr().Unmap()) ipAddr := m.AddrPort().Addr()
if ipAddr.IsValid() {
return isAllowed(ipAddr) && !isDisAllowed(ipAddr)
}
return false
} }
func isAllowed(addr netip.Addr) bool { func isAllowed(addr netip.Addr) bool {
if addr.IsValid() { return prefixesContains(lanAllowedIPs, addr)
for _, prefix := range lanAllowedIPs {
if prefix.Contains(addr) {
return true
}
}
}
return false
} }
func isDisAllowed(addr netip.Addr) bool { func isDisAllowed(addr netip.Addr) bool {
if addr.IsValid() { return prefixesContains(lanDisAllowedIPs, addr)
for _, prefix := range lanDisAllowedIPs {
if prefix.Contains(addr) {
return true
}
}
}
return false
} }

View File

@ -61,3 +61,19 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
return metadata return metadata
} }
func prefixesContains(prefixes []netip.Prefix, addr netip.Addr) bool {
if len(prefixes) == 0 {
return false
}
if !addr.IsValid() {
return false
}
addr = addr.Unmap().WithZone("") // netip.Prefix.Contains returns false if ip has an IPv6 zone
for _, prefix := range prefixes {
if prefix.Contains(addr) {
return true
}
}
return false
}

View File

@ -40,7 +40,7 @@ func (i *IPCIDR) Match(metadata *C.Metadata) (bool, string) {
if i.isSourceIP { if i.isSourceIP {
ip = metadata.SrcIP ip = metadata.SrcIP
} }
return ip.IsValid() && i.ipnet.Contains(ip), i.adapter return ip.IsValid() && i.ipnet.Contains(ip.WithZone("")), i.adapter
} }
func (i *IPCIDR) Adapter() string { func (i *IPCIDR) Adapter() string {