diff --git a/internal/gateway/lan.go b/internal/gateway/lan.go index 6ba627aa4..437e5c223 100644 --- a/internal/gateway/lan.go +++ b/internal/gateway/lan.go @@ -168,6 +168,33 @@ func LANURLs() []string { return []string{Scheme() + "://" + net.JoinHostPort(h, Port())} } var out []string + for _, h := range LANHosts() { + out = append(out, Scheme()+"://"+net.JoinHostPort(h, Port())) + } + return out +} + +// LANHosts are this computer's IPv4 addresses other machines can reach it +// at, on every interface that is up: a private one, and a public one an +// ISP assigns to the network card itself. The gateway's addresses and +// `magpie web`'s links are both made from these, so the two list the +// same computer the same way (a static public address was in one and not +// the other). +func LANHosts() []string { + var out []string + for _, ip := range hostIPs() { + if ip.To4() == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() { + continue + } + out = append(out, ip.String()) + } + return out +} + +// hostIPs are the addresses on this computer's interfaces that are up, +// loopback's left out; a variable for the tests. +var hostIPs = func() []net.IP { + var out []net.IP ifs, _ := net.Interfaces() for _, i := range ifs { if i.Flags&net.FlagUp == 0 || i.Flags&net.FlagLoopback != 0 { @@ -175,11 +202,9 @@ func LANURLs() []string { } addrs, _ := i.Addrs() for _, a := range addrs { - n, ok := a.(*net.IPNet) - if !ok || n.IP.To4() == nil || n.IP.IsLinkLocalUnicast() { - continue + if n, ok := a.(*net.IPNet); ok { + out = append(out, n.IP) } - out = append(out, Scheme()+"://"+net.JoinHostPort(n.IP.String(), Port())) } } return out diff --git a/internal/gateway/lan_hosts_test.go b/internal/gateway/lan_hosts_test.go new file mode 100644 index 000000000..12b19a4b4 --- /dev/null +++ b/internal/gateway/lan_hosts_test.go @@ -0,0 +1,35 @@ +package gateway + +import ( + "net" + "slices" + "testing" +) + +// A computer whose ISP assigns a static public address to its network +// card is reached there as well as at its private one: both are offered, +// in the interfaces' order. Loopback, link-local and IPv6 are not. +func TestLANHostsKeepAPublicAddress(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{"203.236.50.70", "10.77.70.2", "100.64.0.1", "169.254.3.4", "127.0.0.2", "fe80::1", "2001:db8::5"} { + out = append(out, net.ParseIP(s)) + } + return out + } + want := []string{"203.236.50.70", "10.77.70.2", "100.64.0.1"} + if got := LANHosts(); !slices.Equal(got, want) { + t.Fatalf("LANHosts: %v, want %v", got, want) + } + var urls []string + for _, h := range want { + urls = append(urls, Scheme()+"://"+net.JoinHostPort(h, Port())) + } + if got := LANURLs(); !slices.Equal(got, urls) { + t.Errorf("LANURLs: %v, want %v", got, urls) + } +} diff --git a/internal/gui/web.go b/internal/gui/web.go index 4ad0843ee..9be29b8a8 100644 --- a/internal/gui/web.go +++ b/internal/gui/web.go @@ -324,21 +324,14 @@ func NetworkLinks(port, key string) []string { // LANAddrs are this computer's addresses on the local network, for the // links to print when the page is served there: MAGPIE_PUBLIC_URL's host -// when set (in a container, whose own addresses the network can't reach). +// when set (in a container, whose own addresses the network can't reach), +// else the gateway's own (gateway.LANHosts), so Settings lists one +// computer's addresses the same way twice. func LANAddrs() []string { if h := gateway.PublicHost(); h != "" { return []string{h} } - var out []string - as, _ := net.InterfaceAddrs() - for _, a := range as { - n, ok := a.(*net.IPNet) - if !ok || n.IP.IsLoopback() || n.IP.To4() == nil || !n.IP.IsPrivate() { - continue - } - out = append(out, n.IP.String()) - } - return out + return gateway.LANHosts() } // webServed is all `magpie web` on port serves: the gateway's client diff --git a/internal/gui/web_links_test.go b/internal/gui/web_links_test.go index d8573f54c..49020f795 100644 --- a/internal/gui/web_links_test.go +++ b/internal/gui/web_links_test.go @@ -1,8 +1,11 @@ package gui import ( + "net" "slices" "testing" + + "github.com/yetone/magpie/internal/gateway" ) // magpie web behind a reverse proxy (#372): MAGPIE_PUBLIC_URL with no port @@ -25,3 +28,17 @@ func TestNetworkLinksPublicURL(t *testing.T) { } } } + +// The links printed for other machines are at the gateway's own addresses +// (gateway.LANHosts), one for one: Settings showed a computer's static +// public address under the gateway and not under the page. +func TestNetworkLinksAtTheGatewaysHosts(t *testing.T) { + t.Setenv("MAGPIE_PUBLIC_URL", "") + var want []string + for _, h := range gateway.LANHosts() { + want = append(want, "http://"+net.JoinHostPort(h, "3430")+"/?k=k") + } + if got := NetworkLinks("3430", "/?k=k"); !slices.Equal(got, want) { + t.Errorf("%v, want %v", got, want) + } +}