diff --git a/internal/gateway/lan.go b/internal/gateway/lan.go index b5a75bd1d..05105818e 100644 --- a/internal/gateway/lan.go +++ b/internal/gateway/lan.go @@ -6,6 +6,7 @@ import ( "log" "net" "net/http" + "net/netip" "net/url" "os" "path/filepath" @@ -16,6 +17,7 @@ import ( "github.com/yetone/magpie/internal/access" "github.com/yetone/magpie/internal/budget" + "github.com/yetone/magpie/internal/netguard" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/usage" @@ -182,19 +184,36 @@ func LANURLs() []string { // the other). Private ones come first: in the interfaces' order the public // one came first, and Settings offered it as the address under "Share on // local network" (the same user, asking why sharing was on the internet). +// Of the private ones, RFC 1918's (a home or office network) come before +// carrier-grade NAT's (Tailscale). A proxy's TUN address is left out: +// 198.18.0.0/15 (netguard.FakeIP), which Clash, Mihomo, sing-box, Surge +// and Stash take for their TUN interfaces and fake-ip answers, and which +// no network hands out: no other device reaches this computer there, and +// Settings offered http://198.18.0.1 instead of 192.168.110.x (coeo91 on +// Discord). It goes by the address, not the interface's name: Tailscale's +// is a utun too, and its address is reached. func LANHosts() []string { - var near, far []string + var home, near, far []string for _, ip := range hostIPs() { - if ip.To4() == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() { + if ip.To4() == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() || fakeIP(ip) { continue } - if PublicIP(ip) { + switch { + case ip.IsPrivate(): + home = append(home, ip.String()) + case PublicIP(ip): far = append(far, ip.String()) - } else { + default: near = append(near, ip.String()) } } - return append(near, far...) + return slices.Concat(home, near, far) +} + +// fakeIP: ip is a proxy's TUN or fake-ip address (netguard.FakeIP). +func fakeIP(ip net.IP) bool { + a, ok := netip.AddrFromSlice(ip) + return ok && netguard.FakeIP(a) } // sharedRanges are IPv4 ranges outside RFC 1918 that still aren't the diff --git a/internal/gateway/lan_hosts_test.go b/internal/gateway/lan_hosts_test.go index 6b85da90d..84c0736c2 100644 --- a/internal/gateway/lan_hosts_test.go +++ b/internal/gateway/lan_hosts_test.go @@ -24,7 +24,7 @@ func TestLANHostsKeepAPublicAddress(t *testing.T) { } // private ones first, so the address Settings offers by default is on // this network; the public ones after, in the interfaces' order - want := []string{"10.77.70.2", "100.64.0.1", "198.18.0.1", "203.236.50.70", "8.8.4.4"} + want := []string{"10.77.70.2", "100.64.0.1", "203.236.50.70", "8.8.4.4"} if got := LANHosts(); !slices.Equal(got, want) { t.Fatalf("LANHosts: %v, want %v", got, want) } @@ -35,7 +35,38 @@ func TestLANHostsKeepAPublicAddress(t *testing.T) { if got := LANURLs(); !slices.Equal(got, urls) { t.Errorf("LANURLs: %v, want %v", got, urls) } - if got := PublicURLs(urls); !slices.Equal(got, urls[3:]) { - t.Errorf("PublicURLs: %v, want %v", got, urls[3:]) + if got := PublicURLs(urls); !slices.Equal(got, urls[2:]) { + t.Errorf("PublicURLs: %v, want %v", got, urls[2:]) + } +} + +// A proxy's TUN interface (Clash, Surge, sing-box fake-ip) holds an +// address in 198.18.0.0/15, which may be listed before the Wi-Fi's. It is +// this computer's alone, so Settings never offers it: with it first, +// "Share on local network" showed http://198.18.0.1 instead of the +// 192.168.110.x other devices reach (coeo91 on Discord). A home network's +// address comes before Tailscale's carrier-grade NAT one. +func TestLANHostsLeaveOutAProxysTUN(t *testing.T) { + t.Setenv("MAGPIE_PUBLIC_URL", "") + t.Setenv("MAGPIE_ADDR", "") + was := hostIPs + t.Cleanup(func() { hostIPs = was }) + hostIPs = func() []net.IP { + var out []net.IP + for _, s := range []string{"198.18.0.1", "100.101.2.3", "198.19.255.1", "192.168.110.23"} { + out = append(out, net.ParseIP(s)) + } + return out + } + want := []string{"192.168.110.23", "100.101.2.3"} + if got := LANHosts(); !slices.Equal(got, want) { + t.Fatalf("LANHosts: %v, want %v", got, want) + } + if got := LANURLs(); len(got) == 0 || got[0] != Scheme()+"://192.168.110.23:"+Port() { + t.Errorf("LANURLs: %v, want 192.168.110.23 first", got) + } + // it isn't on the internet either + if PublicIP(net.ParseIP("198.18.0.1")) { + t.Error("198.18.0.1 is called an internet address") } }