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"
"github.com/bettercap/bettercap/js"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/log"
"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)
}
// lock vm
I.script.Lock()
// lock vm if ready and available
locked := false
if I.script != nil {
I.script.Lock()
locked = true
}
if _, err := cb.Call(otto.NullValue(), opaque); err != nil {
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)
@ -131,3 +141,20 @@ func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
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()
}