HackBrowserData/browser/browser.go
ᴍᴏᴏɴD4ʀᴋ e48f35cfd3
feat: refactor logger to standard library (#280)
* refactor: Refactor logging to use structured slog package.

- Remove `gookit` dependencies from `go.sum`
- Improve error logging in multiple packages by replacing `log` with `log/slog`
- Update dependencies in `go.mod`
- Add new `logger` package with test cases
- Refactor logging statements in multiple packages to use `slog` instead of `log`
- Change logging format and level in multiple packages for better structured logging

* refactor: Refactor logger package and add handler interface

- Refactor logger package
- Rename `defaultHandler` to `DefaultLogger`
- Move `ReplaceAttr` function to `Logger` struct
- Implement `LogHandler` struct with `slog.Handler` interface
- Add new `Logger` methods for configuration
- Add `SetMaxLevel`, `SetJSONHandler`, `SetTextHandler`, `SetOutput`, `SetVerbose`, `SetReplaceAttrFunc`
- Add verbose flag to `cmd/hack-browser-data/main.go` to increase logging

* refactor: Refactor logger package to use simplified handler initialization.

- Refactor logger package to use Default instead of DefaultLogger
- Update `NewHandler` method to correctly reference `Default` logger and simplify handler initialization
- Update tests for logger to reflect changes in Default usage
- Rename `DefaultLogger` to `Default` and update comments to better reflect its purpose
- Update function calls in hack-browser-data main.go to reflect logger package updates

* refactor: Refactor logging in Chromium implementation

Refactor logging and simplify decryption in chromium files
- Replace logger package import with shared slog package
- Change logging messages to use slog instead of logger
- Simplify decryption process by removing first 5 characters of encrypted key
- Refactor error logging in linux file to use shared slog package
- Replace string concatenation with formatted string in linux error message
2024-01-16 14:06:13 +08:00

123 lines
3.1 KiB
Go

package browser
import (
"log/slog"
"path/filepath"
"sort"
"strings"
"github.com/moond4rk/hackbrowserdata/browser/chromium"
"github.com/moond4rk/hackbrowserdata/browser/firefox"
"github.com/moond4rk/hackbrowserdata/browsingdata"
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
"github.com/moond4rk/hackbrowserdata/utils/typeutil"
)
type Browser interface {
// Name is browser's name
Name() string
// BrowsingData returns all browsing data in the browser.
BrowsingData(isFullExport bool) (*browsingdata.Data, error)
}
// PickBrowsers returns a list of browsers that match the name and profile.
func PickBrowsers(name, profile string) ([]Browser, error) {
var browsers []Browser
clist := pickChromium(name, profile)
for _, b := range clist {
if b != nil {
browsers = append(browsers, b)
}
}
flist := pickFirefox(name, profile)
for _, b := range flist {
if b != nil {
browsers = append(browsers, b)
}
}
return browsers, nil
}
func pickChromium(name, profile string) []Browser {
var browsers []Browser
name = strings.ToLower(name)
if name == "all" {
for _, v := range chromiumList {
if !fileutil.IsDirExists(filepath.Clean(v.profilePath)) {
slog.Warn("find browser failed, profile folder does not exist", "browser", v.name)
continue
}
multiChromium, err := chromium.New(v.name, v.storage, v.profilePath, v.items)
if err != nil {
slog.Error("new chromium error", "err", err)
continue
}
for _, b := range multiChromium {
slog.Warn("find browser success", "browser", b.Name())
browsers = append(browsers, b)
}
}
}
if c, ok := chromiumList[name]; ok {
if profile == "" {
profile = c.profilePath
}
if !fileutil.IsDirExists(filepath.Clean(profile)) {
slog.Error("find browser failed, profile folder does not exist", "browser", c.name)
}
chromiumList, err := chromium.New(c.name, c.storage, profile, c.items)
if err != nil {
slog.Error("new chromium error", "err", err)
}
for _, b := range chromiumList {
slog.Warn("find browser success", "browser", b.Name())
browsers = append(browsers, b)
}
}
return browsers
}
func pickFirefox(name, profile string) []Browser {
var browsers []Browser
name = strings.ToLower(name)
if name == "all" || name == "firefox" {
for _, v := range firefoxList {
if profile == "" {
profile = v.profilePath
} else {
profile = fileutil.ParentDir(profile)
}
if !fileutil.IsDirExists(filepath.Clean(profile)) {
slog.Warn("find browser failed, profile folder does not exist", "browser", v.name)
continue
}
if multiFirefox, err := firefox.New(profile, v.items); err == nil {
for _, b := range multiFirefox {
slog.Warn("find browser success", "browser", b.Name())
browsers = append(browsers, b)
}
} else {
slog.Error("new firefox error", "err", err)
}
}
return browsers
}
return nil
}
func ListBrowsers() []string {
var l []string
l = append(l, typeutil.Keys(chromiumList)...)
l = append(l, typeutil.Keys(firefoxList)...)
sort.Strings(l)
return l
}
func Names() string {
return strings.Join(ListBrowsers(), "|")
}