From d173bb0f79048543451e6c5e3685a4ea79a4c2b4 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 03:22:36 +0800 Subject: [PATCH] =?UTF-8?q?serve:=20the=20banner=20no=20longer=20says=20th?= =?UTF-8?q?e=20gateway=20only=20listens=20on=20localhost=20when=20MAGPIE?= =?UTF-8?q?=5FADDR=20puts=20it=20on=20the=20network=20(the=20Docker=20imag?= =?UTF-8?q?e=20of=20#245=20runs=20it=20on=200.0.0.0)=20=E2=80=94=20there?= =?UTF-8?q?=20it=20takes=20any=20key=20from=20anyone=20who=20reaches=20it?= =?UTF-8?q?=20until=20it=20is=20shared=20from=20Settings,=20and=20it=20now?= =?UTF-8?q?=20says=20so;=20once=20shared,=20it=20says=20other=20machines?= =?UTF-8?q?=20need=20the=20key=20under=20Settings=20=E2=86=92=20Share=20on?= =?UTF-8?q?=20local=20network.=20Tested=20with=20TestKeyNote=20(loopback,?= =?UTF-8?q?=20localhost,=20[::1],=200.0.0.0,=20:port,=20a=20LAN=20address,?= =?UTF-8?q?=20and=20shared).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers_cli.go | 17 ++++++++++++++++- serve_cli_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 serve_cli_test.go diff --git a/providers_cli.go b/providers_cli.go index a12f223fa..9418c08da 100644 --- a/providers_cli.go +++ b/providers_cli.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "maps" + "net" "os" "slices" "strings" @@ -15,6 +16,7 @@ import ( "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/stats" ) @@ -753,6 +755,19 @@ func refreshLive(ctx context.Context) { } } +// keyNote says who the gateway takes any key from: this machine alone, +// unless MAGPIE_ADDR puts it on the network (a server, a Docker image) +// without sharing it from Settings, when it is anyone who reaches it. +func keyNote() string { + if s := settings.Load(); s.LAN && s.LANKey != "" { + return "(anything works from this machine; from others, the key under Settings → Share on local network)" + } + if h, _, err := net.SplitHostPort(gateway.Addr()); err == nil && h != "localhost" && !net.ParseIP(h).IsLoopback() { + return "(anything works, from anyone who reaches it — share it from Settings to require a key)" + } + return "(anything works; the gateway only listens on localhost)" +} + // serve: `magpie serve` — the gateway alone, in the foreground. func serve() error { s := gateway.New() @@ -760,7 +775,7 @@ func serve() error { fmt.Println(green.Render("●"), "magpie gateway on", bold.Render(gateway.URL())) fmt.Println(muted.Render(" OpenAI "), gateway.URL()+"/v1/chat/completions", muted.Render("·"), gateway.URL()+"/v1/responses") fmt.Println(muted.Render(" Anthropic"), gateway.URL()+"/v1/messages") - fmt.Println(muted.Render(" key "), gateway.Token, muted.Render("(anything works; the gateway only listens on localhost)")) + fmt.Println(muted.Render(" key "), gateway.Token, muted.Render(keyNote())) n := len(provider.Catalog()) if n == 0 { fmt.Println(amber.Render("!"), "no models yet ·", "magpie provider add deepseek sk-…") diff --git a/serve_cli_test.go b/serve_cli_test.go new file mode 100644 index 000000000..857227500 --- /dev/null +++ b/serve_cli_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "strings" + "testing" + + "github.com/yetone/magpie/internal/settings" +) + +// The serve banner says the gateway takes any key only from this machine +// while that is true: MAGPIE_ADDR on every interface (a Docker image) and +// not shared from Settings is open to anyone who reaches it. +func TestKeyNote(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + for _, c := range []struct{ addr, want string }{ + {"", "only listens on localhost"}, + {"127.0.0.1:3426", "only listens on localhost"}, + {"localhost:3426", "only listens on localhost"}, + {"[::1]:3426", "only listens on localhost"}, + {"0.0.0.0:3425", "anyone who reaches it"}, + {":3425", "anyone who reaches it"}, + {"192.168.1.5:3425", "anyone who reaches it"}, + } { + t.Setenv("MAGPIE_ADDR", c.addr) + if n := keyNote(); !strings.Contains(n, c.want) { + t.Errorf("MAGPIE_ADDR=%q: %q", c.addr, n) + } + } + if err := settings.Save(settings.Settings{LAN: true, LANKey: "sk-magpie-k"}); err != nil { + t.Fatal(err) + } + t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425") + if n := keyNote(); !strings.Contains(n, "Share on local network") { + t.Error("shared:", n) + } +}