refactor: Optimize traversal browser data logic (#311)

* refactor: Refactor package names and imports for better code organization.
* refactor: Package imports and variable types for consistency
* chore: Disable unused-parameter rule in revive.
* refactor: Refactor and organize data extraction and browserdata parse.
* fix: rename wrong error message info
This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2024-04-12 19:10:41 +08:00
parent c31cf602ed
commit 536f2082f9
35 changed files with 449 additions and 353 deletions
+30 -30
View File
@@ -6,8 +6,8 @@ import (
"path/filepath"
"strings"
"github.com/moond4rk/hackbrowserdata/browsingdata"
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/browserdata"
"github.com/moond4rk/hackbrowserdata/types"
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
"github.com/moond4rk/hackbrowserdata/utils/typeutil"
)
@@ -17,28 +17,28 @@ type Chromium struct {
storage string
profilePath string
masterKey []byte
items []item.Item
itemPaths map[item.Item]string
dataTypes []types.DataType
Paths map[types.DataType]string
}
// New create instance of Chromium browser, fill item's path if item is existed.
func New(name, storage, profilePath string, items []item.Item) ([]*Chromium, error) {
func New(name, storage, profilePath string, dataTypes []types.DataType) ([]*Chromium, error) {
c := &Chromium{
name: name,
storage: storage,
profilePath: profilePath,
items: items,
dataTypes: dataTypes,
}
multiItemPaths, err := c.userItemPaths(c.profilePath, c.items)
multiDataTypePaths, err := c.userDataTypePaths(c.profilePath, c.dataTypes)
if err != nil {
return nil, err
}
chromiumList := make([]*Chromium, 0, len(multiItemPaths))
for user, itemPaths := range multiItemPaths {
chromiumList := make([]*Chromium, 0, len(multiDataTypePaths))
for user, itemPaths := range multiDataTypePaths {
chromiumList = append(chromiumList, &Chromium{
name: fileutil.BrowserName(name, user),
items: typeutil.Keys(itemPaths),
itemPaths: itemPaths,
dataTypes: typeutil.Keys(itemPaths),
Paths: itemPaths,
storage: storage,
})
}
@@ -49,13 +49,13 @@ func (c *Chromium) Name() string {
return c.name
}
func (c *Chromium) BrowsingData(isFullExport bool) (*browsingdata.Data, error) {
items := c.items
func (c *Chromium) BrowsingData(isFullExport bool) (*browserdata.BrowserData, error) {
items := c.dataTypes
if !isFullExport {
items = item.FilterSensitiveItems(c.items)
items = types.FilterSensitiveItems(c.dataTypes)
}
data := browsingdata.New(items)
data := browserdata.New(items)
if err := c.copyItemToLocal(); err != nil {
return nil, err
@@ -75,15 +75,15 @@ func (c *Chromium) BrowsingData(isFullExport bool) (*browsingdata.Data, error) {
}
func (c *Chromium) copyItemToLocal() error {
for i, path := range c.itemPaths {
for i, path := range c.Paths {
filename := i.TempFilename()
var err error
switch {
case fileutil.IsDirExists(path):
if i == item.ChromiumLocalStorage {
if i == types.ChromiumLocalStorage {
err = fileutil.CopyDir(path, filename, "lock")
}
if i == item.ChromiumSessionStorage {
if i == types.ChromiumSessionStorage {
err = fileutil.CopyDir(path, filename, "lock")
}
default:
@@ -97,9 +97,9 @@ func (c *Chromium) copyItemToLocal() error {
return nil
}
// userItemPaths return a map of user to item path, map[profile 1][item's name & path key pair]
func (c *Chromium) userItemPaths(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) {
multiItemPaths := make(map[string]map[item.Item]string)
// userDataTypePaths return a map of user to item path, map[profile 1][item's name & path key pair]
func (c *Chromium) userDataTypePaths(profilePath string, items []types.DataType) (map[string]map[types.DataType]string, error) {
multiItemPaths := make(map[string]map[types.DataType]string)
parentDir := fileutil.ParentDir(profilePath)
err := filepath.Walk(parentDir, chromiumWalkFunc(items, multiItemPaths))
if err != nil {
@@ -109,27 +109,27 @@ func (c *Chromium) userItemPaths(profilePath string, items []item.Item) (map[str
var dir string
for userDir, v := range multiItemPaths {
for _, p := range v {
if strings.HasSuffix(p, item.ChromiumKey.Filename()) {
if strings.HasSuffix(p, types.ChromiumKey.Filename()) {
keyPath = p
dir = userDir
break
}
}
}
t := make(map[string]map[item.Item]string)
t := make(map[string]map[types.DataType]string)
for userDir, v := range multiItemPaths {
if userDir == dir {
continue
}
t[userDir] = v
t[userDir][item.ChromiumKey] = keyPath
fillLocalStoragePath(t[userDir], item.ChromiumLocalStorage)
t[userDir][types.ChromiumKey] = keyPath
fillLocalStoragePath(t[userDir], types.ChromiumLocalStorage)
}
return t, nil
}
// chromiumWalkFunc return a filepath.WalkFunc to find item's path
func chromiumWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc {
func chromiumWalkFunc(items []types.DataType, multiItemPaths map[string]map[types.DataType]string) filepath.WalkFunc {
return func(path string, info fs.FileInfo, err error) error {
for _, v := range items {
if info.Name() != v.Filename() {
@@ -145,18 +145,18 @@ func chromiumWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item
if _, exist := multiItemPaths[profileFolder]; exist {
multiItemPaths[profileFolder][v] = path
} else {
multiItemPaths[profileFolder] = map[item.Item]string{v: path}
multiItemPaths[profileFolder] = map[types.DataType]string{v: path}
}
}
return err
}
}
func fillLocalStoragePath(itemPaths map[item.Item]string, storage item.Item) {
if p, ok := itemPaths[item.ChromiumHistory]; ok {
func fillLocalStoragePath(itemPaths map[types.DataType]string, storage types.DataType) {
if p, ok := itemPaths[types.ChromiumHistory]; ok {
lsp := filepath.Join(filepath.Dir(p), storage.Filename())
if fileutil.IsDirExists(lsp) {
itemPaths[item.ChromiumLocalStorage] = lsp
itemPaths[types.ChromiumLocalStorage] = lsp
}
}
}
+2 -2
View File
@@ -14,7 +14,7 @@ import (
"golang.org/x/crypto/pbkdf2"
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
)
var (
@@ -24,7 +24,7 @@ var (
func (c *Chromium) GetMasterKey() ([]byte, error) {
// don't need chromium key file for macOS
defer os.Remove(item.ChromiumKey.TempFilename())
defer os.Remove(types.ChromiumKey.TempFilename())
// Get the master key from the keychain
// $ security find-generic-password -wa 'Chrome'
var (
+3 -3
View File
@@ -12,13 +12,13 @@ import (
keyring "github.com/ppacher/go-dbus-keyring"
"golang.org/x/crypto/pbkdf2"
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
)
func (c *Chromium) GetMasterKey() ([]byte, error) {
// what is d-bus @https://dbus.freedesktop.org/
// don't need chromium key file for Linux
defer os.Remove(item.ChromiumKey.TempFilename())
defer os.Remove(types.ChromiumKey.TempFilename())
conn, err := dbus.SessionBus()
if err != nil {
@@ -56,7 +56,7 @@ func (c *Chromium) GetMasterKey() ([]byte, error) {
if label == c.storage {
se, err := i.GetSecret(session.Path())
if err != nil {
return nil, fmt.Errorf("get storage from dbus error: %v" + err.Error())
return nil, fmt.Errorf("get storage from dbus: %w", err)
}
secret = se.Value
}
+3 -3
View File
@@ -11,18 +11,18 @@ import (
"github.com/tidwall/gjson"
"github.com/moond4rk/hackbrowserdata/crypto"
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
)
var errDecodeMasterKeyFailed = errors.New("decode master key failed")
func (c *Chromium) GetMasterKey() ([]byte, error) {
b, err := fileutil.ReadFile(item.ChromiumKey.TempFilename())
b, err := fileutil.ReadFile(types.ChromiumKey.TempFilename())
if err != nil {
return nil, err
}
defer os.Remove(item.ChromiumKey.TempFilename())
defer os.Remove(types.ChromiumKey.TempFilename())
encryptedKey := gjson.Get(b, "os_crypt.encrypted_key")
if !encryptedKey.Exists() {