2015-11-21 15:43:40 -05:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2015-12-07 05:58:43 -05:00
|
|
|
"regexp"
|
2015-11-21 15:43:40 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
|
|
|
)
|
|
|
|
|
2015-11-26 06:22:34 -05:00
|
|
|
type StringList []string
|
|
|
|
|
|
|
|
func NewStringList(str ...string) *StringList {
|
|
|
|
list := StringList(str)
|
|
|
|
return &list
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *StringList) UnmarshalJSON(data []byte) error {
|
|
|
|
var strList []string
|
|
|
|
err := json.Unmarshal(data, &strList)
|
|
|
|
if err == nil {
|
|
|
|
*this = make([]string, len(strList))
|
|
|
|
copy(*this, strList)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var str string
|
|
|
|
err = json.Unmarshal(data, &str)
|
|
|
|
if err == nil {
|
|
|
|
*this = make([]string, 0, 1)
|
|
|
|
*this = append(*this, str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Failed to unmarshal string list: " + string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *StringList) Len() int {
|
|
|
|
return len([]string(*this))
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:58:43 -05:00
|
|
|
type DomainMatcher interface {
|
|
|
|
Match(domain string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type PlainDomainMatcher struct {
|
|
|
|
pattern string
|
|
|
|
}
|
|
|
|
|
2015-12-07 06:10:27 -05:00
|
|
|
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
|
|
|
|
return &PlainDomainMatcher{
|
|
|
|
pattern: strings.ToLower(pattern),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:58:43 -05:00
|
|
|
func (this *PlainDomainMatcher) Match(domain string) bool {
|
2015-12-12 11:15:29 -05:00
|
|
|
return strings.Contains(strings.ToLower(domain), this.pattern)
|
2015-12-07 05:58:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type RegexpDomainMatcher struct {
|
|
|
|
pattern *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
|
|
|
|
r, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RegexpDomainMatcher{
|
|
|
|
pattern: r,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *RegexpDomainMatcher) Match(domain string) bool {
|
2015-12-07 06:10:27 -05:00
|
|
|
return this.pattern.MatchString(strings.ToLower(domain))
|
2015-12-07 05:58:43 -05:00
|
|
|
}
|
|
|
|
|
2015-11-21 15:43:40 -05:00
|
|
|
type FieldRule struct {
|
|
|
|
Rule
|
2015-12-07 05:58:43 -05:00
|
|
|
Domain []DomainMatcher
|
2015-11-26 06:22:34 -05:00
|
|
|
IP []*net.IPNet
|
2016-01-15 07:39:36 -05:00
|
|
|
Port *v2net.PortRange
|
2015-11-21 15:43:40 -05:00
|
|
|
Network v2net.NetworkList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
|
|
|
address := dest.Address()
|
2015-12-07 05:58:43 -05:00
|
|
|
if len(this.Domain) > 0 {
|
2015-11-26 06:22:34 -05:00
|
|
|
if !address.IsDomain() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
foundMatch := false
|
2015-12-07 05:58:43 -05:00
|
|
|
for _, domain := range this.Domain {
|
|
|
|
if domain.Match(address.Domain()) {
|
2015-11-26 06:22:34 -05:00
|
|
|
foundMatch = true
|
2015-11-30 07:46:09 -05:00
|
|
|
break
|
2015-11-26 06:22:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundMatch {
|
2015-11-21 15:43:40 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 06:22:34 -05:00
|
|
|
if this.IP != nil && len(this.IP) > 0 {
|
|
|
|
if !(address.IsIPv4() || address.IsIPv6()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
foundMatch := false
|
|
|
|
for _, ipnet := range this.IP {
|
|
|
|
if ipnet.Contains(address.IP()) {
|
|
|
|
foundMatch = true
|
2015-11-30 07:46:09 -05:00
|
|
|
break
|
2015-11-26 06:22:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundMatch {
|
2015-11-21 15:43:40 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.Port != nil {
|
2015-12-16 17:53:38 -05:00
|
|
|
port := dest.Port()
|
2016-01-15 07:39:36 -05:00
|
|
|
if port.Value() < this.Port.From.Value() || port.Value() > this.Port.To.Value() {
|
2015-11-21 15:43:40 -05:00
|
|
|
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-26 06:22:34 -05:00
|
|
|
Domain *StringList `json:"domain"`
|
|
|
|
IP *StringList `json:"ip"`
|
2016-01-15 07:39:36 -05:00
|
|
|
Port *v2net.PortRange `json:"port"`
|
2015-11-22 16:14:27 -05:00
|
|
|
Network *v2netjson.NetworkList `json:"network"`
|
2015-11-21 15:43:40 -05:00
|
|
|
}
|
|
|
|
rawFieldRule := RawFieldRule{}
|
|
|
|
err := json.Unmarshal(data, &rawFieldRule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
this.Type = rawFieldRule.Type
|
|
|
|
this.OutboundTag = rawFieldRule.OutboundTag
|
2015-11-22 16:14:27 -05:00
|
|
|
|
|
|
|
hasField := false
|
2015-11-26 06:22:34 -05:00
|
|
|
if rawFieldRule.Domain != nil && rawFieldRule.Domain.Len() > 0 {
|
2015-12-07 05:58:43 -05:00
|
|
|
this.Domain = make([]DomainMatcher, rawFieldRule.Domain.Len())
|
|
|
|
for idx, rawDomain := range *(rawFieldRule.Domain) {
|
|
|
|
var matcher DomainMatcher
|
|
|
|
if strings.HasPrefix(rawDomain, "regexp:") {
|
|
|
|
rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
matcher = rawMatcher
|
|
|
|
} else {
|
2015-12-07 06:10:27 -05:00
|
|
|
matcher = NewPlainDomainMatcher(rawDomain)
|
2015-12-07 05:58:43 -05:00
|
|
|
}
|
|
|
|
this.Domain[idx] = matcher
|
|
|
|
}
|
2015-11-22 16:14:27 -05:00
|
|
|
hasField = true
|
|
|
|
}
|
|
|
|
|
2015-11-26 06:22:34 -05:00
|
|
|
if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
|
|
|
|
this.IP = make([]*net.IPNet, 0, rawFieldRule.IP.Len())
|
|
|
|
for _, ipStr := range *(rawFieldRule.IP) {
|
|
|
|
_, ipNet, err := net.ParseCIDR(ipStr)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Invalid IP range in router rule: " + err.Error())
|
|
|
|
}
|
|
|
|
this.IP = append(this.IP, ipNet)
|
2015-11-22 16:14:27 -05:00
|
|
|
}
|
|
|
|
hasField = true
|
2015-11-21 15:43:40 -05:00
|
|
|
}
|
|
|
|
if rawFieldRule.Port != nil {
|
|
|
|
this.Port = rawFieldRule.Port
|
2015-11-22 16:14:27 -05:00
|
|
|
hasField = true
|
2015-11-21 15:43:40 -05:00
|
|
|
}
|
|
|
|
if rawFieldRule.Network != nil {
|
|
|
|
this.Network = rawFieldRule.Network
|
2015-11-22 16:14:27 -05:00
|
|
|
hasField = true
|
|
|
|
}
|
|
|
|
if !hasField {
|
|
|
|
return errors.New("This rule has no effective fields.")
|
2015-11-21 15:43:40 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|