mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
refactor router rules
This commit is contained in:
parent
5e2583ec8d
commit
5df2a1c6e6
@ -1,25 +1,14 @@
|
|||||||
// +build json
|
|
||||||
|
|
||||||
package rules
|
package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseChinaIPRule(data []byte) (*Rule, error) {
|
func NewChinaIPRule(tag string) *Rule {
|
||||||
rawRule := new(JsonRule)
|
|
||||||
err := json.Unmarshal(data, rawRule)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Router: Invalid router rule: ", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Rule{
|
return &Rule{
|
||||||
Tag: rawRule.OutboundTag,
|
Tag: tag,
|
||||||
Condition: NewIPv4Matcher(chinaIPNet),
|
Condition: NewIPv4Matcher(chinaIPNet),
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
19
app/router/rules/chinaip_json.go
Normal file
19
app/router/rules/chinaip_json.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseChinaIPRule(data []byte) (*Rule, error) {
|
||||||
|
rawRule := new(JsonRule)
|
||||||
|
err := json.Unmarshal(data, rawRule)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Router: Invalid router rule: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewChinaIPRule(rawRule.OutboundTag), nil
|
||||||
|
}
|
27
app/router/rules/chinaip_json_test.go
Normal file
27
app/router/rules/chinaip_json_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package rules_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/app/router/rules"
|
||||||
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChinaIPJson(t *testing.T) {
|
||||||
|
v2testing.Current(t)
|
||||||
|
|
||||||
|
rule := ParseRule([]byte(`{
|
||||||
|
"type": "chinaip",
|
||||||
|
"outboundTag": "x"
|
||||||
|
}`))
|
||||||
|
assert.StringLiteral(rule.Tag).Equals("x")
|
||||||
|
assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn
|
||||||
|
assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com
|
||||||
|
assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com
|
||||||
|
assert.Bool(rule.Apply(makeDestination("120.135.126.1"))).IsTrue()
|
||||||
|
|
||||||
|
assert.Bool(rule.Apply(makeDestination("8.8.8.8"))).IsFalse()
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
// +build json
|
|
||||||
|
|
||||||
package rules_test
|
package rules_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -19,9 +17,7 @@ func makeDestination(ip string) v2net.Destination {
|
|||||||
func TestChinaIP(t *testing.T) {
|
func TestChinaIP(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
rule := ParseRule([]byte(`{
|
rule := NewChinaIPRule("tag")
|
||||||
"type": "chinaip"
|
|
||||||
}`))
|
|
||||||
assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn
|
assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn
|
||||||
assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com
|
assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com
|
||||||
assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com
|
assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com
|
||||||
|
@ -1,23 +1,10 @@
|
|||||||
// +build json
|
|
||||||
|
|
||||||
package rules
|
package rules
|
||||||
|
|
||||||
import (
|
func NewChinaSitesRule(tag string) *Rule {
|
||||||
"encoding/json"
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func parseChinaSitesRule(data []byte) (*Rule, error) {
|
|
||||||
rawRule := new(JsonRule)
|
|
||||||
err := json.Unmarshal(data, rawRule)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Router: Invalid router rule: ", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Rule{
|
return &Rule{
|
||||||
Tag: rawRule.OutboundTag,
|
Tag: tag,
|
||||||
Condition: chinaSitesConds,
|
Condition: chinaSitesConds,
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
21
app/router/rules/chinasites_json.go
Normal file
21
app/router/rules/chinasites_json.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseChinaSitesRule(data []byte) (*Rule, error) {
|
||||||
|
rawRule := new(JsonRule)
|
||||||
|
err := json.Unmarshal(data, rawRule)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Router: Invalid router rule: ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rule{
|
||||||
|
Tag: rawRule.OutboundTag,
|
||||||
|
Condition: chinaSitesConds,
|
||||||
|
}, nil
|
||||||
|
}
|
27
app/router/rules/chinasites_json_test.go
Normal file
27
app/router/rules/chinasites_json_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package rules_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/app/router/rules"
|
||||||
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChinaSitesJson(t *testing.T) {
|
||||||
|
v2testing.Current(t)
|
||||||
|
|
||||||
|
rule := ParseRule([]byte(`{
|
||||||
|
"type": "chinasites",
|
||||||
|
"outboundTag": "y"
|
||||||
|
}`))
|
||||||
|
assert.StringLiteral(rule.Tag).Equals("y")
|
||||||
|
assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue()
|
||||||
|
assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue()
|
||||||
|
assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue()
|
||||||
|
assert.Bool(rule.Apply(makeDomainDestination("12306.cn"))).IsTrue()
|
||||||
|
|
||||||
|
assert.Bool(rule.Apply(makeDomainDestination("v2ray.com"))).IsFalse()
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
// +build json
|
|
||||||
|
|
||||||
package rules_test
|
package rules_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -18,9 +16,7 @@ func makeDomainDestination(domain string) v2net.Destination {
|
|||||||
func TestChinaSites(t *testing.T) {
|
func TestChinaSites(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
rule := ParseRule([]byte(`{
|
rule := NewChinaSitesRule("tag")
|
||||||
"type": "chinasites"
|
|
||||||
}`))
|
|
||||||
assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue()
|
assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue()
|
||||||
assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue()
|
assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue()
|
||||||
assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue()
|
assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue()
|
||||||
|
Loading…
Reference in New Issue
Block a user