1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

Fix field rule

This commit is contained in:
V2Ray 2015-11-22 22:14:27 +01:00
parent 1cdd3e6647
commit 790f6c038c
2 changed files with 53 additions and 13 deletions

View File

@ -20,14 +20,14 @@ type FieldRule struct {
func (this *FieldRule) Apply(dest v2net.Destination) bool { func (this *FieldRule) Apply(dest v2net.Destination) bool {
address := dest.Address() address := dest.Address()
if len(this.Domain) > 0 && address.IsDomain() { if len(this.Domain) > 0 {
if !strings.Contains(address.Domain(), this.Domain) { if !address.IsDomain() || !strings.Contains(address.Domain(), this.Domain) {
return false return false
} }
} }
if this.IP != nil && (address.IsIPv4() || address.IsIPv6()) { if this.IP != nil {
if !this.IP.Contains(address.IP()) { if !(address.IsIPv4() || address.IsIPv6()) || !this.IP.Contains(address.IP()) {
return false return false
} }
} }
@ -53,8 +53,8 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
Rule Rule
Domain string `json:"domain"` Domain string `json:"domain"`
IP string `json:"ip"` IP string `json:"ip"`
Port *v2netjson.PortRange Port *v2netjson.PortRange `json:"port"`
Network *v2netjson.NetworkList Network *v2netjson.NetworkList `json:"network"`
} }
rawFieldRule := RawFieldRule{} rawFieldRule := RawFieldRule{}
err := json.Unmarshal(data, &rawFieldRule) err := json.Unmarshal(data, &rawFieldRule)
@ -63,17 +63,31 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
} }
this.Type = rawFieldRule.Type this.Type = rawFieldRule.Type
this.OutboundTag = rawFieldRule.OutboundTag this.OutboundTag = rawFieldRule.OutboundTag
hasField := false
if len(rawFieldRule.Domain) > 0 {
this.Domain = rawFieldRule.Domain this.Domain = rawFieldRule.Domain
hasField = true
}
if len(rawFieldRule.IP) > 0 {
_, ipNet, err := net.ParseCIDR(rawFieldRule.IP) _, ipNet, err := net.ParseCIDR(rawFieldRule.IP)
if err != nil { if err != nil {
return errors.New("Invalid IP range in router rule: " + err.Error()) return errors.New("Invalid IP range in router rule: " + err.Error())
} }
this.IP = ipNet this.IP = ipNet
hasField = true
}
if rawFieldRule.Port != nil { if rawFieldRule.Port != nil {
this.Port = rawFieldRule.Port this.Port = rawFieldRule.Port
hasField = true
} }
if rawFieldRule.Network != nil { if rawFieldRule.Network != nil {
this.Network = rawFieldRule.Network this.Network = rawFieldRule.Network
hasField = true
}
if !hasField {
return errors.New("This rule has no effective fields.")
} }
return nil return nil
} }

View File

@ -43,3 +43,29 @@ func TestIPMatching(t *testing.T) {
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80)) dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
assert.Bool(rule.Apply(dest)).IsTrue() assert.Bool(rule.Apply(dest)).IsTrue()
} }
func TestPortNotMatching(t *testing.T) {
assert := unit.Assert(t)
rawJson := `{
"type": "field",
"port": "80-100",
"tag": "test"
}`
rule := parseRule([]byte(rawJson))
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
assert.Bool(rule.Apply(dest)).IsFalse()
}
func TestDomainNotMatching(t *testing.T) {
assert := unit.Assert(t)
rawJson := `{
"type": "field",
"domain": "google.com",
"tag": "test"
}`
rule := parseRule([]byte(rawJson))
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
assert.Bool(rule.Apply(dest)).IsFalse()
}