Allow to set static to a pointer and add IsIPV6 helper (#1540)

* Allow to set static to a pointer and add IsIPV6 helper
This commit is contained in:
AlteredCoder 2022-05-19 16:28:25 +02:00 committed by GitHub
parent fe09737d80
commit 1e1741aa45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View file

@ -58,6 +58,7 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
"XMLGetAttributeValue": XMLGetAttributeValue,
"XMLGetNodeValue": XMLGetNodeValue,
"IpToRange": IpToRange,
"IsIPV6": IsIPV6,
}
for k, v := range ctx {
ExprLib[k] = v
@ -180,6 +181,17 @@ func IpInRange(ip string, ipRange string) bool {
return false
}
func IsIPV6(ip string) bool {
ipParsed := net.ParseIP(ip)
if ipParsed == nil {
log.Debugf("'%s' is not a valid IP", ip)
return false
}
// If it's a valid IP and can't be converted to IPv4 then it is an IPv6
return ipParsed.To4() == nil
}
func IpToRange(ip string, cidr string) string {
cidr = strings.TrimPrefix(cidr, "/")
mask, err := strconv.Atoi(cidr)

View file

@ -17,7 +17,6 @@ import (
"strconv"
"github.com/davecgh/go-spew/spew"
"github.com/mohae/deepcopy"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
@ -38,7 +37,7 @@ func SetTargetByName(target string, value string, evt *types.Event) bool {
log.Debugf("setting target %s to %s", target, value)
defer func() {
if r := recover(); r != nil {
log.Errorf("Runtime error while trying to set '%s' in %s : %+v", target, spew.Sdump(evt), r)
log.Errorf("Runtime error while trying to set '%s': %+v", target, r)
return
}
}()
@ -65,11 +64,17 @@ func SetTargetByName(target string, value string, evt *types.Event) bool {
case reflect.Struct:
tmp := iter.FieldByName(f)
if !tmp.IsValid() {
log.Debugf("%s IsValid false", f)
log.Debugf("'%s' is not a valid target because '%s' is not valid", target, f)
return false
}
if tmp.Kind() == reflect.Ptr {
tmp = reflect.Indirect(tmp)
}
iter = tmp
break
case reflect.Ptr:
tmp := iter.Elem()
iter = reflect.Indirect(tmp.FieldByName(f))
default:
log.Errorf("unexpected type %s in '%s'", iter.Kind(), target)
return false