mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-05-27 12:52:27 +02:00
6294ba4028
Implement MatchingEngine in internal/rulematcher package to enable configurable DNS policy rule evaluation order and behavior. New components: - MatchingConfig: Configuration for rule order and stop behavior - MatchingEngine: Orchestrates rule matching with configurable order - MatchingResult: Standardized result structure - DefaultMatchingConfig(): Maintains backward compatibility Key features: - Configurable rule evaluation order (e.g., domain-first, MAC-first) - StopOnFirstMatch configuration option - Graceful handling of invalid rule types - Comprehensive test coverage for all scenarios The engine supports custom matching strategies while preserving the default Networks → Macs → Domains order for backward compatibility. This enables future configuration-driven rule matching without breaking existing functionality.
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package rulematcher
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/Control-D-Inc/ctrld"
|
|
)
|
|
|
|
// RuleType represents the type of rule being matched
|
|
type RuleType string
|
|
|
|
const (
|
|
RuleTypeNetwork RuleType = "network"
|
|
RuleTypeMac RuleType = "mac"
|
|
RuleTypeDomain RuleType = "domain"
|
|
)
|
|
|
|
// RuleMatcher defines the interface for matching different types of rules
|
|
type RuleMatcher interface {
|
|
Match(ctx context.Context, request *MatchRequest) *MatchResult
|
|
Type() RuleType
|
|
}
|
|
|
|
// MatchRequest contains all the information needed for rule matching
|
|
type MatchRequest struct {
|
|
SourceIP net.IP
|
|
SourceMac string
|
|
Domain string
|
|
Policy *ctrld.ListenerPolicyConfig
|
|
Config *ctrld.Config
|
|
}
|
|
|
|
// MatchResult represents the result of a rule matching operation
|
|
type MatchResult struct {
|
|
Matched bool
|
|
Targets []string
|
|
MatchedRule string
|
|
RuleType RuleType
|
|
}
|
|
|
|
// MatchingConfig defines the configuration for rule matching behavior
|
|
type MatchingConfig struct {
|
|
Order []RuleType `json:"order" yaml:"order"`
|
|
StopOnFirstMatch bool `json:"stop_on_first_match" yaml:"stop_on_first_match"`
|
|
}
|
|
|
|
// DefaultMatchingConfig returns the default matching configuration
|
|
// This maintains backward compatibility with the current behavior
|
|
func DefaultMatchingConfig() *MatchingConfig {
|
|
return &MatchingConfig{
|
|
Order: []RuleType{RuleTypeNetwork, RuleTypeMac, RuleTypeDomain},
|
|
StopOnFirstMatch: true,
|
|
}
|
|
}
|