Files
magpie/internal/davsync/archive.go
T
yetone 258d04f569 gateway: a request archive, off unless turned on, keeps each call's headers and bodies in the user's own S3 bucket and a recent call reads its own back (Jorben on Discord: 请求存档) — the Gateway page's recent calls only ever held the last bodies in memory, the first 256 KB of each, with no headers, gone at a restart. A switch over Recent calls ("Request archive", POST /api/settings/archive, settings' requestArchive) turns it on, refused with a reason while sync isn't set up with an s3:// bucket; with it on, each call the gateway serves gets an id (UTC HHMMSS-<16 hex>, said back in X-Magpie-Archive-Id) and, once it is recorded, its method, path and query, request headers and body and response status, headers and body go as one JSON file to <prefix>/magpie/archive/<YYYY-MM-DD>/<id>.json in the bucket sync keeps its backup in, signed with the same client (davsync.Bucket, no new SDK). Before anything leaves, Authorization, x-api-key, Cookie, Set-Cookie, any header or JSON field or query parameter named for a key, token, secret or password, and anything that looks like a key or token anywhere in the text (the redaction rules' patterns, in a stream's data lines too) become [REDACTED] — gone, no placeholder kept; the archive is plain JSON, readable in the bucket, not sealed with the passphrase. Uploads go one at a time on their own goroutine from a queue of 16 calls, each body capped at the 256 KB the call already keeps and marked truncated; a full queue drops the call with a log line, a failed upload is logged and shown under the switch until the next one goes, and no request waits on any of it. A recent call the archive kept shows its date and id under its bodies, and "Fetch from archive" (GET /api/archive?date=&id=, only a date and an id of that shape) shows the request's and response's headers and bodies as the bucket has them, or says it isn't there yet. The hook is in record() and the Call literal, so PR #239's request log lines are not touched; the archived call carries its usage, and with #239 its vendor request id. Only calls through serve() are archived, not Codex's backend, image or video calls. TestArchiveOn, TestArchiveOff, TestArchiveName, TestRequestArchiveToS3 (a real gateway request through the signature-checking fake S3), TestScrub and request-archive.test.cjs fail without the change; TestSettingsSaveKeepsWhatItDoesNotSend now sets requestArchive. A sandbox magpie web with a fake vendor and a fake S3 server uploaded nothing while off; on, it uploaded a signed archive with none of eight planted secrets in it, read it back through /api/archive and in the page, and with the S3 server down served the call in 0.1 s and said why the upload failed. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (Claude account tests and TestClaudeRunEndsWhenTheClientLeaves pass alone); request-archive, click-scroll, old-webkit, sse-body, gateway-fold pass.
2026-10-01 17:13:34 +08:00

76 lines
2.0 KiB
Go

package davsync
import (
"context"
"errors"
"io"
"net/http"
"strings"
)
// Bucket is the S3 sync's bucket for files other than the backup — the
// gateway's request archive — each at <prefix>/magpie/<name>, beside the
// backup, written as it is: no passphrase seals them, so they can be read
// in the bucket too.
type Bucket struct{ s *s3 }
// ErrNoObject is a name with nothing at it.
var ErrNoObject = errors.New("not in the bucket")
// S3Bucket is the bucket sync keeps the backup in; false when sync is off
// or to a WebDAV folder.
func S3Bucket() (*Bucket, bool) {
c, ok := Load()
if !ok || !c.S3() {
return nil, false
}
s, err := newS3(c)
if err != nil {
return nil, false
}
return &Bucket{s}, true
}
// at is the client for name rather than the backup.
func (b *Bucket) at(name string) *s3 {
s := *b.s
s.key = strings.TrimSuffix(s.key, file) + strings.TrimLeft(name, "/")
return &s
}
// Where names the bucket, its prefix and its server, as messages do.
func (b *Bucket) Where() string { return b.at("").where() }
// Put writes data at name, over what is there.
func (b *Bucket) Put(ctx context.Context, name string, data []byte) error {
s := b.at(name)
res, err := s.send(ctx, http.MethodPut, data, map[string]string{"Content-Type": "application/json"})
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
io.Copy(io.Discard, io.LimitReader(res.Body, 64<<10))
return nil
}
return s.explain("write", readError(res))
}
// Get reads what is at name; ErrNoObject when there is nothing.
func (b *Bucket) Get(ctx context.Context, name string) ([]byte, error) {
s := b.at(name)
res, err := s.send(ctx, http.MethodGet, nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
e := readError(res)
if e.Status == http.StatusNotFound && e.Code != "NoSuchBucket" {
return nil, ErrNoObject
}
return nil, s.explain("read", e)
}
return io.ReadAll(io.LimitReader(res.Body, 16<<20))
}