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 (
"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)
}

View file

@ -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))
}

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 {
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
}