feat: support custom browser profile path

This commit is contained in:
moond4rk
2020-12-20 15:18:19 +08:00
committed by ᴍᴏᴏɴD4ʀᴋ
parent 138c33dade
commit 3fa262f66c
3 changed files with 111 additions and 21 deletions
+77 -6
View File
@@ -3,6 +3,7 @@ package core
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
@@ -49,10 +50,10 @@ const (
)
var (
errItemNotSupported = errors.New(`item not supported, default is "all", choose from history|password|bookmark|cookie`)
errBrowserNotSupported = errors.New("browser not supported")
errChromeSecretIsEmpty = errors.New("chrome secret is empty")
errDbusSecretIsEmpty = errors.New("dbus secret key is empty")
errItemNotSupported = errors.New(`item not supported, default is "all", choose from history|password|bookmark|cookie`)
errBrowserNotSupported = errors.New("browser not supported")
errChromeSecretIsEmpty = errors.New("chrome secret is empty")
errDbusSecretIsEmpty = errors.New("dbus secret key is empty")
)
var (
@@ -170,7 +171,7 @@ func NewFirefox(profile, key, name, storage string) (Browser, error) {
return &Firefox{profilePath: profile, keyPath: key, name: name}, nil
}
//
// GetAllItems return all item with firefox
func (f *Firefox) GetAllItems() ([]data.Item, error) {
var items []data.Item
for item, choice := range firefoxItems {
@@ -259,8 +260,43 @@ func PickBrowser(name string) ([]Browser, error) {
return nil, errBrowserNotSupported
}
// PickCustomBrowser pick single browser with custom browser profile path and key file path (windows only).
// If custom key file path is empty, but the current browser requires key file (chromium for windows version > 80)
// key file path will be automatically found in the profile path's parent directory.
func PickCustomBrowser(browserName, cusProfile, cusKey string) ([]Browser, error) {
var (
browsers []Browser
)
browserName = strings.ToLower(browserName)
supportBrowser := strings.Join(ListBrowser(), "|")
if browserName == "all" {
return nil, fmt.Errorf("can't select all browser, pick one from %s with -b flag\n", supportBrowser)
}
if choice, ok := browserList[browserName]; ok {
// if this browser need key path
if choice.KeyPath != "" {
var err error
// if browser need key path and cusKey is empty, try to get key path with profile dir
if cusKey == "" {
cusKey, err = getKeyPath(cusProfile)
if err != nil {
return nil, err
}
}
}
if err := checkKeyPath(cusKey); err != nil {
return nil, err
}
b, err := choice.New(cusProfile, cusKey, choice.Name, choice.Storage)
browsers = append(browsers, b)
return browsers, err
} else {
return nil, fmt.Errorf("%s not support, pick one from %s with -b flag\n", browserName, supportBrowser)
}
}
func getItemPath(profilePath, file string) (string, error) {
p, err := filepath.Glob(profilePath + file)
p, err := filepath.Glob(filepath.Join(profilePath, file))
if err != nil {
return "", err
}
@@ -270,6 +306,41 @@ func getItemPath(profilePath, file string) (string, error) {
return "", fmt.Errorf("find %s failed", file)
}
// getKeyPath try get key file path with browser's profile path
// default key file path is in the parent directory of the profile dir, and name is [Local State]
func getKeyPath(profilePath string) (string, error) {
if _, err := os.Stat(filepath.Clean(profilePath)); os.IsNotExist(err) {
return "", err
}
parentDir := getParentDirectory(profilePath)
keyPath := filepath.Join(parentDir, "Local State")
return keyPath, nil
}
// check key file path is exist
func checkKeyPath(keyPath string) error {
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
return fmt.Errorf("secret key path not exist, please check %s", keyPath)
}
return nil
}
func getParentDirectory(dir string) string {
var (
length int
)
// filepath.Clean(dir) auto remove
dir = strings.ReplaceAll(filepath.Clean(dir), `\`, `/`)
length = strings.LastIndex(dir, "/")
if length > 0 {
if length > len([]rune(dir)) {
length = len([]rune(dir))
}
return string([]rune(dir)[:length])
}
return ""
}
func ListBrowser() []string {
var l []string
for k := range browserList {
+4
View File
@@ -3,6 +3,7 @@ package core
import (
"encoding/base64"
"errors"
"fmt"
"os"
"hack-browser-data/core/decrypt"
@@ -109,6 +110,9 @@ func (c *Chromium) InitSecretKey() error {
if c.keyPath == "" {
return nil
}
if _, err := os.Stat(c.keyPath); os.IsNotExist(err) {
return fmt.Errorf("%s secret key path is empty", c.name)
}
keyFile, err := utils.ReadFile(c.keyPath)
if err != nil {
return err