diff --git a/modules/http_proxy_script.go b/modules/http_proxy_script.go index f4d3d6e1..d745f82b 100644 --- a/modules/http_proxy_script.go +++ b/modules/http_proxy_script.go @@ -58,22 +58,11 @@ func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScr return } - // define builtins - 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 - }) + err = s.defineBuiltins() + if err != nil { + log.Error("Error while defining builtin functions: %s", err) + return + } // run onLoad if defined if s.hasCallback("onLoad") { diff --git a/modules/http_proxy_script_builtins.go b/modules/http_proxy_script_builtins.go new file mode 100644 index 00000000..48bd7a60 --- /dev/null +++ b/modules/http_proxy_script_builtins.go @@ -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 +}