Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RuleSet #202

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ func (v *ConditionChan) Len() int {
return len(*v)
}

type OrConditionChan []Condition

func NewOrConditionChan() *OrConditionChan {
var condChan OrConditionChan = make([]Condition, 0, 8)
return &condChan
}

func (v *OrConditionChan) Add(cond Condition) *OrConditionChan {
*v = append(*v, cond)
return v
}

// Apply applies all conditions registered in this chan.
func (v *OrConditionChan) Apply(ctx routing.Context) bool {
for _, cond := range *v {
if cond.Apply(ctx) {
return true
}
}
return false
}

func (v *OrConditionChan) Len() int {
return len(*v)
}

var matcherTypeMap = map[Domain_Type]strmatcher.Type{
Domain_Plain: strmatcher.Substr,
Domain_Regex: strmatcher.Regex,
Expand Down
66 changes: 65 additions & 1 deletion app/router/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestRoutingRule(t *testing.T) {
}

for _, test := range cases {
cond, err := test.rule.BuildCondition()
cond, err := test.rule.BuildCondition(new(RulesetManager))
common.Must(err)

for _, subtest := range test.test {
Expand Down Expand Up @@ -444,3 +444,67 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
_ = matcher.Apply(ctx)
}
}

func TestRuleSet(t *testing.T) {
type ruleTest struct {
input routing.Context
output bool
}

cases := []struct {
rule *RoutingRule
manager *RulesetManager
test []ruleTest
}{
{
&RoutingRule{RuleSet: "ruleset1", SourcePortList: &net.PortList{Range: []*net.PortRange{{From: 1000, To: 10000}}}},
func() *RulesetManager {
r := NewRSManager()
r.Add("ruleset1", &RoutingRules{Rules: []*RoutingRule{
{InboundTag: []string{"in1"}},
}, Identifier: "ruleset1"})
return r
}(),
[]ruleTest{
{withInbound(&session.Inbound{Tag: "in1", Source: net.TCPDestination(net.LocalHostIP, 8972)}), true},
{withInbound(&session.Inbound{Tag: "in1", Source: net.TCPDestination(net.LocalHostIP, 972)}), false},
{withInbound(&session.Inbound{Tag: "in2", Source: net.TCPDestination(net.LocalHostIP, 8972)}), false},
{withInbound(&session.Inbound{Tag: "in2", Source: net.TCPDestination(net.LocalHostIP, 972)}), false},
},
},
{
&RoutingRule{RuleSet: "ruleset1", SourcePortList: &net.PortList{Range: []*net.PortRange{{From: 5000, To: 10000}}}},
func() *RulesetManager {
r := NewRSManager()
r.Add("ruleset1", &RoutingRules{Rules: []*RoutingRule{
{SourcePortList: &net.PortList{Range: []*net.PortRange{{From: 1000, To: 10000}}}, RuleSet: "ruleset2"},
}, Identifier: "ruleset1"})
r.Add("ruleset2", &RoutingRules{Rules: []*RoutingRule{
{InboundTag: []string{"in1"}},
}, Identifier: "ruleset2"})
return r
}(),
[]ruleTest{
{withInbound(&session.Inbound{Tag: "in1", Source: net.TCPDestination(net.LocalHostIP, 8972)}), true},
{withInbound(&session.Inbound{Tag: "in1", Source: net.TCPDestination(net.LocalHostIP, 972)}), false},
{withInbound(&session.Inbound{Tag: "in2", Source: net.TCPDestination(net.LocalHostIP, 8972)}), false},
{withInbound(&session.Inbound{Tag: "in2", Source: net.TCPDestination(net.LocalHostIP, 972)}), false},
{withInbound(&session.Inbound{Tag: "in1", Source: net.TCPDestination(net.LocalHostIP, 3972)}), false},
{withInbound(&session.Inbound{Tag: "in2", Source: net.TCPDestination(net.LocalHostIP, 3972)}), false},
},
},
}

for _, test := range cases {
cond, err := test.rule.BuildCondition(test.manager)
common.Must(err)

for _, subtest := range test.test {
actual := cond.Apply(subtest.input)
if actual != subtest.output {
t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
}
}
}

}
36 changes: 34 additions & 2 deletions app/router/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *Rule) Apply(ctx routing.Context) bool {
return r.Condition.Apply(ctx)
}

func (rr *RoutingRule) BuildCondition() (Condition, error) {
func (rr *RoutingRule) BuildCondition(rsm *RulesetManager) (*ConditionChan, error) {
conds := NewConditionChan()

if len(rr.Domain) > 0 {
Expand Down Expand Up @@ -138,7 +138,15 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
conds.Add(cond)
}

if conds.Len() == 0 {
if rr.RuleSet != "" {
if rsCond, err := rsm.getRuleSet(rr.RuleSet); err != nil {
return nil, err
} else {
conds.Add(rsCond)
}
}

if conds.Len() == 0 && rr.RuleSet == "" {
return nil, newError("this rule has no effective fields").AtWarning()
}

Expand All @@ -152,3 +160,27 @@ func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
ohm: ohm,
}, nil
}

func (rrs *RoutingRules) BuildCondition(rsm *RulesetManager) (Condition, error) {
conds := NewOrConditionChan()

for _, rr := range rrs.Rules {
if rr.GetTag() != "" || rr.GetBalancingTag() != "" {
newError("ignoring tag in rule set").AtWarning().WriteToLog()
}

if rr.RuleSet == rrs.Identifier {
return nil, newError("import cycle found for tag: " + rrs.Identifier)
}
cond, err := rr.BuildCondition(rsm)
if err != nil {
return nil, err
}
conds.Add(cond)
}
if conds.Len() == 0 {
return nil, newError("this rule set has no effective fields").AtWarning()
}

return conds, nil
}
Loading