gateway: sharing never offers a proxy's TUN address (198.18/15), and a home network's comes first
coeo91 on Discord: with magpie shared, Settings' "Share on local network" showed http://198.18.0.1:<port> instead of the 192.168.110.x other devices reach. 198.18.0.0/15 is what Clash, Mihomo, sing-box, Surge and Stash take for their TUN interfaces and fake-ip answers; it was counted as private (sharedRanges) and kept in the interfaces' order, so it came first. LANHosts, which every list of addresses goes through (Settings, the sidebar line, Connect, the TUI/CLI banner, `magpie web`'s links), now leaves out any address netguard.FakeIP names, and puts RFC 1918 addresses before carrier-grade NAT (Tailscale) ones, then public ones. It goes by the address, not the interface's name: Tailscale's interface is a utun too, and its address is reached. Run (mote, mote-run, at this tree): gofmt -l; go vet for darwin, linux (-tags nogui) and windows of internal/gateway, internal/gui, internal/cli; go test -count=1 -timeout 45m of internal/gateway (ok), internal/netguard (ok), internal/cli (ok), internal/gui (one red: TestMainWindowCorners/ macOS_26_SDK on macOS 27, open as #3949, nothing to do with this change). Broken by hand: with lan.go at origin/main, TestLANHostsLeaveOutAProxysTUN and TestLANHostsKeepAPublicAddress are red (198.18.0.1 listed first). Not tried: a real Clash TUN on a machine.
This commit is contained in:
+24
-5
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user