mirror of https://github.com/v2ray/v2ray-core
				
				
				
			use session in router
							parent
							
								
									f37b04a690
								
							
						
					
					
						commit
						aae99a8e98
					
				|  | @ -49,7 +49,7 @@ func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta | |||
| 	destination := session.Destination | ||||
| 
 | ||||
| 	if this.router != nil { | ||||
| 		if tag, err := this.router.TakeDetour(destination); err == nil { | ||||
| 		if tag, err := this.router.TakeDetour(session); err == nil { | ||||
| 			if handler := this.ohm.GetHandler(tag); handler != nil { | ||||
| 				log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].") | ||||
| 				dispatcher = handler | ||||
|  |  | |||
|  | @ -6,10 +6,11 @@ import ( | |||
| 	"strings" | ||||
| 
 | ||||
| 	v2net "v2ray.com/core/common/net" | ||||
| 	"v2ray.com/core/proxy" | ||||
| ) | ||||
| 
 | ||||
| type Condition interface { | ||||
| 	Apply(dest v2net.Destination) bool | ||||
| 	Apply(session *proxy.SessionInfo) bool | ||||
| } | ||||
| 
 | ||||
| type ConditionChan []Condition | ||||
|  | @ -24,9 +25,9 @@ func (this *ConditionChan) Add(cond Condition) *ConditionChan { | |||
| 	return this | ||||
| } | ||||
| 
 | ||||
| func (this *ConditionChan) Apply(dest v2net.Destination) bool { | ||||
| func (this *ConditionChan) Apply(session *proxy.SessionInfo) bool { | ||||
| 	for _, cond := range *this { | ||||
| 		if !cond.Apply(dest) { | ||||
| 		if !cond.Apply(session) { | ||||
| 			return false | ||||
| 		} | ||||
| 	} | ||||
|  | @ -49,9 +50,9 @@ func (this *AnyCondition) Add(cond Condition) *AnyCondition { | |||
| 	return this | ||||
| } | ||||
| 
 | ||||
| func (this *AnyCondition) Apply(dest v2net.Destination) bool { | ||||
| func (this *AnyCondition) Apply(session *proxy.SessionInfo) bool { | ||||
| 	for _, cond := range *this { | ||||
| 		if cond.Apply(dest) { | ||||
| 		if cond.Apply(session) { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
|  | @ -72,7 +73,8 @@ func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool { | ||||
| func (this *PlainDomainMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	dest := session.Destination | ||||
| 	if !dest.Address.Family().IsDomain() { | ||||
| 		return false | ||||
| 	} | ||||
|  | @ -94,7 +96,8 @@ func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) { | |||
| 	}, nil | ||||
| } | ||||
| 
 | ||||
| func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool { | ||||
| func (this *RegexpDomainMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	dest := session.Destination | ||||
| 	if !dest.Address.Family().IsDomain() { | ||||
| 		return false | ||||
| 	} | ||||
|  | @ -103,20 +106,26 @@ func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool { | |||
| } | ||||
| 
 | ||||
| type CIDRMatcher struct { | ||||
| 	cidr *net.IPNet | ||||
| 	cidr     *net.IPNet | ||||
| 	onSource bool | ||||
| } | ||||
| 
 | ||||
| func NewCIDRMatcher(ip []byte, mask uint32) (*CIDRMatcher, error) { | ||||
| func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) { | ||||
| 	cidr := &net.IPNet{ | ||||
| 		IP:   net.IP(ip), | ||||
| 		Mask: net.CIDRMask(int(mask), len(ip)), | ||||
| 	} | ||||
| 	return &CIDRMatcher{ | ||||
| 		cidr: cidr, | ||||
| 		cidr:     cidr, | ||||
| 		onSource: onSource, | ||||
| 	}, nil | ||||
| } | ||||
| 
 | ||||
| func (this *CIDRMatcher) Apply(dest v2net.Destination) bool { | ||||
| func (this *CIDRMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	dest := session.Destination | ||||
| 	if this.onSource { | ||||
| 		dest = session.Source | ||||
| 	} | ||||
| 	if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) { | ||||
| 		return false | ||||
| 	} | ||||
|  | @ -124,16 +133,22 @@ func (this *CIDRMatcher) Apply(dest v2net.Destination) bool { | |||
| } | ||||
| 
 | ||||
| type IPv4Matcher struct { | ||||
| 	ipv4net *v2net.IPNet | ||||
| 	ipv4net  *v2net.IPNet | ||||
| 	onSource bool | ||||
| } | ||||
| 
 | ||||
| func NewIPv4Matcher(ipnet *v2net.IPNet) *IPv4Matcher { | ||||
| func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher { | ||||
| 	return &IPv4Matcher{ | ||||
| 		ipv4net: ipnet, | ||||
| 		ipv4net:  ipnet, | ||||
| 		onSource: onSource, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (this *IPv4Matcher) Apply(dest v2net.Destination) bool { | ||||
| func (this *IPv4Matcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	dest := session.Destination | ||||
| 	if this.onSource { | ||||
| 		dest = session.Source | ||||
| 	} | ||||
| 	if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) { | ||||
| 		return false | ||||
| 	} | ||||
|  | @ -150,8 +165,8 @@ func NewPortMatcher(portRange v2net.PortRange) *PortMatcher { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (this *PortMatcher) Apply(dest v2net.Destination) bool { | ||||
| 	return this.port.Contains(dest.Port) | ||||
| func (this *PortMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	return this.port.Contains(session.Destination.Port) | ||||
| } | ||||
| 
 | ||||
| type NetworkMatcher struct { | ||||
|  | @ -164,6 +179,28 @@ func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (this *NetworkMatcher) Apply(dest v2net.Destination) bool { | ||||
| 	return this.network.HasNetwork(dest.Network) | ||||
| func (this *NetworkMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	return this.network.HasNetwork(session.Destination.Network) | ||||
| } | ||||
| 
 | ||||
| type UserMatcher struct { | ||||
| 	user []string | ||||
| } | ||||
| 
 | ||||
| func NewUserMatcher(users []string) *UserMatcher { | ||||
| 	return &UserMatcher{ | ||||
| 		user: users, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (this *UserMatcher) Apply(session *proxy.SessionInfo) bool { | ||||
| 	if session.User == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	for _, u := range this.user { | ||||
| 		if u == session.User.Email { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  |  | |||
|  | @ -5,6 +5,7 @@ import ( | |||
| 	"net" | ||||
| 
 | ||||
| 	v2net "v2ray.com/core/common/net" | ||||
| 	"v2ray.com/core/proxy" | ||||
| ) | ||||
| 
 | ||||
| type Rule struct { | ||||
|  | @ -12,8 +13,8 @@ type Rule struct { | |||
| 	Condition Condition | ||||
| } | ||||
| 
 | ||||
| func (this *Rule) Apply(dest v2net.Destination) bool { | ||||
| 	return this.Condition.Apply(dest) | ||||
| func (this *Rule) Apply(session *proxy.SessionInfo) bool { | ||||
| 	return this.Condition.Apply(session) | ||||
| } | ||||
| 
 | ||||
| func (this *RoutingRule) BuildCondition() (Condition, error) { | ||||
|  | @ -46,7 +47,7 @@ func (this *RoutingRule) BuildCondition() (Condition, error) { | |||
| 				ipv4Net.AddIP(ip.Ip, byte(ip.Prefix)) | ||||
| 			case net.IPv6len: | ||||
| 				hasIpv6 = true | ||||
| 				matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix) | ||||
| 				matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, false) | ||||
| 				if err != nil { | ||||
| 					return nil, err | ||||
| 				} | ||||
|  | @ -58,11 +59,11 @@ func (this *RoutingRule) BuildCondition() (Condition, error) { | |||
| 
 | ||||
| 		if !ipv4Net.IsEmpty() && hasIpv6 { | ||||
| 			cond := NewAnyCondition() | ||||
| 			cond.Add(NewIPv4Matcher(ipv4Net)) | ||||
| 			cond.Add(NewIPv4Matcher(ipv4Net, false)) | ||||
| 			cond.Add(ipv6Cond) | ||||
| 			conds.Add(cond) | ||||
| 		} else if !ipv4Net.IsEmpty() { | ||||
| 			conds.Add(NewIPv4Matcher(ipv4Net)) | ||||
| 			conds.Add(NewIPv4Matcher(ipv4Net, false)) | ||||
| 		} else if hasIpv6 { | ||||
| 			conds.Add(ipv6Cond) | ||||
| 		} | ||||
|  | @ -76,6 +77,43 @@ func (this *RoutingRule) BuildCondition() (Condition, error) { | |||
| 		conds.Add(NewNetworkMatcher(this.NetworkList)) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(this.SourceCidr) > 0 { | ||||
| 		ipv4Net := v2net.NewIPNet() | ||||
| 		ipv6Cond := NewAnyCondition() | ||||
| 		hasIpv6 := false | ||||
| 
 | ||||
| 		for _, ip := range this.SourceCidr { | ||||
| 			switch len(ip.Ip) { | ||||
| 			case net.IPv4len: | ||||
| 				ipv4Net.AddIP(ip.Ip, byte(ip.Prefix)) | ||||
| 			case net.IPv6len: | ||||
| 				hasIpv6 = true | ||||
| 				matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, true) | ||||
| 				if err != nil { | ||||
| 					return nil, err | ||||
| 				} | ||||
| 				ipv6Cond.Add(matcher) | ||||
| 			default: | ||||
| 				return nil, errors.New("Router: Invalid IP length.") | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if !ipv4Net.IsEmpty() && hasIpv6 { | ||||
| 			cond := NewAnyCondition() | ||||
| 			cond.Add(NewIPv4Matcher(ipv4Net, true)) | ||||
| 			cond.Add(ipv6Cond) | ||||
| 			conds.Add(cond) | ||||
| 		} else if !ipv4Net.IsEmpty() { | ||||
| 			conds.Add(NewIPv4Matcher(ipv4Net, true)) | ||||
| 		} else if hasIpv6 { | ||||
| 			conds.Add(ipv6Cond) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if len(this.UserEmail) > 0 { | ||||
| 		conds.Add(NewUserMatcher(this.UserEmail)) | ||||
| 	} | ||||
| 
 | ||||
| 	if conds.Len() == 0 { | ||||
| 		return nil, errors.New("Router: This rule has no effective fields.") | ||||
| 	} | ||||
|  |  | |||
|  | @ -116,6 +116,8 @@ type RoutingRule struct { | |||
| 	Cidr        []*CIDR                             `protobuf:"bytes,3,rep,name=cidr" json:"cidr,omitempty"` | ||||
| 	PortRange   *v2ray_core_common_net.PortRange    `protobuf:"bytes,4,opt,name=port_range,json=portRange" json:"port_range,omitempty"` | ||||
| 	NetworkList *v2ray_core_common_net1.NetworkList `protobuf:"bytes,5,opt,name=network_list,json=networkList" json:"network_list,omitempty"` | ||||
| 	SourceCidr  []*CIDR                             `protobuf:"bytes,6,rep,name=source_cidr,json=sourceCidr" json:"source_cidr,omitempty"` | ||||
| 	UserEmail   []string                            `protobuf:"bytes,7,rep,name=user_email,json=userEmail" json:"user_email,omitempty"` | ||||
| } | ||||
| 
 | ||||
| func (m *RoutingRule) Reset()                    { *m = RoutingRule{} } | ||||
|  | @ -151,6 +153,13 @@ func (m *RoutingRule) GetNetworkList() *v2ray_core_common_net1.NetworkList { | |||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func (m *RoutingRule) GetSourceCidr() []*CIDR { | ||||
| 	if m != nil { | ||||
| 		return m.SourceCidr | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| type Config struct { | ||||
| 	DomainStrategy Config_DomainStrategy `protobuf:"varint,1,opt,name=domain_strategy,json=domainStrategy,enum=v2ray.core.app.router.Config_DomainStrategy" json:"domain_strategy,omitempty"` | ||||
| 	Rule           []*RoutingRule        `protobuf:"bytes,2,rep,name=rule" json:"rule,omitempty"` | ||||
|  | @ -180,34 +189,37 @@ func init() { | |||
| func init() { proto.RegisterFile("v2ray.com/core/app/router/config.proto", fileDescriptor0) } | ||||
| 
 | ||||
| var fileDescriptor0 = []byte{ | ||||
| 	// 458 bytes of a gzipped FileDescriptorProto
 | ||||
| 	0x1f, 0x8b, 0x08, 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0x75, 0xc7, | ||||
| 	0x12, 0x3f, 0xf5, 0x96, 0x33, 0xe2, 0x92, 0xec, 0x31, 0x04, 0x3d, 0x83, 0x66, 0x30, 0x5d, 0xd7, | ||||
| 	0x94, 0x8b, 0xf8, 0x56, 0x1f, 0x12, 0x56, 0xb1, 0xcb, 0xd8, 0xcb, 0x30, 0x04, 0x27, 0xc5, 0x29, | ||||
| 	0x41, 0x0b, 0xf0, 0xb9, 0xb2, 0x1d, 0xe7, 0xc4, 0xe7, 0x0a, 0x3d, 0x80, 0x50, 0x69, 0xb6, 0xe3, | ||||
| 	0x97, 0xb6, 0xd8, 0x1d, 0x32, 0x64, 0xd9, 0x2f, 0x1f, 0x22, 0x22, 0x3b, 0xc3, 0x45, 0x45, 0xba, | ||||
| 	0x9a, 0xa1, 0x18, 0x26, 0x86, 0x56, 0x56, 0x38, 0x23, 0x7d, 0x88, 0x5e, 0x43, 0xb8, 0xb5, 0xa3, | ||||
| 	0x25, 0x7e, 0x3a, 0x59, 0x46, 0xab, 0x27, 0xff, 0x9d, 0x9f, 0x0c, 0x30, 0xca, 0x21, 0x28, 0xf9, | ||||
| 	0x56, 0x27, 0x13, 0x2b, 0x7a, 0x34, 0x22, 0xea, 0x67, 0x25, 0x16, 0x44, 0xef, 0x00, 0xfa, 0x35, | ||||
| 	0x6f, 0x34, 0x15, 0x15, 0x4b, 0x82, 0xd4, 0x5b, 0x46, 0xab, 0xf4, 0xba, 0xcc, 0x6d, 0x1a, 0x0b, | ||||
| 	0x66, 0xf0, 0x5a, 0x6a, 0x43, 0x7a, 0x8e, 0xcc, 0xd4, 0x55, 0x88, 0x3e, 0xc0, 0x7c, 0xb8, 0xc0, | ||||
| 	0xa6, 0xe6, 0xad, 0x49, 0xa6, 0xb6, 0x44, 0x36, 0x52, 0xe2, 0xcc, 0xa1, 0x1f, 0x79, 0x6b, 0x48, | ||||
| 	0x24, 0xfe, 0x26, 0xd9, 0x6f, 0x0f, 0xc2, 0x13, 0x6b, 0x17, 0x74, 0x0e, 0x77, 0xdd, 0x6f, 0x36, | ||||
| 	0xad, 0xd1, 0xd4, 0xb0, 0x6a, 0x3f, 0xdc, 0xf0, 0xc5, 0xd8, 0x77, 0x9c, 0xcd, 0xdc, 0x2a, 0x3e, | ||||
| 	0x0f, 0x1a, 0xb2, 0xd8, 0xde, 0xc8, 0x7b, 0x3f, 0xe8, 0xae, 0x66, 0xc3, 0x3e, 0xc7, 0xfc, 0x70, | ||||
| 	0xed, 0x2a, 0xc4, 0xf2, 0xd9, 0x5b, 0x58, 0xdc, 0xac, 0x8c, 0x6e, 0x43, 0xf0, 0xbe, 0x2d, 0x5a, | ||||
| 	0x67, 0x81, 0xf3, 0x96, 0x15, 0x2a, 0xf6, 0x50, 0x0c, 0xf3, 0x42, 0x15, 0xbb, 0x33, 0x29, 0x3e, | ||||
| 	0x51, 0x53, 0x7e, 0x8f, 0xfd, 0xe3, 0x97, 0xf0, 0xb0, 0x94, 0xcd, 0xe1, 0x3e, 0xc7, 0x91, 0x1b, | ||||
| 	0x7a, 0xdd, 0x9b, 0xf6, 0x6b, 0xe8, 0x1e, 0xbf, 0x85, 0xd6, 0xc3, 0xaf, 0xfe, 0x04, 0x00, 0x00, | ||||
| 	0xff, 0xff, 0xa7, 0x28, 0xf6, 0x96, 0x53, 0x03, 0x00, 0x00, | ||||
| 	// 501 bytes of a gzipped FileDescriptorProto
 | ||||
| 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x93, 0xd1, 0x6e, 0xd3, 0x3e, | ||||
| 	0x14, 0xc6, 0xff, 0x49, 0xd3, 0xfc, 0xc9, 0x49, 0x29, 0x91, 0x05, 0x28, 0x0c, 0x26, 0x45, 0x11, | ||||
| 	0x82, 0x5e, 0xa0, 0x04, 0x15, 0x01, 0x37, 0x48, 0x88, 0x75, 0xbb, 0xa8, 0x04, 0x53, 0x65, 0xd8, | ||||
| 	0x0d, 0x37, 0x95, 0x49, 0xdd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, 0xba, 0x0f, | ||||
| 	0xa1, 0x54, 0x74, 0xcb, 0x2e, 0x6c, 0xb3, 0xdb, 0x78, 0xa8, 0xf2, 0x9f, 0x3e, 0xc4, 0x58, 0xf4, | ||||
| 	0x9a, 0xf1, 0x1a, 0xf7, 0x0d, 0x45, 0x09, 0x8c, 0x34, 0xa9, 0xad, 0x30, 0xc2, 0xe6, 0x88, 0x5e, | ||||
| 	0x42, 0xb8, 0xb1, 0xd6, 0x52, 0x3f, 0x1b, 0xcd, 0xe2, 0xf9, 0xe1, 0x5f, 0xfd, 0xe3, 0x01, 0x46, | ||||
| 	0x25, 0x04, 0x15, 0xdb, 0xa8, 0x74, 0x64, 0x45, 0x0f, 0xf7, 0x88, 0x8c, 0x57, 0x6c, 0x41, 0xf4, | ||||
| 	0x16, 0xc0, 0xac, 0x79, 0xad, 0x08, 0xaf, 0x69, 0x1a, 0x64, 0xde, 0x2c, 0x9e, 0x67, 0x57, 0x65, | ||||
| 	0x6e, 0xd3, 0x05, 0xa7, 0xba, 0x58, 0x09, 0xa5, 0xb1, 0xe1, 0x70, 0x24, 0x2f, 0x8f, 0xe8, 0x04, | ||||
| 	0x26, 0xc3, 0x0b, 0xac, 0x1b, 0xd6, 0xe9, 0x74, 0x6c, 0x5b, 0xe4, 0x7b, 0x5a, 0x9c, 0x3a, 0xf4, | ||||
| 	0x3d, 0xeb, 0x34, 0x8e, 0xf9, 0xef, 0x02, 0xbd, 0x81, 0xb8, 0x13, 0xbd, 0xaa, 0xe8, 0xda, 0xfa, | ||||
| 	0x0f, 0xff, 0xed, 0x1f, 0x1c, 0xbf, 0x30, 0x5f, 0x71, 0x08, 0xd0, 0x77, 0x54, 0xad, 0x69, 0x4b, | ||||
| 	0x58, 0x93, 0xfe, 0x9f, 0x8d, 0x66, 0x11, 0x8e, 0xcc, 0xcd, 0x89, 0xb9, 0xc8, 0x7f, 0x78, 0x10, | ||||
| 	0x2e, 0x6c, 0x16, 0xd1, 0x19, 0xdc, 0x71, 0xab, 0x5a, 0x77, 0x5a, 0x11, 0x4d, 0xeb, 0xdd, 0x10, | ||||
| 	0x90, 0x67, 0xfb, 0x66, 0xb9, 0x0c, 0xbb, 0x3d, 0x7f, 0x1c, 0x34, 0x78, 0xba, 0xb9, 0x56, 0x9b, | ||||
| 	0xb0, 0xa9, 0xbe, 0xa1, 0xc3, 0x63, 0xed, 0x0b, 0xdb, 0x95, 0x27, 0xc7, 0x96, 0xcf, 0x5f, 0xc3, | ||||
| 	0xf4, 0x7a, 0x67, 0x74, 0x0b, 0x82, 0x77, 0xdd, 0xb2, 0x73, 0xf9, 0x3a, 0xeb, 0xe8, 0x52, 0x26, | ||||
| 	0x1e, 0x4a, 0x60, 0xb2, 0x94, 0xcb, 0xed, 0xa9, 0xe0, 0x1f, 0x88, 0xae, 0xbe, 0x26, 0xfe, 0xd1, | ||||
| 	0x73, 0x78, 0x50, 0x89, 0xf6, 0xe6, 0x39, 0x47, 0xb1, 0x33, 0xbd, 0x32, 0x7f, 0xc4, 0xe7, 0xd0, | ||||
| 	0x5d, 0x7e, 0x09, 0xed, 0x0f, 0xf2, 0xe2, 0x57, 0x00, 0x00, 0x00, 0xff, 0xff, 0x3a, 0x89, 0xc1, | ||||
| 	0x51, 0xb0, 0x03, 0x00, 0x00, | ||||
| } | ||||
|  |  | |||
|  | @ -40,6 +40,8 @@ message RoutingRule { | |||
|   repeated CIDR cidr = 3; | ||||
|   v2ray.core.common.net.PortRange port_range = 4; | ||||
|   v2ray.core.common.net.NetworkList network_list = 5; | ||||
|   repeated CIDR source_cidr = 6; | ||||
|   repeated string user_email = 7; | ||||
| } | ||||
| 
 | ||||
| message Config { | ||||
|  |  | |||
|  | @ -8,6 +8,7 @@ import ( | |||
| 	"v2ray.com/core/common/loader" | ||||
| 	"v2ray.com/core/common/log" | ||||
| 	v2net "v2ray.com/core/common/net" | ||||
| 	"v2ray.com/core/proxy" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
|  | @ -22,15 +23,15 @@ var ( | |||
| type Router struct { | ||||
| 	domainStrategy Config_DomainStrategy | ||||
| 	rules          []Rule | ||||
| 	cache          *RoutingTable | ||||
| 	dnsServer      dns.Server | ||||
| 	//	cache          *RoutingTable
 | ||||
| 	dnsServer dns.Server | ||||
| } | ||||
| 
 | ||||
| func NewRouter(config *Config, space app.Space) *Router { | ||||
| 	r := &Router{ | ||||
| 		domainStrategy: config.DomainStrategy, | ||||
| 		cache:          NewRoutingTable(), | ||||
| 		rules:          make([]Rule, len(config.Rule)), | ||||
| 		//cache:          NewRoutingTable(),
 | ||||
| 		rules: make([]Rule, len(config.Rule)), | ||||
| 	} | ||||
| 
 | ||||
| 	space.InitializeApplication(func() error { | ||||
|  | @ -74,12 +75,13 @@ func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination { | |||
| 	return dests | ||||
| } | ||||
| 
 | ||||
| func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) { | ||||
| func (this *Router) takeDetourWithoutCache(session *proxy.SessionInfo) (string, error) { | ||||
| 	for _, rule := range this.rules { | ||||
| 		if rule.Apply(dest) { | ||||
| 		if rule.Apply(session) { | ||||
| 			return rule.Tag, nil | ||||
| 		} | ||||
| 	} | ||||
| 	dest := session.Destination | ||||
| 	if this.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() { | ||||
| 		log.Info("Router: Looking up IP for ", dest) | ||||
| 		ipDests := this.ResolveIP(dest) | ||||
|  | @ -87,7 +89,11 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro | |||
| 			for _, ipDest := range ipDests { | ||||
| 				log.Info("Router: Trying IP ", ipDest) | ||||
| 				for _, rule := range this.rules { | ||||
| 					if rule.Apply(ipDest) { | ||||
| 					if rule.Apply(&proxy.SessionInfo{ | ||||
| 						Source:      session.Source, | ||||
| 						Destination: ipDest, | ||||
| 						User:        session.User, | ||||
| 					}) { | ||||
| 						return rule.Tag, nil | ||||
| 					} | ||||
| 				} | ||||
|  | @ -98,15 +104,15 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro | |||
| 	return "", ErrNoRuleApplicable | ||||
| } | ||||
| 
 | ||||
| func (this *Router) TakeDetour(dest v2net.Destination) (string, error) { | ||||
| 	destStr := dest.String() | ||||
| 	found, tag, err := this.cache.Get(destStr) | ||||
| 	if !found { | ||||
| 		tag, err := this.takeDetourWithoutCache(dest) | ||||
| 		this.cache.Set(destStr, tag, err) | ||||
| 		return tag, err | ||||
| 	} | ||||
| func (this *Router) TakeDetour(session *proxy.SessionInfo) (string, error) { | ||||
| 	//destStr := dest.String()
 | ||||
| 	//found, tag, err := this.cache.Get(destStr)
 | ||||
| 	//if !found {
 | ||||
| 	tag, err := this.takeDetourWithoutCache(session) | ||||
| 	//this.cache.Set(destStr, tag, err)
 | ||||
| 	return tag, err | ||||
| 	//}
 | ||||
| 	//return tag, err
 | ||||
| } | ||||
| 
 | ||||
| type RouterFactory struct{} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 Darien Raymond
						Darien Raymond