From d9133b251719f3f71d313256295609d62383e2c2 Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Fri, 31 Mar 2023 23:36:24 +0800 Subject: [PATCH] fix: problem that cannot use unsorted ip set in dns response routing --- component/routing/domain_matcher/ahocorasick_slimtrie.go | 2 -- config/parser.go | 2 +- pkg/trie/trie.go | 5 ++++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/component/routing/domain_matcher/ahocorasick_slimtrie.go b/component/routing/domain_matcher/ahocorasick_slimtrie.go index 908e17e..3f9fc78 100644 --- a/component/routing/domain_matcher/ahocorasick_slimtrie.go +++ b/component/routing/domain_matcher/ahocorasick_slimtrie.go @@ -12,7 +12,6 @@ import ( "github.com/sirupsen/logrus" "github.com/v2rayA/ahocorasick-domain" "regexp" - "sort" "strings" ) @@ -185,7 +184,6 @@ func (n *AhocorasickSlimtrie) Build() (err error) { continue } toBuild = ToSuffixTrieStrings(toBuild) - sort.Strings(toBuild) n.trie[i], err = trie.NewTrie(toBuild, ValidDomainChars) if err != nil { return err diff --git a/config/parser.go b/config/parser.go index 32f6895..8be8dc0 100644 --- a/config/parser.go +++ b/config/parser.go @@ -143,7 +143,7 @@ func ParamParser(to reflect.Value, section *config_parser.Section, ignoreType [] // Assign. "to" should have field "Rules". structField, ok := to.Type().FieldByName("Rules") if !ok || structField.Type != reflect.TypeOf([]*config_parser.RoutingRule{}) { - return fmt.Errorf("unexpected type: \"routing rule\": %v", itemVal.String(true, false, false)) + return fmt.Errorf("cannot use routing rule in this context: %v", itemVal.String(true, false, false)) } if structField.Tag.Get("mapstructure") != "_" { return fmt.Errorf("a []*RoutingRule field \"Rules\" with mapstructure:\"_\" is required in struct %v to parse section", to.Type().String()) diff --git a/pkg/trie/trie.go b/pkg/trie/trie.go index 18a84b4..31560df 100644 --- a/pkg/trie/trie.go +++ b/pkg/trie/trie.go @@ -5,8 +5,10 @@ package trie import ( "fmt" + "github.com/daeuniverse/dae/common" "github.com/daeuniverse/dae/common/bitlist" "math/bits" + "sort" ) type ValidChars struct { @@ -86,8 +88,9 @@ type Trie struct { // NewTrie creates a new *Trie struct, from a slice of sorted strings. func NewTrie(keys []string, chars *ValidChars) (*Trie, error) { - // Check chars. + keys = common.Deduplicate(keys) + sort.Strings(keys) for _, key := range keys { for _, c := range []byte(key) { if !chars.IsValidChar(c) {