mirror of
https://github.com/bettercap/bettercap
synced 2025-07-16 10:03:39 -07:00
perf: wrote benchmark file for proxy script onRequest evaluation
This commit is contained in:
parent
e543582257
commit
7d4a4521f4
2 changed files with 51 additions and 11 deletions
|
@ -24,17 +24,10 @@ type ProxyScript struct {
|
||||||
cbCache map[string]bool
|
cbCache map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScript) {
|
func LoadProxyScriptSource(path, source string, sess *session.Session) (err error, s *ProxyScript) {
|
||||||
log.Info("Loading proxy script %s ...", path)
|
|
||||||
|
|
||||||
raw, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s = &ProxyScript{
|
s = &ProxyScript{
|
||||||
Path: path,
|
Path: path,
|
||||||
Source: string(raw),
|
Source: source,
|
||||||
VM: otto.New(),
|
VM: otto.New(),
|
||||||
|
|
||||||
sess: sess,
|
sess: sess,
|
||||||
|
@ -94,6 +87,17 @@ func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScr
|
||||||
return
|
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 {
|
func (s *ProxyScript) hasCallback(name string) bool {
|
||||||
s.cbCacheLock.Lock()
|
s.cbCacheLock.Lock()
|
||||||
defer s.cbCacheLock.Unlock()
|
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) {
|
func (s *ProxyScript) doRequestDefines(req *http.Request) (err error, jsres *JSResponse) {
|
||||||
// convert request and define empty response to be optionally filled
|
// convert request and define empty response to be optionally filled
|
||||||
jsreq := NewJSRequest(req)
|
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)
|
log.Error("Error while defining request: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -127,7 +131,6 @@ func (s *ProxyScript) doRequestDefines(req *http.Request) (err error, jsres *JSR
|
||||||
log.Error("Error while defining response: %s", err)
|
log.Error("Error while defining response: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
modules/http_proxy_script_test.go
Normal file
37
modules/http_proxy_script_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue