Skip to content

Commit

Permalink
fix and make more readable implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
teocci committed Aug 15, 2021
1 parent 0ed9b3c commit 8380fd6
Show file tree
Hide file tree
Showing 27 changed files with 1,631 additions and 1,315 deletions.
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,56 @@ Temporary Items
# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml

core/browser/Cookies
8 changes: 6 additions & 2 deletions .idea/go-chrome-cookies.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed core/browser/Cookies
Empty file.
126 changes: 11 additions & 115 deletions core/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,16 @@
package browser

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/teocci/go-chrome-cookies/core/data"
"github.com/teocci/go-chrome-cookies/core/throw"
"github.com/teocci/go-chrome-cookies/logger"
)

const (
chromeName = "Chrome"
chromeBetaName = "Chrome Beta"
chromiumName = "Chromium"
edgeName = "Microsoft Edge"
firefoxName = "Firefox"
firefoxBetaName = "Firefox Beta"
firefoxDevName = "Firefox Dev"
firefoxNightlyName = "Firefox Nightly"
firefoxESRName = "Firefox ESR"
speed360Name = "360speed"
qqBrowserName = "qq"
braveName = "Brave"
operaName = "Opera"
operaGXName = "OperaGX"
vivaldiName = "Vivaldi"
)

type Browser interface {
// InitSecretKey is init chrome secret key, firefox's key always empty
InitSecretKey() error
Expand All @@ -47,82 +29,10 @@ type Browser interface {

// GetItem return single one from the password|bookmark|cookie|history
GetItem(itemName string) (data.Item, error)
}

const (
cookie = "cookie"
history = "history"
bookmark = "bookmark"
download = "download"
password = "password"
creditCard = "credit-card"
)

var (
errItemNotSupported = errors.New(`item not supported, default is "all", choose from history|download|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 (
chromiumItems = map[string]struct {
mainFile string
newItem func(mainFile, subFile string) data.Item
}{
bookmark: {
mainFile: data.ChromeBookmarkFile,
newItem: data.NewBookmarks,
},
cookie: {
mainFile: data.ChromeCookieFile,
newItem: data.NewCookies,
},
history: {
mainFile: data.ChromeHistoryFile,
newItem: data.NewHistoryData,
},
download: {
mainFile: data.ChromeDownloadFile,
newItem: data.NewDownloads,
},
password: {
mainFile: data.ChromePasswordFile,
newItem: data.NewCPasswords,
},
creditCard: {
mainFile: data.ChromeCreditFile,
newItem: data.NewCCards,
},
}
firefoxItems = map[string]struct {
mainFile string
subFile string
newItem func(mainFile, subFile string) data.Item
}{
bookmark: {
mainFile: data.FirefoxDataFile,
newItem: data.NewBookmarks,
},
cookie: {
mainFile: data.FirefoxCookieFile,
newItem: data.NewCookies,
},
history: {
mainFile: data.FirefoxDataFile,
newItem: data.NewHistoryData,
},
download: {
mainFile: data.FirefoxDataFile,
newItem: data.NewDownloads,
},
password: {
mainFile: data.FirefoxKey4File,
subFile: data.FirefoxLoginFile,
newItem: data.NewFPasswords,
},
}
)
// ListItems return list of items
ListItems() []string
}

// PickBrowser return a list of browser interface
func PickBrowser(name string) ([]Browser, error) {
Expand All @@ -142,11 +52,11 @@ func PickBrowser(name string) ([]Browser, error) {
browsers = append(browsers, b)
return browsers, err
}
return nil, errBrowserNotSupported
return nil, throw.ErrorBrowserNotSupported()
}

// 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)
// 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 (
Expand Down Expand Up @@ -180,7 +90,9 @@ func PickCustomBrowser(browserName, cusProfile, cusKey string) ([]Browser, error
}
}

func getItemPath(profilePath, file string) (string, error) {
// GetItemPath try to get item file path with the browser's profile path
// default key file path is in the parent directory of the profile dir, and name is [Local State]
func GetItemPath(profilePath, file string) (string, error) {
p, err := filepath.Glob(filepath.Join(profilePath, file))
if err != nil {
return "", err
Expand All @@ -202,7 +114,7 @@ func getKeyPath(profilePath string) (string, error) {
return keyPath, nil
}

// check key file path is exist
// checkKeyPath check if key file path 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)
Expand All @@ -224,20 +136,4 @@ func getParentDirectory(dir string) string {
return string([]rune(dir)[:length])
}
return ""
}

func ListBrowser() []string {
var l []string
for k := range browserList {
l = append(l, k)
}
return l
}

func ListItem() []string {
var l []string
for k := range chromiumItems {
l = append(l, k)
}
return l
}
66 changes: 56 additions & 10 deletions core/browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package browser

import (
"database/sql"
"fmt"
"github.com/teocci/go-chrome-cookies/core/data"
"github.com/teocci/go-chrome-cookies/logger"
"reflect"
"testing"
)
Expand All @@ -16,36 +19,47 @@ func TestListBrowser(t *testing.T) {
}
}

func TestListItem(t *testing.T) {
items := ListItem()
for index, item := range items {
fmt.Printf("items[%d] : %s\n", index, reflect.ValueOf(item).String())
}
}

func TestPickBrowser(t *testing.T) {
name := "chrome"
browsers, err := PickBrowser(name)
if err != nil {
fmt.Printf("err: %s\n", err)
}
chrome := browsers[0]
var chrome Browser
chrome = browsers[0]
err = chrome.InitSecretKey()
if err != nil {
fmt.Printf("err: %s\n", err)
}

fmt.Printf("browser: %s\n", chrome.GetName())

item, err := chrome.GetItem("cookie")
if err != nil {
fmt.Printf("err: %s\n", err)
}

fmt.Printf("item: %s\n", reflect.ValueOf(item).String())
key := chrome.GetSecretKey()
str := string(key)
fmt.Println("key:", str)

fmt.Printf("key: %s\n", reflect.ValueOf(key).String())
err = item.CopyDB()
if err != nil {
fmt.Printf("err: %s\n", err)
}

err = item.ChromeParse(key)
if err != nil {
fmt.Printf("err: %s\n", err)
}

err = item.OutPut(data.GetFormat(data.FormatNameConsole), "D:/Temp/", "chrome")
if err != nil {
return
}

//fmt.Printf("item: %s\n", item)
}

Expand All @@ -57,7 +71,7 @@ func TestNew(t *testing.T) {
// {"", fmt.Errorf("")},
// {"foo", fmt.Errorf("foo")},
// {"foo", New("foo")},
// {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
// {"string with format specifiers: %v", throw.New("string with format specifiers: %v")},
//}
//
//for _, tt := range tests {
Expand All @@ -66,4 +80,36 @@ func TestNew(t *testing.T) {
// t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
// }
//}
}
}

func TestDB(t *testing.T) {
cookieDB, err := sql.Open("sqlite3", data.ChromeCookieFile)
if err != nil {
fmt.Printf("err: %s\n", err)
}
defer func() {
if err := cookieDB.Close(); err != nil {
logger.Debug(err)
}
}()

rows, err := cookieDB.Query("SELECT name FROM sqlite_master WHERE type='table';")
if err != nil {
fmt.Printf("err: %s\n", err)
}
defer func() {
if err := rows.Close(); err != nil {
logger.Debug(err)
}
}()

fmt.Println("rows:" )
for rows.Next() {
var name string
err := rows.Scan(&name)
if err != nil {
fmt.Printf("err: %s\n", err)
}
fmt.Println("name:", name )
}
}
Loading

0 comments on commit 8380fd6

Please sign in to comment.