github.com/sagernet/sing-box@v1.9.0-rc.20/route/rule_item_domain.go (about) 1 package route 2 3 import ( 4 "strings" 5 6 "github.com/sagernet/sing-box/adapter" 7 "github.com/sagernet/sing/common/domain" 8 ) 9 10 var _ RuleItem = (*DomainItem)(nil) 11 12 type DomainItem struct { 13 matcher *domain.Matcher 14 description string 15 } 16 17 func NewDomainItem(domains []string, domainSuffixes []string) *DomainItem { 18 var description string 19 if dLen := len(domains); dLen > 0 { 20 if dLen == 1 { 21 description = "domain=" + domains[0] 22 } else if dLen > 3 { 23 description = "domain=[" + strings.Join(domains[:3], " ") + "...]" 24 } else { 25 description = "domain=[" + strings.Join(domains, " ") + "]" 26 } 27 } 28 if dsLen := len(domainSuffixes); dsLen > 0 { 29 if len(description) > 0 { 30 description += " " 31 } 32 if dsLen == 1 { 33 description += "domain_suffix=" + domainSuffixes[0] 34 } else if dsLen > 3 { 35 description += "domain_suffix=[" + strings.Join(domainSuffixes[:3], " ") + "...]" 36 } else { 37 description += "domain_suffix=[" + strings.Join(domainSuffixes, " ") + "]" 38 } 39 } 40 return &DomainItem{ 41 domain.NewMatcher(domains, domainSuffixes), 42 description, 43 } 44 } 45 46 func NewRawDomainItem(matcher *domain.Matcher) *DomainItem { 47 return &DomainItem{ 48 matcher, 49 "domain/domain_suffix=<binary>", 50 } 51 } 52 53 func (r *DomainItem) Match(metadata *adapter.InboundContext) bool { 54 var domainHost string 55 if metadata.Domain != "" { 56 domainHost = metadata.Domain 57 } else { 58 domainHost = metadata.Destination.Fqdn 59 } 60 if domainHost == "" { 61 return false 62 } 63 return r.matcher.Match(strings.ToLower(domainHost)) 64 } 65 66 func (r *DomainItem) String() string { 67 return r.description 68 }