From 7d4a4521f452c5ca4b86539fa92cedd8c1a77b81 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 11 Jan 2018 01:34:39 +0100 Subject: [PATCH] perf: wrote benchmark file for proxy script onRequest evaluation --- modules/http_proxy_script.go | 25 ++++++++++++--------- modules/http_proxy_script_test.go | 37 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 modules/http_proxy_script_test.go diff --git a/modules/http_proxy_script.go b/modules/http_proxy_script.go index d745f82b..fc03ebbb 100644 --- a/modules/http_proxy_script.go +++ b/modules/http_proxy_script.go @@ -24,17 +24,10 @@ type ProxyScript struct { cbCache map[string]bool } -func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScript) { - log.Info("Loading proxy script %s ...", path) - - raw, err := ioutil.ReadFile(path) - if err != nil { - return - } - +func LoadProxyScriptSource(path, source string, sess *session.Session) (err error, s *ProxyScript) { s = &ProxyScript{ Path: path, - Source: string(raw), + Source: source, VM: otto.New(), sess: sess, @@ -94,6 +87,17 @@ func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScr return } +func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScript) { + log.Info("Loading proxy script %s ...", path) + + raw, err := ioutil.ReadFile(path) + if err != nil { + return + } + + return LoadProxyScriptSource(path, string(raw), sess) +} + func (s *ProxyScript) hasCallback(name string) bool { s.cbCacheLock.Lock() defer s.cbCacheLock.Unlock() @@ -117,7 +121,7 @@ func (s *ProxyScript) hasCallback(name string) bool { func (s *ProxyScript) doRequestDefines(req *http.Request) (err error, jsres *JSResponse) { // convert request and define empty response to be optionally filled jsreq := NewJSRequest(req) - if err = s.VM.Set("req", &jsreq); err != nil { + if err = s.VM.Set("req", req); err != nil { log.Error("Error while defining request: %s", err) return } @@ -127,7 +131,6 @@ func (s *ProxyScript) doRequestDefines(req *http.Request) (err error, jsres *JSR log.Error("Error while defining response: %s", err) return } - return } diff --git a/modules/http_proxy_script_test.go b/modules/http_proxy_script_test.go new file mode 100644 index 00000000..96f7d21d --- /dev/null +++ b/modules/http_proxy_script_test.go @@ -0,0 +1,37 @@ +package modules + +import ( + "net/http" + "testing" + + "github.com/evilsocket/bettercap-ng/log" + "github.com/evilsocket/bettercap-ng/session" +) + +func getScript(src string) *HttpProxyScript { + sess := session.Session{} + sess.Env = session.NewEnvironment(&sess) + + err, script := LoadProxyScriptSource("", src, &sess) + if err != nil { + log.Fatal("%s", err) + } + return script +} + +func getRequest() *http.Request { + req, err := http.NewRequest("GET", "http://www.google.com/", nil) + if err != nil { + log.Fatal("%s", err) + } + return req +} + +func BenchmarkOnRequest(b *testing.B) { + script := getScript("function onRequest(req,res){}") + req := getRequest() + + for n := 0; n < b.N; n++ { + script.OnRequest(req) + } +}