mihomo/rules/common/domain.go

47 lines
778 B
Go
Raw Normal View History

package common
2018-09-09 15:01:46 +08:00
import (
2022-06-18 18:13:54 +08:00
"golang.org/x/net/idna"
"strings"
2018-09-09 15:01:46 +08:00
C "github.com/Dreamacro/clash/constant"
)
type Domain struct {
2022-03-13 01:21:23 +08:00
*Base
2022-06-18 18:13:54 +08:00
domain string
rawDomain string
adapter string
2018-09-09 15:01:46 +08:00
}
func (d *Domain) RuleType() C.RuleType {
return C.Domain
}
func (d *Domain) Match(metadata *C.Metadata) bool {
2018-09-30 12:25:52 +08:00
if metadata.AddrType != C.AtypDomainName {
2018-09-09 15:01:46 +08:00
return false
}
2018-09-30 12:25:52 +08:00
return metadata.Host == d.domain
2018-09-09 15:01:46 +08:00
}
func (d *Domain) Adapter() string {
return d.adapter
}
func (d *Domain) Payload() string {
2022-06-18 18:13:54 +08:00
return d.rawDomain
2018-09-09 15:01:46 +08:00
}
2022-03-13 01:21:23 +08:00
func NewDomain(domain string, adapter string) *Domain {
2022-06-18 18:13:54 +08:00
actualDomain, _ := idna.ToASCII(domain)
2018-09-09 15:01:46 +08:00
return &Domain{
2022-06-18 18:13:54 +08:00
Base: &Base{},
domain: strings.ToLower(actualDomain),
adapter: adapter,
rawDomain: domain,
2018-09-09 15:01:46 +08:00
}
}
2022-03-13 01:21:23 +08:00
var _ C.Rule = (*Domain)(nil)