mirror of
https://github.com/bettercap/bettercap
synced 2025-07-13 00:23:49 -07:00
new: added saveJSON function to the session scripting runtime
This commit is contained in:
parent
eff8135d99
commit
28371084d3
2 changed files with 29 additions and 2 deletions
|
@ -3,6 +3,7 @@ package session
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/js"
|
"github.com/bettercap/bettercap/js"
|
||||||
"github.com/evilsocket/islazy/log"
|
"github.com/evilsocket/islazy/log"
|
||||||
|
@ -84,13 +85,38 @@ func jsOnEventFunc(call otto.FunctionCall) otto.Value {
|
||||||
return js.NullValue
|
return js.NullValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsSaveJSONFunc(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
if argc != 2 {
|
||||||
|
return js.ReportError("saveJSON accepts one object and one string arguments")
|
||||||
|
} else if argv[0].IsObject() == false {
|
||||||
|
return js.ReportError("saveJSON accepts one object and one string arguments")
|
||||||
|
} else if argv[1].IsString() == false {
|
||||||
|
return js.ReportError("saveJSON accepts one object and one string arguments")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj := argv[0]
|
||||||
|
fileName := argv[1].String()
|
||||||
|
|
||||||
|
if exp, err := obj.Export(); err != nil {
|
||||||
|
return js.ReportError("error exporting object: %v", err)
|
||||||
|
} else if raw, err := json.Marshal(exp); err != nil {
|
||||||
|
return js.ReportError("error serializing object: %v", err)
|
||||||
|
} else if err = ioutil.WriteFile(fileName, raw, os.ModePerm); err != nil {
|
||||||
|
return js.ReportError("error writing to '%s': %v", fileName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return js.NullValue
|
||||||
|
}
|
||||||
|
|
||||||
func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
|
func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
|
||||||
argv := call.ArgumentList
|
argv := call.ArgumentList
|
||||||
argc := len(argv)
|
argc := len(argv)
|
||||||
if argc != 1 {
|
if argc != 1 {
|
||||||
return js.ReportError("LoadJSON accepts one string argument")
|
return js.ReportError("loadJSON accepts one string argument")
|
||||||
} else if argv[0].IsString() == false {
|
} else if argv[0].IsString() == false {
|
||||||
return js.ReportError("LoadJSON accepts one string argument")
|
return js.ReportError("loadJSON accepts one string argument")
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := argv[0].String()
|
fileName := argv[0].String()
|
||||||
|
|
|
@ -313,6 +313,7 @@ func (s *Session) Start() error {
|
||||||
plugin.Defines["env"] = jsEnvFunc
|
plugin.Defines["env"] = jsEnvFunc
|
||||||
plugin.Defines["run"] = jsRunFunc
|
plugin.Defines["run"] = jsRunFunc
|
||||||
plugin.Defines["loadJSON"] = jsLoadJSONFunc
|
plugin.Defines["loadJSON"] = jsLoadJSONFunc
|
||||||
|
plugin.Defines["saveJSON"] = jsSaveJSONFunc
|
||||||
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