Files
magpie/internal/netguard/netguard_test.go
T
yetoneandggbdpq 240f3eb4c5 gateway: a name a fake-IP resolver answers is fetched through its TUN or a local proxy (#2593)
With Clash's TUN fake-ip (Clash Verge Rev on Windows, TomXPRIME), Surge's
enhanced mode (h4rk8s) or Loon on, the system resolver answers every name
in 198.18.0.0/15, which the download guard (image_fetch_guard.go) listed
as special. So web_fetch, a caller's image (fetchPicture), a vendor's
image or video by URL and generate_image's download (imagemcp) refused
every URL as "a private address". All of them go through publicTransport;
nothing else uses it.

What changed:
- The address rule moved into internal/netguard (Public, FakeIP,
  LoonPaired), shared by the gateway's guard and a provider's icon
  (icon.go's publicIP is now netguard.Public or FakeIP, as #252 meant).
  The icon guard gets the gateway's stricter list with it (Teredo, 240/4,
  NAT64 and 6to4 forms of a private address are refused there too).
- publicTransport: a name (never an address literal) whose every answer
  is in 198.18.0.0/15 is fetched. With no proxy (pure TUN) the fake
  address is pinned and dialled, and the dial guard lets that one address
  in, no other. Through a proxy on this computer (loopback or localhost)
  the proxy is given the name, never the fake address: h4rk8s showed a
  real Surge CONNECT by name works and by the 198.18 address doesn't. A
  plain http:// request's Host to that proxy is the checked name, never
  the caller's own Host. Through a proxy elsewhere it is refused.
- An answer with any other address beside the fake ones (private or
  public) is judged as before, so fake + 192.168.x is refused; loopback,
  RFC 1918, link-local with 169.254.169.254, CGNAT, unspecified,
  multicast and documentation stay refused; every non-fake answer is
  still pinned; redirects are re-checked as before.
- Loon's synthesized AAAA (fd27:712::/96 holding an A answer of the same
  reply) is left out and the A answer judged in its place, as icon.go
  already did, so a Loon answer isn't refused as fc00::/7.
- docs/subsystems/gateway-routing.md says all of this, and the trust it
  asks for: where a fake-IP name really goes is decided by the fake-IP
  resolver, which magpie can't see, so a name pointing at the user's
  network is reached if that resolver's rules send it there. The icon
  path already accepted this (#252).

Credit: ggbdpq found the cause and wrote both patches on the issue
(hunt/2593-webfetch-fakeip, then hunt51-a-fakeip-proxy: the fake-only,
loopback-proxy, name-not-literal predicate and its test shapes, which
this follows); h4rk8s's real Surge comparison showed the pin had to go
for a proxy; TomXPRIME's Windows report showed pure TUN needs the direct
dial.

Run (darwin/arm64, worktree rebased on origin/main 226551d0d):
- New tests in internal/gateway/fakeip_test.go, with a fake resolver, a
  local proxy recording what it is asked and a local server standing in
  for the TUN (dialPinned seam); nothing reaches the network:
  TestFakeIPNameGoesToALocalProxyByName (web_fetch reads the page; proxy
  asked "http://example.com:80/", CONNECT example.com:443, a foreign Host
  ignored, Loon pair, redirect to a mixed answer refused),
  TestFakeIPNameDialledThroughTheTUN (198.18.0.7:80 dialled, guard
  refuses 198.18.0.8, 198.19.1.1, 127.0.0.1, 192.168.1.10),
  TestFakeIPRefusesTheRest (13 URLs refused with no proxy, a 127.0.0.1
  proxy and a localhost proxy; a proxy elsewhere refused; neither proxy
  nor TUN asked). internal/netguard: TestPublic, TestFakeIP,
  TestLoonPaired.
- Red on the old behaviour: fakeAnswer forced false (what main does)
  fails ByName and DialledThroughTheTUN with the private-address refusal.
  Mutations, each red: no byName (proxy asked 198.18), literals allowed,
  any-fake answer counted, proxy elsewhere allowed, dial guard letting any
  address in, the caller's Host to an http proxy, Loon not dropped.
- Existing guard tests (TestPublicAddr, TestEditImage*, TestWebFetch*,
  TestPrivateDialer*, TestFetchPrivate*) and provider's icon tests pass.
- gofmt -l clean; go vet darwin, GOOS=linux and GOOS=windows -tags nogui.
- Full suite at 30611fe68 (this change on 226551d0d): set -o pipefail;
  go test -timeout 45m ./... exit 0, 69 packages ok, no FAIL (14 min).

Not tried: a real Clash, Clash Verge, Surge or Loon (none on this
machine); the proxy and the TUN are local stand-ins, so whether a real
fake-IP core routes the name as h4rk8s saw is their evidence, not this
run's. A SOCKS proxy on loopback is not covered by a test (Go sends the
name in the SOCKS request the same way). Kept open for the reporters.

Co-authored-by: ggbdpq <67929842+ggbdpq@users.noreply.github.com>
2026-10-22 19:53:16 +08:00

64 lines
2.3 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)
}
}
}