serve: the banner no longer says the gateway only listens on localhost when MAGPIE_ADDR puts it on the network (the Docker image of #245 runs it on 0.0.0.0) — there it takes any key from anyone who reaches it until it is shared from Settings, and it now says so; once shared, it says other machines need the key under Settings → Share on local network. Tested with TestKeyNote (loopback, localhost, [::1], 0.0.0.0, :port, a LAN address, and shared).

This commit is contained in:
yetone
2026-09-30 03:22:36 +08:00
parent 6d69272732
commit d173bb0f79
2 changed files with 53 additions and 1 deletions
+16 -1
View File
@@ -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-…")
+37
View File
@@ -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)
}
}