mirror of
https://github.com/bettercap/bettercap
synced 2025-07-11 15:46:59 -07:00
refact: refactored proxy script builtins in separated file.
This commit is contained in:
parent
b8dd20ceae
commit
611e3fe078
2 changed files with 35 additions and 16 deletions
|
@ -58,22 +58,11 @@ func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// define builtins
|
err = s.defineBuiltins()
|
||||||
s.VM.Set("readFile", func(call otto.FunctionCall) otto.Value {
|
if err != nil {
|
||||||
filename := call.Argument(0).String()
|
log.Error("Error while defining builtin functions: %s", err)
|
||||||
raw, err := ioutil.ReadFile(filename)
|
return
|
||||||
if err != nil {
|
}
|
||||||
log.Error("Could not read %s: %s", filename, err)
|
|
||||||
return otto.Value{}
|
|
||||||
}
|
|
||||||
|
|
||||||
v, err := s.VM.ToValue(string(raw))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Could not convert to string: %s", err)
|
|
||||||
return otto.Value{}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
})
|
|
||||||
|
|
||||||
// run onLoad if defined
|
// run onLoad if defined
|
||||||
if s.hasCallback("onLoad") {
|
if s.hasCallback("onLoad") {
|
||||||
|
|
30
modules/http_proxy_script_builtins.go
Normal file
30
modules/http_proxy_script_builtins.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package modules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/evilsocket/bettercap-ng/log"
|
||||||
|
|
||||||
|
"github.com/robertkrimen/otto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// define functions available to proxy scripts
|
||||||
|
func (s *ProxyScript) defineBuiltins() error {
|
||||||
|
s.VM.Set("readFile", func(call otto.FunctionCall) otto.Value {
|
||||||
|
filename := call.Argument(0).String()
|
||||||
|
raw, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not read %s: %s", filename, err)
|
||||||
|
return otto.Value{}
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := s.VM.ToValue(string(raw))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not convert to string: %s", err)
|
||||||
|
return otto.Value{}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue