Files
ctrld/internal/rulematcher/domain.go
Cuong Manh Le 3afdaef6e6 refactor: extract rule matching logic into internal/rulematcher package
Extract DNS policy rule matching logic from dns_proxy.go into a dedicated
internal/rulematcher package to improve code organization and maintainability.

The new package provides:
- RuleMatcher interface for extensible rule matching
- NetworkRuleMatcher for IP-based network rules
- MacRuleMatcher for MAC address-based rules
- DomainRuleMatcher for domain/wildcard rules
- Comprehensive unit tests for all matchers

This refactoring improves:
- Separation of concerns between DNS proxy and rule matching
- Testability with isolated rule matcher components
- Reusability of rule matching logic across the codebase
- Maintainability with focused, single-responsibility modules
2025-10-09 19:12:06 +07:00

37 lines
967 B
Go

package rulematcher
import (
"context"
)
// DomainRuleMatcher handles matching of domain-based rules
type DomainRuleMatcher struct{}
// Type returns the rule type for domain matcher
func (d *DomainRuleMatcher) Type() RuleType {
return RuleTypeDomain
}
// Match evaluates domain rules against the requested domain
func (d *DomainRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
if req.Policy == nil || len(req.Policy.Rules) == 0 {
return &MatchResult{Matched: false, RuleType: RuleTypeDomain}
}
for _, rule := range req.Policy.Rules {
// There's only one entry per rule, config validation ensures this.
for source, targets := range rule {
if source == req.Domain || wildcardMatches(source, req.Domain) {
return &MatchResult{
Matched: true,
Targets: targets,
MatchedRule: source,
RuleType: RuleTypeDomain,
}
}
}
}
return &MatchResult{Matched: false, RuleType: RuleTypeDomain}
}