Files
magpie/internal/gateway/fakeip_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

253 lines
9.3 KiB
Go

package gateway
import (
"context"
"errors"
"net"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// #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, and every web_fetch, caller's image and
// vendor's image was refused as "a private address". These tests stand a
// fake resolver in for the fake-IP one, a local server for the TUN or the
// proxy; nothing reaches the network.
// answers points lookupIP at several addresses per name.
func answers(t *testing.T, names map[string][]string) {
t.Helper()
was := lookupIP
lookupIP = func(_ context.Context, host string) ([]netip.Addr, error) {
as, ok := names[host]
if !ok {
return nil, errors.New("no such host in the test: " + host)
}
var out []netip.Addr
for _, a := range as {
out = append(out, netip.MustParseAddr(a))
}
return out, nil
}
t.Cleanup(func() { lookupIP = was })
}
// useProxy sets magpie's proxy ("direct" for none) for the test.
func useProxy(t *testing.T, v string) {
t.Helper()
was := settings.Load()
st := was
st.Proxy = v
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { settings.Save(was) })
}
// tun stands a local server in for the TUN: a direct request's dial to the
// pinned address goes there, once the guard has said the address may be
// dialled. It records the addresses dialled, and fails the test when the
// same dial guard would let in another address in the fake range, or a
// private one.
func tun(t *testing.T, srv *httptest.Server) func() []string {
t.Helper()
var mu sync.Mutex
var dialled []string
was := dialPinned
dialPinned = func(ctx context.Context, d *net.Dialer, network, addr string) (net.Conn, error) {
mu.Lock()
dialled = append(dialled, addr)
mu.Unlock()
if err := d.Control(network, addr, nil); err != nil {
return nil, err
}
for _, other := range []string{"198.18.0.8:80", "198.19.1.1:443", "127.0.0.1:80", "192.168.1.10:80"} {
if other != addr && d.Control(network, other, nil) == nil {
t.Errorf("dialling %s, the guard would let %s in too", addr, other)
}
}
return (&net.Dialer{}).DialContext(ctx, network, srv.Listener.Addr().String())
}
t.Cleanup(func() { dialPinned = was })
return func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), dialled...) }
}
// recorder is a proxy on this computer that records what it is asked: the
// URL of a plain request, the authority of a CONNECT.
func fakeProxy(t *testing.T, body string) (*httptest.Server, func() []string) {
t.Helper()
var mu sync.Mutex
var asked []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
if r.Method == http.MethodConnect {
asked = append(asked, "CONNECT "+r.Host)
} else {
asked = append(asked, r.URL.String()+" Host="+r.Host)
}
mu.Unlock()
if r.Method == http.MethodConnect {
w.WriteHeader(http.StatusOK) // and forwards nowhere: TLS fails after
return
}
switch r.URL.Path {
case "/to-private":
http.Redirect(w, r, "http://inside.test/", http.StatusFound)
default:
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(body))
}
}))
t.Cleanup(srv.Close)
return srv, func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), asked...) }
}
func guardedGet(u string) error {
req, _ := http.NewRequest("GET", u, nil)
resp, err := publicClient.Do(req)
if err == nil {
resp.Body.Close()
}
return err
}
// Through a proxy on this computer (Clash's 127.0.0.1:7890, Surge's
// 6152), a name the fake-IP resolver answered is handed to the proxy by
// its name: a real proxy can't route the 198.18 address (h4rk8s: CONNECT
// by the name went through, by the fake address didn't). web_fetch reads
// the page; https:// is a CONNECT by name.
func TestFakeIPNameGoesToALocalProxyByName(t *testing.T) {
fresh(t)
proxy, asked := fakeProxy(t, "<html><head><title>Example Domain</title></head><body><p>fetched by name</p></body></html>")
useProxy(t, proxy.URL)
answers(t, map[string][]string{
"example.com": {"198.18.0.7"},
"loon.test": {"198.18.1.57", "fd27:712::c612:139"}, // Loon's AAAA for its own A
"inside.test": {"198.18.0.9", "192.168.1.10"},
})
h := lanGuard(New().Handler())
got, isErr := fetchText_(t, h, fetchCall("http://example.com/", ""))
if isErr || !strings.Contains(got, "fetched by name") {
t.Fatalf("web_fetch of a fake-IP name through a local proxy: %v %q", isErr, got)
}
if a := asked(); len(a) != 1 || a[0] != "http://example.com:80/ Host=example.com:80" {
t.Fatalf("the proxy was asked %q, want the name", a)
}
if err := guardedGet("http://loon.test/x.png"); err != nil {
t.Fatalf("Loon's answer: %v", err)
}
if a := asked(); len(a) != 2 || !strings.HasPrefix(a[1], "http://loon.test:80/x.png") {
t.Fatalf("Loon: the proxy was asked %q", a)
}
// https://: the CONNECT names the host, never 198.18
if err := guardedGet("https://example.com/x.png"); err == nil {
t.Fatal("a CONNECT that forwards nowhere answered")
}
if a := asked(); len(a) != 3 || a[2] != "CONNECT example.com:443" {
t.Fatalf("https: the proxy was asked %q", a)
}
// a request's own Host can't name the proxy another target
req, _ := http.NewRequest("GET", "http://example.com/", nil)
req.Host = "169.254.169.254"
if resp, err := publicClient.Do(req); err == nil {
resp.Body.Close()
}
if a := asked(); len(a) != 4 || a[3] != "http://example.com:80/ Host=example.com:80" {
t.Fatalf("a foreign Host: the proxy was asked %q", a)
}
// a redirect is judged again: one to a name with a private answer
// beside its fake one is refused before the proxy is asked for it
if err := guardedGet("http://example.com/to-private"); !errors.Is(err, errPrivateAddress) {
t.Fatalf("a redirect to a mixed answer: %v", err)
}
if a := asked(); len(a) != 5 {
t.Fatalf("a redirect to a mixed answer reached the proxy: %q", a)
}
}
// Pure TUN, no proxy set (TomXPRIME: Windows, Clash Verge Rev, no
// HTTPS_PROXY): the fake address is pinned and dialled, and the TUN
// carries it. The dial guard lets that one address in, no other in the
// range.
func TestFakeIPNameDialledThroughTheTUN(t *testing.T) {
fresh(t)
useProxy(t, "direct")
page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("through the tun " + r.Host))
}))
t.Cleanup(page.Close)
dialled := tun(t, page)
answers(t, map[string][]string{"example.com": {"198.18.0.7"}, "real.test": {"93.184.216.34", "fd27:712::5db8:d822"}})
h := lanGuard(New().Handler())
got, isErr := fetchText_(t, h, fetchCall("http://example.com/", ""))
if isErr || !strings.Contains(got, "through the tun example.com") {
t.Fatalf("web_fetch of a fake-IP name with no proxy: %v %q", isErr, got)
}
if d := dialled(); len(d) != 1 || d[0] != "198.18.0.7:80" {
t.Fatalf("dialled %q, want the pinned fake address", d)
}
// a real answer is pinned as ever; Loon's AAAA beside it isn't dialled
if err := guardedGet("http://real.test/"); err != nil {
t.Fatalf("a real answer: %v", err)
}
if d := dialled(); len(d) != 2 || d[1] != "93.184.216.34:80" {
t.Fatalf("dialled %q", d)
}
}
// What stays refused, with a proxy on this computer and without: a
// private answer, a fake answer beside a private (or any real) one, a
// fake address written as the URL, and a fake answer through a proxy
// elsewhere. Neither the proxy nor the TUN is asked.
func TestFakeIPRefusesTheRest(t *testing.T) {
fresh(t)
page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("reached")) }))
t.Cleanup(page.Close)
dialled := tun(t, page)
answers(t, map[string][]string{
"example.com": {"198.18.0.7"},
"lan.test": {"192.168.1.10"},
"loop.test": {"127.0.0.1"},
"meta.test": {"169.254.169.254"},
"cgnat.test": {"100.64.0.1"},
"ten.test": {"10.0.0.1"},
"mixed.test": {"198.18.0.9", "192.168.1.10"},
"mixed6.test": {"198.18.0.9", "::1"},
"mixedpub.test": {"198.18.0.9", "93.184.216.34"},
"loonlan.test": {"fd27:712::a00:1", "10.0.0.1"},
"loonbare.test": {"fd27:712::c612:139"},
})
refused := []string{
"http://lan.test/", "http://loop.test/", "http://meta.test/latest/meta-data/", "http://cgnat.test/",
"http://ten.test/", "http://mixed.test/", "http://mixed6.test/", "http://mixedpub.test/",
"http://loonlan.test/", "http://loonbare.test/", "http://198.18.0.7/", "https://198.18.0.7/",
"http://[::ffff:198.18.0.7]/",
}
proxy, asked := fakeProxy(t, "reached")
for _, p := range []string{"direct", proxy.URL, "http://localhost:" + proxy.URL[strings.LastIndex(proxy.URL, ":")+1:]} {
useProxy(t, p)
for _, u := range refused {
if err := guardedGet(u); !errors.Is(err, errPrivateAddress) {
t.Errorf("proxy %s, %s: %v, want the private-address refusal", p, u, err)
}
}
}
// a proxy elsewhere can't be handed the name for a fake answer
// (203.0.113.9 is never dialled: the request is refused first)
useProxy(t, "http://203.0.113.9:3128")
if err := guardedGet("http://example.com/"); !errors.Is(err, errPrivateAddress) {
t.Errorf("a fake answer through a proxy elsewhere: %v", err)
}
if a, d := asked(), dialled(); len(a) != 0 || len(d) != 0 {
t.Fatalf("the proxy was asked %q, the TUN dialled %q", a, d)
}
}