Files
HackBrowserData/browser/browser.go
T
Roger a0b4412bf2 fix: share key retriever across all browsers to avoid repeated prompts (#560)
* fix: share key retriever across all browsers to avoid repeated password prompts
2026-04-06 21:57:52 +08:00

141 lines
4.1 KiB
Go

package browser
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/moond4rk/hackbrowserdata/browser/chromium"
"github.com/moond4rk/hackbrowserdata/browser/firefox"
"github.com/moond4rk/hackbrowserdata/crypto/keyretriever"
"github.com/moond4rk/hackbrowserdata/log"
"github.com/moond4rk/hackbrowserdata/types"
)
// Browser is the interface that both chromium.Browser and firefox.Browser implement.
type Browser interface {
BrowserName() string
ProfileName() string
ProfileDir() string
Extract(categories []types.Category) (*types.BrowserData, error)
}
// PickOptions configures which browsers to pick.
type PickOptions struct {
Name string // browser name filter: "all"|"chrome"|"firefox"|...
ProfilePath string // custom profile directory override
KeychainPassword string // macOS keychain password (ignored on other platforms)
}
// PickBrowsers returns browsers matching the given options.
// When Name is "all", all known browsers are tried.
// ProfilePath overrides the default user data directory (only when targeting a specific browser).
func PickBrowsers(opts PickOptions) ([]Browser, error) {
return pickFromConfigs(platformBrowsers(), opts)
}
// pickFromConfigs is the testable core of PickBrowsers. It iterates over
// platform browser configs, discovers installed profiles, and injects a
// shared key retriever into Chromium browsers for decryption.
func pickFromConfigs(configs []types.BrowserConfig, opts PickOptions) ([]Browser, error) {
name := strings.ToLower(opts.Name)
if name == "" {
name = "all"
}
// Create a single key retriever shared across all Chromium browsers.
// On macOS this avoids repeated password prompts; on other platforms
// it's harmless (DPAPI reads Local State per-profile, D-Bus is stateless).
retriever := keyretriever.DefaultRetriever(opts.KeychainPassword)
var browsers []Browser
for _, cfg := range configs {
if name != "all" && cfg.Key != name {
continue
}
// Override profile directory when targeting a specific browser.
if opts.ProfilePath != "" && name != "all" {
if cfg.Kind == types.Firefox {
cfg.UserDataDir = filepath.Dir(filepath.Clean(opts.ProfilePath))
} else {
cfg.UserDataDir = opts.ProfilePath
}
}
found, err := newBrowsers(cfg)
if err != nil {
log.Errorf("browser %s: %v", cfg.Name, err)
continue
}
if len(found) == 0 {
log.Debugf("browser %s not found at %s", cfg.Name, cfg.UserDataDir)
continue
}
// Inject the shared key retriever into browsers that need it.
// Chromium browsers implement retrieverSetter; Firefox does not.
for _, b := range found {
if setter, ok := b.(retrieverSetter); ok {
setter.SetRetriever(retriever)
}
}
browsers = append(browsers, found...)
}
return browsers, nil
}
// retrieverSetter is implemented by browsers that need an external key retriever.
// This allows pickFromConfigs to inject the shared retriever after construction
// without coupling the Browser interface to Chromium-specific concerns.
type retrieverSetter interface {
SetRetriever(keyretriever.KeyRetriever)
}
// newBrowsers dispatches to the correct engine based on BrowserKind
// and converts engine-specific types to the Browser interface.
func newBrowsers(cfg types.BrowserConfig) ([]Browser, error) {
switch cfg.Kind {
case types.Chromium, types.ChromiumYandex, types.ChromiumOpera:
found, err := chromium.NewBrowsers(cfg)
if err != nil {
return nil, err
}
result := make([]Browser, len(found))
for i, b := range found {
result[i] = b
}
return result, nil
case types.Firefox:
found, err := firefox.NewBrowsers(cfg)
if err != nil {
return nil, err
}
result := make([]Browser, len(found))
for i, b := range found {
result[i] = b
}
return result, nil
default:
return nil, fmt.Errorf("unknown browser kind: %d", cfg.Kind)
}
}
// ListBrowsers returns sorted keys of all platform browsers.
func ListBrowsers() []string {
var l []string
for _, cfg := range platformBrowsers() {
l = append(l, cfg.Key)
}
sort.Strings(l)
return l
}
// Names returns a pipe-separated list of browser keys for CLI help text.
func Names() string {
return strings.Join(ListBrowsers(), "|")
}