2016-10-12 14:11:13 +00:00
|
|
|
package router
|
2016-01-17 15:20:49 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-01-17 15:20:49 +00:00
|
|
|
"net"
|
|
|
|
"regexp"
|
2016-05-24 19:55:46 +00:00
|
|
|
"strings"
|
2016-01-17 15:20:49 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2017-01-26 19:46:44 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-10-18 21:01:39 +00:00
|
|
|
"v2ray.com/core/proxy"
|
2016-01-17 15:20:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Condition interface {
|
2017-01-26 19:46:44 +00:00
|
|
|
Apply(ctx context.Context) bool
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConditionChan []Condition
|
|
|
|
|
|
|
|
func NewConditionChan() *ConditionChan {
|
|
|
|
var condChan ConditionChan = make([]Condition, 0, 8)
|
|
|
|
return &condChan
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *ConditionChan) Add(cond Condition) *ConditionChan {
|
|
|
|
*v = append(*v, cond)
|
|
|
|
return v
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *ConditionChan) Apply(ctx context.Context) bool {
|
2016-11-27 20:39:09 +00:00
|
|
|
for _, cond := range *v {
|
2017-01-26 19:46:44 +00:00
|
|
|
if !cond.Apply(ctx) {
|
2016-01-17 15:20:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *ConditionChan) Len() int {
|
|
|
|
return len(*v)
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 13:40:46 +00:00
|
|
|
type AnyCondition []Condition
|
|
|
|
|
|
|
|
func NewAnyCondition() *AnyCondition {
|
|
|
|
var anyCond AnyCondition = make([]Condition, 0, 8)
|
|
|
|
return &anyCond
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AnyCondition) Add(cond Condition) *AnyCondition {
|
|
|
|
*v = append(*v, cond)
|
|
|
|
return v
|
2016-01-24 13:40:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *AnyCondition) Apply(ctx context.Context) bool {
|
2016-11-27 20:39:09 +00:00
|
|
|
for _, cond := range *v {
|
2017-01-26 19:46:44 +00:00
|
|
|
if cond.Apply(ctx) {
|
2016-01-24 13:40:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AnyCondition) Len() int {
|
|
|
|
return len(*v)
|
2016-01-24 13:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:20:49 +00:00
|
|
|
type PlainDomainMatcher struct {
|
2016-05-24 19:55:46 +00:00
|
|
|
pattern string
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
|
|
|
|
return &PlainDomainMatcher{
|
2016-05-24 19:55:46 +00:00
|
|
|
pattern: pattern,
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *PlainDomainMatcher) Apply(ctx context.Context) bool {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-20 09:53:05 +00:00
|
|
|
if !dest.Address.Family().IsDomain() {
|
2016-01-17 15:20:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-09-20 09:53:05 +00:00
|
|
|
domain := dest.Address.Domain()
|
2016-11-27 20:39:09 +00:00
|
|
|
return strings.Contains(domain, v.pattern)
|
2016-01-17 15:20:49 +00: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
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *RegexpDomainMatcher) Apply(ctx context.Context) bool {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-20 09:53:05 +00:00
|
|
|
if !dest.Address.Family().IsDomain() {
|
2016-01-17 15:20:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-09-20 09:53:05 +00:00
|
|
|
domain := dest.Address.Domain()
|
2016-11-27 20:39:09 +00:00
|
|
|
return v.pattern.MatchString(strings.ToLower(domain))
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CIDRMatcher struct {
|
2016-10-18 21:01:39 +00:00
|
|
|
cidr *net.IPNet
|
|
|
|
onSource bool
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 21:01:39 +00:00
|
|
|
func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
|
2016-10-11 21:02:44 +00:00
|
|
|
cidr := &net.IPNet{
|
|
|
|
IP: net.IP(ip),
|
|
|
|
Mask: net.CIDRMask(int(mask), len(ip)),
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
return &CIDRMatcher{
|
2016-10-18 21:01:39 +00:00
|
|
|
cidr: cidr,
|
|
|
|
onSource: onSource,
|
2016-01-17 15:20:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *CIDRMatcher) Apply(ctx context.Context) bool {
|
2017-01-28 08:04:29 +00:00
|
|
|
ips := make([]net.IP, 0, 4)
|
2017-01-27 20:19:46 +00:00
|
|
|
if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
|
|
|
|
for _, rip := range resolveIPs {
|
2017-01-28 08:04:29 +00:00
|
|
|
if !rip.Family().IsIPv6() {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-27 20:19:46 +00:00
|
|
|
ips = append(ips, rip.IP())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
var dest v2net.Destination
|
2017-02-09 21:49:38 +00:00
|
|
|
var ok bool
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.onSource {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok = proxy.SourceFromContext(ctx)
|
2017-01-26 19:46:44 +00:00
|
|
|
} else {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok = proxy.TargetFromContext(ctx)
|
2016-10-18 21:01:39 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-02-09 21:49:38 +00:00
|
|
|
if ok && dest.Address.Family().IsIPv6() {
|
2017-01-27 20:19:46 +00:00
|
|
|
ips = append(ips, dest.Address.IP())
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
if v.cidr.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IPv4Matcher struct {
|
2016-10-18 21:01:39 +00:00
|
|
|
ipv4net *v2net.IPNet
|
|
|
|
onSource bool
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 21:01:39 +00:00
|
|
|
func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
|
2016-01-17 15:20:49 +00:00
|
|
|
return &IPv4Matcher{
|
2016-10-18 21:01:39 +00:00
|
|
|
ipv4net: ipnet,
|
|
|
|
onSource: onSource,
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *IPv4Matcher) Apply(ctx context.Context) bool {
|
2017-01-28 08:04:29 +00:00
|
|
|
ips := make([]net.IP, 0, 4)
|
2017-01-27 20:19:46 +00:00
|
|
|
if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
|
|
|
|
for _, rip := range resolveIPs {
|
2017-01-28 08:04:29 +00:00
|
|
|
if !rip.Family().IsIPv4() {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-27 20:19:46 +00:00
|
|
|
ips = append(ips, rip.IP())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
var dest v2net.Destination
|
2017-02-09 21:49:38 +00:00
|
|
|
var ok bool
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.onSource {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok = proxy.SourceFromContext(ctx)
|
2017-01-26 19:46:44 +00:00
|
|
|
} else {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok = proxy.TargetFromContext(ctx)
|
2016-10-18 21:01:39 +00:00
|
|
|
}
|
2017-01-27 19:38:01 +00:00
|
|
|
|
2017-02-09 21:49:38 +00:00
|
|
|
if ok && dest.Address.Family().IsIPv4() {
|
2017-01-27 20:19:46 +00:00
|
|
|
ips = append(ips, dest.Address.IP())
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
if v.ipv4net.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PortMatcher struct {
|
|
|
|
port v2net.PortRange
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
|
|
|
|
return &PortMatcher{
|
|
|
|
port: portRange,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *PortMatcher) Apply(ctx context.Context) bool {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
return v.port.Contains(dest.Port)
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NetworkMatcher struct {
|
|
|
|
network *v2net.NetworkList
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
|
|
|
|
return &NetworkMatcher{
|
|
|
|
network: network,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *NetworkMatcher) Apply(ctx context.Context) bool {
|
2017-02-09 21:49:38 +00:00
|
|
|
dest, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
return v.network.HasNetwork(dest.Network)
|
2016-10-18 21:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserMatcher struct {
|
|
|
|
user []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMatcher(users []string) *UserMatcher {
|
|
|
|
return &UserMatcher{
|
|
|
|
user: users,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *UserMatcher) Apply(ctx context.Context) bool {
|
|
|
|
user := protocol.UserFromContext(ctx)
|
|
|
|
if user == nil {
|
2016-10-18 21:01:39 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
for _, u := range v.user {
|
2017-01-26 19:46:44 +00:00
|
|
|
if u == user.Email {
|
2016-10-18 21:01:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
2016-11-13 20:23:34 +00:00
|
|
|
|
|
|
|
type InboundTagMatcher struct {
|
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
|
|
|
|
return &InboundTagMatcher{
|
|
|
|
tags: tags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
|
2017-02-09 21:49:38 +00:00
|
|
|
tag, ok := proxy.InboundTagFromContext(ctx)
|
|
|
|
if !ok {
|
2016-11-13 20:23:34 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
for _, t := range v.tags {
|
2017-01-26 19:46:44 +00:00
|
|
|
if t == tag {
|
2016-11-13 20:23:34 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|