refactor: replace util with go generics

pull/125/head
ᴍᴏᴏɴD4ʀᴋ 3 years ago
parent c9feef66ab
commit 0dc28a077f
  1. 1
      go.mod
  2. 5
      go.sum
  3. 17
      internal/browingdata/bookmark.go
  4. 22
      internal/browingdata/cookie.go
  5. 10
      internal/browingdata/download.go
  6. 6
      internal/browingdata/history.go
  7. 12
      internal/browingdata/password.go
  8. 6
      internal/browser/browser.go
  9. 2
      internal/browser/browser_linux.go
  10. 1
      internal/item/sql.go
  11. 2
      internal/log/log.go
  12. 8
      internal/utils/fileutil/filetutil.go
  13. 35
      internal/utils/typeutil/typeutil.go
  14. 69
      internal/utils/utils.go

@ -13,6 +13,7 @@ require (
github.com/tidwall/gjson v1.9.3 github.com/tidwall/gjson v1.9.3
github.com/urfave/cli/v2 v2.4.0 github.com/urfave/cli/v2 v2.4.0
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd
) )
require ( require (

@ -12,11 +12,8 @@ github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw= github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/goutil v0.4.4/go.mod h1:qlGVh0PI+WnWSjYnIocfz/7tkeogxL6+EDNP1mRe+7o=
github.com/gookit/goutil v0.5.0 h1:SrbfjqZ8iprxJOfKZVT0yGJ4/82afr4Qa0RQwON19I4= github.com/gookit/goutil v0.5.0 h1:SrbfjqZ8iprxJOfKZVT0yGJ4/82afr4Qa0RQwON19I4=
github.com/gookit/goutil v0.5.0/go.mod h1:pq1eTibwb2wN96jrci0xy7xogWzzo9CihOQJEAvz4yQ= github.com/gookit/goutil v0.5.0/go.mod h1:pq1eTibwb2wN96jrci0xy7xogWzzo9CihOQJEAvz4yQ=
github.com/gookit/slog v0.2.1 h1:EI47PlvpoPwxwkNTYaevN6iBNjo9nBxo0aQLiq+MTdk=
github.com/gookit/slog v0.2.1/go.mod h1:CsbWzAaZA2FjLuGzvfTAR/QvFisZG31deOcZ05H++NI=
github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448 h1:7kNDAgYDAu/5X+PbFfzGToaKJiDcghKv7B4XOGosrPM= github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448 h1:7kNDAgYDAu/5X+PbFfzGToaKJiDcghKv7B4XOGosrPM=
github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448/go.mod h1:9Sh1Utw3LVG0kqN++ankdfrCVQ7yixKDL1+YrYvIulU= github.com/gookit/slog v0.2.2-0.20220415153407-dd89ed7b0448/go.mod h1:9Sh1Utw3LVG0kqN++ankdfrCVQ7yixKDL1+YrYvIulU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@ -57,6 +54,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd h1:zVFyTKZN/Q7mNRWSs1GOYnHM9NiFSJ54YVRsD0rNWT4=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

@ -10,8 +10,8 @@ import (
"hack-browser-data/internal/item" "hack-browser-data/internal/item"
"hack-browser-data/internal/log" "hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/fileutil" "hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -61,7 +61,7 @@ func getBookmarkChildren(value gjson.Result, w *ChromiumBookmark) (children gjso
ID: value.Get(bookmarkID).Int(), ID: value.Get(bookmarkID).Int(),
Name: value.Get(bookmarkName).String(), Name: value.Get(bookmarkName).String(),
URL: value.Get(bookmarkUrl).String(), URL: value.Get(bookmarkUrl).String(),
DateAdded: utils.TimeEpochFormat(value.Get(bookmarkAdded).Int()), DateAdded: typeutil.TimeEpoch(value.Get(bookmarkAdded).Int()),
} }
children = value.Get(bookmarkChildren) children = value.Get(bookmarkChildren)
if nodeType.Exists() { if nodeType.Exists() {
@ -114,9 +114,9 @@ func (f *FirefoxBookmark) Parse(masterKey []byte) error {
*f = append(*f, bookmark{ *f = append(*f, bookmark{
ID: id, ID: id,
Name: title, Name: title,
Type: utils.BookmarkType(bType), Type: bookmarkType(bType),
URL: url, URL: url,
DateAdded: utils.TimeStampFormat(dateAdded / 1000000), DateAdded: typeutil.TimeStamp(dateAdded / 1000000),
}) })
} }
sort.Slice(*f, func(i, j int) bool { sort.Slice(*f, func(i, j int) bool {
@ -128,3 +128,12 @@ func (f *FirefoxBookmark) Parse(masterKey []byte) error {
func (f *FirefoxBookmark) Name() string { func (f *FirefoxBookmark) Name() string {
return "bookmark" return "bookmark"
} }
func bookmarkType(a int64) string {
switch a {
case 1:
return "url"
default:
return "folder"
}
}

@ -10,7 +10,7 @@ import (
"hack-browser-data/internal/decrypter" "hack-browser-data/internal/decrypter"
"hack-browser-data/internal/item" "hack-browser-data/internal/item"
"hack-browser-data/internal/log" "hack-browser-data/internal/log"
"hack-browser-data/internal/utils" "hack-browser-data/internal/utils/typeutil"
) )
type ChromiumCookie []cookie type ChromiumCookie []cookie
@ -43,12 +43,12 @@ func (c *ChromiumCookie) Parse(masterKey []byte) error {
Host: host, Host: host,
Path: path, Path: path,
encryptValue: encryptValue, encryptValue: encryptValue,
IsSecure: utils.IntToBool(isSecure), IsSecure: typeutil.IntToBool(isSecure),
IsHTTPOnly: utils.IntToBool(isHTTPOnly), IsHTTPOnly: typeutil.IntToBool(isHTTPOnly),
HasExpire: utils.IntToBool(hasExpire), HasExpire: typeutil.IntToBool(hasExpire),
IsPersistent: utils.IntToBool(isPersistent), IsPersistent: typeutil.IntToBool(isPersistent),
CreateDate: utils.TimeEpochFormat(createDate), CreateDate: typeutil.TimeEpoch(createDate),
ExpireDate: utils.TimeEpochFormat(expireDate), ExpireDate: typeutil.TimeEpoch(expireDate),
} }
// TODO: replace DPAPI // TODO: replace DPAPI
if len(encryptValue) > 0 { if len(encryptValue) > 0 {
@ -102,10 +102,10 @@ func (f *FirefoxCookie) Parse(masterKey []byte) error {
KeyName: name, KeyName: name,
Host: host, Host: host,
Path: path, Path: path,
IsSecure: utils.IntToBool(isSecure), IsSecure: typeutil.IntToBool(isSecure),
IsHTTPOnly: utils.IntToBool(isHttpOnly), IsHTTPOnly: typeutil.IntToBool(isHttpOnly),
CreateDate: utils.TimeStampFormat(creationTime / 1000000), CreateDate: typeutil.TimeStamp(creationTime / 1000000),
ExpireDate: utils.TimeStampFormat(expiry), ExpireDate: typeutil.TimeStamp(expiry),
Value: value, Value: value,
}) })
} }

@ -8,7 +8,7 @@ import (
"hack-browser-data/internal/item" "hack-browser-data/internal/item"
"hack-browser-data/internal/log" "hack-browser-data/internal/log"
"hack-browser-data/internal/utils" "hack-browser-data/internal/utils/typeutil"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -40,8 +40,8 @@ func (c *ChromiumDownload) Parse(masterKey []byte) error {
TargetPath: targetPath, TargetPath: targetPath,
Url: tabUrl, Url: tabUrl,
TotalBytes: totalBytes, TotalBytes: totalBytes,
StartTime: utils.TimeEpochFormat(startTime), StartTime: typeutil.TimeEpoch(startTime),
EndTime: utils.TimeEpochFormat(endTime), EndTime: typeutil.TimeEpoch(endTime),
MimeType: mimeType, MimeType: mimeType,
} }
*c = append(*c, data) *c = append(*c, data)
@ -98,8 +98,8 @@ func (f *FirefoxDownload) Parse(masterKey []byte) error {
TargetPath: path, TargetPath: path,
Url: url, Url: url,
TotalBytes: fileSize.Int(), TotalBytes: fileSize.Int(),
StartTime: utils.TimeStampFormat(dateAdded / 1000000), StartTime: typeutil.TimeStamp(dateAdded / 1000000),
EndTime: utils.TimeStampFormat(endTime.Int() / 1000), EndTime: typeutil.TimeStamp(endTime.Int() / 1000),
}) })
} }
} }

