Builtins (http.modules): Log levels and base64 dec/enc

Define more builtin functions in http.modules:
+ More log levels: log_deb, log_info, log_warn, log_error, log_fatal.
+ Add btoa/atob javascript functions.
Modify wiki to include this: https://github.com/bettercap/bettercap/wiki/http.modules
This commit is contained in:
b3tt3rfun 2018-03-14 13:35:09 +01:00 committed by GitHub
parent c778032545
commit 953732e4b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ package modules
import (
"io/ioutil"
"sync"
"encoding/base64"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
@ -109,6 +110,69 @@ func (s *ProxyScript) defineBuiltins() error {
return otto.Value{}
})
// log debug
s.VM.Set("log_debug", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Debug("%s", v.String())
}
return otto.Value{}
})
// log info
s.VM.Set("log_info", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Info("%s", v.String())
}
return otto.Value{}
})
// log warning
s.VM.Set("log_warn", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Warning("%s", v.String())
}
return otto.Value{}
})
// log error
s.VM.Set("log_error", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Error("%s", v.String())
}
return otto.Value{}
})
// log fatal
s.VM.Set("log_fatal", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Fatal("%s", v.String())
}
return otto.Value{}
})
// javascript btoa function
s.VM.Set("btoa", func(call otto.FunctionCall) otto.Value {
varValue := base64.StdEncoding.EncodeToString([]byte(call.Argument(0).String()))
v, err := s.VM.ToValue(varValue)
if err != nil {
return errOtto("Could not convert to string: %s", varValue)
}
return v
})
// javascript atob function
s.VM.Set("atob", func(call otto.FunctionCall) otto.Value {
varValue, err := base64.StdEncoding.DecodeString(call.Argument(0).String())
if err != nil {
return errOtto("Could not decode string: %s", call.Argument(0).String())
}
v, err := s.VM.ToValue(string(varValue))
if err != nil {
return errOtto("Could not convert to string: %s", varValue)
}
return v
})
// read or write environment variable
s.VM.Set("env", func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList