Files
magpie/internal/gateway/archive.go
T
yetone 5ec2375688 gateway: a call's HTTP headers both ways, masked, folded under its bodies
usrname1833 on Discord asked to see the raw request and response HTTP
headers (查看原始请求和原始响应的http头), to debug as with a capture.
magpie kept the client's request headers and its own response headers
only in the request archive, and nothing of what went upstream.

Now a call keeps its headers both ways (internal/gateway/wire_headers.go):
- the client's, as they reached magpie: headerLogged in Handler() clones
  them for every request that isn't a GET, HEAD or OPTIONS;
- each request magpie sent upstream for the call: method, address,
  headers, and the answer's status, protocol, headers and time, or the
  error. rpmTransport (under s.client) records it as it is sent;
  provider.SendsThroughPlugin records a plugin account's hand-off, shown
  as handed to the plugin, since the plugin's own request to its vendor
  is made in Bun and magpie doesn't see it (the view says so). The first
  8 tries are kept, the rest counted.
Recording copies each header map; nothing is masked or sorted as a
request goes. Secrets are taken out when the headers are read
(Call.Headers): by what each header is (new redact.SecretHeader, now
behind redact.ScrubHeader: Authorization, cookies, x-api-key, api-key,
any *-Key, *-Token, *-Secret, *-Auth, *-Signature, but Idempotency-Key
and a count of tokens such as anthropic-ratelimit-tokens-remaining),
any Bearer or Basic value, and redact.Scrub on the rest; an address
loses its password, a secret-named query value (?key=) and its fragment.
They live with the call in the 40-call ring; /api/gateway/call gives
them as "headers". The request archive keeps the upstream ones as
"upstream" beside the request's and response's headers, masked the same
way. No body is stored that wasn't before.

GUI: under a recent call's bodies on Gateway and a request's on Routing,
"HTTP headers" folds open the client's headers, then each upstream
request beside its answer (wireHeadersView, plugins/usage/page.js); the
archive's copy shows its upstream requests too. Strings in all four
languages. docs/subsystems/gui-shell.md describes it.

Also changed by SecretHeader: the request archive no longer masks a
rate-limit count header (…-tokens-remaining), which the old name rule
took for a token. (provider.SecretHeader, which backups use, is another
function and unchanged.)

Tests: TestRecentCallHeadersBothWays (a fake vendor; the client's key and
cookie, magpie's upstream key and the vendor's Set-Cookie all masked;
status, protocol and response headers kept), TestRecentCallHeadersEachTry
(a 429 then the next key, both kept in order), TestPluginHandOffHeaders,
TestScrubURL, TestSecretHeaderRealNames (real header names both ways),
TestArchiveOnThisComputer and TestRecentCallBodiesOnDemand extended,
call-headers.test.cjs (Gateway, Routing and the archive's copy; chromium
and webkit; en at 1100px, zh, zh-TW, ja, de at 380px and Routing at 440;
nothing out of the box, no left-border accent).

