new: exposing session environment to proxy scripts

This commit is contained in:
evilsocket 2018-01-08 01:50:15 +01:00
commit 5b969ffa9e
4 changed files with 20 additions and 5 deletions

View file

@ -5,6 +5,8 @@ import (
"net/http"
"sync"
"github.com/evilsocket/bettercap-ng/session"
"github.com/robertkrimen/otto"
)
@ -20,7 +22,7 @@ type ProxyScript struct {
cbCache map[string]bool
}
func LoadProxyScript(path string) (err error, s *ProxyScript) {
func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScript) {
log.Infof("Loading proxy script %s ...", path)
raw, err := ioutil.ReadFile(path)
@ -29,9 +31,10 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
}
s = &ProxyScript{
Path: path,
Source: string(raw),
VM: otto.New(),
Path: path,
Source: string(raw),
VM: otto.New(),
gil: &sync.Mutex{},
onRequestScript: nil,
onResponseScript: nil,
@ -45,6 +48,13 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
return
}
// define session pointer
err = s.VM.Set("env", *sess.Env.Storage())
if err != nil {
log.Errorf("Error while defining environment: %s", err)
return
}
// run onLoad if defined
if s.hasCallback("onLoad") {
_, err = s.VM.Run("onLoad()")