fix: cannot route for sniffed domain with port (#542)

Co-authored-by: Sumire (菫) <151038614+sumire88@users.noreply.github.com>
This commit is contained in:
mzz
2024-06-16 14:47:02 +08:00
committed by GitHub
parent e6bb146593
commit df76e0d6b9
5 changed files with 25 additions and 9 deletions

View File

@ -100,11 +100,12 @@ func (n *AhocorasickSlimtrie) MatchDomainBitmap(domain string) (bitmap []uint32)
bitmap = make([]uint32, N) bitmap = make([]uint32, N)
domain = strings.ToLower(strings.TrimSuffix(domain, ".")) domain = strings.ToLower(strings.TrimSuffix(domain, "."))
// Domain should consist of 'a'-'z' and '.' and '-' // Domain should consist of 'a'-'z' and '.' and '-'
for _, b := range []byte(domain) { // NOTE: DO NOT VERIFY THE DOMAIN TO MATCH: https://github.com/daeuniverse/dae/issues/528
if !ahocorasick.IsValidChar(b) { // for _, b := range []byte(domain) {
return bitmap // if !ahocorasick.IsValidChar(b) {
} // return bitmap
} // }
// }
// Suffix matching. // Suffix matching.
suffixTrieDomain := ToSuffixTrieString("^" + domain) suffixTrieDomain := ToSuffixTrieString("^" + domain)
for _, i := range n.validTrieIndexes { for _, i := range n.validTrieIndexes {

View File

@ -6,11 +6,12 @@
package domain_matcher package domain_matcher
import ( import (
"math/rand"
"testing"
"github.com/daeuniverse/dae/common/consts" "github.com/daeuniverse/dae/common/consts"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"math/rand"
"testing"
) )
func TestAhocorasickSlimtrie(t *testing.T) { func TestAhocorasickSlimtrie(t *testing.T) {

View File

@ -60,7 +60,7 @@ func (s *Sniffer) SniffHttp() (d string, err error) {
continue continue
} }
if strings.EqualFold(string(key), "host") { if strings.EqualFold(string(key), "host") {
return strings.TrimSpace(string(value)), nil return string(value), nil
} }
} }
return "", ErrNotFound return "", ErrNotFound

View File

@ -75,7 +75,7 @@ func sniffGroup(sniffs ...sniff) (d string, err error) {
for _, sniffer := range sniffs { for _, sniffer := range sniffs {
d, err = sniffer() d, err = sniffer()
if err == nil { if err == nil {
return d, nil return NormalizeDomain(d), nil
} }
if err != ErrNotApplicable { if err != ErrNotApplicable {
return "", err return "", err

View File

@ -8,6 +8,8 @@ package sniffing
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"strings"
) )
var ( var (
@ -19,3 +21,15 @@ var (
func IsSniffingError(err error) bool { func IsSniffingError(err error) bool {
return errors.Is(err, Error) return errors.Is(err, Error)
} }
func NormalizeDomain(host string) string {
host = strings.ToLower(strings.TrimSpace(host))
if strings.HasSuffix(host, "]") {
// Sniffed domain may be like `[2606:4700:20::681a:d1f]`. We should remove the brackets.
return strings.Trim(host, "[]")
}
if domain, _, err := net.SplitHostPort(host); err == nil {
return domain
}
return strings.TrimSuffix(host, ".")
}