jiezhengj (Discord, 11-08): under Clash Verge Rev's TUN (mihomo
1.19.33, enhanced-mode fake-ip, fake-ip-range 198.18.0.1/16) every
web_fetch of `magpie mcp search` answered "it is at a private address".
#2593 already lets a name whose every answer is in 198.18.0.0/15 through,
so that wasn't it. Clash Verge Rev writes dns.ipv6: true and
fake-ip-range6: fdfe:dcba:9876::1/64 into its default config (and fills
in 2001:2::/64 when IPv6 is on without one), and mihomo then answers
AAAA from that pool too (dns/middleware.go withFakeIP, read at
v1.19.33). The resolver's answer was 198.18.x.y beside fdfe:dcba:9876::z,
the ULA is private, a fake answer beside a private one is refused, so
every name failed.
netguard.FakePaired names that AAAA: an IPv6 address in fc00::/7 or
2001:2::/48 while the same answer holds an IPv4 one in 198.18.0.0/15.
It is dropped where Loon's paired AAAA is dropped (dropLoon in the
gateway's publicTransport, iconIPs for a provider's icon, the two
places that share netguard), and the fake A is judged and pinned in its
place. 2001:2::/48 (RFC 5180 benchmarking) joins Public's special ranges;
it wasn't covered by 2001::/32 and passed as public.
Why this opens nothing new: the AAAA is never dialled. What is dialled
is the fake A, exactly as when the resolver answered the A alone, which
#2593 already allows: an address in 198.18.0.0/15, which no host on a
normal network uses, so without a TUN it goes to the default route and
reaches the internet or nothing. A hostile DNS that pairs 198.18.0.9
with fd00::<a LAN host> gets the 198.18 address dialled, never the LAN
one. Through a proxy on this computer the proxy gets the name, as for a
pure fake answer; a hostile resolver could already give a proxy another
answer for that name than it gave magpie, so the pairing adds no route.
What stays refused (tests below): a ULA or 2001:2:: alone or beside a
real A, a fake pair beside 10.0.0.1 or ::1, a fake A beside a public
address, and ULA / 2001:2:: / 198.18 address literals.
Ran (on mote; whole packages at base f1a4b5751, the -run set, vet and
gofmt again after the rebase onto the next main):
- go test ./internal/netguard ./internal/provider ./internal/gateway
-run 'FakePaired|FakeIP|Loon|Icon|Public|Fetch|Guard|Private': ok
- go test -timeout 45m ./internal/netguard ./internal/provider
./internal/gateway (whole packages): ok (netguard 0.2s, provider
130s, gateway 838s)
- go vet for darwin, GOOS=linux and GOOS=windows on those packages,
gofmt -l internal cmd: clean
Broken by hand: with netguard.go, image_fetch_guard.go and icon.go back
at origin/main, TestFakeIPWithItsIPv6Answer fails with the reporter's
own message ("couldn't fetch http://example.com/: it is at a private
address ..."), TestFakeIPRefusesTheRest lets http://[2001:2::7]/
through, and TestIconIPsLoon refuses fdfe:dcba:9876::5 + 198.18.0.5 and
keeps 2001:2::5. Green with the fix.
Not tried: a real Clash Verge Rev / mihomo TUN on mote or the Mac (the
answers are a fake resolver in the tests, nothing reaches the network);
sing-box's inet6_range is covered only as far as it is a ULA. Not run:
the full ./... suite (mote's suite.lock had five full runs queued).
Reported-by: jiezhengj (Discord)
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package netguard
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
)
|
|
|
|
func TestPublic(t *testing.T) {
|
|
for s, want := range map[string]bool{
|
|
"127.0.0.1": false, "10.1.2.3": false, "172.16.0.1": false, "192.168.1.1": false,
|
|
"169.254.169.254": false, "100.64.0.1": false, "0.0.0.0": false, "224.0.0.1": false,
|
|
"192.0.2.1": false, "198.51.100.1": false, "203.0.113.1": false, "240.0.0.1": false,
|
|
"198.18.0.0": false, "198.19.255.255": false, "::ffff:198.18.0.7": false, // fake-IP: FakeIP's, not public
|
|
"::1": false, "fc00::1": false, "fe80::1": false, "2001::1": false, "2001:db8::1": false,
|
|
"64:ff9b::7f00:1": false, "2002:c612:7::": false,
|
|
"8.8.8.8": true, "93.184.216.34": true, "198.17.255.255": true, "198.20.0.0": true,
|
|
"2606:4700::1111": true, "64:ff9b::808:808": true,
|
|
} {
|
|
if got := Public(netip.MustParseAddr(s)); got != want {
|
|
t.Errorf("Public(%s) = %v, want %v", s, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFakeIP(t *testing.T) {
|
|
for s, want := range map[string]bool{
|
|
"198.18.0.0": true, "198.18.0.7": true, "198.19.255.255": true, "::ffff:198.18.0.7": true,
|
|
"198.17.255.255": false, "198.20.0.0": false, "127.0.0.1": false, "10.0.0.1": false,
|
|
"fd27:712::c612:7": false, "2002:c612:7::": false,
|
|
} {
|
|
if got := FakeIP(netip.MustParseAddr(s)); got != want {
|
|
t.Errorf("FakeIP(%s) = %v, want %v", s, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoonPaired(t *testing.T) {
|
|
addrs := func(ss ...string) []netip.Addr {
|
|
var out []netip.Addr
|
|
for _, s := range ss {
|
|
out = append(out, netip.MustParseAddr(s))
|
|
}
|
|
return out
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
a string
|
|
answers []netip.Addr
|
|
want bool
|
|
}{
|
|
{"paired with a fake A", "fd27:712::c612:139", addrs("198.18.1.57", "fd27:712::c612:139"), true},
|
|
{"paired with a real A", "fd27:712::c600:1061", addrs("fd27:712::c600:1061", "198.0.16.97"), true},
|
|
{"paired, the A IPv4-mapped", "fd27:712::c600:1061", addrs("::ffff:198.0.16.97"), true},
|
|
{"unpaired", "fd27:712::c600:1061", addrs("fd27:712::c600:1061"), false},
|
|
{"another A", "fd27:712::c600:1061", addrs("8.8.8.8"), false},
|
|
{"outside Loon's /96", "fd27:712:1::c600:1061", addrs("198.0.16.97"), false},
|
|
{"an IPv4 address", "198.0.16.97", addrs("198.0.16.97"), false},
|
|
} {
|
|
if got := LoonPaired(netip.MustParseAddr(c.a), c.answers); got != c.want {
|
|
t.Errorf("%s: %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFakePaired(t *testing.T) {
|
|
addrs := func(ss ...string) []netip.Addr {
|
|
var out []netip.Addr
|
|
for _, s := range ss {
|
|
out = append(out, netip.MustParseAddr(s))
|
|
}
|
|
return out
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
a string
|
|
answers []netip.Addr
|
|
want bool
|
|
}{
|
|
{"Clash Verge Rev's default range6", "fdfe:dcba:9876::7", addrs("198.18.0.7", "fdfe:dcba:9876::7"), true},
|
|
{"its filled-in 2001:2::/64", "2001:2::7", addrs("2001:2::7", "198.18.0.7"), true},
|
|
{"a sing-box fc00::/18", "fc00::7", addrs("198.19.0.7"), true},
|
|
{"the A IPv4-mapped", "fdfe:dcba:9876::7", addrs("::ffff:198.18.0.7"), true},
|
|
{"beside a real A", "fdfe:dcba:9876::7", addrs("93.184.216.34"), false},
|
|
{"beside a private A", "fdfe:dcba:9876::7", addrs("10.0.0.1"), false},
|
|
{"alone", "fdfe:dcba:9876::7", addrs("fdfe:dcba:9876::7"), false},
|
|
{"loopback", "::1", addrs("198.18.0.7"), false},
|
|
{"link-local", "fe80::1", addrs("198.18.0.7"), false},
|
|
{"outside 2001:2::/48", "2001:3::7", addrs("198.18.0.7"), false},
|
|
{"an IPv4 address", "10.0.0.1", addrs("198.18.0.7"), false},
|
|
{"the fake A itself", "198.18.0.7", addrs("198.18.0.7"), false},
|
|
} {
|
|
if got := FakePaired(netip.MustParseAddr(c.a), c.answers); got != c.want {
|
|
t.Errorf("%s: %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|