refactor: replace util with go generics

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-17 16:35:40 +08:00
parent c9feef66ab
commit 0dc28a077f
14 changed files with 83 additions and 113 deletions
+13 -4
View File
@@ -10,8 +10,8 @@ import (
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
_ "github.com/mattn/go-sqlite3"
)
@@ -61,7 +61,7 @@ func getBookmarkChildren(value gjson.Result, w *ChromiumBookmark) (children gjso
ID: value.Get(bookmarkID).Int(),
Name: value.Get(bookmarkName).String(),
URL: value.Get(bookmarkUrl).String(),
DateAdded: utils.TimeEpochFormat(value.Get(bookmarkAdded).Int()),
DateAdded: typeutil.TimeEpoch(value.Get(bookmarkAdded).Int()),
}
children = value.Get(bookmarkChildren)
if nodeType.Exists() {
@@ -114,9 +114,9 @@ func (f *FirefoxBookmark) Parse(masterKey []byte) error {
*f = append(*f, bookmark{
ID: id,
Name: title,
Type: utils.BookmarkType(bType),
Type: bookmarkType(bType),
URL: url,
DateAdded: utils.TimeStampFormat(dateAdded / 1000000),
DateAdded: typeutil.TimeStamp(dateAdded / 1000000),
})
}
sort.Slice(*f, func(i, j int) bool {
@@ -128,3 +128,12 @@ func (f *FirefoxBookmark) Parse(masterKey []byte) error {
func (f *FirefoxBookmark) Name() string {
return "bookmark"
}
func bookmarkType(a int64) string {
switch a {
case 1:
return "url"
default:
return "folder"
}
}
+11 -11
View File
@@ -10,7 +10,7 @@ import (
"hack-browser-data/internal/decrypter"
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/typeutil"
)
type ChromiumCookie []cookie
@@ -43,12 +43,12 @@ func (c *ChromiumCookie) Parse(masterKey []byte) error {
Host: host,
Path: path,
encryptValue: encryptValue,
IsSecure: utils.IntToBool(isSecure),
IsHTTPOnly: utils.IntToBool(isHTTPOnly),
HasExpire: utils.IntToBool(hasExpire),
IsPersistent: utils.IntToBool(isPersistent),
CreateDate: utils.TimeEpochFormat(createDate),
ExpireDate: utils.TimeEpochFormat(expireDate),
IsSecure: typeutil.IntToBool(isSecure),
IsHTTPOnly: typeutil.IntToBool(isHTTPOnly),
HasExpire: typeutil.IntToBool(hasExpire),
IsPersistent: typeutil.IntToBool(isPersistent),
CreateDate: typeutil.TimeEpoch(createDate),
ExpireDate: typeutil.TimeEpoch(expireDate),
}
// TODO: replace DPAPI
if len(encryptValue) > 0 {
@@ -102,10 +102,10 @@ func (f *FirefoxCookie) Parse(masterKey []byte) error {
KeyName: name,
Host: host,
Path: path,
IsSecure: utils.IntToBool(isSecure),
IsHTTPOnly: utils.IntToBool(isHttpOnly),
CreateDate: utils.TimeStampFormat(creationTime / 1000000),
ExpireDate: utils.TimeStampFormat(expiry),
IsSecure: typeutil.IntToBool(isSecure),
IsHTTPOnly: typeutil.IntToBool(isHttpOnly),
CreateDate: typeutil.TimeStamp(creationTime / 1000000),
ExpireDate: typeutil.TimeStamp(expiry),
Value: value,
})
}
+5 -5
View File
@@ -8,7 +8,7 @@ import (
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/typeutil"
_ "github.com/mattn/go-sqlite3"
"github.com/tidwall/gjson"
@@ -40,8 +40,8 @@ func (c *ChromiumDownload) Parse(masterKey []byte) error {
TargetPath: targetPath,
Url: tabUrl,
TotalBytes: totalBytes,
StartTime: utils.TimeEpochFormat(startTime),
EndTime: utils.TimeEpochFormat(endTime),
StartTime: typeutil.TimeEpoch(startTime),
EndTime: typeutil.TimeEpoch(endTime),
MimeType: mimeType,
}
*c = append(*c, data)
@@ -98,8 +98,8 @@ func (f *FirefoxDownload) Parse(masterKey []byte) error {
TargetPath: path,
Url: url,
TotalBytes: fileSize.Int(),
StartTime: utils.TimeStampFormat(dateAdded / 1000000),
EndTime: utils.TimeStampFormat(endTime.Int() / 1000),
StartTime: typeutil.TimeStamp(dateAdded / 1000000),
EndTime: typeutil.TimeStamp(endTime.Int() / 1000),
})
}
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/typeutil"
)
type ChromiumHistory []history
@@ -40,7 +40,7 @@ func (c *ChromiumHistory) Parse(masterKey []byte) error {
Url: url,
Title: title,
VisitCount: visitCount,
LastVisitTime: utils.TimeEpochFormat(lastVisitTime),
LastVisitTime: typeutil.TimeEpoch(lastVisitTime),
}
*c = append(*c, data)
}
@@ -91,7 +91,7 @@ func (f *FirefoxHistory) Parse(masterKey []byte) error {
Title: title,
Url: url,
VisitCount: visitCount,
LastVisitTime: utils.TimeStampFormat(visitDate / 1000000),
LastVisitTime: typeutil.TimeStamp(visitDate / 1000000),
})
}
sort.Slice(*f, func(i, j int) bool {
+6 -6
View File
@@ -15,7 +15,7 @@ import (
"hack-browser-data/internal/decrypter"
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils"
"hack-browser-data/internal/utils/typeutil"
)
type ChromiumPassword []loginData
@@ -59,9 +59,9 @@ func (c *ChromiumPassword) Parse(masterKey []byte) error {
}
}
if create > time.Now().Unix() {
login.CreateDate = utils.TimeEpochFormat(create)
login.CreateDate = typeutil.TimeEpoch(create)
} else {
login.CreateDate = utils.TimeStampFormat(create)
login.CreateDate = typeutil.TimeStamp(create)
}
login.Password = string(password)
*c = append(*c, login)
@@ -119,9 +119,9 @@ func (c *YandexPassword) Parse(masterKey []byte) error {
}
}
if create > time.Now().Unix() {
login.CreateDate = utils.TimeEpochFormat(create)
login.CreateDate = typeutil.TimeEpoch(create)
} else {
login.CreateDate = utils.TimeStampFormat(create)
login.CreateDate = typeutil.TimeStamp(create)
}
login.Password = string(password)
*c = append(*c, login)
@@ -252,7 +252,7 @@ func getFirefoxLoginData(loginJson string) (l []loginData, err error) {
}
m.encryptUser = user
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)
}
}
+3 -3
View File
@@ -43,7 +43,7 @@ func pickChromium(name, profile string) []Browser {
if name == "all" {
for _, v := range chromiumList {
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)
} else {
// 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 {
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)
}
} else {
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 {
log.Error(err)
}
+1 -1
View File
@@ -74,7 +74,7 @@ var (
firefoxProfilePath = homeDir + "/.mozilla/firefox/"
chromeProfilePath = homeDir + "/.config/google-chrome/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/"
chromeBetaProfilePath = homeDir + "/.config/google-chrome-beta/Default/"
operaProfilePath = homeDir + "/.config/opera/Default/"
-1
View File
@@ -1 +0,0 @@
package item
+1 -1
View File
@@ -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
func newStdLogger(level slog.Level) *slog.SugaredLogger {
+2 -6
View File
@@ -45,11 +45,11 @@ func ReadFile(filename string) (string, error) {
// CopyItemToLocal copies the file from the provided path to the local path
func CopyItemToLocal(itemPaths map[item.Item]string) error {
for i, path := range itemPaths {
for i, p := range itemPaths {
// var dstFilename = item.TempName()
var filename = i.String()
// TODO: Handle read file error
d, err := ioutil.ReadFile(path)
d, err := ioutil.ReadFile(p)
if err != nil {
fmt.Println(err.Error())
}
@@ -113,7 +113,3 @@ func CompressDir(dir string) error {
log.Debugf("Compress success, zip filename is %s", filename)
return nil
}
// func CleanProfilePath(p string) string {
//
// }
+35
View File
@@ -1,5 +1,11 @@
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
func Keys[K comparable, V any](m map[K]V) []K {
r := make([]K, 0, len(m))
@@ -8,3 +14,32 @@ func Keys[K comparable, V any](m map[K]V) []K {
}
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
}
-69
View File
@@ -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
}