Files
magpie/internal/gui/public_url_test.go
T
yetone 9451ad59f1 gateway: MAGPIE_PUBLIC_URL at a private address is the local network's, the Docker bridge's other containers', alike in Connect's menu
jiezhengj (Discord) runs magpie in Docker on a NAS, MAGPIE_PUBLIC_URL at
the NAS's own 192.168.31.218. The web UI's Gateway list called that
address "Public address" (公网地址), which reads as the internet's, and
Connect's Base URL menu offered 本机 / 公网地址 / 局域网, where 局域网 was
really the Docker bridge's 172.17.0.1 that other containers on the host
use, while the list above it named that one "Other containers on this
host".

The classification itself was right (gateway.PublicIP never takes
RFC 1918, CGNAT or ULA for the internet); the labels weren't: any
MAGPIE_PUBLIC_URL was "Public address", and Connect's menu had its own
three-way rule that knew nothing of the bridge or of internet addresses.

- gateway.URLKind(u): "internet" (PublicIP), "local" (any other IP
  literal) or "" (a name, not looked up). /api/settings and
  /api/providers' gateway carry it as publicKind beside publicURL.
- settings.js addrWho(g, u, others) is the one rule: this computer;
  the bridge address as other containers'; an internet address as such;
  MAGPIE_PUBLIC_URL by its kind (private: Local network; a name: Public
  address); anything else Local network. The Gateway list (each label
  said once for the lines it heads; local, then other containers, then
  internet), Connect's Base URL menu and About's MAGPIE_PUBLIC_URL row
  all use it.
- "Public address" now only names a MAGPIE_PUBLIC_URL that is a host
  name; zh 公网地址 → 对外地址, zh-TW 公開位址 → 對外位址 (ja/de already
  neutral enough: 公開アドレス, Öffentliche Adresse). No new strings.

Run, on mote (sandbox HOME, Go 1.26.8):
- go test -run 'URLKind|PublicURL|LAN|Container|DefaultGateway|HostNetwork'
  ./internal/gateway ./internal/gui: ok, at 2dac9b84e and again at this
  rebased head on 6b7ee4a98
  (then rebased onto a7113d5c9, which shares no file with this, and
  not run again).
