mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-05-27 12:52:27 +02:00
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.
This commit is contained in:
committed by
Cuong Manh Le
parent
9b1f102315
commit
6aedc2b2d3
@@ -317,7 +317,7 @@ func (lc *ListenerConfig) IsDirectDnsListener() bool {
|
|||||||
|
|
||||||
// MatchingConfig defines the configuration for rule matching behavior
|
// MatchingConfig defines the configuration for rule matching behavior
|
||||||
type MatchingConfig struct {
|
type MatchingConfig struct {
|
||||||
Order []string `mapstructure:"order" toml:"order,omitempty" json:"order" yaml:"order"`
|
Order []string `mapstructure:"order" toml:"order,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenerPolicyConfig specifies the policy rules for ctrld to filter incoming requests.
|
// ListenerPolicyConfig specifies the policy rules for ctrld to filter incoming requests.
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ import (
|
|||||||
// DomainRuleMatcher handles matching of domain-based rules
|
// DomainRuleMatcher handles matching of domain-based rules
|
||||||
type DomainRuleMatcher struct{}
|
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
|
// Match evaluates domain rules against the requested domain
|
||||||
func (d *DomainRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
func (d *DomainRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
||||||
if req.Policy == nil || len(req.Policy.Rules) == 0 {
|
if req.Policy == nil || len(req.Policy.Rules) == 0 {
|
||||||
|
|||||||
@@ -1,3 +1,40 @@
|
|||||||
|
// Package rulematcher provides a flexible rule matching engine for DNS request routing.
|
||||||
|
//
|
||||||
|
// The rulematcher package implements a policy-based DNS routing system that allows
|
||||||
|
// configuring different types of rules to determine which upstream DNS servers should
|
||||||
|
// handle specific requests. It supports three types of rules:
|
||||||
|
//
|
||||||
|
// - Network rules: Match requests based on source IP address ranges
|
||||||
|
// - MAC rules: Match requests based on source MAC addresses
|
||||||
|
// - Domain rules: Match requests based on requested domain names
|
||||||
|
//
|
||||||
|
// The matching engine uses a configurable priority order to determine which rules
|
||||||
|
// take precedence when multiple rules match. By default, the priority order is:
|
||||||
|
// Network -> MAC -> Domain, with Domain rules having the highest priority and
|
||||||
|
// overriding all other matches.
|
||||||
|
//
|
||||||
|
// Example usage:
|
||||||
|
//
|
||||||
|
// config := &MatchingConfig{
|
||||||
|
// Order: []RuleType{RuleTypeNetwork, RuleTypeMac, RuleTypeDomain},
|
||||||
|
// }
|
||||||
|
// engine := NewMatchingEngine(config)
|
||||||
|
//
|
||||||
|
// request := &MatchRequest{
|
||||||
|
// SourceIP: net.ParseIP("192.168.1.100"),
|
||||||
|
// SourceMac: "aa:bb:cc:dd:ee:ff",
|
||||||
|
// Domain: "example.com",
|
||||||
|
// Policy: policyConfig,
|
||||||
|
// Config: appConfig,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// result := engine.FindUpstreams(ctx, request)
|
||||||
|
// if result.Matched {
|
||||||
|
// // Use result.Upstreams to route the request
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// The package maintains backward compatibility with existing behavior while
|
||||||
|
// providing a clean, extensible interface for adding new rule types.
|
||||||
package rulematcher
|
package rulematcher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -8,11 +8,6 @@ import (
|
|||||||
// MacRuleMatcher handles matching of MAC address-based rules
|
// MacRuleMatcher handles matching of MAC address-based rules
|
||||||
type MacRuleMatcher struct{}
|
type MacRuleMatcher struct{}
|
||||||
|
|
||||||
// Type returns the rule type for MAC matcher
|
|
||||||
func (m *MacRuleMatcher) Type() RuleType {
|
|
||||||
return RuleTypeMac
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match evaluates MAC address rules against the source MAC address
|
// Match evaluates MAC address rules against the source MAC address
|
||||||
func (m *MacRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
func (m *MacRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
||||||
if req.Policy == nil || len(req.Policy.Macs) == 0 {
|
if req.Policy == nil || len(req.Policy.Macs) == 0 {
|
||||||
|
|||||||
@@ -8,11 +8,6 @@ import (
|
|||||||
// NetworkRuleMatcher handles matching of network-based rules
|
// NetworkRuleMatcher handles matching of network-based rules
|
||||||
type NetworkRuleMatcher struct{}
|
type NetworkRuleMatcher struct{}
|
||||||
|
|
||||||
// Type returns the rule type for network matcher
|
|
||||||
func (n *NetworkRuleMatcher) Type() RuleType {
|
|
||||||
return RuleTypeNetwork
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match evaluates network rules against the source IP address
|
// Match evaluates network rules against the source IP address
|
||||||
func (n *NetworkRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
func (n *NetworkRuleMatcher) Match(ctx context.Context, req *MatchRequest) *MatchResult {
|
||||||
if req.Policy == nil || len(req.Policy.Networks) == 0 {
|
if req.Policy == nil || len(req.Policy.Networks) == 0 {
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ const (
|
|||||||
// RuleMatcher defines the interface for matching different types of rules
|
// RuleMatcher defines the interface for matching different types of rules
|
||||||
type RuleMatcher interface {
|
type RuleMatcher interface {
|
||||||
Match(ctx context.Context, request *MatchRequest) *MatchResult
|
Match(ctx context.Context, request *MatchRequest) *MatchResult
|
||||||
Type() RuleType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchRequest contains all the information needed for rule matching
|
// MatchRequest contains all the information needed for rule matching
|
||||||
|
|||||||
Reference in New Issue
Block a user