Files
HackBrowserData/internal/app/masterkeys.go
T
Roger 7e6f325508 refactor(app): per-stage inputs and per-dir planning (#689)
* refactor(app): per-command flags and per-stage inputs

One Options bag fed every stage and cmd patched restore's keys into it after validation; each stage now takes what it
reads, dir-level failures are their own type, and cmd prints what a run exported.

* refactor(app): group the plan by dir and profile

Execute now walks the plan's own shape instead of filtering a flat list and looking profiles up by name.

* refactor(app): file code by command and stage

Pure moves: each command's flags and validation sit beside its stage, and execute.go keeps only the runner.

* refactor: read the keychain password in cmd

Prompting and reading stdin are terminal work, so app now receives the password func; cobra already rejects both flags.

* refactor(app): plan each user data dir right before it runs

A run-wide Plan had to drop its extractors and rebuild them with the keychain; per-dir planning keeps one extractor.
2026-11-09 17:25:36 +08:00

58 lines
1.8 KiB
Go

package app
import (
"context"
"sync"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/browser/safari"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// loginKeychain is the run's one login keychain, shared by Chromium's v10 key and Safari's passwords; nil off macOS.
type loginKeychain interface {
masterkey.LoginKeychain
safari.LoginKeychain
}
type dirKeysFunc func(ctx context.Context, dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterkey.Set, error)
// dirKeys retrieves a dir's keys on first use, for all its profiles; Go 1.20 has no sync.OnceValues.
type dirKeys struct {
once sync.Once
retrieve dirKeysFunc // nil for a dir whose base keeps no master keys
dir browser.UserDataDir
dirSnapshot snapshot.Snapshot
keys masterkey.Set
err error
}
func (k *dirKeys) get(ctx context.Context) masterkey.Set {
k.once.Do(func() {
if k.retrieve != nil {
k.keys, k.err = k.retrieve(ctx, k.dir, k.dirSnapshot)
}
})
return k.keys
}
// keysFromMachine asks this machine's secret stores; on macOS that includes the run's login keychain.
func keysFromMachine(keychain masterkey.LoginKeychain) dirKeysFunc {
return func(ctx context.Context, dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterkey.Set, error) {
locator, err := chromium.KeyLocator(dir, dirSnapshot)
if err != nil {
return nil, err
}
locator.Keychain = keychain
return masterkey.RetrieveAll(ctx, locator)
}
}
func keysFromFile(keys map[browser.UserDataDirID]masterkey.Set) dirKeysFunc {
return func(_ context.Context, dir browser.UserDataDir, _ snapshot.Snapshot) (masterkey.Set, error) {
return keys[dir.ID], nil
}
}