Files
HackBrowserData/internal/app/dump.go
T
Roger c733ac9d6d feat(storage): add --max-storage-value flag (#703)
Each browser capped storage values at its own byte limit, or not at all; one flag now caps decoded characters for every browser (default 2048, 0 = unlimited).
2026-11-19 16:47:37 +08:00

86 lines
2.4 KiB
Go

package app
import (
"context"
"fmt"
"os"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/export"
"github.com/moond4rk/hackbrowserdata/internal/log"
)
// DumpFlags are dump's command-line values as cobra parsed them.
type DumpFlags struct {
Browser string
Categories string
Format string
OutputDir string
Zip bool
ProfilePath string
MaxStorageValue int
}
type DumpOptions struct {
Target Target
Categories []category.Category
Output Output
Keychain KeychainConfig
}
// ValidateDump checks every flag before any browser or secret is touched, so a bad flag fails fast.
func ValidateDump(flags DumpFlags, keychainPassword func(context.Context) (string, error)) (DumpOptions, error) {
browsers, err := parseBrowsers(flags.Browser)
if err != nil {
return DumpOptions{}, err
}
categories, err := parseCategories(flags.Categories)
if err != nil {
return DumpOptions{}, err
}
format, err := export.ParseFormat(flags.Format)
if err != nil {
return DumpOptions{}, err
}
if err := checkOutputDir(flags.OutputDir); err != nil {
return DumpOptions{}, err
}
profilePath, err := checkProfilePath(browsers, flags.ProfilePath)
if err != nil {
return DumpOptions{}, err
}
if err := checkMaxStorageValue(flags.MaxStorageValue); err != nil {
return DumpOptions{}, err
}
home, err := homeDir()
if err != nil {
return DumpOptions{}, err
}
return DumpOptions{
Target: Target{Browsers: browsers, ProfilePath: profilePath, HomeDir: home},
Categories: categories,
Output: Output{Dir: flags.OutputDir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue},
Keychain: KeychainConfig{Path: loginKeychainPath(home), Password: keychainPassword},
}, nil
}
// checkProfilePath keeps -p only for one browser; with -b all it is ignored, as it always was, but now said.
func checkProfilePath(browsers []browser.Browser, path string) (string, error) {
if path == "" {
return "", nil
}
if len(browsers) != 1 {
log.Warnf("-p needs a specific -b; ignoring %s", path)
return "", nil
}
info, err := os.Stat(path)
if err != nil {
return "", fmt.Errorf("profile path: %w", err)
}
if !info.IsDir() {
return "", fmt.Errorf("profile path %s: not a directory", path)
}
return path, nil
}