Files
EdgeNode/internal/waf/action_utils.go

53 lines
1.2 KiB
Go
Raw Permalink Normal View History

2020-10-08 15:06:42 +08:00
package waf
import (
2021-07-18 15:51:49 +08:00
"encoding/json"
2020-10-08 15:06:42 +08:00
"reflect"
"sync/atomic"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/iwind/TeaGo/maps"
2020-10-08 15:06:42 +08:00
)
var seedActionId int64 = 1
2020-10-08 15:06:42 +08:00
func FindActionInstance(action ActionString, options maps.Map) ActionInterface {
for _, def := range AllActions {
if def.Code == action {
if def.Type != nil {
// create new instance
var ptrValue = reflect.New(def.Type)
var instance = ptrValue.Interface().(ActionInterface)
instance.SetActionId(atomic.AddInt64(&seedActionId, 1))
2020-10-08 15:06:42 +08:00
if len(options) > 0 {
2021-07-18 15:51:49 +08:00
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())
2020-10-08 15:06:42 +08:00
}
}
}
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 ""
}