mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
new: new -script allows to run JS code to instrument session
This commit is contained in:
parent
d5e5abcb9b
commit
40727063ec
13 changed files with 610 additions and 312 deletions
67
session/script_builtin_runtime.go
Normal file
67
session/script_builtin_runtime.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"github.com/bettercap/bettercap/js"
|
||||
"github.com/evilsocket/islazy/log"
|
||||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func jsRunFunc(call otto.FunctionCall) otto.Value {
|
||||
argv := call.ArgumentList
|
||||
argc := len(argv)
|
||||
if argc != 1 {
|
||||
return js.ReportError("run accepts one string argument")
|
||||
} else if argv[0].IsString() == false {
|
||||
return js.ReportError("run accepts one string argument")
|
||||
}
|
||||
|
||||
for _, cmd := range ParseCommands(argv[0].String()) {
|
||||
if err := I.Run(cmd); err != nil {
|
||||
return js.ReportError("error running '%s': %v", cmd, err)
|
||||
}
|
||||
}
|
||||
|
||||
return js.NullValue
|
||||
}
|
||||
|
||||
func jsOnEventFunc(call otto.FunctionCall) otto.Value {
|
||||
argv := call.ArgumentList
|
||||
argc := len(argv)
|
||||
cb := otto.NullValue()
|
||||
filterExpr := ""
|
||||
|
||||
// just one argument, a function to receive all events
|
||||
if argc == 1 {
|
||||
if argv[0].IsFunction() == false {
|
||||
return js.ReportError("the single argument must be a function")
|
||||
}
|
||||
cb = argv[0]
|
||||
} else {
|
||||
if argc != 2 {
|
||||
return js.ReportError("expected two arguments (event_name, callback), got %d", argc)
|
||||
} else if argv[0].IsString() == false {
|
||||
return js.ReportError("first argument must be a string")
|
||||
} else if argv[1].IsFunction() == false {
|
||||
return js.ReportError("second argument must be a function")
|
||||
}
|
||||
|
||||
filterExpr = argv[0].String()
|
||||
cb = argv[1]
|
||||
}
|
||||
|
||||
// start a go routine for this event listener
|
||||
go func(expr string, cb otto.Value) {
|
||||
listener := I.Events.Listen()
|
||||
defer I.Events.Unlisten(listener)
|
||||
|
||||
for event := range listener {
|
||||
if expr == "" || event.Tag == expr {
|
||||
if _, err := cb.Call(otto.NullValue(), event); err != nil {
|
||||
I.Events.Log(log.ERROR, "error dispatching event %s: %v", event.Tag, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}(filterExpr, cb)
|
||||
|
||||
return js.NullValue
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue