diff --git a/caplets/env.go b/caplets/env.go index d62a516f..9504395a 100644 --- a/caplets/env.go +++ b/caplets/env.go @@ -28,6 +28,7 @@ func getUserHomeDir() string { } var ( + UserHomePath = getUserHomeDir() InstallBase = getInstallBase() InstallPathArchive = filepath.Join(InstallBase, "caplets-master") InstallPath = filepath.Join(InstallBase, "caplets") @@ -37,7 +38,7 @@ var ( "./", "./caplets/", InstallPath, - filepath.Join(getUserHomeDir(), "caplets"), + filepath.Join(UserHomePath, "caplets"), } ) diff --git a/modules/caplets/caplets.go b/modules/caplets/caplets.go index 648df2ac..0defca46 100644 --- a/modules/caplets/caplets.go +++ b/modules/caplets/caplets.go @@ -3,12 +3,15 @@ package caplets import ( "fmt" "io" + "io/ioutil" "net/http" "os" + "path/filepath" + "regexp" + "strings" "github.com/bettercap/bettercap/caplets" "github.com/bettercap/bettercap/session" - "github.com/dustin/go-humanize" "github.com/evilsocket/islazy/fs" @@ -43,6 +46,42 @@ func NewCapletsModule(s *session.Session) *CapletsModule { return mod.Update() })) + mod.AddHandler(session.NewModuleHandler("caplets.fixpaths", "", + "Fix the absolute caplets path.", + func(args []string) error { + re := regexp.MustCompile(`(~/|/usr/local/share/bettercap/)`) + return filepath.Walk(caplets.InstallPath, + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() || (filepath.Ext(path) != ".js" && filepath.Ext(path) != ".cap") { + return nil + } + + raw, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + res := re.ReplaceAllStringFunc(string(raw), func(value string) string { + switch value { + case "~/": + return strings.Replace(caplets.UserHomePath, `\`, `/`, -1) + "/" + case "/usr/local/share/bettercap/": + return strings.Replace(caplets.InstallBase, `\`, `/`, -1) + "/" + default: + return value + } + }) + err = ioutil.WriteFile(path, []byte(res), info.Mode()) + if err != nil { + return err + } + return nil + }) + })) + return mod } diff --git a/modules/ui/ui.go b/modules/ui/ui.go index f5d6950e..fde08d7f 100644 --- a/modules/ui/ui.go +++ b/modules/ui/ui.go @@ -10,6 +10,7 @@ import ( "path/filepath" "regexp" + "github.com/bettercap/bettercap/caplets" "github.com/bettercap/bettercap/session" "github.com/google/go-github/github" @@ -36,7 +37,7 @@ func NewUIModule(s *session.Session) *UIModule { } mod.AddParam(session.NewStringParameter("ui.basepath", - "/usr/local/share/bettercap/", + caplets.InstallBase, "", "UI base installation path.")) diff --git a/modules/utils/script_builtins.go b/modules/utils/script_builtins.go index b197e9a9..91177692 100644 --- a/modules/utils/script_builtins.go +++ b/modules/utils/script_builtins.go @@ -297,6 +297,9 @@ func init() { return errOtto("Could not create response object: %s", err) } + err = object.Set("status", resp.StatusCode) + err = object.Set("statusText", resp.Status) + err = object.Set("ok", resp.StatusCode >= 200 && resp.StatusCode <= 299) err = object.Set("body", string(body)) if err != nil { return errOtto("Could not populate response object: %s", err)