Files
magpie/internal/gateway/remote_magpie_prices_test.go
T
yetone 83c963c466 gateway: a gateway MAGPIE_ADDR puts on the network asks callers from other machines, a container's host among them, for an enabled gateway key, as sharing does
What changed
- lanGuard: a request whose peer isn't loopback (or that carries a
  forwarding header) is remote. While the gateway is shared, or
  MAGPIE_ADDR names a host other than loopback/localhost (OnNetwork:
  0.0.0.0 in the Docker image, a server's address), a remote request is
  answered only with an enabled gateway key, on every route behind the
  guard; otherwise it is refused (403) as before. Before, a MAGPIE_ADDR on
  the network with sharing off took remote requests with any key.
  A remote request let in with a key is marked as shared-mode ones are,
  so quotas, quota cards, refresh and prices answer it.
  callerGuard follows the same condition. OpenToAnyone is replaced by
  OnNetwork.
- Loopback is unchanged: any key still works on the gateway's own
  machine and inside the container (HEALTHCHECK included).
- CLI keyNote and the console's Connect note no longer describe an open
  gateway; on the network they say other machines need a gateway key
  (`magpie gateway-key add <name>`). The console shows the Gateway keys
  block and network addresses when OnNetwork (gatewayJSON.onNetwork
  replaces open); the "Open to the network" strings are removed.
- Docs: docker.html (en/zh/ja) and remote.html (en/zh/ja) say callers
  outside the container need a gateway key, how to make one, that
  sharing isn't needed for it, and what an upgrade means for agents that
  sent `magpie`; docs/reference.md and providers-accounts.md likewise.
  A MAGPIE_ADDR pinned to one interface's address now asks a key of this
  computer's agents calling that address too (documented).

Tests
- New TestNetworkGatewayGuardsEveryRoute: from 172.17.0.1 and
  192.168.1.9 every registered route (model APIs, count_tokens,
  systemone, the Responses websocket GET, MCP, Codex backend and
  catalog, /v1/magpie/*, images, embeddings, rerank, videos, Gemini)
  gets 401 without a valid key; with one, models and quotas answer 200.
- TestNetworkGatewayNeedsAKey (was TestExplicitOpenGatewayAccepts...),
  TestLANGuard, TestProxiedLoopbackNeedsTheKey, TestQuotasOverLAN,
  TestMCPProxyStaysHere, session route and remote prices tests, TestOnNetwork,
  serve_cli and gui public_url tests updated. With the old lanGuard
  restored, TestNetworkGatewayNeedsAKey, TestNetworkGatewayGuardsEveryRoute,
  TestLANGuard and TestProxiedLoopbackNeedsTheKey fail.

Run
- go test -tags nogui -count=1 ./internal/gateway ./internal/gui
  ./internal/provider . under a temp HOME: ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
  those packages: clean.
- GUI: public-url and gateway-caller-keys tests, Chromium and WebKit: 22/22.
- Locally built image (OrbStack), -p 127.0.0.1:3481:3425: from the host,
  /v1/models with no key, `magpie` and another made-up key -> 401;
  count_tokens and /mcp/x -> 401; a key from `docker exec ... magpie
  gateway-key add Host` -> models 200, quotas 200; inside the container
  wget /v1/models -> 200; healthcheck exits 0. `web --addr 0.0.0.0:3430`:
  /api/state 401 without the web key, 200 with its cookie.
2026-10-10 09:26:23 +08:00

170 lines
6.5 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// Sorghum on Discord: a magpie with another computer's magpie as its
// provider counts the calls it makes there at what that magpie counts
// them at — the price its user set for the model by hand — so the two
// Usage pages give one cost. A price this magpie's user set for the model
// still wins, and a magpie from before the list told prices changes
// nothing here.
func TestRemoteMagpiePrices(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1000000,"completion_tokens":500000}}`)
}))
t.Cleanup(up.Close)
// a relay's model no catalog prices: only the price set for it by hand
if err := provider.Save(provider.Provider{ID: "relay-a", Name: "Relay A", Key: "k", Models: []string{"house-model-1"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("relay-a", up.URL+"/v1", []catalog.Model{{ID: "house-model-1", Name: "House Model"}}); err != nil {
t.Fatal(err)
}
const model = "relay-a/house-model-1"
if _, ok := provider.EffectivePrice("relay-a", "house-model-1"); ok {
t.Fatal("the fixture's model has a list price; pick one no catalog prices")
}
set := catalog.Price{Input: 3, Output: 12, CacheRead: 0.3, CacheWrite: 3.75}
if err := provider.SetModelPrice(model, &set); err != nil {
t.Fatal(err)
}
gw := httptest.NewServer(lanGuard(New().Handler()))
t.Cleanup(gw.Close)
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Key: "sk-magpie-office", Chat: strings.TrimPrefix(gw.URL, "http://")})
if err != nil {
t.Fatal(err)
}
office, _ := provider.Find(id)
if _, err := office.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
// the remote's hand-set price is its price here
if pr, ok := provider.EffectivePrice("office", model); !ok || !pr.Same(set) {
t.Fatalf("office/%s is priced %+v (%v), want the remote's %+v", model, pr, ok, set)
}
if !office.RemotePriced(model) {
t.Error("the editor isn't told the list price is the remote's")
}
// a call through it costs the same on both Usage pages
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"office/`+model+`","messages":[{"role":"user","content":"hi"}]}`))
w := httptest.NewRecorder()
New().Handler().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("call: %d %s", w.Code, w.Body.String())
}
cost := usage.NewCoster()
costs := map[string]float64{}
for _, rec := range usage.Load(time.Time{}) {
c, ok := cost(rec)
if !ok {
t.Errorf("%s/%s is unpriced", rec.Provider, rec.Model)
}
costs[rec.Provider] = c
}
if want := 3 + 12*0.5; costs["office"] != want || costs["relay-a"] != want {
t.Errorf("costs here and there: %v, want %v each", costs, want)
}
// a price set here for it wins, and fetching the list again keeps it
mine := catalog.Price{Input: 1, Output: 2}
if err := provider.SetModelPrice("office/"+model, &mine); err != nil {
t.Fatal(err)
}
if _, err := office.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
if pr, _ := provider.EffectivePrice("office", model); !pr.Same(mine) {
t.Errorf("the remote's price %+v won over the one set here %+v", pr, mine)
}
// a magpie from before the list told prices: the model stays unpriced
old := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"object":"list","data":[{"id":"relay-a/house-model-1","object":"model","owned_by":"relay-a","display_name":"House Model","native_endpoints":["/v1/chat/completions"]}]}`)
}))
t.Cleanup(old.Close)
oid, err := provider.Add(provider.Provider{ID: "attic", Name: "Attic", Preset: provider.RemoteMagpiePreset, Key: "k", Chat: old.URL})
if err != nil {
t.Fatal(err)
}
attic, _ := provider.Find(oid)
if _, err := attic.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
if pr, ok := provider.EffectivePrice(oid, model); ok || attic.RemotePriced(model) {
t.Errorf("an old remote's list priced it: %+v", pr)
}
}
// What the list tells of prices, and to whom: only to a magpie that asks
// for them, and only from this computer or with a gateway key — never to
// another machine let in without one (MAGPIE_ADDR open to the network).
func TestRemoteMagpiePricesTold(t *testing.T) {
fresh(t)
if err := provider.Save(provider.Provider{ID: "relay-a", Name: "Relay A", Key: "k", Models: []string{"house-model-1"}, Chat: "http://127.0.0.1:9/v1"}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("relay-a", "http://127.0.0.1:9/v1", []catalog.Model{{ID: "house-model-1"}}); err != nil {
t.Fatal(err)
}
set := catalog.Price{Input: 3, Output: 12}
if err := provider.SetModelPrice("relay-a/house-model-1", &set); err != nil {
t.Fatal(err)
}
priced := func(from, key string, ask bool) (int, bool) {
r := httptest.NewRequest("GET", "/v1/models", nil)
r.RemoteAddr = from
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
if ask {
r.Header.Set(provider.PricesHeader, "1")
}
w := httptest.NewRecorder()
lanGuard(New().Handler()).ServeHTTP(w, r)
var v struct {
Data []map[string]json.RawMessage `json:"data"`
}
json.Unmarshal(w.Body.Bytes(), &v)
for _, m := range v.Data {
if _, ok := m["magpie_price"]; ok {
return w.Code, true
}
}
return w.Code, false
}
t.Setenv("MAGPIE_ADDR", "")
if code, got := priced("127.0.0.1:5000", "", true); code != 200 || !got {
t.Errorf("this computer, asking: %d, priced %v", code, got)
}
if _, got := priced("127.0.0.1:5000", "", false); got {
t.Error("prices told to a list that didn't ask (an older magpie, an agent)")
}
t.Setenv("MAGPIE_ADDR", "0.0.0.0:0") // on the network, magpie not shared
if code, got := priced("192.168.1.9:5000", "", true); code != http.StatusUnauthorized || got {
t.Errorf("another machine without a key: %d, priced %v", code, got)
}
t.Setenv("MAGPIE_ADDR", "")
_, secrets := newCaller(t, "Laptop")
if code, got := priced("192.168.1.9:5000", secrets[0], true); code != 200 || !got {
t.Errorf("another machine with a gateway key: %d, priced %v", code, got)
}
}