From 4fe266d954bd87b7a445993d9763fa9b7d48d106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=8D=E1=B4=8F=E1=B4=8F=C9=B4D4=CA=80=E1=B4=8B?= Date: Tue, 19 Apr 2022 22:33:12 +0800 Subject: [PATCH] feat: support export chromium local storage --- go.mod | 1 + go.sum | 7 ++++ .../browingdata/localstorage/localstorage.go | 14 +++++-- internal/browser/chromium/chromium.go | 40 ++++++++++++++----- internal/utils/fileutil/filetutil.go | 23 +++-------- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index 9b5fd94..7df5a94 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448 github.com/json-iterator/go v1.1.12 github.com/mattn/go-sqlite3 v1.14.9 + github.com/otiai10/copy v1.7.0 github.com/ppacher/go-dbus-keyring v1.0.1 github.com/syndtr/goleveldb v1.0.0 github.com/tidwall/gjson v1.9.3 diff --git a/go.sum b/go.sum index 2b1c81f..b84743f 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,13 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE= +github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI= +github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/ppacher/go-dbus-keyring v1.0.1 h1:dM4dMfP5w9MxY+foFHCQiN7izEGpFdKr3tZeMGmvqD0= diff --git a/internal/browingdata/localstorage/localstorage.go b/internal/browingdata/localstorage/localstorage.go index 9c89d36..c129d24 100644 --- a/internal/browingdata/localstorage/localstorage.go +++ b/internal/browingdata/localstorage/localstorage.go @@ -5,7 +5,6 @@ import ( "database/sql" "fmt" "os" - "path" "strings" "github.com/syndtr/goleveldb/leveldb" @@ -25,11 +24,11 @@ type storage struct { } func (c *ChromiumLocalStorage) Parse(masterKey []byte) error { - home, _ := os.UserHomeDir() - db, err := leveldb.OpenFile(path.Join(home, "tmp/Local Storage/leveldb"), nil) + db, err := leveldb.OpenFile(item.TempChromiumLocalStorage, nil) if err != nil { return err } + defer os.RemoveAll(item.TempChromiumLocalStorage) // log.Info("parsing local storage now") defer db.Close() @@ -37,10 +36,17 @@ func (c *ChromiumLocalStorage) Parse(masterKey []byte) error { for iter.Next() { key := iter.Key() value := iter.Value() + // don't parse value upper than 5kB + if len(value) > 1024*5 { + continue + } var s = new(storage) s.fillKey(key) - s.fillValue(value) + // don't save meta data + if s.IsMeta { + continue + } *c = append(*c, *s) } iter.Release() diff --git a/internal/browser/chromium/chromium.go b/internal/browser/chromium/chromium.go index 1d278f8..5a8673d 100644 --- a/internal/browser/chromium/chromium.go +++ b/internal/browser/chromium/chromium.go @@ -71,16 +71,21 @@ func (c *chromium) BrowsingData() (*browingdata.Data, error) { func (c *chromium) copyItemToLocal() error { for i, path := range c.itemPaths { - // var dstFilename = item.TempName() - var filename = i.String() - // TODO: Handle read file error - d, err := ioutil.ReadFile(path) - if err != nil { - return err - } - err = ioutil.WriteFile(filename, d, 0777) - if err != nil { - return err + if fileutil.FolderExists(path) { + if err := fileutil.CopyDir(path, i.String()); err != nil { + return err + } + } else { + var filename = i.String() + // TODO: Handle read file error + d, err := ioutil.ReadFile(path) + if err != nil { + return err + } + err = ioutil.WriteFile(filename, d, 0777) + if err != nil { + return err + } } } return nil @@ -91,7 +96,11 @@ func (c *chromium) getItemPath(profilePath string, items []item.Item) (map[item. parentDir := fileutil.ParentDir(profilePath) baseDir := fileutil.BaseDir(profilePath) err := filepath.Walk(parentDir, chromiumWalkFunc(items, itemPaths, baseDir)) - return itemPaths, err + if err != nil { + return itemPaths, err + } + fillLocalStoragePath(itemPaths, item.ChromiumLocalStorage) + return itemPaths, nil } 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 return err } } + +func fillLocalStoragePath(itemPaths map[item.Item]string, storage item.Item) { + if p, ok := itemPaths[item.ChromiumHistory]; ok { + lsp := filepath.Join(filepath.Dir(p), storage.FileName()) + if fileutil.FolderExists(lsp) { + itemPaths[item.ChromiumLocalStorage] = lsp + } + } +} diff --git a/internal/utils/fileutil/filetutil.go b/internal/utils/fileutil/filetutil.go index e2681a0..4e54c25 100644 --- a/internal/utils/fileutil/filetutil.go +++ b/internal/utils/fileutil/filetutil.go @@ -10,8 +10,9 @@ import ( "path/filepath" "strings" - "hack-browser-data/internal/item" "hack-browser-data/internal/log" + + cp "github.com/otiai10/copy" ) // FileExists checks if the file exists in the provided path @@ -44,22 +45,8 @@ func ReadFile(filename string) (string, error) { return string(s), err } -// CopyItemToLocal copies the file from the provided path to the local path -func CopyItemToLocal(itemPaths map[item.Item]string) error { - for i, p := range itemPaths { - // var dstFilename = item.TempName() - var filename = i.String() - // TODO: Handle read file error - d, err := ioutil.ReadFile(p) - if err != nil { - log.Error(err.Error()) - } - err = ioutil.WriteFile(filename, d, 0777) - if err != nil { - return err - } - } - return nil +func CopyDir(src, dst string) error { + return cp.Copy(src, dst) } func Filename(browser, item, ext string) string { @@ -68,7 +55,7 @@ func Filename(browser, item, ext string) string { } func ParentDir(p string) string { - return filepath.Dir(p) + return filepath.Dir(filepath.Clean(p)) } func BaseDir(p string) string {