From f6c486327fff54d89a8ad298543a5f48503e5760 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 30 Nov 2015 12:46:09 +0000 Subject: [PATCH] faster check in field rule --- app/router/rules/config/json/fieldrule.go | 2 ++ app/router/rules/config/json/fieldrule_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/router/rules/config/json/fieldrule.go b/app/router/rules/config/json/fieldrule.go index caeaf0a32..710c83236 100644 --- a/app/router/rules/config/json/fieldrule.go +++ b/app/router/rules/config/json/fieldrule.go @@ -59,6 +59,7 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool { for _, domain := range *this.Domain { if strings.Contains(address.Domain(), domain) { foundMatch = true + break } } if !foundMatch { @@ -74,6 +75,7 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool { for _, ipnet := range this.IP { if ipnet.Contains(address.IP()) { foundMatch = true + break } } if !foundMatch { diff --git a/app/router/rules/config/json/fieldrule_test.go b/app/router/rules/config/json/fieldrule_test.go index 176804dde..7e3345f21 100644 --- a/app/router/rules/config/json/fieldrule_test.go +++ b/app/router/rules/config/json/fieldrule_test.go @@ -65,6 +65,19 @@ func TestIPMatching(t *testing.T) { assert.Bool(rule.Apply(dest)).IsTrue() } +func TestIPListMatching(t *testing.T) { + assert := unit.Assert(t) + + rawJson := `{ + "type": "field", + "ip": ["10.0.0.0/8", "192.168.0.0/16"], + "tag": "test" + }` + rule := parseRule([]byte(rawJson)) + dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{192, 168, 1, 1}, 80)) + assert.Bool(rule.Apply(dest)).IsTrue() +} + func TestPortNotMatching(t *testing.T) { assert := unit.Assert(t)