-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluascript.go
281 lines (243 loc) · 7.11 KB
/
luascript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright © 2013-2018 Pierre Neidhardt <[email protected]>
// Use of this file is governed by the license that can be found in LICENSE.
// Convert 'input' and 'output' from Go to Lua and from Lua to Go. Almost all
// scripting support is implemented in this file: in case of library change,
// this is the only file that would need some overhaul.
package main
import (
"fmt"
"log"
"github.com/aarzilli/golua/lua"
"github.com/ambrevar/golua/unicode"
"github.com/stevedonovan/luar"
)
const (
// Note: we do not use Lua references (luaL_ref) so that we do not have to
// pass them, together with the Lua state, to the calling goroutine to ensure
// re-entrency.
registryWhitelist = "_whitelist"
registryScripts = "_scripts"
registryActions = "_actions"
)
// Shorthand.
func goToLua(L *lua.State, name string, val interface{}) {
luar.GoToLua(L, val)
L.SetGlobal(name)
}
// Registers a Go function as a global variable and add it to the sandbox.
func sandboxRegister(L *lua.State, name string, f interface{}) {
goToLua(L, name, f)
L.PushString(registryWhitelist)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.GetGlobal(name)
L.SetField(-2, name)
}
// MakeSandbox initializes a Lua state, removes all elements not in the
// whitelist, sets up the debug function if necessary and adds some Go helper
// functions.
// The caller is responsible for closing the Lua state.
func MakeSandbox(logPrint func(v ...interface{})) *lua.State {
L := lua.NewState()
L.OpenLibs()
unicode.GoLuaReplaceFuncs(L)
// Store the whitelist in registry to avoid tampering it.
L.PushString(registryWhitelist)
err := L.DoString(luaWhitelist)
if err != nil {
log.Fatal("Spurious sandbox", err)
}
L.SetTable(lua.LUA_REGISTRYINDEX)
// Register before setting up the sandbox: these functions will be restored
// together with the sandbox.
// The closure allows access to the external logger.
luaDebug := func(L *lua.State) int { return 0 }
luaHelp := func(L *lua.State) int { return 0 }
if logPrint != nil {
luaDebug = func(L *lua.State) int {
var arglist []interface{}
nargs := L.GetTop()
for i := 1; i <= nargs; i++ {
if L.IsString(i) {
arglist = append(arglist, L.ToString(i))
}
}
logPrint(arglist...)
return 0
}
// If debug is off but logPrint!=nil, it means we want to display help.
if !options.Debug {
luaHelp, luaDebug = luaDebug, luaHelp
}
}
sandboxRegister(L, "help", luaHelp)
sandboxRegister(L, "debug", luaDebug)
sandboxRegister(L, "stringnorm", stringNorm)
sandboxRegister(L, "stringrel", stringRel)
// Purge _G from everything but the content of the whitelist.
err = L.DoString(luaSetSandbox)
if err != nil {
log.Fatal("Cannot load function to set sandbox", err)
}
L.PushString(registryWhitelist)
L.GetTable(lua.LUA_REGISTRYINDEX)
err = L.Call(1, 0)
if err != nil {
log.Fatal("Failed to set sandbox", err)
}
// Init script table.
L.PushString(registryScripts)
L.NewTable()
L.SetTable(lua.LUA_REGISTRYINDEX)
// Init action table.
L.PushString(registryActions)
L.NewTable()
L.SetTable(lua.LUA_REGISTRYINDEX)
return L
}
// SandboxCompileAction is like SandboxCompileScripts.
func SandboxCompileAction(L *lua.State, name, code string) {
sandboxCompile(L, registryActions, name, code)
}
// SandboxCompileScript transfers the script buffer to the Lua state L and
// references them in LUA_REGISTRYINDEX.
func SandboxCompileScript(L *lua.State, name, code string) {
sandboxCompile(L, registryScripts, name, code)
}
func sandboxCompile(L *lua.State, registryIndex string, name, code string) {
L.PushString(registryIndex)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.PushString(name)
err := L.LoadString(code)
if err != 0 {
log.Fatalf("%s: %s", name, L.ToString(-1))
L.Pop(2)
} else {
L.SetTable(-3)
}
}
func outputNumbersToStrings(L *lua.State) {
L.GetGlobal("output")
if !L.IsTable(-1) {
L.NewTable()
L.SetGlobal("output")
}
L.GetField(-1, "tags")
if L.IsTable(-1) {
// First key.
L.PushNil()
for L.Next(-2) != 0 {
// Use 'key' at index -2 and 'value' at index -1.
if L.IsString(-2) && L.IsString(-1) {
// Convert numbers to strings.
L.ToString(-1)
L.SetField(-3, L.ToString(-2))
} else {
// Remove 'value' and keep 'key' for next iteration.
L.Pop(1)
}
}
}
L.Pop(1)
L.Pop(1)
}
// RunAction is similar to RunScript.
func RunAction(L *lua.State, action string, input *inputInfo, output *outputInfo, exist *inputInfo) error {
return run(L, registryActions, action, input, output, exist)
}
// RunScript executes script named 'script' with 'input' and 'output' set as global variable.
// Any change made to 'input' is discarded. Change to 'output' are transferred
// back to Go on every script call to guarantee type consistency across script
// calls (Lua is dynamically typed).
func RunScript(L *lua.State, script string, input *inputInfo, output *outputInfo) error {
return run(L, registryScripts, script, input, output, nil)
}
// 'exist' is optional.
func run(L *lua.State, registryIndex string, code string, input *inputInfo, output *outputInfo, exist *inputInfo) error {
// Restore the sandbox.
err := L.DoString(luaRestoreSandbox)
if err != nil {
log.Fatal("Cannot load function to restore sandbox", err)
}
L.PushString(registryWhitelist)
L.GetTable(lua.LUA_REGISTRYINDEX)
err = L.Call(1, 0)
if err != nil {
log.Fatal("Failed to restore sandbox", err)
}
goToLua(L, "input", *input)
goToLua(L, "output", *output)
if exist != nil {
goToLua(L, "existinfo", *exist)
}
// Shortcut (mostly for prescript and postscript).
L.GetGlobal("input")
L.GetField(-1, "tags")
L.SetGlobal("i")
L.Pop(1)
L.GetGlobal("output")
L.GetField(-1, "tags")
L.SetGlobal("o")
L.Pop(1)
// Call the compiled script.
L.PushString(registryIndex)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.PushString(code)
if L.IsTable(-2) {
L.GetTable(-2)
if L.IsFunction(-1) {
err := L.Call(0, 0)
if err != nil {
L.SetTop(0)
return fmt.Errorf("%s", err)
}
} else {
L.Pop(1)
}
} else {
L.Pop(1)
}
L.Pop(1)
// Allow tags to be numbers for convenience.
outputNumbersToStrings(L)
L.GetGlobal("output")
err = luar.LuaToGo(L, -1, &output)
L.Pop(1)
return err
}
// LoadConfig parses the Lua file pointed by 'config' and stores it to options.
func LoadConfig(config string, options *Options) {
L := MakeSandbox(log.Println)
defer L.Close()
err := L.DoFile(config)
if err != nil {
log.Fatalf("error loading config: %s", err)
}
L.GetGlobal("_G")
err = luar.LuaToGo(L, -1, options)
L.Pop(1)
if err != nil {
log.Fatalf("error passing config to go: %s", err)
}
}
// PrintScriptHelp runs 'script' to print the result of the 'help()' function calls.
// The script does not actually do anything.
func PrintScriptHelp(script string) {
L := MakeSandbox(log.Println)
// Scripts expect to receive "input", "output", "i" and "o", even if empty.
input := inputInfo{}
output := outputInfo{}
goToLua(L, "input", input)
goToLua(L, "output", output)
L.GetGlobal("input")
L.GetField(-1, "tags")
L.SetGlobal("i")
L.Pop(1)
L.GetGlobal("output")
L.GetField(-1, "tags")
L.SetGlobal("o")
L.Pop(1)
err := L.DoFile(script)
if err != nil {
log.Fatalf("error parsing script: %s", err)
}
}