Files
ctrld/internal/rulematcher/mac.go
Cuong Manh Le d42a78cba9 docs: add comprehensive package documentation for rulematcher
- Add detailed package documentation to engine.go explaining the rule matching
  system, supported rule types (Network, MAC, Domain), and priority ordering
- Include usage example demonstrating typical API usage patterns
- Remove unused Type() method from RuleMatcher interface and implementations
- Maintain backward compatibility while improving code documentation

The documentation explains the policy-based DNS routing system and how different
rule types interact with configurable priority ordering.
2025-10-09 19:12:06 +07:00

63 lines
1.5 KiB
Go

package rulematcher
import (
"context"
"strings"
)
// MacRuleMatcher handles matching of MAC address-based rules
type MacRuleMatcher struct{}
// Match evaluates MAC address rules against the source MAC address
func (m *MacRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
if req.Policy == nil || len(req.Policy.Macs) == 0 {
return &MatchResult{Matched: false, RuleType: RuleTypeMac}
}
for _, rule := range req.Policy.Macs {
for source, targets := range rule {
if source != "" && (strings.EqualFold(source, req.SourceMac) || wildcardMatches(strings.ToLower(source), strings.ToLower(req.SourceMac))) {
return &MatchResult{
Matched: true,
Targets: targets,
MatchedRule: source, // Return the original source from the rule
RuleType: RuleTypeMac,
}
}
}
}
return &MatchResult{Matched: false, RuleType: RuleTypeMac}
}
// wildcardMatches checks if a wildcard pattern matches a string
// This is copied from the original implementation to maintain compatibility
func wildcardMatches(wildcard, str string) bool {
if wildcard == "" {
return false
}
if wildcard == "*" {
return true
}
if !strings.Contains(wildcard, "*") {
return wildcard == str
}
parts := strings.Split(wildcard, "*")
if len(parts) != 2 {
return false
}
prefix := parts[0]
suffix := parts[1]
if prefix != "" && !strings.HasPrefix(str, prefix) {
return false
}
if suffix != "" && !strings.HasSuffix(str, suffix) {
return false
}
return true
}