소스 검색

faster check in field rule

Darien Raymond 10 년 전
부모
커밋
f6c486327f
2개의 변경된 파일15개의 추가작업 그리고 0개의 파일을 삭제
  1. 2 0
      app/router/rules/config/json/fieldrule.go
  2. 13 0
      app/router/rules/config/json/fieldrule_test.go

+ 2 - 0
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 {

+ 13 - 0
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)