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
+5 -5
View File
@@ -8,7 +8,7 @@ import (
"github.com/moond4rk/hackbrowserdata/browser/chromium"
"github.com/moond4rk/hackbrowserdata/browser/firefox"
"github.com/moond4rk/hackbrowserdata/browsingdata"
"github.com/moond4rk/hackbrowserdata/browserdata"
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
"github.com/moond4rk/hackbrowserdata/utils/typeutil"
)
@@ -17,7 +17,7 @@ type Browser interface {
// Name is browser's name
Name() string
// BrowsingData returns all browsing data in the browser.
BrowsingData(isFullExport bool) (*browsingdata.Data, error)
BrowsingData(isFullExport bool) (*browserdata.BrowserData, error)
}
// PickBrowsers returns a list of browsers that match the name and profile.
@@ -47,7 +47,7 @@ func pickChromium(name, profile string) []Browser {
slog.Warn("find browser failed, profile folder does not exist", "browser", v.name)
continue
}
multiChromium, err := chromium.New(v.name, v.storage, v.profilePath, v.items)
multiChromium, err := chromium.New(v.name, v.storage, v.profilePath, v.dataTypes)
if err != nil {
slog.Error("new chromium error", "err", err)
continue
@@ -65,7 +65,7 @@ func pickChromium(name, profile string) []Browser {
if !fileutil.IsDirExists(filepath.Clean(profile)) {
slog.Error("find browser failed, profile folder does not exist", "browser", c.name)
}
chromiumList, err := chromium.New(c.name, c.storage, profile, c.items)
chromiumList, err := chromium.New(c.name, c.storage, profile, c.dataTypes)
if err != nil {
slog.Error("new chromium error", "err", err)
}
@@ -93,7 +93,7 @@ func pickFirefox(name, profile string) []Browser {
continue
}
if multiFirefox, err := firefox.New(profile, v.items); err == nil {
if multiFirefox, err := firefox.New(profile, v.dataTypes); err == nil {
for _, b := range multiFirefox {
slog.Warn("find browser success", "browser", b.Name())
browsers = append(browsers, b)
+15 -15
View File
@@ -3,7 +3,7 @@
package browser
import (
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
)
var (
@@ -11,85 +11,85 @@ var (
name string
storage string
profilePath string
items []item.Item
dataTypes []types.DataType
}{
"chrome": {
name: chromeName,
storage: chromeStorageName,
profilePath: chromeProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"edge": {
name: edgeName,
storage: edgeStorageName,
profilePath: edgeProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chromium": {
name: chromiumName,
storage: chromiumStorageName,
profilePath: chromiumProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chrome-beta": {
name: chromeBetaName,
storage: chromeBetaStorageName,
profilePath: chromeBetaProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"opera": {
name: operaName,
profilePath: operaProfilePath,
storage: operaStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"opera-gx": {
name: operaGXName,
profilePath: operaGXProfilePath,
storage: operaStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"vivaldi": {
name: vivaldiName,
storage: vivaldiStorageName,
profilePath: vivaldiProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"coccoc": {
name: coccocName,
storage: coccocStorageName,
profilePath: coccocProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"brave": {
name: braveName,
profilePath: braveProfilePath,
storage: braveStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"yandex": {
name: yandexName,
storage: yandexStorageName,
profilePath: yandexProfilePath,
items: item.DefaultYandex,
dataTypes: types.DefaultYandexTypes,
},
"arc": {
name: arcName,
profilePath: arcProfilePath,
storage: arcStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
}
firefoxList = map[string]struct {
name string
storage string
profilePath string
items []item.Item
dataTypes []types.DataType
}{
"firefox": {
name: firefoxName,
profilePath: firefoxProfilePath,
items: item.DefaultFirefox,
dataTypes: types.DefaultFirefoxTypes,
},
}
)
+11 -11
View File
@@ -3,7 +3,7 @@
package browser
import (
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
)
var (
@@ -11,61 +11,61 @@ var (
name string
storage string
profilePath string
items []item.Item
dataTypes []types.DataType
}{
"chrome": {
name: chromeName,
storage: chromeStorageName,
profilePath: chromeProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"edge": {
name: edgeName,
storage: edgeStorageName,
profilePath: edgeProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chromium": {
name: chromiumName,
storage: chromiumStorageName,
profilePath: chromiumProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chrome-beta": {
name: chromeBetaName,
storage: chromeBetaStorageName,
profilePath: chromeBetaProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"opera": {
name: operaName,
profilePath: operaProfilePath,
storage: operaStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"vivaldi": {
name: vivaldiName,
storage: vivaldiStorageName,
profilePath: vivaldiProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"brave": {
name: braveName,
profilePath: braveProfilePath,
storage: braveStorageName,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
}
firefoxList = map[string]struct {
name string
storage string
profilePath string
items []item.Item
dataTypes []types.DataType
}{
"firefox": {
name: firefoxName,
profilePath: firefoxProfilePath,
items: item.DefaultFirefox,
dataTypes: types.DefaultFirefoxTypes,
},
}
)
+18 -18
View File
@@ -3,7 +3,7 @@
package browser
import (
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
)
var (
@@ -11,89 +11,89 @@ var (
name string
profilePath string
storage string
items []item.Item
dataTypes []types.DataType
}{
"chrome": {
name: chromeName,
profilePath: chromeUserDataPath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"edge": {
name: edgeName,
profilePath: edgeProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chromium": {
name: chromiumName,
profilePath: chromiumUserDataPath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"chrome-beta": {
name: chromeBetaName,
profilePath: chromeBetaUserDataPath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"opera": {
name: operaName,
profilePath: operaProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"opera-gx": {
name: operaGXName,
profilePath: operaGXProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"vivaldi": {
name: vivaldiName,
profilePath: vivaldiProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"coccoc": {
name: coccocName,
profilePath: coccocProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"brave": {
name: braveName,
profilePath: braveProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"yandex": {
name: yandexName,
profilePath: yandexProfilePath,
items: item.DefaultYandex,
dataTypes: types.DefaultYandexTypes,
},
"360": {
name: speed360Name,
profilePath: speed360ProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"qq": {
name: qqBrowserName,
profilePath: qqBrowserProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"dc": {
name: dcBrowserName,
profilePath: dcBrowserProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
"sogou": {
name: sogouName,
profilePath: sogouProfilePath,
items: item.DefaultChromium,
dataTypes: types.DefaultChromiumTypes,
},
}
firefoxList = map[string]struct {
name string
storage string
profilePath string
items []item.Item
dataTypes []types.DataType
}{
"firefox": {
name: firefoxName,
profilePath: firefoxProfilePath,
items: item.DefaultFirefox,
dataTypes: types.DefaultFirefoxTypes,
},
}
)
+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() {
+13 -13
View File
@@ -11,9 +11,9 @@ import (
_ "modernc.org/sqlite" // sqlite3 driver TODO: replace with chooseable driver
"github.com/moond4rk/hackbrowserdata/browsingdata"
"github.com/moond4rk/hackbrowserdata/browserdata"
"github.com/moond4rk/hackbrowserdata/crypto"
"github.com/moond4rk/hackbrowserdata/item"
"github.com/moond4rk/hackbrowserdata/types"
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
"github.com/moond4rk/hackbrowserdata/utils/typeutil"
)
@@ -23,15 +23,15 @@ type Firefox struct {
storage string
profilePath string
masterKey []byte
items []item.Item
itemPaths map[item.Item]string
items []types.DataType
itemPaths map[types.DataType]string
}
var ErrProfilePathNotFound = errors.New("profile path not found")
// New returns new Firefox instances.
func New(profilePath string, items []item.Item) ([]*Firefox, error) {
multiItemPaths := make(map[string]map[item.Item]string)
func New(profilePath string, items []types.DataType) ([]*Firefox, error) {
multiItemPaths := make(map[string]map[types.DataType]string)
// ignore walk dir error since it can be produced by a single entry
_ = filepath.WalkDir(profilePath, firefoxWalkFunc(items, multiItemPaths))
@@ -57,7 +57,7 @@ func (f *Firefox) copyItemToLocal() error {
return nil
}
func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) fs.WalkDirFunc {
func firefoxWalkFunc(items []types.DataType, multiItemPaths map[string]map[types.DataType]string) fs.WalkDirFunc {
return func(path string, info fs.DirEntry, err error) error {
for _, v := range items {
if info.Name() == v.Filename() {
@@ -65,7 +65,7 @@ func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]
if _, exist := multiItemPaths[parentBaseDir]; exist {
multiItemPaths[parentBaseDir][v] = path
} else {
multiItemPaths[parentBaseDir] = map[item.Item]string{v: path}
multiItemPaths[parentBaseDir] = map[types.DataType]string{v: path}
}
}
}
@@ -76,7 +76,7 @@ func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]
// GetMasterKey returns master key of Firefox. from key4.db
func (f *Firefox) GetMasterKey() ([]byte, error) {
tempFilename := item.FirefoxKey4.TempFilename()
tempFilename := types.FirefoxKey4.TempFilename()
// Open and defer close of the database.
keyDB, err := sql.Open("sqlite", tempFilename)
@@ -135,7 +135,7 @@ func processMasterKey(metaItem1, metaItem2, nssA11, nssA102 []byte) ([]byte, err
return nil, errors.New("flag verification failed: password-check not found")
}
var keyLin = []byte{248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
keyLin := []byte{248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
if !bytes.Equal(nssA102, keyLin) {
return nil, errors.New("master key verification failed: nssA102 not equal to expected value")
}
@@ -159,13 +159,13 @@ func (f *Firefox) Name() string {
return f.name
}
func (f *Firefox) BrowsingData(isFullExport bool) (*browsingdata.Data, error) {
func (f *Firefox) BrowsingData(isFullExport bool) (*browserdata.BrowserData, error) {
items := f.items
if !isFullExport {
items = item.FilterSensitiveItems(f.items)
items = types.FilterSensitiveItems(f.items)
}
b := browsingdata.New(items)
b := browserdata.New(items)
if err := f.copyItemToLocal(); err != nil {
return nil, err