github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/action_utils.go (about)

     1  package waf
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
     6  	"github.com/iwind/TeaGo/maps"
     7  	"reflect"
     8  	"sync/atomic"
     9  )
    10  
    11  var seedActionId int64 = 1
    12  
    13  func FindActionInstance(action ActionString, options maps.Map) ActionInterface {
    14  	for _, def := range AllActions {
    15  		if def.Code == action {
    16  			if def.Type != nil {
    17  				// create new instance
    18  				var ptrValue = reflect.New(def.Type)
    19  				var instance = ptrValue.Interface().(ActionInterface)
    20  				instance.SetActionId(atomic.AddInt64(&seedActionId, 1))
    21  
    22  				if len(options) > 0 {
    23  					optionsJSON, err := json.Marshal(options)
    24  					if err != nil {
    25  						remotelogs.Error("WAF_FindActionInstance", "encode options to json failed: "+err.Error())
    26  					} else {
    27  						err = json.Unmarshal(optionsJSON, instance)
    28  						if err != nil {
    29  							remotelogs.Error("WAF_FindActionInstance", "decode options from json failed: "+err.Error())
    30  						}
    31  					}
    32  				}
    33  
    34  				return instance
    35  			}
    36  
    37  			// return shared instance
    38  			return def.Instance
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  func FindActionName(action ActionString) string {
    45  	for _, def := range AllActions {
    46  		if def.Code == action {
    47  			return def.Name
    48  		}
    49  	}
    50  	return ""
    51  }