Skip to content

Commit 4fe266d

Browse files
committed
feat: support export chromium local storage
1 parent 2452b26 commit 4fe266d

5 files changed

Lines changed: 52 additions & 33 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448
1010
github.com/json-iterator/go v1.1.12
1111
github.com/mattn/go-sqlite3 v1.14.9
12+
github.com/otiai10/copy v1.7.0
1213
github.com/ppacher/go-dbus-keyring v1.0.1
1314
github.com/syndtr/goleveldb v1.0.0
1415
github.com/tidwall/gjson v1.9.3

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
3838
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
3939
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
4040
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
41+
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
42+
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
43+
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
44+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
45+
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
46+
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
47+
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
4148
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4249
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4350
github.com/ppacher/go-dbus-keyring v1.0.1 h1:dM4dMfP5w9MxY+foFHCQiN7izEGpFdKr3tZeMGmvqD0=

internal/browingdata/localstorage/localstorage.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"fmt"
77
"os"
8-
"path"
98
"strings"
109

1110
"github.com/syndtr/goleveldb/leveldb"
@@ -25,22 +24,29 @@ type storage struct {
2524
}
2625

2726
func (c *ChromiumLocalStorage) Parse(masterKey []byte) error {
28-
home, _ := os.UserHomeDir()
29-
db, err := leveldb.OpenFile(path.Join(home, "tmp/Local Storage/leveldb"), nil)
27+
db, err := leveldb.OpenFile(item.TempChromiumLocalStorage, nil)
3028
if err != nil {
3129
return err
3230
}
31+
defer os.RemoveAll(item.TempChromiumLocalStorage)
3332
// log.Info("parsing local storage now")
3433
defer db.Close()
3534

3635
iter := db.NewIterator(nil, nil)
3736
for iter.Next() {
3837
key := iter.Key()
3938
value := iter.Value()
39+
// don't parse value upper than 5kB
40+
if len(value) > 1024*5 {
41+
continue
42+
}
4043
var s = new(storage)
4144
s.fillKey(key)
42-
4345
s.fillValue(value)
46+
// don't save meta data
47+
if s.IsMeta {
48+
continue
49+
}
4450
*c = append(*c, *s)
4551
}
4652
iter.Release()

internal/browser/chromium/chromium.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@ func (c *chromium) BrowsingData() (*browingdata.Data, error) {
7171

7272
func (c *chromium) copyItemToLocal() error {
7373
for i, path := range c.itemPaths {
74-
// var dstFilename = item.TempName()
75-
var filename = i.String()
76-
// TODO: Handle read file error
77-
d, err := ioutil.ReadFile(path)
78-
if err != nil {
79-
return err
80-
}
81-
err = ioutil.WriteFile(filename, d, 0777)
82-
if err != nil {
83-
return err
74+
if fileutil.FolderExists(path) {
75+
if err := fileutil.CopyDir(path, i.String()); err != nil {
76+
return err
77+
}
78+
} else {
79+
var filename = i.String()
80+
// TODO: Handle read file error
81+
d, err := ioutil.ReadFile(path)
82+
if err != nil {
83+
return err
84+
}
85+
err = ioutil.WriteFile(filename, d, 0777)
86+
if err != nil {
87+
return err
88+
}
8489
}
8590
}
8691
return nil
@@ -91,7 +96,11 @@ func (c *chromium) getItemPath(profilePath string, items []item.Item) (map[item.
9196
parentDir := fileutil.ParentDir(profilePath)
9297
baseDir := fileutil.BaseDir(profilePath)
9398
err := filepath.Walk(parentDir, chromiumWalkFunc(items, itemPaths, baseDir))
94-
return itemPaths, err
99+
if err != nil {
100+
return itemPaths, err
101+
}
102+
fillLocalStoragePath(itemPaths, item.ChromiumLocalStorage)
103+
return itemPaths, nil
95104
}
96105

97106
func chromiumWalkFunc(items []item.Item, itemPaths map[item.Item]string, baseDir string) filepath.WalkFunc {
@@ -110,3 +119,12 @@ func chromiumWalkFunc(items []item.Item, itemPaths map[item.Item]string, baseDir
110119
return err
111120
}
112121
}
122+
123+
func fillLocalStoragePath(itemPaths map[item.Item]string, storage item.Item) {
124+
if p, ok := itemPaths[item.ChromiumHistory]; ok {
125+
lsp := filepath.Join(filepath.Dir(p), storage.FileName())
126+
if fileutil.FolderExists(lsp) {
127+
itemPaths[item.ChromiumLocalStorage] = lsp
128+
}
129+
}
130+
}

internal/utils/fileutil/filetutil.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13-
"hack-browser-data/internal/item"
1413
"hack-browser-data/internal/log"
14+
15+
cp "github.com/otiai10/copy"
1516
)
1617

1718
// FileExists checks if the file exists in the provided path
@@ -44,22 +45,8 @@ func ReadFile(filename string) (string, error) {
4445
return string(s), err
4546
}
4647

47-
// CopyItemToLocal copies the file from the provided path to the local path
48-
func CopyItemToLocal(itemPaths map[item.Item]string) error {
49-
for i, p := range itemPaths {
50-
// var dstFilename = item.TempName()
51-
var filename = i.String()
52-
// TODO: Handle read file error
53-
d, err := ioutil.ReadFile(p)
54-
if err != nil {
55-
log.Error(err.Error())
56-
}
57-
err = ioutil.WriteFile(filename, d, 0777)
58-
if err != nil {
59-
return err
60-
}
61-
}
62-
return nil
48+
func CopyDir(src, dst string) error {
49+
return cp.Copy(src, dst)
6350
}
6451

6552
func Filename(browser, item, ext string) string {
@@ -68,7 +55,7 @@ func Filename(browser, item, ext string) string {
6855
}
6956

7057
func ParentDir(p string) string {
71-
return filepath.Dir(p)
58+
return filepath.Dir(filepath.Clean(p))
7259
}
7360

7461
func BaseDir(p string) string {

0 commit comments

Comments
 (0)