domain_suffix.go 816 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package common
  2. import (
  3. "strings"
  4. C "github.com/metacubex/mihomo/constant"
  5. "golang.org/x/net/idna"
  6. )
  7. type DomainSuffix struct {
  8. *Base
  9. suffix string
  10. adapter string
  11. }
  12. func (ds *DomainSuffix) RuleType() C.RuleType {
  13. return C.DomainSuffix
  14. }
  15. func (ds *DomainSuffix) Match(metadata *C.Metadata) (bool, string) {
  16. domain := metadata.RuleHost()
  17. return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix, ds.adapter
  18. }
  19. func (ds *DomainSuffix) Adapter() string {
  20. return ds.adapter
  21. }
  22. func (ds *DomainSuffix) Payload() string {
  23. return ds.suffix
  24. }
  25. func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
  26. punycode, _ := idna.ToASCII(strings.ToLower(suffix))
  27. return &DomainSuffix{
  28. Base: &Base{},
  29. suffix: punycode,
  30. adapter: adapter,
  31. }
  32. }
  33. //var _ C.Rule = (*DomainSuffix)(nil)