2024-03-07 23:32:07 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-05-15 10:44:56 +08:00
|
|
|
|
|
|
|
"github.com/dlclark/regexp2"
|
2024-03-07 23:32:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DomainRegex struct {
|
|
|
|
*Base
|
2024-05-15 10:44:56 +08:00
|
|
|
regex *regexp2.Regexp
|
2024-03-07 23:32:07 +08:00
|
|
|
adapter string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DomainRegex) RuleType() C.RuleType {
|
|
|
|
return C.DomainRegex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) {
|
|
|
|
domain := metadata.RuleHost()
|
2024-05-15 10:44:56 +08:00
|
|
|
match, _ := dr.regex.MatchString(domain)
|
|
|
|
return match, dr.adapter
|
2024-03-07 23:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DomainRegex) Adapter() string {
|
|
|
|
return dr.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DomainRegex) Payload() string {
|
2024-03-08 22:42:48 +08:00
|
|
|
return dr.regex.String()
|
2024-03-07 23:32:07 +08:00
|
|
|
}
|
|
|
|
|
2024-03-08 22:42:48 +08:00
|
|
|
func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
|
2024-05-15 10:44:56 +08:00
|
|
|
r, err := regexp2.Compile(regex, regexp2.IgnoreCase)
|
2024-03-08 22:42:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-07 23:32:07 +08:00
|
|
|
return &DomainRegex{
|
|
|
|
Base: &Base{},
|
2024-03-08 22:42:48 +08:00
|
|
|
regex: r,
|
2024-03-07 23:32:07 +08:00
|
|
|
adapter: adapter,
|
2024-03-08 22:42:48 +08:00
|
|
|
}, nil
|
2024-03-07 23:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//var _ C.Rule = (*DomainRegex)(nil)
|