mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent. varnamelen and staticcheck ST1020-22 now keep both from coming back.
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/log"
|
|
)
|
|
|
|
func rootCmd() *cobra.Command {
|
|
var verbose bool
|
|
root := &cobra.Command{
|
|
Use: "hack-browser-data",
|
|
Short: "A CLI tool for decrypting and exporting browser data",
|
|
Long: `hack-browser-data decrypts and exports browser data from Chromium-based
|
|
browsers, Firefox, and Safari on Windows, macOS, and Linux.
|
|
|
|
GitHub: https://github.com/moonD4rk/HackBrowserData`,
|
|
PersistentPreRun: func(command *cobra.Command, args []string) {
|
|
if verbose {
|
|
log.SetVerbose()
|
|
}
|
|
},
|
|
}
|
|
|
|
root.CompletionOptions.HiddenDefaultCmd = true
|
|
|
|
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable debug logging")
|
|
|
|
dump := dumpCmd()
|
|
root.AddCommand(dump, dumpKeysCmd(), archiveCmd(), restoreCmd(), listCmd(), versionCmd())
|
|
|
|
// Without a subcommand the root runs dump, as v1.1.0 did, so it carries dump's flags too.
|
|
root.RunE = func(command *cobra.Command, args []string) error {
|
|
dump.SetContext(command.Context())
|
|
return dump.RunE(dump, args)
|
|
}
|
|
dump.Flags().VisitAll(func(flag *pflag.Flag) {
|
|
if root.Flags().Lookup(flag.Name) == nil {
|
|
root.Flags().AddFlag(flag)
|
|
}
|
|
})
|
|
|
|
for _, command := range root.Commands() {
|
|
quietWhenStopped(command)
|
|
}
|
|
quietWhenStopped(root)
|
|
return root
|
|
}
|
|
|
|
// quietWhenStopped keeps a stopped run's error and usage off the screen; main then ends the process by the signal.
|
|
func quietWhenStopped(command *cobra.Command) {
|
|
run := command.RunE
|
|
if run == nil {
|
|
return
|
|
}
|
|
command.RunE = func(invoked *cobra.Command, args []string) error {
|
|
err := run(invoked, args)
|
|
if err != nil && invoked.Context().Err() != nil {
|
|
invoked.SilenceErrors, invoked.SilenceUsage = true, true
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
// stopSignals end the process by default; each first cancels the run, so its temporary copies are removed.
|
|
var stopSignals = []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGHUP}
|
|
|
|
type stopError struct{ received os.Signal }
|
|
|
|
func (e stopError) Error() string { return "stopped by " + e.received.String() }
|
|
|
|
func main() {
|
|
configureDoubleClickMode()
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
signals := make(chan os.Signal, 1)
|
|
for _, received := range stopSignals {
|
|
if !signal.Ignored(received) { // nohup and background jobs keep ignoring what they ignore today
|
|
signal.Notify(signals, received)
|
|
}
|
|
}
|
|
go func() {
|
|
received := <-signals
|
|
signal.Stop(signals) // a second signal ends the process at once
|
|
if received == os.Interrupt {
|
|
log.Warnf("Interrupted; cleaning up (press Ctrl-C again to quit at once)")
|
|
} else {
|
|
log.Warnf("Stopping; cleaning up")
|
|
}
|
|
cancel(stopError{received})
|
|
}()
|
|
err := rootCmd().ExecuteContext(ctx)
|
|
var stop stopError
|
|
if errors.As(context.Cause(ctx), &stop) {
|
|
exitBy(stop.received)
|
|
}
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|