- GUI, chromium and webkit, 18 files (allow-ips, api-key-theme,
  connect-snippet-shell, gateway-card, gateway-connect-keys,
  gateway-caller-keys, connect-address-kept, gateway-keys-off,
  lan-public, lan-docker, phone-connect-menu, public-url,
  public-url-default, side-gateway-lan, key-page, statusline-remote,
  i18n-keys, i18n-en-no-cjk): chromium 152/156, webkit 153/156. Every
  red is red at base 2dac9b84e too and filed: i18n-keys "a plugin's
  words are its own" (#5332, #5327, #5482, #5426), phone-connect-menu
  420 public / no public URL (#4706, #4707), gateway-caller-keys
  chromium en (#3797, passed at base this time).
- Again at this rebased head: lan-docker, public-url-default,
  public-url, lan-public in both engines, 86/86.
- gofmt -l clean; go vet darwin (gateway, gui, cli),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok.
- Full go test -tags nogui -timeout 45m ./... through suite.lock was
  still running at push (the owner's rule: no verified commit waits on
  the queue); a red it shows gets its own issue.

Broken by hand: TestPublicURLSaysItsKind on the old source fails
"publicKind <nil>, want local" (and want internet); the new lan-docker
test with the old JS fails 'Public address' vs 'Local network' and
'公网地址' vs '局域网' (chromium en, zh).

Not tried:
- a real Docker container or NAS; the cases are fixtures.
- magpie web's and the share banner's CLI lines still say "network" for
  every address, internet ones too; left alone.
- an in-container page opened at a public IP the host derived (not in
  lanPublic, not MAGPIE_PUBLIC_URL) is still labelled Local network.
- ja/de "Public address" wording kept.

Reported-by: jiezhengj on Discord
2026-11-08 02:13:14 +08:00

187 lines
6.2 KiB
Go

package gui
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/fx"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/testenv"
)
// Settings shows the local address agents use; the Gateway page advertises
// the public URL for its copy buttons and snippets.
func TestPublicURLInConsole(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.com/magpie///")
// Seed the rate the Settings page reads, so it needs no network.
cache := fx.CachePath()
if err := os.MkdirAll(filepath.Dir(cache), 0o755); err != nil {
t.Fatal(err)
}
b, err := json.Marshal(fx.Rate{CNYPerUSD: 7.25, At: time.Now()})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cache, b, 0o644); err != nil {
t.Fatal(err)
}
fx.Reset()
t.Cleanup(fx.Reset)
if err := settings.Save(settings.Settings{LAN: true, LANKey: "sk-magpie-test"}); err != nil {
t.Fatal(err)
}
want := "https://magpie.example.com/magpie"
if got := settingsState().Gateway; got != gateway.URL() {
t.Errorf("Settings: %q, want %q", got, gateway.URL())
}
g := providersState().Gateway
if g.URL != gateway.URL() {
t.Errorf("Gateway address: %q, want the local %q", g.URL, gateway.URL())
}
if len(g.LANURLs) != 1 || g.LANURLs[0] != want {
t.Errorf("LAN addresses: %v, want [%s]", g.LANURLs, want)
}
if !g.OnNetwork {
t.Error("MAGPIE_ADDR 0.0.0.0 not said to be on the network")
}
// #1551: Connect starts at the public URL, and Settings shows it
if g.PublicURL != want {
t.Errorf("Gateway page's public URL: %q, want %q", g.PublicURL, want)
}
if got := settingsState().PublicURL; got != want {
t.Errorf("Settings' public URL: %q, want %q", got, want)
}
b, err = json.Marshal(g)
if err != nil {
t.Fatal(err)
}
var state map[string]any
if err := json.Unmarshal(b, &state); err != nil {
t.Fatal(err)
}
if state["lan"] != true {
t.Fatalf("LAN state missing from gateway page: %s", b)
}
}
// A gateway MAGPIE_ADDR puts on the network (the Docker image) asks other
// machines for a gateway key whether or not it is shared, so the Gateway
// page shows its keys and offers no key-less "this computer" then.
func TestGatewayOnNetworkInConsole(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; !g.OnNetwork || !g.LAN {
t.Errorf("gateway on the network, not shared, shown without its keys: %+v", g)
}
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; !g.OnNetwork || !g.LAN {
t.Errorf("gateway on the network, shared: %+v", g)
}
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.OnNetwork || g.LAN {
t.Errorf("gateway on loopback, not shared: %+v", g)
}
}
// #1551: MAGPIE_PUBLIC_URL is offered where other machines are let in,
// and nowhere else: on loopback, not shared, nothing reaches the gateway
// through it without a key it can't take.
func TestPublicURLOnlyWhereOffered(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.com")
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.PublicURL != "" {
t.Errorf("loopback, not shared: Gateway page offers %q", g.PublicURL)
}
if u := settingsState().PublicURL; u != "" {
t.Errorf("loopback, not shared: Settings shows %q", u)
}
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.PublicURL != "https://magpie.example.com" {
t.Errorf("shared: Gateway page's public URL %q", g.PublicURL)
}
t.Setenv("MAGPIE_PUBLIC_URL", "")
if g := providersState().Gateway; g.PublicURL != "" {
t.Errorf("no MAGPIE_PUBLIC_URL: Gateway page's public URL %q", g.PublicURL)
}
if u := settingsState().PublicURL; u != "" {
t.Errorf("no MAGPIE_PUBLIC_URL: Settings shows %q", u)
}
}
// jiezhengj on Discord: magpie in Docker on a NAS, MAGPIE_PUBLIC_URL at
// the NAS's 192.168.31.218, and the page called it "Public address"
// (公网地址) as if anyone online reached it. Settings and the Gateway page
// say what kind of address it is, and a private one is the local
// network's.
func TestPublicURLSaysItsKind(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
t.Fatal(err)
}
kind := func(v any) any {
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
t.Fatal(err)
}
return m["publicKind"]
}
for u, want := range map[string]any{
"http://192.168.31.218:3425": "local",
"http://172.17.0.1:3425": "local",
"http://203.0.113.9:3425": "internet",
"https://magpie.example.com": nil,
} {
t.Setenv("MAGPIE_PUBLIC_URL", u)
if got := kind(providersState().Gateway); got != want {
t.Errorf("%s: Gateway page's publicKind %v, want %v", u, got, want)
}
if got := kind(settingsState()); got != want {
t.Errorf("%s: Settings' publicKind %v, want %v", u, got, want)
}
}
}