Make private ip the same as geoip:private

Currently ip.IsPrivate and netip.Addr.IsPrivate only matches a part of private IPs.
This commit is contained in:
hamjin 2024-02-25 22:11:04 +08:00
parent 78b4b11f26
commit 088fdb2ddd
3 changed files with 41 additions and 2 deletions

View file

@ -6,6 +6,28 @@ import (
"net/netip"
)
// Private IP CIDRs
var privateIPCIDRs = []string{
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.88.99.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/3",
"::/127",
"fc00::/7",
"fe80::/10",
"ff00::/8",
}
// IpToAddr converts the net.IP to netip.Addr.
// If slice's length is not 4 or 16, IpToAddr returns netip.Addr{}
func IpToAddr(slice net.IP) netip.Addr {
@ -51,3 +73,18 @@ func UnMasked(p netip.Prefix) netip.Addr {
}
return addr
}
// IsPrivateIP returns whether IP is private
// If IP is private, return true, else return false
func IsPrivateIP(ip netip.Addr) bool {
for _, network := range privateIPCIDRs {
_, subnet, err := net.ParseCIDR(network)
if err != nil {
continue
}
if subnet.Contains(ip.AsSlice()) {
return true
}
}
return false
}

View file

@ -4,6 +4,7 @@ import (
"net/netip"
"strings"
"github.com/metacubex/mihomo/common/nnip"
"github.com/metacubex/mihomo/component/geodata"
"github.com/metacubex/mihomo/component/geodata/router"
"github.com/metacubex/mihomo/component/mmdb"
@ -26,7 +27,7 @@ func (gf *geoipFilter) Match(ip netip.Addr) bool {
if !C.GeodataMode {
codes := mmdb.Instance().LookupCode(ip.AsSlice())
for _, code := range codes {
if !strings.EqualFold(code, gf.code) && !ip.IsPrivate() {
if !strings.EqualFold(code, gf.code) && !nnip.IsPrivateIP(ip) {
return true
}
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/metacubex/mihomo/common/nnip"
"github.com/metacubex/mihomo/component/geodata"
"github.com/metacubex/mihomo/component/geodata/router"
"github.com/metacubex/mihomo/component/mmdb"
@ -32,7 +33,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
}
if strings.EqualFold(g.country, "LAN") {
return ip.IsPrivate() ||
return nnip.IsPrivateIP(ip) ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||