feat: support export chromium local storage

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-19 22:33:12 +08:00
parent 2452b2670a
commit 4fe266d954
5 changed files with 52 additions and 33 deletions
@@ -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()
+29 -11
View File
@@ -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
}
}
}
+5 -18
View File
@@ -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 {