diff options
| -rw-r--r-- | glue/CHEATSHEET.md | 26 | ||||
| -rw-r--r-- | glue/Dockerfile | 4 | ||||
| -rw-r--r-- | glue/box.rb | 6 | ||||
| -rw-r--r-- | glue/build.lua | 20 | ||||
| -rw-r--r-- | glue/glue.go | 22 | ||||
| -rw-r--r-- | glue/libs/gluaexpect/expect.go | 35 | ||||
| -rw-r--r-- | glue/libs/gluasimplebox/sb.go | 100 |
7 files changed, 203 insertions, 10 deletions
diff --git a/glue/CHEATSHEET.md b/glue/CHEATSHEET.md index 798383b..0536567 100644 --- a/glue/CHEATSHEET.md +++ b/glue/CHEATSHEET.md @@ -550,7 +550,7 @@ sh.ls("-la /") #### Piping Piping in sh is done almost like piping in the shell. Just call next command as a method on the previous command. -``` lua +```lua sh.du("-sb"):sort("-rn"):print() ``` @@ -598,7 +598,7 @@ end #### stdout([filename]), stderr([filename]), combinedOutput([filename]) `stdout()`, `stderr()`, and `combinedOutput()` all returns the output of the command as a string. An optional `filename` can be given to the method, in that case the output is also written to the file. The file will be truncated. -``` lua +```lua -- print output of command output = sh.echo("hello world"):combinedOutput("/tmp/output") print(output) @@ -606,7 +606,6 @@ print(output) In the example above will print `hello world` and it will write it to `/tmp/output` - ### Glob Expansion There is no glob expansion done on arguments, however there is a glob functionality in sh. @@ -639,3 +638,24 @@ Returns: string: escaped string gluare.quote returns a string that quotes all regular expression metacharacters inside the given text. ``` + +## `simplebox` + +```lua +local simplebox = require "simplebox" +``` + +Simple encryption + +### API + +#### Create a new instance of simplebox with a newly generated key + +```lua +local simplebox = require "simplebox" +local key = simplebox.genkey() +print("key is: " .. key) +local sb = simplebox.new() + + +``` diff --git a/glue/Dockerfile b/glue/Dockerfile new file mode 100644 index 0000000..23ddabe --- /dev/null +++ b/glue/Dockerfile @@ -0,0 +1,4 @@ +FROM busybox + +ADD glue /glue +CMD /glue diff --git a/glue/box.rb b/glue/box.rb new file mode 100644 index 0000000..3b4d18e --- /dev/null +++ b/glue/box.rb @@ -0,0 +1,6 @@ +from "alpine:edge" + +copy "glue", "/glue" +cmd "/glue" +flatten +tag "xena/glue" diff --git a/glue/build.lua b/glue/build.lua new file mode 100644 index 0000000..31fe844 --- /dev/null +++ b/glue/build.lua @@ -0,0 +1,20 @@ +-- expects glue, $ go get -u github.com/Xe/tools/glue +local sh = require "sh" +sh { abort = true } + +if os.getenv("CGO_ENABLED") ~= "0" then + error("CGO_ENABLED must be set to 1") +end + +print "building glue..." +sh.go("build"):print() +sh.upx("--ultra-brute", "glue"):print() +sh.box("box.rb"):print() + +print "releasing to docker hub" +sh.docker("push", "xena/glue"):print() + +print "moving glue binary to $GOPATH/bin" +sh.mv("glue", (os.getenv("GOPATH") .. "/bin/glue")) + +print "build/release complete" diff --git a/glue/glue.go b/glue/glue.go index 9a685ec..6ddae41 100644 --- a/glue/glue.go +++ b/glue/glue.go @@ -8,8 +8,8 @@ import ( "os" "runtime/pprof" - json "layeh.com/gopher-json" - + "github.com/Xe/tools/glue/libs/gluaexpect" + "github.com/Xe/tools/glue/libs/gluasimplebox" "github.com/ailncode/gluaxmlpath" "github.com/cjoudrey/gluahttp" "github.com/cjoudrey/gluaurl" @@ -25,6 +25,7 @@ import ( "github.com/yuin/gluare" "github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua/parse" + json "layeh.com/gopher-json" ) func main() { @@ -78,6 +79,8 @@ Available options are: L.SetMx(opt_m) } + preload(L) + if opt_v || opt_i { fmt.Println(lua.PackageCopyRight) } @@ -118,6 +121,7 @@ Available options are: fmt.Println(proto.String()) } } + if err := L.DoFile(script); err != nil { fmt.Println(err.Error()) status = 1 @@ -131,6 +135,13 @@ Available options are: } } + if opt_i { + doREPL(L) + } + return status +} + +func preload(L *lua.LState) { L.PreloadModule("re", gluare.Loader) L.PreloadModule("sh", gluash.Loader) L.PreloadModule("markdown", gluamarkdown.Loader) @@ -143,13 +154,10 @@ Available options are: L.PreloadModule("flag", gluaflag.Loader) L.PreloadModule("template", gluatemplate.Loader) L.PreloadModule("url", gluaurl.Loader) + gluaexpect.Preload(L) + gluasimplebox.Preload(L) gluaxmlpath.Preload(L) json.Preload(L) - - if opt_i { - doREPL(L) - } - return status } // do read/eval/print/loop diff --git a/glue/libs/gluaexpect/expect.go b/glue/libs/gluaexpect/expect.go new file mode 100644 index 0000000..b7e57d4 --- /dev/null +++ b/glue/libs/gluaexpect/expect.go @@ -0,0 +1,35 @@ +package gluaexpect + +import ( + "github.com/ThomasRooney/gexpect" + luar "github.com/layeh/gopher-luar" + lua "github.com/yuin/gopher-lua" +) + +func Preload(L *lua.LState) { + L.PreloadModule("expect", Loader) +} + +// Loader is the module loader function. +func Loader(L *lua.LState) int { + mod := L.SetFuncs(L.NewTable(), api) + L.Push(mod) + return 1 +} + +var api = map[string]lua.LGFunction{ + "spawn": spawn, +} + +func spawn(L *lua.LState) int { + cmd := L.CheckString(1) + child, err := gexpect.Spawn(cmd) + if err != nil { + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + return 2 + } + + L.Push(luar.New(L, child)) + return 1 +} diff --git a/glue/libs/gluasimplebox/sb.go b/glue/libs/gluasimplebox/sb.go new file mode 100644 index 0000000..cbb707e --- /dev/null +++ b/glue/libs/gluasimplebox/sb.go @@ -0,0 +1,100 @@ +package gluasimplebox + +import ( + "crypto/rand" + "encoding/base64" + "encoding/hex" + "errors" + + "github.com/brandur/simplebox" + luar "github.com/layeh/gopher-luar" + lua "github.com/yuin/gopher-lua" +) + +func Preload(L *lua.LState) { + L.PreloadModule("simplebox", Loader) +} + +// Loader is the module loader function. +func Loader(L *lua.LState) int { + mod := L.SetFuncs(L.NewTable(), api) + L.Push(mod) + return 1 +} + +var api = map[string]lua.LGFunction{ + "new": newSecretBox, + "genkey": genKey, +} + +func newSecretBox(L *lua.LState) int { + key := L.CheckString(1) + + k, err := parseKey(key) + if err != nil { + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + return 2 + } + + sb := simplebox.NewFromSecretKey(k) + + L.Push(luar.New(L, &box{sb: sb})) + return 1 +} + +func genKey(L *lua.LState) int { + key, err := generateKey() + if err != nil { + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + return 2 + } + + L.Push(lua.LString(base64.URLEncoding.EncodeToString(key[:]))) + return 1 +} + +func generateKey() (*[32]byte, error) { + var k [32]byte + _, err := rand.Read(k[:]) + if err != nil { + return nil, err + } + return &k, nil +} + +func parseKey(s string) (*[32]byte, error) { + k := &[32]byte{} + raw, err := base64.URLEncoding.DecodeString(s) + if err != nil { + return nil, err + } + if n := copy(k[:], raw); n < len(k) { + return nil, errors.New("not valid") + } + return k, nil +} + +type box struct { + sb *simplebox.SimpleBox +} + +func (b *box) Encrypt(data string) string { + result := b.sb.Encrypt([]byte(data)) + return hex.EncodeToString(result) +} + +func (b *box) Decrypt(data string) (string, error) { + d, err := hex.DecodeString(data) + if err != nil { + return "", err + } + + plain, err := b.sb.Decrypt([]byte(d)) + if err != nil { + return "", err + } + + return string(plain), nil +} |
