mirror of
https://github.com/bettercap/bettercap
synced 2025-07-07 13:32:07 -07:00
new: implemented 'env' builtin function for proxy modules in order to get and set session variables
This commit is contained in:
parent
9a8afc312a
commit
abca005651
1 changed files with 29 additions and 0 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
// define functions available to proxy scripts
|
// define functions available to proxy scripts
|
||||||
func (s *ProxyScript) defineBuiltins() error {
|
func (s *ProxyScript) defineBuiltins() error {
|
||||||
|
// used to read a file ... doh
|
||||||
s.VM.Set("readFile", func(call otto.FunctionCall) otto.Value {
|
s.VM.Set("readFile", func(call otto.FunctionCall) otto.Value {
|
||||||
filename := call.Argument(0).String()
|
filename := call.Argument(0).String()
|
||||||
raw, err := ioutil.ReadFile(filename)
|
raw, err := ioutil.ReadFile(filename)
|
||||||
|
@ -27,6 +28,7 @@ func (s *ProxyScript) defineBuiltins() error {
|
||||||
return v
|
return v
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// log something
|
||||||
s.VM.Set("log", func(call otto.FunctionCall) otto.Value {
|
s.VM.Set("log", func(call otto.FunctionCall) otto.Value {
|
||||||
for _, v := range call.ArgumentList {
|
for _, v := range call.ArgumentList {
|
||||||
fmt.Printf("%s", v.String())
|
fmt.Printf("%s", v.String())
|
||||||
|
@ -37,5 +39,32 @@ func (s *ProxyScript) defineBuiltins() error {
|
||||||
return otto.Value{}
|
return otto.Value{}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// read or write environment variable
|
||||||
|
s.VM.Set("env", func(call otto.FunctionCall) otto.Value {
|
||||||
|
argv := call.ArgumentList
|
||||||
|
argc := len(argv)
|
||||||
|
|
||||||
|
if argc == 1 {
|
||||||
|
// get
|
||||||
|
varName := call.Argument(0).String()
|
||||||
|
if found, varValue := s.sess.Env.Get(varName); found == true {
|
||||||
|
v, err := s.VM.ToValue(varValue)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not convert to string: %s", varValue)
|
||||||
|
return otto.Value{}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if argc == 2 {
|
||||||
|
// set
|
||||||
|
varName := call.Argument(0).String()
|
||||||
|
varValue := call.Argument(1).String()
|
||||||
|
s.sess.Env.Set(varName, varValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return otto.Value{}
|
||||||
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue