Files
EdgeNode/internal/waf/action_utils.go
2021-07-18 15:51:49 +08:00

48 lines
1.0 KiB
Go

package waf
import (
"encoding/json"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/iwind/TeaGo/maps"
"reflect"
)
func FindActionInstance(action ActionString, options maps.Map) ActionInterface {
for _, def := range AllActions {
if def.Code == action {
if def.Type != nil {
// create new instance
ptrValue := reflect.New(def.Type)
instance := ptrValue.Interface().(ActionInterface)
if len(options) > 0 {
optionsJSON, err := json.Marshal(options)
if err != nil {
remotelogs.Error("WAF_FindActionInstance", "encode options to json failed: "+err.Error())
} else {
err = json.Unmarshal(optionsJSON, instance)
if err != nil {
remotelogs.Error("WAF_FindActionInstance", "decode options from json failed: "+err.Error())
}
}
}
return instance
}
// return shared instance
return def.Instance
}
}
return nil
}
func FindActionName(action ActionString) string {
for _, def := range AllActions {
if def.Code == action {
return def.Name
}
}
return ""
}