gui: the page's links for other computers are at the gateway's addresses
A user's computer has a static public address from its ISP on the network card itself (203.236.50.70) as well as a private one (10.77.70.2). Settings › Network & sharing listed 203.236.50.70 as the gateway's address but only 10.77.70.2 under "Open magpie in a browser", and they asked why the two differ. The gateway's list (gateway.LANURLs) took every IPv4 address on an interface that is up; the page's (gui.LANAddrs) took private ones only, through net.InterfaceAddrs, down interfaces included. The page server in LAN mode listens on 0.0.0.0, so the public address serves it too. Now one list makes both: gateway.LANHosts, every interface that is up, IPv4, not loopback or link-local, in the interfaces' order. gui.LANAddrs (and so NetworkLinks, the CLI's `magpie web` and daemon links) returns it, MAGPIE_PUBLIC_URL's host still first when set. The page's links now also show a CGNAT/Tailscale 100.64.x address, as the gateway's already did. Run, on mote: go vet ./internal/gateway ./internal/gui ./internal/cli; GOOS=windows go vet ./internal/gateway ./internal/gui; GOOS=linux go vet ./internal/gateway; go test -run "LAN|Lan|URL|TLS|Public|Network|Share|Allow|Container" ./internal/gateway ok; go test ./internal/gui ./internal/cli: cli ok, gui ok but for TestMainWindowCorners/macOS_26_SDK, #3949 (macOS 27, not about addresses). On xiaomi-gpu (Windows): the gateway's LANHosts, PublicURL and TLS address tests ok. New: TestLANHostsKeepAPublicAddress (a stubbed public, private, CGNAT, link-local, loopback and IPv6 address) and TestNetworkLinksAtTheGatewaysHosts. Broken by hand: LANHosts keeping private addresses only, as the page's list did, turns TestLANHostsKeepAPublicAddress red (got [10.77.70.2]). Not run: GOOS=linux vet of internal/gui (needs cgo; the WSL box lacks gtk4 and webkitgtk-6.0 headers); not tried on the reporter's machine.
This commit is contained in:
+29
-4
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+4
-11
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user