Skip to content

Commit

Permalink
fix: cannot route for sniffed domain with port (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: Sumire (菫) <151038614+sumire88@users.noreply.github.com>
  • Loading branch information
mzz2017 and sumire88 authored Jun 16, 2024
1 parent e6bb146 commit df76e0d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
11 changes: 6 additions & 5 deletions component/routing/domain_matcher/ahocorasick_slimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions component/routing/domain_matcher/ahocorasick_slimtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion component/sniffing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion component/sniffing/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions component/sniffing/sniffing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package sniffing
import (
"errors"
"fmt"
"net"
"strings"
)

var (
Expand All @@ -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, ".")
}

0 comments on commit df76e0d

Please sign in to comment.