From 6c766190cb7338f9a04a0d782dd1372be2b453c8 Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Sat, 3 Dec 2022 11:54:41 +0700 Subject: [PATCH] add IsAny method for matchers package --- internal/runner/validator.go | 2 +- pkg/matchers/fuzz.go | 9 --------- pkg/matchers/matchers.go | 31 +++++++++++++++++++++++++++++++ pkg/matchers/regex.go | 12 ------------ pkg/teler/teler.go | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) delete mode 100644 pkg/matchers/fuzz.go create mode 100644 pkg/matchers/matchers.go delete mode 100644 pkg/matchers/regex.go diff --git a/internal/runner/validator.go b/internal/runner/validator.go index 9837e652..f39669c8 100644 --- a/internal/runner/validator.go +++ b/internal/runner/validator.go @@ -103,7 +103,7 @@ func customs(options *common.Options) { matchers.IsBlank(rules[j].Element, "Custom threat rules element") elm := fmt.Sprint("$", rules[j].Element) - if !matchers.IsMatch(fmt.Sprint(`\`, elm), cfg.Logformat) { + if !matchers.IsAny(elm, cfg.Logformat) { err = strings.Replace(errors.ErrNoElement, ":element", elm, -1) err = strings.Replace(err, ":category", custom[i].Name, -1) diff --git a/pkg/matchers/fuzz.go b/pkg/matchers/fuzz.go deleted file mode 100644 index b4038365..00000000 --- a/pkg/matchers/fuzz.go +++ /dev/null @@ -1,9 +0,0 @@ -package matchers - -import "github.com/sahilm/fuzzy" - -func IsMatchFuzz(pattern string, s []string) bool { - matches := fuzzy.Find(pattern, s) - - return len(matches) > 0 -} diff --git a/pkg/matchers/matchers.go b/pkg/matchers/matchers.go new file mode 100644 index 00000000..35d3a1af --- /dev/null +++ b/pkg/matchers/matchers.go @@ -0,0 +1,31 @@ +package matchers + +import ( + "regexp" + "strings" + + "github.com/sahilm/fuzzy" +) + +func IsAny(substr string, s string) bool { + if strings.Index(s, substr) > 0 { + return false + } + + return true +} + +func IsMatch(pattern string, s string) bool { + defer func() { + _ = recover() + }() + + re := regexp.MustCompile(pattern) + return re.FindString(s) != "" +} + +func IsMatchFuzz(pattern string, s []string) bool { + matches := fuzzy.Find(pattern, s) + + return len(matches) > 0 +} diff --git a/pkg/matchers/regex.go b/pkg/matchers/regex.go deleted file mode 100644 index 5ba44a17..00000000 --- a/pkg/matchers/regex.go +++ /dev/null @@ -1,12 +0,0 @@ -package matchers - -import "regexp" - -func IsMatch(pattern string, s string) bool { - defer func() { - _ = recover() - }() - - re := regexp.MustCompile(pattern) - return re.FindString(s) != "" -} diff --git a/pkg/teler/teler.go b/pkg/teler/teler.go index 9124223a..ef50dd39 100644 --- a/pkg/teler/teler.go +++ b/pkg/teler/teler.go @@ -229,7 +229,7 @@ func Analyze(options *common.Options, logs *gonx.Entry) (bool, map[string]string cont = strings.ReplaceAll(cont, `.%EXT%`, ext) } - match = matchers.IsMatch(trimFirst(req.Path), cont) + match = matchers.IsAny(trimFirst(req.Path), cont) } }