From d63122bab378a2b2a89b15a60c68bb65eccdba02 Mon Sep 17 00:00:00 2001 From: Simone Margaritelli Date: Tue, 23 Mar 2021 19:05:58 +0100 Subject: [PATCH] new: new -caplets-path argument to specify an alternative caplets base path (closes #850) --- caplets/env.go | 25 +++++++++++++++++++------ core/options.go | 2 ++ session/session.go | 6 ++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/caplets/env.go b/caplets/env.go index d62a516f..9ba87067 100644 --- a/caplets/env.go +++ b/caplets/env.go @@ -15,7 +15,7 @@ const ( InstallArchive = "https://github.com/bettercap/caplets/archive/master.zip" ) -func getInstallBase() string { +func getDefaultInstallBase() string { if runtime.GOOS == "windows" { return filepath.Join(os.Getenv("ALLUSERSPROFILE"), "bettercap") } @@ -28,10 +28,18 @@ func getUserHomeDir() string { } var ( - InstallBase = getInstallBase() + InstallBase = "" + InstallPathArchive = "" + InstallPath = "" + ArchivePath = "" + LoadPaths = []string(nil) +) + +func Setup(base string) error { + InstallBase = base InstallPathArchive = filepath.Join(InstallBase, "caplets-master") - InstallPath = filepath.Join(InstallBase, "caplets") - ArchivePath = filepath.Join(os.TempDir(), "caplets.zip") + InstallPath = filepath.Join(InstallBase, "caplets") + ArchivePath = filepath.Join(os.TempDir(), "caplets.zip") LoadPaths = []string{ "./", @@ -39,9 +47,7 @@ var ( InstallPath, filepath.Join(getUserHomeDir(), "caplets"), } -) -func init() { for _, path := range str.SplitBy(str.Trim(os.Getenv(EnvVarName)), string(os.PathListSeparator)) { if path = str.Trim(path); len(path) > 0 { LoadPaths = append(LoadPaths, path) @@ -51,4 +57,11 @@ func init() { for i, path := range LoadPaths { LoadPaths[i], _ = filepath.Abs(path) } + + return nil +} + +func init() { + // init with defaults + Setup(getDefaultInstallBase()) } diff --git a/core/options.go b/core/options.go index 62d4e33d..30b0af1e 100644 --- a/core/options.go +++ b/core/options.go @@ -16,6 +16,7 @@ type Options struct { Commands *string CpuProfile *string MemProfile *string + CapletsPath *string } func ParseOptions() (Options, error) { @@ -33,6 +34,7 @@ func ParseOptions() (Options, error) { Commands: flag.String("eval", "", "Run one or more commands separated by ; in the interactive session, used to set variables via command line."), CpuProfile: flag.String("cpu-profile", "", "Write cpu profile `file`."), MemProfile: flag.String("mem-profile", "", "Write memory profile to `file`."), + CapletsPath: flag.String("caplets-path", "", "Specify an alternative base path for caplets."), } flag.Parse() diff --git a/session/session.go b/session/session.go index dcd85160..9943e671 100644 --- a/session/session.go +++ b/session/session.go @@ -224,6 +224,12 @@ func (s *Session) Start() error { return s.Modules[i].Name() < s.Modules[j].Name() }) + if *s.Options.CapletsPath != "" { + if err = caplets.Setup(*s.Options.CapletsPath); err != nil { + return err + } + } + if s.Interface, err = network.FindInterface(*s.Options.InterfaceName); err != nil { return err }