new: added fileExists function to the session scripting runtime

This commit is contained in:
Simone Margaritelli 2022-06-10 23:38:48 +02:00
parent 28371084d3
commit 11d96069ae
2 changed files with 31 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import (
"os" "os"
"github.com/bettercap/bettercap/js" "github.com/bettercap/bettercap/js"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/log" "github.com/evilsocket/islazy/log"
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
) )
@ -72,12 +73,21 @@ func jsOnEventFunc(call otto.FunctionCall) otto.Value {
I.Events.Log(log.ERROR, "error serializing event %s: %v", event.Tag, err) I.Events.Log(log.ERROR, "error serializing event %s: %v", event.Tag, err)
} }
// lock vm // lock vm if ready and available
I.script.Lock() locked := false
if I.script != nil {
I.script.Lock()
locked = true
}
if _, err := cb.Call(otto.NullValue(), opaque); err != nil { if _, err := cb.Call(otto.NullValue(), opaque); err != nil {
I.Events.Log(log.ERROR, "error dispatching event %s: %v", event.Tag, err) I.Events.Log(log.ERROR, "error dispatching event %s: %v", event.Tag, err)
} }
I.script.Unlock()
// unlock vm if ready and available
if locked {
I.script.Unlock()
}
} }
} }
}(filterExpr, cb) }(filterExpr, cb)
@ -131,3 +141,20 @@ func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
return v return v
} }
} }
func jsFileExistsFunc(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return js.ReportError("fileExists accepts one string argument")
} else if argv[0].IsString() == false {
return js.ReportError("fileExists accepts one string argument")
}
fileName := argv[0].String()
if fs.Exists(fileName) {
return otto.TrueValue()
}
return otto.FalseValue()
}

View file

@ -312,6 +312,7 @@ func (s *Session) Start() error {
// js and session // js and session
plugin.Defines["env"] = jsEnvFunc plugin.Defines["env"] = jsEnvFunc
plugin.Defines["run"] = jsRunFunc plugin.Defines["run"] = jsRunFunc
plugin.Defines["fileExists"] = jsFileExistsFunc
plugin.Defines["loadJSON"] = jsLoadJSONFunc plugin.Defines["loadJSON"] = jsLoadJSONFunc
plugin.Defines["saveJSON"] = jsSaveJSONFunc plugin.Defines["saveJSON"] = jsSaveJSONFunc
plugin.Defines["onEvent"] = jsOnEventFunc plugin.Defines["onEvent"] = jsOnEventFunc