@ -9,7 +9,7 @@ import (
"hack-browser-data/internal/item" "hack-browser-data/internal/item"
"hack-browser-data/internal/log" "hack-browser-data/internal/log"
"hack-browser-data/internal/utils" "hack-browser-data/internal/utils/typeutil"
) )
type ChromiumHistory []history type ChromiumHistory []history
@ -40,7 +40,7 @@ func (c *ChromiumHistory) Parse(masterKey []byte) error {
Url: url, Url: url,
Title: title, Title: title,
VisitCount: visitCount, VisitCount: visitCount,
LastVisitTime: utils.TimeEpochFormat(lastVisitTime), LastVisitTime: typeutil.TimeEpoch(lastVisitTime),
} }
*c = append(*c, data) *c = append(*c, data)
} }
@ -91,7 +91,7 @@ func (f *FirefoxHistory) Parse(masterKey []byte) error {
Title: title, Title: title,
Url: url, Url: url,
VisitCount: visitCount, VisitCount: visitCount,
LastVisitTime: utils.TimeStampFormat(visitDate / 1000000), LastVisitTime: typeutil.TimeStamp(visitDate / 1000000),
}) })
} }
sort.Slice(*f, func(i, j int) bool { sort.Slice(*f, func(i, j int) bool {

@ -15,7 +15,7 @@ import (
"hack-browser-data/internal/decrypter" "hack-browser-data/internal/decrypter"
"hack-browser-data/internal/item" "hack-browser-data/internal/item"
"hack-browser-data/internal/log" "hack-browser-data/internal/log"
"hack-browser-data/internal/utils" "hack-browser-data/internal/utils/typeutil"
) )
type ChromiumPassword []loginData type ChromiumPassword []loginData
@ -59,9 +59,9 @@ func (c *ChromiumPassword) Parse(masterKey []byte) error {
} }
} }
if create > time.Now().Unix() { if create > time.Now().Unix() {
login.CreateDate = utils.TimeEpochFormat(create) login.CreateDate = typeutil.TimeEpoch(create)
} else { } else {
login.CreateDate = utils.TimeStampFormat(create) login.CreateDate = typeutil.TimeStamp(create)
} }
login.Password = string(password) login.Password = string(password)
*c = append(*c, login) *c = append(*c, login)
@ -119,9 +119,9 @@ func (c *YandexPassword) Parse(masterKey []byte) error {
} }
} }
if create > time.Now().Unix() { if create > time.Now().Unix() {
login.CreateDate = utils.TimeEpochFormat(create) login.CreateDate = typeutil.TimeEpoch(create)
} else { } else {
login.CreateDate = utils.TimeStampFormat(create) login.CreateDate = typeutil.TimeStamp(create)
} }
login.Password = string(password) login.Password = string(password)
*c = append(*c, login) *c = append(*c, login)
@ -252,7 +252,7 @@ func getFirefoxLoginData(loginJson string) (l []loginData, err error) {
} }
m.encryptUser = user m.encryptUser = user
m.encryptPass = pass m.encryptPass = pass
m.CreateDate = utils.TimeStampFormat(v.Get("timeCreated").Int() / 1000) m.CreateDate = typeutil.TimeStamp(v.Get("timeCreated").Int() / 1000)
l = append(l, m) l = append(l, m)
} }
} }

