jinpwng (#6428, on X) syncs two computers and wants only Library and memory to go between them: each keeps its own providers, settings and models. They also want to see what a sync would change before it applies, and to roll the last one back. What syncs: providers, settings and profiles now have switches like agents' models, library and memory (Config.Providers/Settings/Profiles, *bool, nil = synced as before, so an existing setup syncs exactly what it did). The sync form shows Providers (and its API keys), Settings and Agents' models up front; Profiles, usage and sign-ins sit under Advanced, closed unless one is set away from its default. The form sends a part's choice only when it is or was off. A part left out stays as it is here and on the server; turning it back on joins it like a first sync (st.Off, rejoin). The sync's line says which parts each computer keeps ("Stays as it is on each computer: ..."). The setup text and the CLI carry it: magpie <webdav|s3|github> set providers=no settings=no profiles=no. Preview: davsync.Preview (plan.go) says, per part, what syncing now would bring in, send up, or leave; library items and memory files by name, sign-ins and plugins gone. Preview... in Settings > Sync shows it, and Sync now from there runs SyncPreviewed(token): if the server or this computer changed since, it says so and shows the new plan instead of syncing something else. CLI: magpie <kind> preview. Undo: a sync that brings parts in records them (st.Applied) and keeps a sealed copy of this computer first (keepSealed, as Restore does). Undo the last sync puts those parts back from that copy, on this computer only (st.Local, so the next sync sends it up rather than fetching the server's again). If one of them changed here since, it asks first (409 changed, Undo anyway). CLI: magpie <kind> undo-sync [anyway]. Restore's own Undo is unchanged. docs/subsystems/sync-backup.md and the site's sync docs (en, zh, ja) have a section on it. i18n: zh, zh-TW, ja, de. Run (worktree at base48e394146): - go test -count=1 . ./internal/davsync ./internal/cli: ok (mote). Broken by hand, each went red: syncs() forced true (scope tests), rejoin's again forced false, UndoSync without the st.Local update. - go test -count=1 ./internal/gui: TestCoreScriptBudget was over by 85 bytes; the sync-left line moved into syncfiles.js, now ok. - go vet ./internal/gui, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. gofmt -l: nothing. - node --check app.js, syncfiles.js, sync-scope.test.cjs: ok. - GUI (mica), every test file that opens Settings > Sync or calls davsync (22 files) plus i18n-keys, Chromium and WebKit, 1100 and 420px, en/zh/zh-TW/ja/de: 453 of 454 pass. The red is s3-sync webkit en "picking S3 must not scroll the page" (0 !== 98), the same on origin/main52c9880cb(#3413, #2448). An earlier run also had library-new-skill chromium en 420 red, the same on main (#6539). sync-scope.test.cjs is new: on the old code there is no Providers switch, Preview or Undo, so it fails at once. - Rebased on origin/main7eecbcc56(app.js and i18n.js changed there): TestCoreScriptBudget, go test . ./internal/davsync, node --check, and i18n-keys, sync-scope, webdav-settings-refresh (both engines): ok. - The full go test ./... was started through mote's suite lock when this was pushed; its result goes on #6428. Not tried: a real WebDAV, S3 or GitHub server (the tests use fakes); the real app on Linux WebKitGTK. "Save and sync" from the form, after a preview, syncs without the preview's token check.
116 lines
4.0 KiB
Go
116 lines
4.0 KiB
Go
package davsync
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/backup"
|
|
"github.com/yetone/magpie/internal/lazyre"
|
|
)
|
|
|
|
// SetupPrefix begins a sync setup copied as text (WJaunita45604 on X: one
|
|
// copy, pasted on another computer, sets sync up there the same).
|
|
const SetupPrefix = "magpie-sync:"
|
|
|
|
// Setup is what a setup copied as text holds: the server, its password or
|
|
// token, and what is synced. Never the passphrase: the text is sealed with
|
|
// it, so it is typed on the other computer to open the text, as it is
|
|
// typed there to open the server's file.
|
|
type Setup struct {
|
|
URL string `json:"url"`
|
|
User string `json:"user,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
Region string `json:"region,omitempty"`
|
|
PathStyle bool `json:"pathStyle,omitempty"`
|
|
Branch string `json:"branch,omitempty"`
|
|
// Providers, Settings and Profiles are false when left out of sync;
|
|
// nil, as from a magpie before they could be, is synced.
|
|
Providers *bool `json:"providers,omitempty"`
|
|
Settings *bool `json:"settings,omitempty"`
|
|
Profiles *bool `json:"profiles,omitempty"`
|
|
Keys bool `json:"keys"`
|
|
Agents bool `json:"agents"`
|
|
Library bool `json:"library"`
|
|
Memory bool `json:"memory"`
|
|
Usage bool `json:"usage,omitempty"`
|
|
SignIns bool `json:"signIns,omitempty"`
|
|
}
|
|
|
|
// ErrSetupOff is SetupText with sync off: there is nothing to copy.
|
|
var ErrSetupOff = errors.New("sync isn't set up on this computer")
|
|
|
|
// ErrNotSetup is OpenSetupText given text that isn't a copied setup.
|
|
var ErrNotSetup = errors.New("this isn't a sync setup copied from magpie")
|
|
|
|
// ErrSetupPassphrase is OpenSetupText with a passphrase that doesn't open it.
|
|
var ErrSetupPassphrase = errors.New("the passphrase doesn't open this setup: type the one sync uses on the other computer")
|
|
|
|
// SetupText is this computer's sync setup as one line of text, sealed with
|
|
// its passphrase (AES-GCM under a PBKDF2 key, as the server's file is), so
|
|
// the text alone gives no one the server's password.
|
|
func SetupText() (string, error) {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return "", ErrSetupOff
|
|
}
|
|
if c.Passphrase == "" {
|
|
return "", ErrSetupOff
|
|
}
|
|
b, err := json.Marshal(Setup{URL: c.URL, User: c.User, Password: c.Password, Endpoint: c.Endpoint, Region: c.Region,
|
|
PathStyle: c.PathStyle, Branch: c.Branch, Keys: c.Keys, Agents: c.Agents, Library: c.library(), Memory: c.memoryOn(),
|
|
Usage: c.Usage, SignIns: c.SignIns, Providers: offOnly(c.Providers), Settings: offOnly(c.Settings), Profiles: offOnly(c.Profiles)})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sealed, err := backup.SealData(b, c.Passphrase)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return SetupPrefix + base64.RawURLEncoding.EncodeToString(sealed), nil
|
|
}
|
|
|
|
// offOnly is b as a setup carries it: false, or nil for synced.
|
|
func offOnly(b *bool) *bool {
|
|
if yes(b) {
|
|
return nil
|
|
}
|
|
f := false
|
|
return &f
|
|
}
|
|
|
|
// setupBody is the setup's text after its prefix, to the first character
|
|
// that can't be in it: line breaks and spaces a chat or a mail wrapped it
|
|
// with are in it, and taken out after.
|
|
var setupBody = lazyre.New(`^[A-Za-z0-9_\-+/=\s]*`)
|
|
|
|
// OpenSetupText opens what SetupText gave, with the passphrase it was sealed
|
|
// with. Text before the setup, and line breaks a chat put in it, are skipped.
|
|
func OpenSetupText(text, pass string) (Setup, error) {
|
|
i := strings.Index(text, SetupPrefix)
|
|
if i < 0 {
|
|
return Setup{}, ErrNotSetup
|
|
}
|
|
body := text[i+len(SetupPrefix):]
|
|
body = strings.Join(strings.Fields(setupBody.FindString(body)), "")
|
|
body = strings.NewReplacer("+", "-", "/", "_", "=", "").Replace(body)
|
|
sealed, err := base64.RawURLEncoding.DecodeString(body)
|
|
if err != nil {
|
|
return Setup{}, ErrNotSetup
|
|
}
|
|
plain, err := backup.OpenData(sealed, pass)
|
|
if errors.Is(err, backup.ErrPassphrase) {
|
|
return Setup{}, ErrSetupPassphrase
|
|
}
|
|
if err != nil {
|
|
return Setup{}, ErrNotSetup
|
|
}
|
|
var s Setup
|
|
if json.Unmarshal(plain, &s) != nil || s.URL == "" {
|
|
return Setup{}, ErrNotSetup
|
|
}
|
|
return s, nil
|
|
}
|