mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
* 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.
98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
|
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
|
|
"github.com/moond4rk/hackbrowserdata/internal/browser/firefox"
|
|
"github.com/moond4rk/hackbrowserdata/internal/browser/safari"
|
|
)
|
|
|
|
// Discovery keeps a failure per browser whose data could not be read, so it is reported, not taken as absence.
|
|
type Discovery struct {
|
|
Dirs []browser.UserDataDir
|
|
DirFailures []DirFailure
|
|
}
|
|
|
|
// Discover applies the target's profile path only when one browser is named.
|
|
func Discover(target Target) Discovery {
|
|
var discovery Discovery
|
|
for _, supportedBrowser := range browser.Supported() {
|
|
if target.BrowserName != allBrowsers && supportedBrowser.Name != target.BrowserName {
|
|
continue
|
|
}
|
|
discoverOptions := browser.DiscoverOptions{HomeDir: target.HomeDir}
|
|
if target.BrowserName != allBrowsers {
|
|
discoverOptions.ProfilePath = target.ProfilePath
|
|
}
|
|
dirs, err := discover(supportedBrowser, discoverOptions)
|
|
discovery.Dirs = append(discovery.Dirs, withData(dirs)...)
|
|
if err != nil {
|
|
discovery.DirFailures = append(discovery.DirFailures, DirFailure{UserDataDirID: browser.UserDataDirID(supportedBrowser.Name), Err: err})
|
|
}
|
|
}
|
|
return discovery
|
|
}
|
|
|
|
// withData drops profiles holding no file any category reads, such as a bare Preferences, but keeps unreadable ones.
|
|
func withData(dirs []browser.UserDataDir) []browser.UserDataDir {
|
|
extractors := extractorsWith(nil)
|
|
var out []browser.UserDataDir
|
|
for _, dir := range dirs {
|
|
var profiles []browser.Profile
|
|
for _, profile := range dir.Profiles {
|
|
if hasData(dir, profile, extractors(dir, profile)) {
|
|
profiles = append(profiles, profile)
|
|
}
|
|
}
|
|
if len(profiles) > 0 {
|
|
dir.Profiles = profiles
|
|
out = append(out, dir)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func hasData(dir browser.UserDataDir, profile browser.Profile, extractors browser.Extractors) bool {
|
|
for _, extractor := range extractors {
|
|
if len(extractor.Sources) == 0 {
|
|
continue
|
|
}
|
|
if _, ok, err := locate(dir, profile, extractor.Sources); ok || err != nil {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// discover dispatches on Base here, not in browser, because the base packages import browser.
|
|
func discover(supportedBrowser browser.Browser, options browser.DiscoverOptions) ([]browser.UserDataDir, error) {
|
|
switch supportedBrowser.Base {
|
|
case browser.Chromium:
|
|
return chromium.Discover(supportedBrowser, options)
|
|
case browser.Firefox:
|
|
return firefox.Discover(supportedBrowser, options)
|
|
case browser.Safari:
|
|
return safari.Discover(supportedBrowser, options)
|
|
default:
|
|
return nil, fmt.Errorf("%s: no base package", supportedBrowser.Name)
|
|
}
|
|
}
|
|
|
|
// extractorsWith hands Safari the run's login keychain, which its default profile's password extractor reads.
|
|
func extractorsWith(keychain safari.LoginKeychain) extractorsFunc {
|
|
return func(dir browser.UserDataDir, profile browser.Profile) browser.Extractors {
|
|
switch dir.Browser.Base {
|
|
case browser.Chromium:
|
|
return chromium.Extractors(dir, profile)
|
|
case browser.Firefox:
|
|
return firefox.Extractors(dir, profile)
|
|
case browser.Safari:
|
|
return safari.Extractors(dir, profile, keychain)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|