Files
HackBrowserData/internal/app/keys.go
T
Roger 0ed920f846 docs: trim code comments to the why (#676)
Comments that restated names or narrated the code are gone, and multi-line ones are cut to a line of why.
Only comments change: each edited Go file has the same token stream as before. winapi and abe come later.
2026-11-02 00:03:03 +08:00

80 lines
2.2 KiB
Go

package app
import (
"context"
"fmt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/log"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// DumpKeys keeps the keys that worked when a dir's others fail, and leaves out a dir with none.
// It fails only when the run is stopped, so a stopped run writes no keys.json.
func DumpKeys(ctx context.Context, d Discovery, req Request) (masterkey.Dump, error) {
return dumpKeysWith(ctx, d, machineKeys(loginKeychain(req)))
}
func dumpKeysWith(ctx context.Context, d Discovery, keys keySource) (masterkey.Dump, error) {
dump := masterkey.NewDump()
for _, dir := range d.Dirs {
if dir.Def.Base != browser.Chromium {
continue
}
set, err := retrieveKeys(ctx, dir, keys)
if ctxErr := ctx.Err(); ctxErr != nil {
return masterkey.Dump{}, fmt.Errorf("run stopped: %w", ctxErr)
}
if err != nil {
status := "partial"
if len(set) == 0 {
status = "failed"
}
log.Warnf("dump-keys: %s %s: %v", dir.Def.Name, status, err)
}
if len(set) == 0 {
continue
}
v := masterkey.Vault{
Browser: dir.Def.ID,
Kind: chromium.VaultKind(dir.Def),
UserDataDir: dir.Path,
Profiles: profileNames(dir),
Keys: set,
}
if string(dir.ID) != dir.Def.ID {
v.ID = string(dir.ID)
}
dump.Vaults = append(dump.Vaults, v)
}
return dump, nil
}
func retrieveKeys(ctx context.Context, dir browser.UserDataDir, keys keySource) (masterkey.Set, error) {
d, err := snapshot.Temp()
if err != nil {
return nil, err
}
defer closeSnapshot(d)
var files []snapshot.File
for _, src := range chromium.KeySources(dir) {
if f, ok, err := firstOnDisk(dir, browser.Profile{}, src); err == nil && ok {
files = append(files, f)
}
}
if err := d.Capture(ctx, files); err != nil {
return nil, err
}
return keys(ctx, dir, d.Snapshot(string(dir.ID)))
}
func profileNames(dir browser.UserDataDir) []string {
names := make([]string, 0, len(dir.Profiles))
for _, p := range dir.Profiles {
names = append(names, p.Name)
}
return names
}