@ -3,6 +3,7 @@ package core
import (
import (
"errors"
"errors"
"fmt"
"fmt"
"os"
"path/filepath"
"path/filepath"
"strings"
"strings"
@ -49,10 +50,10 @@ const (
)
)
var (
var (
errItemNotSupported = errors . New ( ` item not supported, default is "all", choose from history|password|bookmark|cookie ` )
errItemNotSupported = errors . New ( ` item not supported, default is "all", choose from history|password|bookmark|cookie ` )
errBrowserNotSupported = errors . New ( "browser not supported" )
errBrowserNotSupported = errors . New ( "browser not supported" )
errChromeSecretIsEmpty = errors . New ( "chrome secret is empty" )
errChromeSecretIsEmpty = errors . New ( "chrome secret is empty" )
errDbusSecretIsEmpty = errors . New ( "dbus secret key is empty" )
errDbusSecretIsEmpty = errors . New ( "dbus secret key is empty" )
)
)
var (
var (
@ -170,7 +171,7 @@ func NewFirefox(profile, key, name, storage string) (Browser, error) {
return & Firefox { profilePath : profile , keyPath : key , name : name } , nil
return & Firefox { profilePath : profile , keyPath : key , name : name } , nil
}
}
//
// GetAllItems return all item with firefox
func ( f * Firefox ) GetAllItems ( ) ( [ ] data . Item , error ) {
func ( f * Firefox ) GetAllItems ( ) ( [ ] data . Item , error ) {
var items [ ] data . Item
var items [ ] data . Item
for item , choice := range firefoxItems {
for item , choice := range firefoxItems {
@ -259,8 +260,43 @@ func PickBrowser(name string) ([]Browser, error) {
return nil , errBrowserNotSupported
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 ) {
func getItemPath ( profilePath , file string ) ( string , error ) {
p , err := filepath . Glob ( profilePath + file )
p , err := filepath . Glob ( filepath . Join ( profilePath , file ) )
if err != nil {
if err != nil {
return "" , err
return "" , err
}
}
@ -270,6 +306,41 @@ func getItemPath(profilePath, file string) (string, error) {
return "" , fmt . Errorf ( "find %s failed" , file )
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 {
func ListBrowser ( ) [ ] string {
var l [ ] string
var l [ ] string
for k := range browserList {
for k := range browserList {