2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2022-07-02 14:55:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-11 10:44:59 +00:00
|
|
|
"github.com/sagernet/sing/common/domain"
|
2022-07-02 14:55:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ RuleItem = (*DomainItem)(nil)
|
|
|
|
|
|
|
|
type DomainItem struct {
|
|
|
|
matcher *domain.Matcher
|
2022-07-04 07:51:41 +00:00
|
|
|
description string
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDomainItem(domains []string, domainSuffixes []string) *DomainItem {
|
|
|
|
var description string
|
|
|
|
if dLen := len(domains); dLen > 0 {
|
|
|
|
if dLen == 1 {
|
|
|
|
description = "domain=" + domains[0]
|
|
|
|
} else if dLen > 3 {
|
|
|
|
description = "domain=[" + strings.Join(domains[:3], " ") + "...]"
|
|
|
|
} else {
|
|
|
|
description = "domain=[" + strings.Join(domains, " ") + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dsLen := len(domainSuffixes); dsLen > 0 {
|
|
|
|
if len(description) > 0 {
|
|
|
|
description += " "
|
|
|
|
}
|
|
|
|
if dsLen == 1 {
|
2024-02-01 02:42:12 +00:00
|
|
|
description += "domain_suffix=" + domainSuffixes[0]
|
2022-07-02 14:55:10 +00:00
|
|
|
} else if dsLen > 3 {
|
2024-02-01 02:42:12 +00:00
|
|
|
description += "domain_suffix=[" + strings.Join(domainSuffixes[:3], " ") + "...]"
|
2022-07-02 14:55:10 +00:00
|
|
|
} else {
|
2024-02-01 02:42:12 +00:00
|
|
|
description += "domain_suffix=[" + strings.Join(domainSuffixes, " ") + "]"
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &DomainItem{
|
2024-07-17 09:57:35 +00:00
|
|
|
domain.NewMatcher(domains, domainSuffixes, false),
|
2022-07-04 07:51:41 +00:00
|
|
|
description,
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 05:24:12 +00:00
|
|
|
func NewRawDomainItem(matcher *domain.Matcher) *DomainItem {
|
|
|
|
return &DomainItem{
|
|
|
|
matcher,
|
|
|
|
"domain/domain_suffix=<binary>",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
func (r *DomainItem) Match(metadata *adapter.InboundContext) bool {
|
|
|
|
var domainHost string
|
|
|
|
if metadata.Domain != "" {
|
|
|
|
domainHost = metadata.Domain
|
|
|
|
} else {
|
|
|
|
domainHost = metadata.Destination.Fqdn
|
|
|
|
}
|
|
|
|
if domainHost == "" {
|
|
|
|
return false
|
|
|
|
}
|
2023-02-02 07:14:43 +00:00
|
|
|
return r.matcher.Match(strings.ToLower(domainHost))
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DomainItem) String() string {
|
|
|
|
return r.description
|
|
|
|
}
|