示例#1
0
文件: probe.go 项目: hackerlist/monty
func (p ProbeRunner) Run() {
	var err error
	var state *lua.State
	var fun *luar.LuaObject
	var res interface{}

	r := p.Error

	args := make(map[string]interface{})

	if err = json.Unmarshal([]byte(p.Probe.Arguments), &args); err != nil {
		goto out
	}

	state = mkstate()
	defer state.Close()

	err = state.DoString(fmt.Sprintf("fun = function(args) %s end", p.Script.Code))
	if err != nil {
		goto out
	}

	state.GetGlobal("fun")
	fun = luar.NewLuaObject(state, -1)

	if res, err = fun.Call(args); err != nil {
		goto out
	} else if res == nil {
		// if nil, that means no error. just go to out.
		goto out
	} else if status, ok := res.(string); !ok {
		// if it's not a string, that's bad. luar seems to convert go errors to strings..
		err = fmt.Errorf("script resulted in non-string return value %q", res)
	} else if status != "" {
		// if the string is not empty that's an error.
		err = fmt.Errorf("probe error: %s", status)
	}

out:
	r <- err
	close(r)
}