mirror of
https://github.com/bettercap/bettercap
synced 2025-07-10 23:33:28 -07:00
new: added loadJSON function to the session scripting runtime
This commit is contained in:
parent
22de9d3d4f
commit
eff8135d99
2 changed files with 27 additions and 0 deletions
|
@ -2,12 +2,16 @@ package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/js"
|
"github.com/bettercap/bettercap/js"
|
||||||
"github.com/evilsocket/islazy/log"
|
"github.com/evilsocket/islazy/log"
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// see https://github.com/robertkrimen/otto/issues/213
|
||||||
|
var jsRuntime = otto.New()
|
||||||
|
|
||||||
func jsRunFunc(call otto.FunctionCall) otto.Value {
|
func jsRunFunc(call otto.FunctionCall) otto.Value {
|
||||||
argv := call.ArgumentList
|
argv := call.ArgumentList
|
||||||
argc := len(argv)
|
argc := len(argv)
|
||||||
|
@ -79,3 +83,25 @@ func jsOnEventFunc(call otto.FunctionCall) otto.Value {
|
||||||
|
|
||||||
return js.NullValue
|
return js.NullValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
if argc != 1 {
|
||||||
|
return js.ReportError("LoadJSON accepts one string argument")
|
||||||
|
} else if argv[0].IsString() == false {
|
||||||
|
return js.ReportError("LoadJSON accepts one string argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName := argv[0].String()
|
||||||
|
var obj interface{}
|
||||||
|
if rawData, err := ioutil.ReadFile(fileName); err != nil {
|
||||||
|
return js.ReportError("can't read '%s': %v", fileName, err)
|
||||||
|
} else if err = json.Unmarshal(rawData, &obj); err != nil {
|
||||||
|
return js.ReportError("can't parse '%s': %v", fileName, err)
|
||||||
|
} else if v, err := jsRuntime.ToValue(obj); err != nil {
|
||||||
|
return js.ReportError("could not convert '%s' to javascript object: %s", fileName, err)
|
||||||
|
} else {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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["loadJSON"] = jsLoadJSONFunc
|
||||||
plugin.Defines["onEvent"] = jsOnEventFunc
|
plugin.Defines["onEvent"] = jsOnEventFunc
|
||||||
plugin.Defines["session"] = s
|
plugin.Defines["session"] = s
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue