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)
domain = strings.ToLower(strings.TrimSuffix(domain, "."))
// Domain should consist of 'a'-'z' and '.' and '-'
for _, b := range []byte(domain) {
if !ahocorasick.IsValidChar(b) {
return bitmap
}
}
// NOTE: DO NOT VERIFY THE DOMAIN TO MATCH: https://github.com/daeuniverse/dae/issues/528
// for _, b := range []byte(domain) {
// if !ahocorasick.IsValidChar(b) {
// return bitmap
// }
// }
// Suffix matching.
suffixTrieDomain := ToSuffixTrieString("^" + domain)
for _, i := range n.validTrieIndexes {

View File

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

View File

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

View File

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

View File

@ -8,6 +8,8 @@ package sniffing
import (
"errors"
"fmt"
"net"
"strings"
)
var (
@ -19,3 +21,15 @@ var (
func IsSniffingError(err error) bool {
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, ".")
}