Added caplets windows compatibility

This commit is contained in:
realgam3 2019-09-06 17:25:54 +03:00
commit c46bb905b9
3 changed files with 25 additions and 5 deletions

View file

@ -2,7 +2,9 @@ package caplets
import ( import (
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"runtime"
"github.com/evilsocket/islazy/str" "github.com/evilsocket/islazy/str"
) )
@ -11,22 +13,36 @@ const (
EnvVarName = "CAPSPATH" EnvVarName = "CAPSPATH"
Suffix = ".cap" Suffix = ".cap"
InstallArchive = "https://github.com/bettercap/caplets/archive/master.zip" 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 ( var (
InstallBase = getInstallBase()
InstallPathArchive = filepath.Join(InstallBase, "caplets-master") InstallPathArchive = filepath.Join(InstallBase, "caplets-master")
InstallPath = filepath.Join(InstallBase, "caplets") InstallPath = filepath.Join(InstallBase, "caplets")
ArchivePath = filepath.Join(os.TempDir(), "caplets.zip")
LoadPaths = []string{ LoadPaths = []string{
"./", "./",
"./caplets/", "./caplets/",
InstallPath, InstallPath,
filepath.Join(getUserHomeDir(), "caplets"),
} }
) )
func init() { 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 { if path = str.Trim(path); len(path) > 0 {
LoadPaths = append(LoadPaths, path) LoadPaths = append(LoadPaths, path)
} }

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -26,6 +27,9 @@ func List() []*Caplet {
for _, fileName := range append(files, files2...) { for _, fileName := range append(files, files2...) {
if _, err := os.Stat(fileName); err == nil { if _, err := os.Stat(fileName); err == nil {
base := strings.Replace(fileName, searchPath+"/", "", -1) base := strings.Replace(fileName, searchPath+"/", "", -1)
if runtime.GOOS == "windows" {
base = strings.Replace(fileName, searchPath+"\\", "", -1)
}
base = strings.Replace(base, Suffix, "", -1) base = strings.Replace(base, Suffix, "", -1)
if err, caplet := Load(base); err != nil { if err, caplet := Load(base); err != nil {
@ -58,7 +62,7 @@ func Load(name string) (error, *Caplet) {
name += Suffix name += Suffix
} }
if name[0] != '/' { if !filepath.IsAbs(name) {
for _, path := range LoadPaths { for _, path := range LoadPaths {
names = append(names, filepath.Join(path, name)) names = append(names, filepath.Join(path, name))
} }

View file

@ -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 { if err != nil {
return err return err
} }
@ -140,7 +140,7 @@ func (mod *CapletsModule) Update() error {
mod.Info("installing caplets to %s ...", caplets.InstallPath) 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 return err
} }