Run (on mote, macOS 27.2, through mote-run, unless named):
- Full suite at ebd22b1b4 (the same change on 5daba8337): gofmt -l
  empty, GOOS=windows go vet ./... ok, GOOS=linux go vet -tags nogui
  ./... ok, go test -count=1 -timeout 45m ./...: every package ok but
  four tests, none in this change's code, each filed:
  TestEveryRouteHasACommandOrIsNamed (internal/cli,
  /api/usage/checkin-account from e87140242, #4826),
  TestCodexPoolSaysTheClientsVersion (#4807: another test's Codex
  identity, passes alone, 3 of 3 here), TestMainWindowCorners/macOS_26_SDK
  (#3949, macOS 27), TestClosingWindowMarksPageOutOfSight (passes
  alone; filed #4924).
- Rebased on a3768cdd1 (it changed internal/gateway, routing/client.js
  and i18n.js): gofmt, go vet, Linux (nogui) and Windows vet of
  internal/... ok; go test of internal/gateway, gui, redact, provider:
  ok but TestCodexPoolSaysTheClientsVersion (#4807, ok 3 of 3 alone
  with this change's gateway tests) and TestMainWindowCorners (#3949);
  TestCoreScriptBudget ok. GUI: call-headers, routing-bodies,
  recent-call-bodies, request-archive and i18n-keys, 58 of 58.
- Rebased again on 2c90922b0 (app.js and internal/provider, no file of
  this change): TestCoreScriptBudget and TestRecentCall* ok;
  i18n-keys plus call-headers 24 of 24 three times out of four at load
  ~130 on mote. The first try had 1 red of 24 whose name was lost
  (its output was grepped, not kept); it did not come back in 3 runs.
- At the pushed head, rebased on 898c314b7, then 3e61d814e and 8d4d8b2d5 (gateway search, redact; vet, internal/redact and the header tests again ok) (app.css, app.js, i18n.js,
  gui-shell.md and internal/gateway changed): gofmt, vet of gateway and
  gui ok; TestCoreScriptBudget, TestRecentCall*, TestRouteCall,
  TestPluginHandOffHeaders, TestArchiveOnThisComputer, TestScrubURL ok;
  i18n-keys, call-headers, routing-bodies, recent-call-bodies 54 of 54.
- call-headers.test.cjs 20 of 20 (chromium and webkit; en at 1100, zh,
  zh-TW, ja, de at 380; Routing at 440), screenshots looked over.
- GUI: not the whole suite. mote had three other sessions' full GUI
  suites running and the Linux box one, so 131 files the change can
  reach (gateway*, routing bodies/log/steady/story/time, recent*,
  call-*, request-archive, masking*, usage*, i18n*, gui-*, account-eye)
  ran on the Windows box: 1739 of 1862. Every red file was run again at
  5daba8337 on the same box: routing-bodies, gateway-card,
  gateway-systemone, masking-demo, usage-ledger's Requests and the rest
  are red there too (Windows WebKit and Windows-only), and account-eye,
  routing-log-icons, usage-ledger-leave and -probe pass alone at this
  change. call-headers' 4 webkit Routing reds on Windows are
  routing-bodies' own (".rt-day" never visible in Windows WebKit).

Broken by hand, each then put back:
- ScrubHeader back on the name rule: TestSecretHeaderRealNames red
  ("a count of tokens: [REDACTED]").
- rpm.go's upstreamBegins taken out: TestRecentCallHeadersBothWays,
  TestRecentCallHeadersEachTry, TestArchiveOnThisComputer and
  TestRecentCallBodiesOnDemand red.
- scrubHeaders off the sent headers: TestPluginHandOffHeaders red with
  the plugin's key shown.
- Gateway client.js's toggle taken out: the 5 chromium recent-call GUI
  tests red.

Not tried:
- The whole GUI suite (above), and the GUI on Linux.
- A real account or the real app: only fakes.
- What a plugin sends its vendor (made in Bun, not seen by magpie; the
  view says so), and headers Go's transport adds itself
  (Accept-Encoding, Content-Length).
- magpie's own requests (System One's decision, titles, search) keep no
  headers; a gateway served by another magpie shows none.

Asked by usrname1833 on Discord.
2026-11-03 02:25:23 +08:00

364 lines
12 KiB
Go

package gateway
// The request archive: with it on (settings' RequestArchive), each call the
// gateway serves is kept — the headers and bodies both ways, every secret
// taken out first — in the user's own S3 bucket, the one sync keeps its
// backup in, at <prefix>/magpie/archive/<date>/<id>.json, or, while sync
// has no bucket and RequestArchiveLocal is on, in the same file on this
// computer (archive_local.go): the call's date,
// in UTC, and an id of its own, sent back on the response as
// X-Magpie-Archive-Id. The call, and its rows in the usage log, carry
// "<date>/<id>", for the Gateway and Usage pages to read it back by.
//
// The bodies are kept whole, not as Recent calls has them (the first 256
// KB), up to archiveLimit each (#447): the response is written to a
// temporary file as it goes to the agent, and the request to one once it
// is answered, so a call waiting for its upload holds no body in memory.
//
// The upload goes after the request, one at a time and never holding one
// up: a queue of a few calls, and a call that finds it full is dropped,
// with a line in the log, as is one the bucket refuses.
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"sync"
"time"
"github.com/yetone/magpie/internal/redact"
"github.com/yetone/magpie/internal/settings"
)
// Putter is a bucket the archive is written to.
type Putter interface {
Put(ctx context.Context, name string, data []byte) error
}
// ArchiveBucket is the bucket the archive goes to, and false when there is
// none (sync is off, or to a WebDAV folder): set by main to the S3 sync's.
// With none, the archive is this computer's (archiveDest).
var ArchiveBucket func() (Putter, bool)
// ArchiveHeader carries a call's archive id on its response.
const ArchiveHeader = "X-Magpie-Archive-Id"
// archiveQueue is how many calls wait for their upload at most: their
// bodies wait in files, so only their headers are held, however fast calls
// come.
const archiveQueue = 16
// archiveLimit is how much of each body the archive keeps, in bytes:
// settings' RequestArchiveMaxMB, 32 MiB unless set, at most a GiB. A body
// is read whole into memory once, for its secrets to be taken out, while
// its call is uploaded, one call at a time.
func archiveLimit() int64 {
mb := settings.Load().RequestArchiveMaxMB
if mb <= 0 {
mb = 32
}
return int64(min(mb, 1024)) << 20
}
// wire is what an archived call keeps besides what Call has: the request's
// method, path, headers and whole body, the response's, and where it goes.
type wire struct {
name string // <date>/<id>
method, path string
query string // its secrets taken out on the way (a key= Gemini's way)
req http.Header
body []byte // the request's, until it is written to reqFull
reqFull *spool
res *captureResponseWriter
to Putter
}
// archiveDest is where a call is archived, and false when it isn't: the S3
// bucket sync is to while RequestArchive is on, or, with no bucket, this
// computer's folder while RequestArchiveLocal is on (archive_local.go). A
// bucket that goes away never sends calls to the disk in its place: that
// takes the local switch, turned on at this computer.
func archiveDest() (Putter, bool) {
s := settings.Load()
if !s.RequestArchive && !s.RequestArchiveLocal {
return nil, false // off, as it is unless turned on: sync's file isn't read
}
if ArchiveBucket != nil {
if b, ok := ArchiveBucket(); ok && b != nil {
return b, s.RequestArchive
}
}
if s.RequestArchiveLocal {
return LocalArchive(), true
}
return nil, false
}
// archiving is r's wire when the archive is on, nil otherwise: nothing is
// kept, and nothing sent. body is the request's, as Recent calls has it
// before the first 256 KB is cut from it.
func archiving(r *http.Request, res *captureResponseWriter, start time.Time, body []byte) *wire {
to, ok := archiveDest()
if !ok {
return nil
}
name, id := archiveID(start)
res.Header().Set(ArchiveHeader, id)
res.full = &spool{limit: archiveLimit()}
return &wire{name: name, method: r.Method, path: r.URL.Path,
query: r.URL.RawQuery, req: r.Header.Clone(), body: body, res: res, to: to}
}
// archiveID is a new call's name in the archive, "<date>/<id>", and its
// id: the date it began, in UTC, and an id of its own.
func archiveID(start time.Time) (name, id string) {
b := make([]byte, 8)
rand.Read(b)
id = start.UTC().Format("150405") + "-" + hex.EncodeToString(b)
return start.UTC().Format("2006-01-02") + "/" + id, id
}
// archiveOwn keeps a call magpie made itself, which no client sent it
// (a Jev classifier's System One question, systemOne), as archive keeps
// one it served: sent is the request as it went, with the headers magpie
// signed it with (their secrets are taken out as a client's are), and
// status, head and answer what came back. It is the name c is kept at,
// "" with the archive off.
func archiveOwn(c Call, sent *http.Request, body []byte, status int, head http.Header, answer []byte) string {
to, ok := archiveDest()
if !ok || sent == nil {
return ""
}
name, _ := archiveID(c.Time)
c.Archive, c.Status, c.wire = name, status, nil
w := &wire{name: name, method: sent.Method, path: sent.URL.Path, query: sent.URL.RawQuery, req: sent.Header.Clone(), to: to}
req, res := &spool{limit: archiveLimit()}, &spool{limit: archiveLimit()}
req.add(body)
res.add(answer)
if head == nil {
head = http.Header{}
}
queueArchive(archiveJob{c: c, w: w, resHead: head.Clone(), req: req, res: res})
return name
}
// archiveName is where the archive keeps c, "<date>/<id>", or "" when it
// isn't kept: what its usage rows carry.
func (c *Call) archiveName() string {
if c.wire != nil {
return c.wire.name
}
return c.Archive
}
// discardArchive drops what a call's archive kept that never went to the
// queue — a request that ended without being recorded.
func discardArchive(res *captureResponseWriter) {
if res.full != nil {
res.full.discard()
}
}
// ArchiveName is where the archive keeps the call named "<date>/<id>",
// under the bucket's magpie folder; false for a name not of that shape,
// which is never one magpie made.
func ArchiveName(date, id string) (string, bool) {
if _, err := time.Parse("2006-01-02", date); err != nil || len(id) == 0 || len(id) > 64 {
return "", false
}
for _, c := range id {
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c == '-') {
return "", false
}
}
return "archive/" + date + "/" + id + ".json", true
}
// Archived is a call as the archive keeps it.
type Archived struct {
ID string `json:"id"` // <date>/<id>
Call Call `json:"call"` // as Recent calls has it, its bodies below
Request ArchivePart `json:"request"`
Response ArchivePart `json:"response"`
// Upstream: each request magpie sent upstream for it, its headers
// both ways (wire_headers.go), their secrets taken out; none in an
// archive from before them
Upstream []UpstreamHeaders `json:"upstream,omitempty"`
// UpstreamMore: the upstream requests past those, not kept
UpstreamMore int `json:"upstreamMore,omitempty"`
}
// ArchivePart is one way of a call: the request, or the response.
type ArchivePart struct {
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
Status int `json:"status,omitempty"`
Headers map[string][]string `json:"headers"`
Body string `json:"body"`
// Size is the whole body's, in bytes, as the gateway had it, before
// its secrets were taken out; none in an archive from before #447
Size int64 `json:"size,omitempty"`
// Truncated: only the start of the body is kept — its first
// RequestArchiveMaxMB, or, in an archive from before #447 (no Size),
// its first 256 KB
Truncated bool `json:"truncated,omitempty"`
}
type archiveJob struct {
c Call
w *wire
resHead http.Header
req, res *spool
}
var (
archiveOnce sync.Once
archiveJobs chan archiveJob
// archivePending counts the uploads queued and under way, for a test
// to wait for
archivePending sync.WaitGroup
archiveMu sync.Mutex
archiveErr string // the last upload's failure, "" once one succeeds
archiveErrAt time.Time
)
// archive queues c's upload; c.wire is set.
func archive(c Call) {
w := c.wire
// the response's file is the job's now, and the request's body goes
// to one, so nothing whole is held while the call waits
req := &spool{limit: archiveLimit()}
req.add(w.body)
w.body = nil
res := w.res.full
w.res.full = nil
if res == nil {
res = &spool{}
}
queueArchive(archiveJob{c: c, w: w, resHead: w.res.Header().Clone(), req: req, res: res})
}
// queueArchive puts j in the upload queue, or drops it when the queue is
// full.
func queueArchive(j archiveJob) {
w := j.w
archiveOnce.Do(func() {
archiveJobs = make(chan archiveJob, archiveQueue)
go func() {
for j := range archiveJobs {
upload(j)
archivePending.Done()
}
}()
})
req, res := j.req, j.res
archivePending.Add(1)
select {
case archiveJobs <- j:
default:
archivePending.Done()
req.discard()
res.discard()
log.Printf("request archive: %s dropped: %d uploads already waiting", w.name, archiveQueue)
}
}
func upload(j archiveJob) {
defer j.req.discard()
defer j.res.discard()
c := j.c
c.wire = nil
c.RequestBody, c.ResponseBody = "", ""
reqBody, err1 := j.req.read()
resBody, err2 := j.res.read()
if err := errors.Join(err1, err2); err != nil {
archiveFailed(j.w.name, err)
return
}
c.Error, c.Fallback = redact.Scrub(c.Error), redact.Scrub(c.Fallback)
path := j.w.path
if j.w.query != "" {
path += "?" + scrubQuery(j.w.query)
}
a := Archived{ID: j.w.name, Call: c,
Request: ArchivePart{Method: j.w.method, Path: path, Headers: scrubHeaders(j.w.req),
Body: string(redact.ScrubJSON(reqBody)), Size: j.req.size, Truncated: j.req.cut()},
Response: ArchivePart{Status: c.Status, Headers: scrubHeaders(j.resHead),
Body: string(redact.ScrubJSON(resBody)), Size: j.res.size, Truncated: j.res.cut()},
}
if h := j.c.headers.masked(); h != nil {
a.Upstream, a.UpstreamMore = h.Upstream, h.More
}
// as it reads in the bucket: a query's & and a body's <tags> as they are
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(a)
if err == nil {
// a minute, and a second more for each 256 KB a slow link takes
ctx, cancel := context.WithTimeout(context.Background(), time.Minute+time.Duration(buf.Len()>>18)*time.Second)
err = j.w.to.Put(ctx, "archive/"+j.w.name+".json", buf.Bytes())
cancel()
}
if err != nil {
archiveFailed(j.w.name, err)
return
}
archiveMu.Lock()
archiveErr = ""
archiveMu.Unlock()
}
func archiveFailed(name string, err error) {
archiveMu.Lock()
defer archiveMu.Unlock()
archiveErr, archiveErrAt = err.Error(), time.Now()
log.Printf("request archive: %s not uploaded: %v", name, err)
}
// ArchiveError is the last upload's failure, and when; "" once one after
// it went.
func ArchiveError() (string, time.Time) {
archiveMu.Lock()
defer archiveMu.Unlock()
return archiveErr, archiveErrAt
}
func scrubHeaders(h http.Header) map[string][]string {
out := make(map[string][]string, len(h))
for k, vs := range h {
s := make([]string, len(vs))
for i, v := range vs {
s[i] = redact.ScrubHeader(k, v)
}
out[k] = s
}
return out
}
// scrubQuery is a query string with the values of secret-named fields
// (key=, access_token=) and any secret in the others taken out.
func scrubQuery(q string) string {
vs, err := url.ParseQuery(q)
if err != nil {
return redact.Scrub(q)
}
for k, v := range vs {
for i := range v {
if redact.SecretName(k) {
v[i] = redact.Scrubbed
} else {
v[i] = redact.Scrub(v[i])
}
}
}
return vs.Encode()
}