From 9be87717ccafc3e4f8fb3413cee0b7ffe40cc433 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Mon, 19 Mar 2018 19:11:35 +0100 Subject: [PATCH] new: new writeFile(path, data) proxy module builtin function --- modules/base_proxy_script.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/base_proxy_script.go b/modules/base_proxy_script.go index cc35305b..6c238097 100644 --- a/modules/base_proxy_script.go +++ b/modules/base_proxy_script.go @@ -102,6 +102,24 @@ func (s *ProxyScript) defineBuiltins() error { return v }) + s.VM.Set("writeFile", func(call otto.FunctionCall) otto.Value { + argv := call.ArgumentList + argc := len(argv) + if argc != 2 { + return errOtto("writeFile: expected 2 arguments, %d given instead.", argc) + } + + filename := argv[0].String() + data := argv[1].String() + + err := ioutil.WriteFile(filename, []byte(data), 0644) + if err != nil { + return errOtto("Could not write %d bytes to %s: %s", len(data), filename, err) + } + + return otto.NullValue() + }) + // log something s.VM.Set("log", func(call otto.FunctionCall) otto.Value { for _, v := range call.ArgumentList {