CyberCaius on Discord: "请求存档只能存在 s3 存储桶里,不能保存在本地吗". The Usage page's Request archive switch refused without an s3:// sync. Now, with no bucket, the same switch keeps each call in <settings.Dir()>/request-archive/<date>/<id>.json: the same file the bucket gets, masked the same way (Archived, archiveRequest), written to a temporary file beside it, synced and renamed, 0600 in a 0700 folder. Past its size (RequestArchiveLocalMB, 1 GiB unless set) the oldest calls go first, down to a tenth under it; the call just written is never removed, empty day folders go, and a .part file an hour old is removed on the next scan. The running total is counted once and then added to, so a write walks the folder only once it is over. On by default or a switch: a switch, still off unless turned on. Bodies can hold secrets, so nothing is written to a disk where the user didn't ask. The local switch is its own field, RequestArchiveLocal, in KeepOwn with its size, so sync never turns it on at another computer, and RequestArchive synced from a computer with a bucket keeps nothing on one without (archiveDest). The GUI switch writes RequestArchive while sync has a bucket and RequestArchiveLocal while it has none; the tooltip names the folder and the size, and a failed write reads "Not saved" (en, zh, zh-TW, ja, de). GET /api/archive reads this computer's copy first, then the bucket. The Settings page's save keeps both fields (api.go). Docs: sync-backup.md and gui-shell.md say where the archive goes and why the switch is per computer; the privacy pages (en, zh, ja) say what the folder holds, that it is off by default, and how large it grows. The READMEs' Privacy is about what is sent and is unchanged. app.js stays under TestCoreScriptBudget (its comment was shortened, and one t() call folded); no budget raised. Run (worktree diff on5222a4b12, sandbox HOME): - go test -tags nogui -count=1 ./internal/settings/ ./internal/gui/ ./internal/gateway/: ok (29s, 73s, 507s) - node --test request-archive, request-archive-usage, routing-bodies, gui-zh-tw, gui-ja, gui-de, i18n-keys (chromium and webkit): 37/37 pass - gofmt -l internal/: clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; go build for darwin, linux (nogui), windows: ok - TestCoreScriptBudget: ok After rebasing onto origin/main3840333c5: -run Budget|Archive|SaveKeeps| RequestArchiveLocal in gui, gateway, settings ok; request-archive and i18n-keys 8/8; gofmt clean; linux and windows vet clean. Broken by hand, each red, then put back: - archiveDest's local branch off: TestArchiveOnThisComputer red - RequestArchive also writing to disk: TestArchiveNeverOnDiskUnasked red - no pruning: TestLocalArchiveRemovesOldestPastItsSize red - KeepOwn line removed: TestRequestArchiveLocalIsThisComputers red - POST setting RequestArchive with no bucket: TestArchiveSwitchKeepsOnThisComputer red - GET not reading the local copy: TestArchiveSwitchKeepsOnThisComputer red - api.go not carrying the fields: TestSettingsSaveKeepsWhatItDoesNotSend red - writing in place, not through temp+rename: TestArchiveOnThisComputer red (mode 0644, a .part left) Not tried: the real app with a real agent's calls (the gateway tests drive real requests through serve with a fake vendor); Windows' file modes (0600 is skipped there); the full ./... suite before the push (the machine had a queue of sessions' suites; it is queued after).
243 lines
7.6 KiB
Go
243 lines
7.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/davsync"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// archiveStore is the bucket a call is read back from: sync's S3 one, or
|
|
// a test's.
|
|
type archiveStore interface {
|
|
Open(ctx context.Context, name string) (io.ReadCloser, int64, error)
|
|
}
|
|
|
|
var archiveBucket = func() (archiveStore, bool) {
|
|
b, ok := davsync.S3Bucket()
|
|
return b, ok
|
|
}
|
|
|
|
// localArchive is the archive on this computer, kept while sync has no S3
|
|
// bucket (gateway/archive_local.go).
|
|
var localArchive = func() archiveStore { return gateway.LocalArchive() }
|
|
|
|
// archivePreviewBody is the most of a body the page is sent to show; a
|
|
// longer one is left out, for the file to be downloaded (#447). Its size
|
|
// is told here, so a large body never reaches the page.
|
|
const archivePreviewBody = 256 << 10
|
|
|
|
// archiveReadMost is the largest archive read here to be shown: two bodies
|
|
// of the archive's 32 MiB and their escapes. A larger one is only
|
|
// downloaded, never read into memory.
|
|
const archiveReadMost = 80 << 20
|
|
|
|
// archivePreview is an archived call as the page shows it: a body longer
|
|
// than archivePreviewBody left out (Omitted), or, for an archive over
|
|
// archiveReadMost, nothing but its size (Large).
|
|
type archivePreview struct {
|
|
ID string `json:"id,omitempty"`
|
|
Call *gateway.Call `json:"call,omitempty"`
|
|
Request *previewPart `json:"request,omitempty"`
|
|
Response *previewPart `json:"response,omitempty"`
|
|
Bytes int64 `json:"bytes"` // the archive file's, -1 when not known
|
|
Large bool `json:"large,omitempty"`
|
|
}
|
|
|
|
type previewPart struct {
|
|
gateway.ArchivePart
|
|
Omitted bool `json:"omitted,omitempty"`
|
|
}
|
|
|
|
func preview(p gateway.ArchivePart) *previewPart {
|
|
if p.Size == 0 {
|
|
p.Size = int64(len(p.Body)) // archived before #447
|
|
}
|
|
out := &previewPart{ArchivePart: p}
|
|
if len(p.Body) > archivePreviewBody {
|
|
out.Body, out.Omitted = "", true
|
|
}
|
|
return out
|
|
}
|
|
|
|
// openArchive is the call named by r's ?date= and ?id=, as it is read from
|
|
// this computer's archive, or else the bucket, its size, and "<date>-<id>";
|
|
// the error is the page's to show. A call kept here while sync had no
|
|
// bucket is read here after one is set up, and the other way round.
|
|
func openArchive(r *http.Request) (io.ReadCloser, int64, string, error) {
|
|
date, id := r.URL.Query().Get("date"), r.URL.Query().Get("id")
|
|
name, ok := gateway.ArchiveName(date, id)
|
|
if !ok {
|
|
return nil, 0, "", fmt.Errorf("%s/%s is not a request archive's date and id", date, id)
|
|
}
|
|
if body, size, err := localArchive().Open(r.Context(), name); !errors.Is(err, gateway.ErrNotArchived) {
|
|
if err != nil {
|
|
return nil, 0, "", err
|
|
}
|
|
return body, size, date + "-" + id, nil
|
|
}
|
|
b, ok := archiveBucket()
|
|
if !ok {
|
|
return nil, 0, "", errors.New("Not in the archive on this computer: it is saved just after the call, the oldest are removed past the archive's size, or the save failed (magpie's log says why)")
|
|
}
|
|
body, size, err := b.Open(r.Context(), name)
|
|
if errors.Is(err, davsync.ErrNoObject) {
|
|
return nil, 0, "", errors.New("Not in the archive yet: it is uploaded just after the call, or the upload failed (magpie's log says why)")
|
|
}
|
|
if err != nil {
|
|
return nil, 0, "", err
|
|
}
|
|
return body, size, date + "-" + id, nil
|
|
}
|
|
|
|
// archiveRoutes are the request archive: its switch on the Usage page, a
|
|
// call read back from this computer or the bucket by its date and id, for
|
|
// the Usage and Routing pages, and its file, downloaded whole
|
|
// (gateway/archive.go). With sync to an S3 bucket the switch is
|
|
// RequestArchive, which sync carries to every computer; with none it is
|
|
// RequestArchiveLocal, this computer's own, and calls are kept on its disk.
|
|
func archiveRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("POST /api/settings/archive", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct{ On bool }
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
s := settings.Load()
|
|
if _, ok := davsync.S3Bucket(); ok {
|
|
s.RequestArchive = in.On
|
|
} else {
|
|
s.RequestArchiveLocal = in.On
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
writeJSON(rw, archiveState())
|
|
})
|
|
mux.HandleFunc("GET /api/archive", func(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
|
|
defer cancel()
|
|
body, size, name, err := openArchive(r.WithContext(ctx))
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
defer body.Close()
|
|
if size > archiveReadMost {
|
|
writeJSON(rw, archivePreview{Bytes: size, Large: true})
|
|
return
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(body, archiveReadMost+1))
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if len(data) > archiveReadMost {
|
|
writeJSON(rw, archivePreview{Bytes: -1, Large: true})
|
|
return
|
|
}
|
|
var a gateway.Archived
|
|
if err := json.Unmarshal(data, &a); err != nil {
|
|
fail(rw, fmt.Errorf("%s in the bucket is not a request archive: %v", name, err))
|
|
return
|
|
}
|
|
writeJSON(rw, archivePreview{ID: a.ID, Call: &a.Call, Request: preview(a.Request), Response: preview(a.Response), Bytes: int64(len(data))})
|
|
})
|
|
// the archive's file, whole and as the bucket has it, to the browser
|
|
// that asks: magpie web's
|
|
mux.HandleFunc("GET /api/archive/file", func(rw http.ResponseWriter, r *http.Request) {
|
|
body, size, name, err := openArchive(r)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
defer body.Close()
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
rw.Header().Set("Content-Disposition", `attachment; filename="magpie-request-`+name+`.json"`)
|
|
if size >= 0 {
|
|
rw.Header().Set("Content-Length", fmt.Sprint(size))
|
|
}
|
|
io.Copy(rw, body)
|
|
})
|
|
// and, in the app, to Downloads
|
|
mux.HandleFunc("POST /api/archive/export", func(rw http.ResponseWriter, r *http.Request) {
|
|
body, _, name, err := openArchive(r)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
defer body.Close()
|
|
path, err := saveDownload(downloads(), "magpie-request-"+name, ".json", body)
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
writeJSON(rw, map[string]any{"path": tilde(path)})
|
|
})
|
|
}
|
|
|
|
// saveDownload writes what r reads to dir/<stem><ext>, or <stem>-2<ext>…
|
|
// when that is there: whole, or not at all.
|
|
func saveDownload(dir, stem, ext string, r io.Reader) (string, error) {
|
|
tmp, err := os.CreateTemp(dir, "."+stem+"-*.part")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_, err = io.Copy(tmp, r)
|
|
if cerr := tmp.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
os.Remove(tmp.Name())
|
|
return "", err
|
|
}
|
|
name := filepath.Join(dir, stem+ext)
|
|
for i := 2; ; i++ { // never over an earlier one
|
|
if _, err := os.Stat(name); err != nil {
|
|
break
|
|
}
|
|
name = filepath.Join(dir, fmt.Sprintf("%s-%d%s", stem, i, ext))
|
|
}
|
|
if err := os.Rename(tmp.Name(), name); err != nil {
|
|
os.Remove(tmp.Name())
|
|
return "", err
|
|
}
|
|
return name, nil
|
|
}
|
|
|
|
// archiveJSON is the request archive as the Usage page shows it: on or
|
|
// off, the bucket it goes to, or, when sync isn't to S3, the folder on this
|
|
// computer and the size it is kept under, and why the last upload or save
|
|
// failed, if it did.
|
|
type archiveJSON struct {
|
|
On bool `json:"on"`
|
|
Bucket string `json:"bucket,omitempty"`
|
|
Folder string `json:"folder,omitempty"`
|
|
Cap int64 `json:"cap,omitempty"` // bytes
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func archiveState() archiveJSON {
|
|
s := settings.Load()
|
|
var a archiveJSON
|
|
if b, ok := davsync.S3Bucket(); ok {
|
|
a.On, a.Bucket = s.RequestArchive, b.Where()
|
|
} else {
|
|
a.On, a.Folder, a.Cap = s.RequestArchiveLocal, tilde(gateway.LocalArchive().Dir), gateway.LocalArchiveCap()
|
|
}
|
|
if a.On {
|
|
a.Error, _ = gateway.ArchiveError()
|
|
}
|
|
return a
|
|
}
|