mirror of
https://github.com/bettercap/bettercap
synced 2025-07-10 23:33:28 -07:00
misc: refactored and ported the plugin system to islazy/plugin.Plugin (huge optimization)
This commit is contained in:
parent
788c8d5994
commit
dd2e8784dc
9 changed files with 374 additions and 335 deletions
|
@ -1,129 +1,64 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/bettercap/bettercap/log"
|
||||
"github.com/bettercap/bettercap/session"
|
||||
|
||||
"github.com/robertkrimen/otto"
|
||||
|
||||
"github.com/evilsocket/islazy/plugin"
|
||||
)
|
||||
|
||||
type HttpProxyScript struct {
|
||||
*ProxyScript
|
||||
onRequestScript *otto.Script
|
||||
onResponseScript *otto.Script
|
||||
onCommandScript *otto.Script
|
||||
}
|
||||
*plugin.Plugin
|
||||
|
||||
func LoadHttpProxyScriptSource(path, source string, sess *session.Session) (err error, s *HttpProxyScript) {
|
||||
err, ps := LoadProxyScriptSource(path, source, sess)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s = &HttpProxyScript{
|
||||
ProxyScript: ps,
|
||||
onRequestScript: nil,
|
||||
onResponseScript: nil,
|
||||
onCommandScript: nil,
|
||||
}
|
||||
|
||||
if s.hasCallback("onRequest") {
|
||||
s.onRequestScript, err = s.VM.Compile("", "onRequest(req, res)")
|
||||
if err != nil {
|
||||
log.Error("Error while compiling onRequest callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if s.hasCallback("onResponse") {
|
||||
s.onResponseScript, err = s.VM.Compile("", "onResponse(req, res)")
|
||||
if err != nil {
|
||||
log.Error("Error while compiling onResponse callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if s.hasCallback("onCommand") {
|
||||
s.onCommandScript, err = s.VM.Compile("", "onCommand(cmd)")
|
||||
if err != nil {
|
||||
log.Error("Error while compiling onCommand callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
doOnRequest bool
|
||||
doOnResponse bool
|
||||
doOnCommand bool
|
||||
}
|
||||
|
||||
func LoadHttpProxyScript(path string, sess *session.Session) (err error, s *HttpProxyScript) {
|
||||
log.Info("loading proxy script %s ...", path)
|
||||
|
||||
raw, err := ioutil.ReadFile(path)
|
||||
plug, err := plugin.Load(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return LoadHttpProxyScriptSource(path, string(raw), sess)
|
||||
}
|
||||
|
||||
func (s *HttpProxyScript) doRequestDefines(req *http.Request) (err error, jsreq *JSRequest, jsres *JSResponse) {
|
||||
jsreq = NewJSRequest(req)
|
||||
if err = s.VM.Set("req", jsreq); err != nil {
|
||||
log.Error("Error while defining request: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
// define session pointer
|
||||
if err = plug.Set("env", sess.Env.Data); err != nil {
|
||||
log.Error("Error while defining environment: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsres = NewJSResponse(nil)
|
||||
if err = s.VM.Set("res", jsres); err != nil {
|
||||
log.Error("Error while defining response: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *HttpProxyScript) doResponseDefines(res *http.Response) (err error, jsreq *JSRequest, jsres *JSResponse) {
|
||||
jsreq = NewJSRequest(res.Request)
|
||||
if err = s.VM.Set("req", jsreq); err != nil {
|
||||
log.Error("Error while defining request: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
// run onLoad if defined
|
||||
if plug.HasFunc("onLoad") {
|
||||
if _, err = plug.Call("onLoad"); err != nil {
|
||||
log.Error("Error while executing onLoad callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
jsres = NewJSResponse(res)
|
||||
if err = s.VM.Set("res", jsres); err != nil {
|
||||
log.Error("Error while defining response: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *HttpProxyScript) doCommandDefines(cmd string) (err error) {
|
||||
if err = s.VM.Set("cmd", cmd); err != nil {
|
||||
log.Error("Error while defining cmd: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
s = &HttpProxyScript{
|
||||
Plugin: plug,
|
||||
doOnRequest: plug.HasFunc("onRequest"),
|
||||
doOnResponse: plug.HasFunc("onResponse"),
|
||||
doOnCommand: plug.HasFunc("onCommand"),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *HttpProxyScript) OnRequest(original *http.Request) (jsreq *JSRequest, jsres *JSResponse) {
|
||||
var err error
|
||||
if s.doOnRequest {
|
||||
jsreq := NewJSRequest(original)
|
||||
jsres := NewJSResponse(nil)
|
||||
|
||||
if s.onRequestScript != nil {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if err, jsreq, jsres = s.doRequestDefines(original); err != nil {
|
||||
log.Error("Error while running bootstrap definitions: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
if _, err := s.Call("onRequest", jsreq, jsres); err != nil {
|
||||
log.Error("%s", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if _, err = s.VM.Run(s.onRequestScript); err != nil {
|
||||
log.Error("Error while executing onRequest callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if jsreq.WasModified() {
|
||||
} else if jsreq.WasModified() {
|
||||
jsreq.UpdateHash()
|
||||
return jsreq, nil
|
||||
} else if jsres.WasModified() {
|
||||
|
@ -136,23 +71,14 @@ func (s *HttpProxyScript) OnRequest(original *http.Request) (jsreq *JSRequest, j
|
|||
}
|
||||
|
||||
func (s *HttpProxyScript) OnResponse(res *http.Response) (jsreq *JSRequest, jsres *JSResponse) {
|
||||
var err error
|
||||
if s.doOnResponse {
|
||||
jsreq := NewJSRequest(res.Request)
|
||||
jsres := NewJSResponse(res)
|
||||
|
||||
if s.onResponseScript != nil {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if err, jsreq, jsres = s.doResponseDefines(res); err != nil {
|
||||
log.Error("Error while running bootstrap definitions: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
if _, err := s.Call("onResponse", jsreq, jsres); err != nil {
|
||||
log.Error("%s", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if _, err = s.VM.Run(s.onResponseScript); err != nil {
|
||||
log.Error("Error while executing onRequest callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if jsres.WasModified() {
|
||||
} else if jsres.WasModified() {
|
||||
jsres.UpdateHash()
|
||||
return nil, jsres
|
||||
}
|
||||
|
@ -162,19 +88,11 @@ func (s *HttpProxyScript) OnResponse(res *http.Response) (jsreq *JSRequest, jsre
|
|||
}
|
||||
|
||||
func (s *HttpProxyScript) OnCommand(cmd string) bool {
|
||||
if s.onCommandScript != nil {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if err := s.doCommandDefines(cmd); err != nil {
|
||||
log.Error("Error while running bootstrap onCommand definitions: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
if s.doOnCommand {
|
||||
if ret, err := s.Call("onCommand", cmd); err != nil {
|
||||
log.Error("Error while executing onCommand callback: %+v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
if ret, err := s.VM.Run(s.onCommandScript); err != nil {
|
||||
log.Error("Error while executing onCommand callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
|
||||
return false
|
||||
} else if v, err := ret.ToBoolean(); err == nil {
|
||||
} else if v, ok := ret.(bool); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue