v2ray-core/app/router/rules/config/json/fieldrule.go

94 lines
1.9 KiB
Go
Raw Normal View History

2015-11-21 20:43:40 +00:00
package json
import (
"encoding/json"
"errors"
"net"
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
)
type FieldRule struct {
Rule
Domain string
IP *net.IPNet
Port v2net.PortRange
Network v2net.NetworkList
}
func (this *FieldRule) Apply(dest v2net.Destination) bool {
address := dest.Address()
2015-11-22 21:14:27 +00:00
if len(this.Domain) > 0 {
if !address.IsDomain() || !strings.Contains(address.Domain(), this.Domain) {
2015-11-21 20:43:40 +00:00
return false
}
}
2015-11-22 21:14:27 +00:00
if this.IP != nil {
if !(address.IsIPv4() || address.IsIPv6()) || !this.IP.Contains(address.IP()) {
2015-11-21 20:43:40 +00:00
return false
}
}
if this.Port != nil {
port := address.Port()
if port < this.Port.From() || port > this.Port.To() {
return false
}
}
if this.Network != nil {
if !this.Network.HasNetwork(v2net.Network(dest.Network())) {
return false
}
}
return true
}
func (this *FieldRule) UnmarshalJSON(data []byte) error {
type RawFieldRule struct {
Rule
2015-11-22 21:14:27 +00:00
Domain string `json:"domain"`
IP string `json:"ip"`
Port *v2netjson.PortRange `json:"port"`
Network *v2netjson.NetworkList `json:"network"`
2015-11-21 20:43:40 +00:00
}
rawFieldRule := RawFieldRule{}
err := json.Unmarshal(data, &rawFieldRule)
if err != nil {
return err
}
this.Type = rawFieldRule.Type
this.OutboundTag = rawFieldRule.OutboundTag
2015-11-22 21:14:27 +00:00
hasField := false
if len(rawFieldRule.Domain) > 0 {
this.Domain = rawFieldRule.Domain
hasField = true
}
if len(rawFieldRule.IP) > 0 {
_, ipNet, err := net.ParseCIDR(rawFieldRule.IP)
if err != nil {
return errors.New("Invalid IP range in router rule: " + err.Error())
}
this.IP = ipNet
hasField = true
2015-11-21 20:43:40 +00:00
}
if rawFieldRule.Port != nil {
this.Port = rawFieldRule.Port
2015-11-22 21:14:27 +00:00
hasField = true
2015-11-21 20:43:40 +00:00
}
if rawFieldRule.Network != nil {
this.Network = rawFieldRule.Network
2015-11-22 21:14:27 +00:00
hasField = true
}
if !hasField {
return errors.New("This rule has no effective fields.")
2015-11-21 20:43:40 +00:00
}
return nil
}