feat(safari): multi-profile support (#581)

* feat(safari): multi-profile support
This commit is contained in:
Roger
2026-04-21 15:50:36 +08:00
committed by GitHub
parent 7b9a973c9c
commit d75738b90f
8 changed files with 676 additions and 103 deletions
+46 -49
View File
@@ -10,45 +10,49 @@ import (
"github.com/moond4rk/hackbrowserdata/types"
)
// Browser represents Safari browser data ready for extraction.
// Safari has a single flat data directory (no profile subdirectories)
// and stores most data unencrypted (passwords live in macOS Keychain).
// Browser is one Safari profile's data ready for extraction. Passwords come from the shared macOS
// Keychain; everything else reads from the profile's directories.
type Browser struct {
cfg types.BrowserConfig
dataDir string // absolute path to ~/Library/Safari
keychainPassword string // macOS login password for Keychain unlock
sources map[types.Category][]sourcePath // Category → candidate paths
sourcePaths map[types.Category]resolvedPath // Category → discovered absolute path
profile profileContext
keychainPassword string
sourcePaths map[types.Category]resolvedPath
}
// SetKeychainPassword sets the macOS login password used to unlock
// the Keychain for Safari password extraction.
func (b *Browser) SetKeychainPassword(password string) {
b.keychainPassword = password
}
func (b *Browser) SetKeychainPassword(password string) { b.keychainPassword = password }
// NewBrowsers checks whether Safari data exists at cfg.UserDataDir and returns
// a single Browser if any known source files are found. Unlike Chromium/Firefox,
// Safari has no profile directories — the data directory is used directly.
// NewBrowsers returns one Browser per Safari profile with resolvable data. Named profiles are
// enumerated from SafariTabs.db.
func NewBrowsers(cfg types.BrowserConfig) ([]*Browser, error) {
sourcePaths := resolveSourcePaths(safariSources, cfg.UserDataDir)
if len(sourcePaths) == 0 {
return nil, nil
var browsers []*Browser
for _, p := range discoverSafariProfiles(cfg.UserDataDir) {
paths := resolveProfilePaths(p)
if len(paths) == 0 {
continue
}
browsers = append(browsers, &Browser{
cfg: cfg,
profile: p,
sourcePaths: paths,
})
}
return []*Browser{{
cfg: cfg,
dataDir: cfg.UserDataDir,
sources: safariSources,
sourcePaths: sourcePaths,
}}, nil
return browsers, nil
}
func resolveProfilePaths(p profileContext) map[types.Category]resolvedPath {
return resolveSourcePaths(buildSources(p))
}
func (b *Browser) BrowserName() string { return b.cfg.Name }
func (b *Browser) ProfileDir() string { return b.dataDir }
func (b *Browser) ProfileName() string { return "default" }
func (b *Browser) ProfileName() string { return b.profile.name }
func (b *Browser) ProfileDir() string {
if b.profile.isDefault() {
return b.profile.legacyHome
}
return filepath.Join(b.profile.container, "Safari", "Profiles", b.profile.uuidUpper)
}
// Extract copies browser files to a temp directory and extracts data
// for the requested categories.
func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, error) {
session, err := filemanager.NewSession()
if err != nil {
@@ -60,9 +64,11 @@ func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, erro
data := &types.BrowserData{}
for _, cat := range categories {
// Password is stored in macOS Keychain, not in a file.
// Keychain is user-scope, not per-profile — attribute only to default to avoid duplicates.
if cat == types.Password {
b.extractCategory(data, cat, "")
if b.profile.isDefault() {
b.extractCategory(data, cat, "")
}
continue
}
path, ok := tempPaths[cat]
@@ -74,8 +80,6 @@ func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, erro
return data, nil
}
// CountEntries copies browser files to a temp directory and counts entries
// per category without full extraction.
func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]int, error) {
session, err := filemanager.NewSession()
if err != nil {
@@ -88,7 +92,9 @@ func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]
counts := make(map[types.Category]int)
for _, cat := range categories {
if cat == types.Password {
counts[cat] = b.countCategory(cat, "")
if b.profile.isDefault() {
counts[cat] = b.countCategory(cat, "")
}
continue
}
path, ok := tempPaths[cat]
@@ -100,7 +106,6 @@ func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]
return counts, nil
}
// acquireFiles copies source files to the session temp directory.
func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
tempPaths := make(map[types.Category]string)
for _, cat := range categories {
@@ -118,7 +123,6 @@ func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.
return tempPaths
}
// extractCategory calls the appropriate extract function for a category.
func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, path string) {
var err error
switch cat {
@@ -131,7 +135,7 @@ func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, p
case types.Bookmark:
data.Bookmarks, err = extractBookmarks(path)
case types.Download:
data.Downloads, err = extractDownloads(path)
data.Downloads, err = extractDownloads(path, b.profile.downloadOwnerUUID())
default:
return
}
@@ -140,7 +144,6 @@ func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, p
}
}
// countCategory calls the appropriate count function for a category.
func (b *Browser) countCategory(cat types.Category, path string) int {
var count int
var err error
@@ -154,7 +157,7 @@ func (b *Browser) countCategory(cat types.Category, path string) int {
case types.Bookmark:
count, err = countBookmarks(path)
case types.Download:
count, err = countDownloads(path)
count, err = countDownloads(path, b.profile.downloadOwnerUUID())
default:
// Unsupported categories silently return 0.
}
@@ -164,25 +167,22 @@ func (b *Browser) countCategory(cat types.Category, path string) int {
return count
}
// resolvedPath holds the absolute path and type for a discovered source.
type resolvedPath struct {
absPath string
isDir bool
}
// resolveSourcePaths checks which sources actually exist in dataDir.
// Candidates are tried in priority order; the first existing path wins.
func resolveSourcePaths(sources map[types.Category][]sourcePath, dataDir string) map[types.Category]resolvedPath {
// resolveSourcePaths returns only paths that exist; first matching candidate wins per category.
func resolveSourcePaths(sources map[types.Category][]sourcePath) map[types.Category]resolvedPath {
resolved := make(map[types.Category]resolvedPath)
for cat, candidates := range sources {
for _, sp := range candidates {
abs := filepath.Join(dataDir, sp.rel)
info, err := os.Stat(abs)
info, err := os.Stat(sp.abs)
if err != nil {
continue
}
if sp.isDir == info.IsDir() {
resolved[cat] = resolvedPath{abs, sp.isDir}
resolved[cat] = resolvedPath{sp.abs, sp.isDir}
break
}
}
@@ -190,12 +190,9 @@ func resolveSourcePaths(sources map[types.Category][]sourcePath, dataDir string)
return resolved
}
// coreDataEpochOffset is the number of seconds between the Unix epoch
// (1970-01-01) and the Core Data epoch (2001-01-01).
// Safari's History.db uses the Core Data epoch (2001-01-01) instead of Unix epoch.
const coreDataEpochOffset = 978307200
// coredataTimestamp converts a Core Data timestamp (seconds since 2001-01-01)
// to a time.Time. Safari's History.db uses this epoch for visit_time.
func coredataTimestamp(seconds float64) time.Time {
return time.Unix(int64(seconds)+coreDataEpochOffset, 0)
}