2025-02-08 06:23:36 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2025-02-12 00:34:14 +03:00
|
|
|
"strings"
|
2025-02-08 06:23:36 +03:00
|
|
|
|
|
|
|
"github.com/IGLOU-EU/go-wildcard/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
2025-02-12 00:34:14 +03:00
|
|
|
ID ID `yaml:"id"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Rule string `yaml:"rule"`
|
|
|
|
Enable bool `yaml:"enable"`
|
2025-02-08 06:23:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Rule) IsEnabled() bool {
|
|
|
|
return d.Enable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Rule) IsMatch(domainName string) bool {
|
|
|
|
switch d.Type {
|
|
|
|
case "wildcard":
|
|
|
|
return wildcard.Match(d.Rule, domainName)
|
|
|
|
case "regex":
|
|
|
|
ok, _ := regexp.MatchString(d.Rule, domainName)
|
|
|
|
return ok
|
2025-02-12 00:34:14 +03:00
|
|
|
case "domain":
|
2025-02-08 06:23:36 +03:00
|
|
|
return domainName == d.Rule
|
2025-02-12 00:34:14 +03:00
|
|
|
case "namespace":
|
|
|
|
if domainName == d.Rule {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.HasSuffix(domainName, "."+d.Rule)
|
2025-02-08 06:23:36 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|