mirror of
https://github.com/bettercap/bettercap
synced 2025-07-11 15:46:59 -07:00
perf: callback checks are now cached
This commit is contained in:
parent
3719c6d806
commit
8c34f9616e
1 changed files with 31 additions and 11 deletions
|
@ -13,7 +13,10 @@ type ProxyScript struct {
|
||||||
Path string
|
Path string
|
||||||
Source string
|
Source string
|
||||||
VM *otto.Otto
|
VM *otto.Otto
|
||||||
gil *sync.Mutex
|
|
||||||
|
gil *sync.Mutex
|
||||||
|
cbCacheLock *sync.Mutex
|
||||||
|
cbCache map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
||||||
|
@ -25,16 +28,17 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
||||||
}
|
}
|
||||||
|
|
||||||
s = &ProxyScript{
|
s = &ProxyScript{
|
||||||
Path: path,
|
Path: path,
|
||||||
Source: string(raw),
|
Source: string(raw),
|
||||||
VM: otto.New(),
|
VM: otto.New(),
|
||||||
gil: &sync.Mutex{},
|
gil: &sync.Mutex{},
|
||||||
|
cbCacheLock: &sync.Mutex{},
|
||||||
|
cbCache: make(map[string]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = s.VM.Run(s.Source)
|
_, err = s.VM.Run(s.Source)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cb, err := s.VM.Get("onLoad")
|
if s.hasCallback("onLoad") {
|
||||||
if err == nil && cb.IsFunction() {
|
|
||||||
_, err = s.VM.Run("onLoad()")
|
_, err = s.VM.Run("onLoad()")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while executing onLoad callback: %s", err)
|
log.Errorf("Error while executing onLoad callback: %s", err)
|
||||||
|
@ -46,6 +50,24 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ProxyScript) hasCallback(name string) bool {
|
||||||
|
s.cbCacheLock.Lock()
|
||||||
|
defer s.cbCacheLock.Unlock()
|
||||||
|
|
||||||
|
has, found := s.cbCache[name]
|
||||||
|
if found == false {
|
||||||
|
cb, err := s.VM.Get(name)
|
||||||
|
if err == nil && cb.IsFunction() {
|
||||||
|
has = true
|
||||||
|
} else {
|
||||||
|
has = false
|
||||||
|
}
|
||||||
|
s.cbCache[name] = has
|
||||||
|
}
|
||||||
|
|
||||||
|
return has
|
||||||
|
}
|
||||||
|
|
||||||
func (s ProxyScript) reqToJS(req *http.Request) JSRequest {
|
func (s ProxyScript) reqToJS(req *http.Request) JSRequest {
|
||||||
headers := make([]JSHeader, 0)
|
headers := make([]JSHeader, 0)
|
||||||
for key, values := range req.Header {
|
for key, values := range req.Header {
|
||||||
|
@ -117,8 +139,7 @@ func (s *ProxyScript) doResponseDefines(res *http.Response) (err error, jsres *J
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProxyScript) OnRequest(req *http.Request) *JSResponse {
|
func (s *ProxyScript) OnRequest(req *http.Request) *JSResponse {
|
||||||
cb, err := s.VM.Get("onRequest")
|
if s.hasCallback("onRequest") {
|
||||||
if err == nil && cb.IsFunction() {
|
|
||||||
s.gil.Lock()
|
s.gil.Lock()
|
||||||
defer s.gil.Unlock()
|
defer s.gil.Unlock()
|
||||||
|
|
||||||
|
@ -143,8 +164,7 @@ func (s *ProxyScript) OnRequest(req *http.Request) *JSResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProxyScript) OnResponse(res *http.Response) *JSResponse {
|
func (s *ProxyScript) OnResponse(res *http.Response) *JSResponse {
|
||||||
cb, err := s.VM.Get("onResponse")
|
if s.hasCallback("onResponse") {
|
||||||
if err == nil && cb.IsFunction() {
|
|
||||||
s.gil.Lock()
|
s.gil.Lock()
|
||||||
defer s.gil.Unlock()
|
defer s.gil.Unlock()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue