Files
yetone da834411b5 usage: with WebDAV or S3 sync, each computer shares its usage and sees the others', the total and each computer's part (#542) — sync carried keys, agents and the library, and each computer's Usage page counted only the calls made on it. Sync now has a Usage tick (off unless picked, usage=yes in magpie webdav/magpie s3): a computer with it on puts its own calls up as one file a day, magpie/usage/<id>-<day>.magpie-usage beside the bundle (under the prefix's usage/ on S3), each a gzipped list of times, agents, providers, models, tokens, costs and statuses, sealed with the sync passphrase (backup.SealData, the key cached per passphrase so a sync derives it once); no request body, session, archive name or key goes, a call another computer passed on over the LAN (Via) isn't put up again, and an error is cut to 300 characters. The id is a random one kept in usage-computer.json, which no backup carries, with the host name beside it. A computer only ever writes and deletes its own names, so none overwrites another's; the files are not in the bundle and its hash, so sharing usage never makes a sync or a conflict of the bundle; the first sync puts up the last 90 days, after that only yesterday and today, at most every 15 minutes (a Save or Sync now at once), and a day past 90 is deleted by the computer it is of. The others' files, listed (PROPFIND Depth 1; ListObjectsV2) and fetched only when changed, are kept in usage-others and read with the ledger, as Record.Computer; a failure is said in the sync row ("Couldn't share usage") and never fails the sync. The Requests page and the panel's Usage tab get a Computer pick (this computer, other computers when more than one, each by name) sending computer=this|others|<id>; a row from another computer says "· on <name>" beside its agent, and its details name the computer and say what was said stays there, nothing asked of this computer's session files for it. With no other computer's usage there is no pick and nothing new on either page, and an older magpie never lists or reads usage/. TestUsageSharedBetweenComputers (computers a and b over a fake WebDAV under httptest: each sees 4 calls, the totals and its own and the other's part, nothing counted twice over repeated syncs, the bundle not put again for usage, a new call reaching the other, a day past the window deleted), TestUsageSharedThroughS3 (the same through a fake S3, signed list and delete, a prefix with a space and a plus) and TestUsageOffAsksNothing are new and don't build without the change; usage-computers.test.cjs (WebKit, en and zh: no pick without other computers, the pick and computer=this, another computer's row and details with no /content asked, the panel's pick, the sync form's tick posting usage: true, no click moving the page) fails without it on all but the no-other-computers case. s3-sync, sync-other-kind, panel-usage, request-archive-usage, click-scroll and usage-ledger-chart pass in WebKit. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestFXInState failed once on its TempDir cleanup, passes alone). Turning the Usage tick off, or sync off, lets the other computers' usage kept here go (DropAllShared), so the Usage page counts this computer's alone again: TestUsageSharedBetweenComputers checks both, and fails without it.
2026-10-02 17:47:36 +08:00

94 lines
2.4 KiB
Go

package backup
import (
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"sync"
)
// dataFormat is a file of bytes sealed as a backup is, but not one: the
// usage a computer shares through sync (#542). Open refuses it.
const dataFormat = "magpie-data"
// keys are the keys derived this run, by passphrase and salt: a computer
// seals every file it shares with one salt, and opens each other
// computer's with that one's, so a sync derives a key once a computer
// rather than once a file, PBKDF2 being slow on purpose.
var (
keysMu sync.Mutex
keys = map[[32]byte][]byte{}
salts = map[[32]byte][]byte{}
)
func derived(pass string, e envelope) ([]byte, error) {
k := sha256.Sum256(append([]byte(pass+"\x00"), e.Salt...))
keysMu.Lock()
defer keysMu.Unlock()
if key, ok := keys[k]; ok {
return key, nil
}
key, err := deriveKey(pass, e)
if err != nil {
return nil, err
}
keys[k] = key
return key, nil
}
// SealData seals data under the passphrase, as Seal does a bundle.
func SealData(data []byte, pass string) ([]byte, error) {
if pass == "" {
return nil, errors.New("sealing needs a passphrase")
}
p := sha256.Sum256([]byte(pass))
keysMu.Lock()
salt := salts[p]
if salt == nil {
salt = make([]byte, 16)
rand.Read(salt)
salts[p] = salt
}
keysMu.Unlock()
e := envelope{Format: dataFormat, Version: 1, KDF: "pbkdf2-sha256", Iterations: iterations, Salt: salt, Nonce: make([]byte, 12)}
rand.Read(e.Nonce)
key, err := derived(pass, e)
if err != nil {
return nil, err
}
gcm, err := gcmOf(key)
if err != nil {
return nil, err
}
e.Data = gcm.Seal(nil, e.Nonce, data, header(e))
return json.Marshal(e)
}
// OpenData opens what SealData sealed.
func OpenData(sealed []byte, pass string) ([]byte, error) {
var e envelope
if json.Unmarshal(sealed, &e) != nil || e.Format != dataFormat {
return nil, errors.New("not a file magpie sealed")
}
if e.Version != 1 || e.KDF != "pbkdf2-sha256" {
return nil, errors.New("sealed by a newer magpie")
}
if e.Iterations < 100_000 || e.Iterations > 10_000_000 || len(e.Nonce) != 12 || len(e.Salt) < 16 {
return nil, errors.New("not a file magpie sealed")
}
key, err := derived(pass, e)
if err != nil {
return nil, err
}
gcm, err := gcmOf(key)
if err != nil {
return nil, err
}
plain, err := gcm.Open(nil, e.Nonce, e.Data, header(e))
if err != nil {
return nil, ErrPassphrase
}
return plain, nil
}