From a68487c3ed997c22360ca66b118bb5919877bae1 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Sun, 17 Dec 2023 17:04:46 +0100 Subject: [PATCH] add slice support on matching --- config.yml | 20 +++++++++----------- dnsutils/message.go | 12 +++++++++++- pipeline.go | 12 ++++++------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/config.yml b/config.yml index c8f073b8..2eb519ca 100644 --- a/config.yml +++ b/config.yml @@ -124,25 +124,19 @@ pipelines: listen-port: 6000 routes: [ apple-txt ] - - name: godnsconnector-next - dnstap: - remote-address: 192.168.1.210 - remote-port: 6000 - - name: godnsconnector-next - dnstap: - remote-address: 192.168.1.210 - remote-port: 6000 - name: apple-txt dnsmessage: matching: include: dns.qtype: "TXT" - dns.qname: "^*.apple.com$" + dns.qname: + - "^*.apple.com$" + - "^*.google.com$" policy: "drop-unmatched" transforms: atags: - tags: [ "TXT:apple" ] - routes: [ outputfile,godnsconnector-next ] + tags: [ "TXT:apple", "TXT:google" ] + routes: [ outputfile, console ] - name: outputfile logfile: @@ -151,6 +145,10 @@ pipelines: max-files: 10 mode: flat-json + - name: console + stdout: + mode: text + ################################################ # list of supported collectors ################################################ diff --git a/dnsutils/message.go b/dnsutils/message.go index fa0bfef0..f0413669 100644 --- a/dnsutils/message.go +++ b/dnsutils/message.go @@ -866,7 +866,7 @@ func (dm *DNSMessage) Matching(matching map[string]interface{}, operator string) reflectedValue := reflect.ValueOf(value) switch operator { case MatchingModeInclude: - // regex support for string + // string if reflectedValue.Kind() == reflect.String { pattern := regexp.MustCompile(reflectedValue.Interface().(string)) if !pattern.MatchString(fieldValue.Interface().(string)) { @@ -874,8 +874,18 @@ func (dm *DNSMessage) Matching(matching map[string]interface{}, operator string) break } + // list + } else if reflectedValue.Kind() == reflect.Slice { + subMatch := false + for i := 0; i < reflectedValue.Len(); i++ { + pattern := regexp.MustCompile(reflectedValue.Index(i).Interface().(string)) + subMatch = subMatch || pattern.MatchString(fieldValue.Interface().(string)) + } + isMatch = subMatch + // other types } else if value != fieldValue.Interface() { + fmt.Println(reflectedValue.Kind()) isMatch = false break } diff --git a/pipeline.go b/pipeline.go index 19ce5a6b..53ce8436 100644 --- a/pipeline.go +++ b/pipeline.go @@ -73,7 +73,7 @@ func InitPipelines(mapLoggers map[string]dnsutils.Worker, mapCollectors map[stri // check if the name of each stanza is uniq for _, stanza := range config.Pipelines { if err := StanzaNameIsUniq(stanza.Name, config); err != nil { - panic(fmt.Sprintf("[main] - pipeline stanza with name=%s is duplicated", stanza.Name)) + panic(fmt.Sprintf("[pipeline] - stanza with name=%s is duplicated", stanza.Name)) } } @@ -81,7 +81,7 @@ func InitPipelines(mapLoggers map[string]dnsutils.Worker, mapCollectors map[stri for _, stanza := range config.Pipelines { for _, route := range stanza.Routes { if err := IsRouteExist(route, config); err != nil { - panic(fmt.Sprintf("[main] - pipeline stanza=%s route=%s doest not exist", stanza.Name, route)) + panic(fmt.Sprintf("[pipeline] - stanza=%s route=%s doest not exist", stanza.Name, route)) } } } @@ -186,16 +186,16 @@ func InitPipelines(mapLoggers map[string]dnsutils.Worker, mapCollectors map[stri // search in collectors if _, ok := mapCollectors[route]; ok { mapCollectors[stanza.Name].AddRoute(mapCollectors[route]) - logger.Info("[main] - pipeline routing stanza=%s to=%s", stanza.Name, route) + logger.Info("[pipeline] - routing stanza=%s to=%s", stanza.Name, route) } else if _, ok := mapLoggers[route]; ok { mapCollectors[stanza.Name].AddRoute(mapLoggers[route]) - logger.Info("[main] - pipeline routing stanza=%s to=%s", stanza.Name, route) + logger.Info("[pipeline] - routing stanza=%s to=%s", stanza.Name, route) } else { - panic(fmt.Sprintf("[main] - pipeline routing error with stanza=%s to=%s doest not exist", stanza.Name, route)) + panic(fmt.Sprintf("[pipeline] - routing error with stanza=%s to=%s doest not exist", stanza.Name, route)) } } } else { - logger.Info("main - stanza=%v doest not exist", stanza.Name) + logger.Info("[pipeline] - stanza=%v doest not exist", stanza.Name) } } }