diff --git a/caplets/env.go b/caplets/env.go index 4c803b6e..24bf8545 100644 --- a/caplets/env.go +++ b/caplets/env.go @@ -2,7 +2,9 @@ package caplets import ( "os" + "os/user" "path/filepath" + "runtime" "github.com/evilsocket/islazy/str" ) @@ -11,22 +13,36 @@ const ( EnvVarName = "CAPSPATH" Suffix = ".cap" InstallArchive = "https://github.com/bettercap/caplets/archive/master.zip" - InstallBase = "/usr/local/share/bettercap/" ) +func getInstallBase() string { + if runtime.GOOS == "windows" { + return filepath.Join(os.Getenv("ALLUSERSPROFILE"), "bettercap") + } + return "/usr/local/share/bettercap/" +} + +func getUserHomeDir() string { + usr, _ := user.Current() + return usr.HomeDir +} + var ( + InstallBase = getInstallBase() InstallPathArchive = filepath.Join(InstallBase, "caplets-master") InstallPath = filepath.Join(InstallBase, "caplets") + ArchivePath = filepath.Join(os.TempDir(), "caplets.zip") LoadPaths = []string{ "./", "./caplets/", InstallPath, + filepath.Join(getUserHomeDir(), "caplets"), } ) func init() { - for _, path := range str.SplitBy(str.Trim(os.Getenv(EnvVarName)), ":") { + 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) } diff --git a/caplets/manager.go b/caplets/manager.go index cb004d0c..99c84df2 100644 --- a/caplets/manager.go +++ b/caplets/manager.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "sort" "strings" "sync" @@ -26,6 +27,9 @@ func List() []*Caplet { for _, fileName := range append(files, files2...) { if _, err := os.Stat(fileName); err == nil { base := strings.Replace(fileName, searchPath+"/", "", -1) + if runtime.GOOS == "windows" { + base = strings.Replace(fileName, searchPath+"\\", "", -1) + } base = strings.Replace(base, Suffix, "", -1) if err, caplet := Load(base); err != nil { @@ -58,7 +62,7 @@ func Load(name string) (error, *Caplet) { name += Suffix } - if name[0] != '/' { + if !filepath.IsAbs(name) { for _, path := range LoadPaths { names = append(names, filepath.Join(path, name)) } diff --git a/modules/caplets/caplets.go b/modules/caplets/caplets.go index 61b5589a..648df2ac 100644 --- a/modules/caplets/caplets.go +++ b/modules/caplets/caplets.go @@ -120,7 +120,7 @@ func (mod *CapletsModule) Update() error { } } - out, err := os.Create("/tmp/caplets.zip") + out, err := os.Create(caplets.ArchivePath) if err != nil { return err } @@ -140,7 +140,7 @@ func (mod *CapletsModule) Update() error { mod.Info("installing caplets to %s ...", caplets.InstallPath) - if _, err = zip.Unzip("/tmp/caplets.zip", caplets.InstallBase); err != nil { + if _, err = zip.Unzip(caplets.ArchivePath, caplets.InstallBase); err != nil { return err }