@ -43,7 +43,7 @@ func pickChromium(name, profile string) []Browser {
if name == "all" { if name == "all" {
for _, v := range chromiumList { for _, v := range chromiumList {
if b, err := chromium.New(v.name, v.storage, v.profilePath, v.items); err == nil { if b, err := chromium.New(v.name, v.storage, v.profilePath, v.items); err == nil {
log.Infof("find browser %s success", b.Name()) log.Noticef("find browser %s success", b.Name())
browsers = append(browsers, b) browsers = append(browsers, b)
} else { } else {
// TODO: show which browser find failed // TODO: show which browser find failed
@ -85,12 +85,12 @@ func pickFirefox(name, profile string) []Browser {
} }
if multiFirefox, err := firefox.New(v.name, v.storage, profile, v.items); err == nil { if multiFirefox, err := firefox.New(v.name, v.storage, profile, v.items); err == nil {
for _, b := range multiFirefox { for _, b := range multiFirefox {
log.Infof("find browser: %s success", b.Name()) log.Noticef("find browser: firefox %s success", b.Name())
browsers = append(browsers, b) browsers = append(browsers, b)
} }
} else { } else {
if strings.Contains(err.Error(), "profile path is not exist") { if strings.Contains(err.Error(), "profile path is not exist") {
log.Infof("find browser: %s failed, profile path is not exist", v.name) log.Noticef("find browser: firefox %s failed, profile path is not exist", v.name)
} else { } else {
log.Error(err) log.Error(err)
} }

@ -74,7 +74,7 @@ var (
firefoxProfilePath = homeDir + "/.mozilla/firefox/" firefoxProfilePath = homeDir + "/.mozilla/firefox/"
chromeProfilePath = homeDir + "/.config/google-chrome/Default/" chromeProfilePath = homeDir + "/.config/google-chrome/Default/"
chromiumProfilePath = homeDir + "/.config/chromium/Default/" chromiumProfilePath = homeDir + "/.config/chromium/Default/"
edgeProfilePath = homeDir + "/.config/microsoft-edge*/Default/" edgeProfilePath = homeDir + "/.config/microsoft-edge/Default/"
braveProfilePath = homeDir + "/.config/BraveSoftware/Brave-Browser/Default/" braveProfilePath = homeDir + "/.config/BraveSoftware/Brave-Browser/Default/"
chromeBetaProfilePath = homeDir + "/.config/google-chrome-beta/Default/" chromeBetaProfilePath = homeDir + "/.config/google-chrome-beta/Default/"
operaProfilePath = homeDir + "/.config/opera/Default/" operaProfilePath = homeDir + "/.config/opera/Default/"

@ -1 +0,0 @@
package item

@ -17,7 +17,7 @@ func Init(l string) {
} }
} }
const template = "[{{datetime}}] [{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n" const template = "hack-browser-data [{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n"
// NewStdLogger instance // NewStdLogger instance
func newStdLogger(level slog.Level) *slog.SugaredLogger { func newStdLogger(level slog.Level) *slog.SugaredLogger {

@ -45,11 +45,11 @@ func ReadFile(filename string) (string, error) {
// CopyItemToLocal copies the file from the provided path to the local path // CopyItemToLocal copies the file from the provided path to the local path
func CopyItemToLocal(itemPaths map[item.Item]string) error { func CopyItemToLocal(itemPaths map[item.Item]string) error {
for i, path := range itemPaths { for i, p := range itemPaths {
// var dstFilename = item.TempName() // var dstFilename = item.TempName()
var filename = i.String() var filename = i.String()
// TODO: Handle read file error // TODO: Handle read file error
d, err := ioutil.ReadFile(path) d, err := ioutil.ReadFile(p)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -113,7 +113,3 @@ func CompressDir(dir string) error {
log.Debugf("Compress success, zip filename is %s", filename) log.Debugf("Compress success, zip filename is %s", filename)
return nil return nil
} }
// func CleanProfilePath(p string) string {
//
// }

@ -1,5 +1,11 @@
package typeutil package typeutil
import (
"time"
"golang.org/x/exp/constraints"
)
// Keys returns a slice of the keys of the map. based with go 1.18 generics // Keys returns a slice of the keys of the map. based with go 1.18 generics
func Keys[K comparable, V any](m map[K]V) []K { func Keys[K comparable, V any](m map[K]V) []K {
r := make([]K, 0, len(m)) r := make([]K, 0, len(m))
@ -8,3 +14,32 @@ func Keys[K comparable, V any](m map[K]V) []K {
} }
return r return r
} }
func IntToBool[T constraints.Signed](a T) bool {
switch a {
case 0, -1:
return false
}
return true
}
func TimeStamp(stamp int64) time.Time {
s := time.Unix(stamp, 0)
if s.Local().Year() > 9999 {
return time.Date(9999, 12, 13, 23, 59, 59, 0, time.Local)
}
return s
}
func TimeEpoch(epoch int64) time.Time {
maxTime := int64(99633311740000000)
if epoch > maxTime {
return time.Date(2049, 1, 1, 1, 1, 1, 1, time.Local)
}
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.Local)
d := time.Duration(epoch)
for i := 0; i < 1000; i++ {
t = t.Add(d)
}
return t
}

@ -1,69 +0,0 @@
package utils
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
func IntToBool(a int) bool {
switch a {
case 0, -1:
return false
}
return true
}
func BookmarkType(a int64) string {
switch a {
case 1:
return "url"
default:
return "folder"
}
}
func TimeStampFormat(stamp int64) time.Time {
s1 := time.Unix(stamp, 0)
if s1.Local().Year() > 9999 {
return time.Date(9999, 12, 13, 23, 59, 59, 0, time.Local)
}
return s1
}
func TimeEpochFormat(epoch int64) time.Time {
maxTime := int64(99633311740000000)
if epoch > maxTime {
return time.Date(2049, 1, 1, 1, 1, 1, 1, time.Local)
}
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC)
d := time.Duration(epoch)
for i := 0; i < 1000; i++ {
t = t.Add(d)
}
return t
}
func WriteFile(filename string, data []byte) error {
err := ioutil.WriteFile(filename, data, 0644)
if err != nil {
return nil
}
return err
}
func FormatFilename(dir, browser, filename, format string) string {
r := strings.Replace(strings.TrimSpace(strings.ToLower(browser)), " ", "_", -1)
p := path.Join(dir, fmt.Sprintf("%s_%s.%s", r, filename, format))
return p
}
func MakeDir(dirName string) error {
if _, err := os.Stat(dirName); os.IsNotExist(err) {
return os.Mkdir(dirName, 0700)
}
return nil
}
Loading…
Cancel
Save