feat: update to v0.4.0, based by generics

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-18 02:11:33 +08:00
parent 6217ca3bed
commit aa3326f1a3
16 changed files with 252 additions and 135 deletions
+14 -18
View File
@@ -9,14 +9,14 @@ import (
"hack-browser-data/internal/browser/firefox"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
)
type Browser interface {
// Name is browser's name
Name() string
GetMasterKey() ([]byte, error)
// GetBrowsingData returns the browsing data for the browser.
GetBrowsingData() (*browingdata.Data, error)
// BrowsingData returns all browsing data in the browser.
BrowsingData() (*browingdata.Data, error)
}
func PickBrowser(name, profile string) ([]Browser, error) {
@@ -47,11 +47,11 @@ func pickChromium(name, profile string) []Browser {
browsers = append(browsers, b)
} else {
// TODO: show which browser find failed
if strings.Contains(err.Error(), "profile path is not exist") {
log.Infof("find browser %s failed, profile path is not exist", v.name)
if strings.Contains(err.Error(), "profile folder is not exist") {
log.Errorf("find browser %s failed, profile folder is not exist, maybe not installed", v.name)
continue
} else {
log.Error("new chromium error:", err)
log.Errorf("new chromium error:", err)
}
}
}
@@ -62,10 +62,10 @@ func pickChromium(name, profile string) []Browser {
}
b, err := chromium.New(c.name, c.storage, profile, c.items)
if err != nil {
if strings.Contains(err.Error(), "profile path is not exist") {
log.Infof("find browser %s failed, profile path is not exist", c.name)
if strings.Contains(err.Error(), "profile folder is not exist") {
log.Fatalf("find browser %s failed, profile folder is not exist, maybe not installed", c.name)
} else {
log.Error("new chromium error:", err)
log.Fatalf("new chromium error:", err)
}
}
browsers = append(browsers, b)
@@ -89,8 +89,8 @@ func pickFirefox(name, profile string) []Browser {
browsers = append(browsers, b)
}
} else {
if strings.Contains(err.Error(), "profile path is not exist") {
log.Noticef("find browser firefox %s failed, profile path is not exist", v.name)
if strings.Contains(err.Error(), "profile folder is not exist") {
log.Errorf("find browser firefox %s failed, profile folder is not exist", v.name)
} else {
log.Error(err)
}
@@ -104,12 +104,8 @@ func pickFirefox(name, profile string) []Browser {
func ListBrowser() []string {
var l []string
for c := range chromiumList {
l = append(l, c)
}
for f := range firefoxList {
l = append(l, f)
}
l = append(l, typeutil.Keys(chromiumList)...)
l = append(l, typeutil.Keys(firefoxList)...)
return l
}
+3 -3
View File
@@ -30,7 +30,7 @@ func New(name, storage, profilePath string, items []item.Item) (*chromium, error
}
// TODO: Handle file path is not exist
if !fileutil.FolderExists(profilePath) {
return nil, fmt.Errorf("%s profile path is not exist: %s", name, profilePath)
return nil, fmt.Errorf("%s profile folder is not exist: %s", name, profilePath)
}
itemsPaths, err := c.getItemPath(profilePath, items)
if err != nil {
@@ -46,7 +46,7 @@ func (c *chromium) Name() string {
return c.name
}
func (c *chromium) GetBrowsingData() (*browingdata.Data, error) {
func (c *chromium) BrowsingData() (*browingdata.Data, error) {
b := browingdata.New(c.items)
if err := c.copyItemToLocal(); err != nil {
@@ -72,7 +72,7 @@ func (c *chromium) copyItemToLocal() error {
// TODO: Handle read file error
d, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err.Error())
return err
}
err = ioutil.WriteFile(filename, d, 0777)
if err != nil {
+2 -2
View File
@@ -2,6 +2,7 @@ package chromium
import (
"crypto/sha1"
"errors"
"os"
"github.com/godbus/dbus/v5"
@@ -49,8 +50,7 @@ func (c *chromium) GetMasterKey() ([]byte, error) {
if label == c.storage {
se, err := i.GetSecret(session.Path())
if err != nil {
log.Error(err)
return nil, err
return nil, errors.New("get storage from dbus error:" + err.Error())
}
chromiumSecret = se.Value
}
+5 -5
View File
@@ -26,7 +26,7 @@ type firefox struct {
// New returns a new firefox instance.
func New(name, storage, profilePath string, items []item.Item) ([]*firefox, error) {
if !fileutil.FolderExists(profilePath) {
return nil, fmt.Errorf("%s profile path is not exist: %s", name, profilePath)
return nil, fmt.Errorf("%s profile folder is not exist: %s", name, profilePath)
}
f := &firefox{
name: name,
@@ -36,7 +36,7 @@ func New(name, storage, profilePath string, items []item.Item) ([]*firefox, erro
}
multiItemPaths, err := f.getMultiItemPath(f.profilePath, f.items)
if err != nil {
if strings.Contains(err.Error(), "profile path is not exist") {
if strings.Contains(err.Error(), "profile folder is not exist") {
log.Error(err)
return nil, nil
}
@@ -45,7 +45,7 @@ func New(name, storage, profilePath string, items []item.Item) ([]*firefox, erro
var firefoxList []*firefox
for name, itemPaths := range multiItemPaths {
firefoxList = append(firefoxList, &firefox{
name: name,
name: fmt.Sprintf("firefox-%s", name),
items: typeutil.Keys(itemPaths),
itemPaths: itemPaths,
})
@@ -66,7 +66,7 @@ func (f *firefox) copyItemToLocal() error {
// TODO: Handle read file error
d, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err.Error())
return err
}
err = ioutil.WriteFile(filename, d, 0777)
if err != nil {
@@ -100,7 +100,7 @@ func (f *firefox) Name() string {
return f.name
}
func (f *firefox) GetBrowsingData() (*browingdata.Data, error) {
func (f *firefox) BrowsingData() (*browingdata.Data, error) {
b := browingdata.New(f.items)
if err := f.copyItemToLocal(); err != nil {