mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
refactor router rules
This commit is contained in:
parent
5e2583ec8d
commit
5df2a1c6e6
@ -1,25 +1,14 @@
|
||||
// +build json
|
||||
|
||||
package rules
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
func NewChinaIPRule(tag string) *Rule {
|
||||
return &Rule{
|
||||
Tag: rawRule.OutboundTag,
|
||||
Tag: tag,
|
||||
Condition: NewIPv4Matcher(chinaIPNet),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
@ -19,9 +17,7 @@ func makeDestination(ip string) v2net.Destination {
|
||||
func TestChinaIP(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
"type": "chinaip"
|
||||
}`))
|
||||
rule := NewChinaIPRule("tag")
|
||||
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
|
||||
|
@ -1,23 +1,10 @@
|
||||
// +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
|
||||
}
|
||||
func NewChinaSitesRule(tag string) *Rule {
|
||||
return &Rule{
|
||||
Tag: rawRule.OutboundTag,
|
||||
Tag: tag,
|
||||
Condition: chinaSitesConds,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
@ -18,9 +16,7 @@ func makeDomainDestination(domain string) v2net.Destination {
|
||||
func TestChinaSites(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
"type": "chinasites"
|
||||
}`))
|
||||
rule := NewChinaSitesRule("tag")
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user