1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00

resolve to ip on demand

This commit is contained in:
Darien Raymond 2017-11-15 12:55:47 +01:00
parent f3c5df8798
commit 26f005e822
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 101 additions and 53 deletions

View File

@ -258,8 +258,9 @@ func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error)
func (v *CIDRMatcher) Apply(ctx context.Context) bool {
ips := make([]net.IP, 0, 4)
if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
for _, rip := range resolveIPs {
if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
resolvedIPs := resolver.Resolve()
for _, rip := range resolvedIPs {
if !rip.Family().IsIPv6() {
continue
}
@ -301,8 +302,9 @@ func NewIPv4Matcher(ipnet *net.IPNetTable, onSource bool) *IPv4Matcher {
func (v *IPv4Matcher) Apply(ctx context.Context) bool {
ips := make([]net.IP, 0, 4)
if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
for _, rip := range resolveIPs {
if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
resolvedIPs := resolver.Resolve()
for _, rip := range resolvedIPs {
if !rip.Family().IsIPv4() {
continue
}

View File

@ -54,17 +54,21 @@ const (
Config_UseIp Config_DomainStrategy = 1
// Resolve to IP if the domain doesn't match any rules.
Config_IpIfNonMatch Config_DomainStrategy = 2
// Resolve to IP if any rule requires IP matching.
Config_IpOnDemand Config_DomainStrategy = 3
)
var Config_DomainStrategy_name = map[int32]string{
0: "AsIs",
1: "UseIp",
2: "IpIfNonMatch",
3: "IpOnDemand",
}
var Config_DomainStrategy_value = map[string]int32{
"AsIs": 0,
"UseIp": 1,
"IpIfNonMatch": 2,
"IpOnDemand": 3,
}
func (x Config_DomainStrategy) String() string {
@ -318,45 +322,45 @@ func init() {
func init() { proto.RegisterFile("v2ray.com/core/app/router/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 626 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0xdf, 0x6e, 0xd3, 0x30,
0x14, 0xc6, 0x49, 0xda, 0x66, 0xcb, 0x49, 0x29, 0x91, 0xc5, 0x50, 0x18, 0x0c, 0x4a, 0x84, 0xa0,
0x17, 0x28, 0x91, 0xca, 0xbf, 0x1b, 0xd0, 0x34, 0xba, 0x69, 0xaa, 0x04, 0x53, 0xe5, 0x6d, 0x5c,
0xc0, 0x45, 0x94, 0xa5, 0x5e, 0x88, 0x68, 0x6d, 0xcb, 0x71, 0xc6, 0x7a, 0xc7, 0x0b, 0xf0, 0x22,
0x3c, 0x0d, 0x8f, 0x84, 0x6c, 0xa7, 0xb0, 0xa2, 0x05, 0x26, 0xee, 0x6c, 0xe7, 0xf7, 0x9d, 0xf3,
0xe5, 0xf8, 0x1c, 0xc3, 0xa3, 0xb3, 0xa1, 0x48, 0x17, 0x51, 0xc6, 0xe6, 0x71, 0xc6, 0x04, 0x89,
0x53, 0xce, 0x63, 0xc1, 0x2a, 0x49, 0x44, 0x9c, 0x31, 0x7a, 0x5a, 0xe4, 0x11, 0x17, 0x4c, 0x32,
0xb4, 0xb1, 0xe4, 0x04, 0x89, 0x52, 0xce, 0x23, 0xc3, 0x6c, 0x3e, 0xfc, 0x43, 0x9e, 0xb1, 0xf9,
0x9c, 0xd1, 0x98, 0x12, 0x19, 0x73, 0x26, 0xa4, 0x11, 0x6f, 0x3e, 0x6e, 0xa6, 0x28, 0x91, 0x5f,
0x98, 0xf8, 0x6c, 0xc0, 0xf0, 0xab, 0x05, 0xce, 0x2e, 0x9b, 0xa7, 0x05, 0x45, 0x2f, 0xa0, 0x2d,
0x17, 0x9c, 0x04, 0x56, 0xdf, 0x1a, 0xf4, 0x86, 0x61, 0x74, 0x69, 0xfe, 0xc8, 0xc0, 0xd1, 0xd1,
0x82, 0x13, 0xac, 0x79, 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, 0x68, 0x1d, 0xda, 0x3b, 0xe5, 0xb8, 0x34, 0x43, 0x7d, 0x5c,
0x92, 0x31, 0xf7, 0x2d, 0xe4, 0x43, 0x77, 0xcc, 0xc7, 0xa7, 0x07, 0x8c, 0xbe, 0x4b, 0x65, 0xf6,
0xc9, 0xb7, 0xdf, 0xbc, 0x86, 0xdb, 0x19, 0x9b, 0x5f, 0x9e, 0x67, 0x62, 0x7d, 0x70, 0xcc, 0xea,
0xbb, 0xbd, 0xf1, 0x7e, 0x88, 0xd3, 0x45, 0x34, 0x52, 0xc4, 0x0e, 0xe7, 0xda, 0x02, 0x11, 0x27,
0x8e, 0x7e, 0xa3, 0x9e, 0xfe, 0x0c, 0x00, 0x00, 0xff, 0xff, 0x6d, 0xa3, 0xd9, 0xd0, 0x33, 0x05,
0x00, 0x00,
// 640 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0xcd, 0x6e, 0xd4, 0x3a,
0x14, 0xc7, 0x6f, 0xe6, 0xab, 0x9d, 0x93, 0xb9, 0x73, 0x23, 0xeb, 0x16, 0x0d, 0x85, 0xc2, 0x10,
0x21, 0x98, 0x05, 0x4a, 0xa4, 0xe1, 0x63, 0x05, 0xaa, 0xca, 0xb4, 0xaa, 0x22, 0x41, 0x19, 0xb9,
0x2d, 0x0b, 0x58, 0x44, 0x6e, 0xe2, 0x86, 0x88, 0x89, 0x6d, 0x39, 0x4e, 0xe9, 0xec, 0x78, 0x01,
0x5e, 0x84, 0xa7, 0xe2, 0x51, 0x90, 0xed, 0x0c, 0xb4, 0xa8, 0x81, 0x8a, 0x9d, 0xed, 0xfc, 0xfe,
0xe7, 0xfc, 0x73, 0x7c, 0x8e, 0xe1, 0xc1, 0xd9, 0x54, 0x92, 0x65, 0x90, 0xf0, 0x22, 0x4c, 0xb8,
0xa4, 0x21, 0x11, 0x22, 0x94, 0xbc, 0x52, 0x54, 0x86,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x9e, 0xf4, 0x71, 0x5f, 0x9f, 0xec, 0xe9, 0x03, 0x74,
0x17, 0xdc, 0x9c, 0x9d, 0xf0, 0x8a, 0xa5, 0xb1, 0x2e, 0xf3, 0xba, 0xf9, 0x0e, 0xf5, 0xd1, 0x11,
0xc9, 0xfc, 0x6f, 0x0e, 0xf4, 0x66, 0xe6, 0x05, 0x40, 0xc7, 0xf0, 0x9f, 0xad, 0x65, 0x5c, 0x2a,
0x49, 0x14, 0xcd, 0x96, 0xf5, 0x54, 0x3e, 0x6a, 0x32, 0x63, 0x5f, 0x0e, 0x7b, 0x11, 0x87, 0xb5,
0x06, 0x0f, 0xd3, 0x4b, 0x7b, 0x3d, 0xe1, 0xb2, 0x5a, 0xd0, 0xfa, 0x36, 0x9b, 0x26, 0xfc, 0x42,
0x4f, 0x60, 0xc3, 0xfb, 0xfb, 0x30, 0xbc, 0x1c, 0x19, 0xad, 0x43, 0x67, 0xa7, 0x8c, 0x4a, 0x3b,
0xd4, 0xc7, 0x25, 0x8d, 0x84, 0xe7, 0x20, 0x0f, 0x06, 0x91, 0x88, 0x4e, 0x0f, 0x38, 0x7b, 0x4d,
0x54, 0xf2, 0xc1, 0x6b, 0xa1, 0x21, 0x40, 0x24, 0xde, 0xb0, 0x5d, 0x5a, 0x10, 0x96, 0x7a, 0xed,
0x97, 0x2f, 0xe0, 0x66, 0xc2, 0x8b, 0xab, 0xf3, 0xce, 0x9d, 0x77, 0x3d, 0xbb, 0xfa, 0xda, 0xda,
0x78, 0x3b, 0xc5, 0x64, 0x19, 0xcc, 0x34, 0xb1, 0x23, 0x84, 0xb1, 0x44, 0xe5, 0x49, 0xcf, 0xbc,
0x59, 0x8f, 0xbf, 0x07, 0x00, 0x00, 0xff, 0xff, 0x53, 0x7c, 0xa8, 0x94, 0x43, 0x05, 0x00, 0x00,
}

View File

@ -76,6 +76,9 @@ message Config {
// Resolve to IP if the domain doesn't match any rules.
IpIfNonMatch = 2;
// Resolve to IP if any rule requires IP matching.
IpOnDemand = 3;
}
DomainStrategy domain_strategy = 1;
repeated RoutingRule rule = 2;

View File

@ -64,7 +64,42 @@ func (r *Router) resolveIP(dest net.Destination) []net.Address {
return dests
}
type ipResolver struct {
ip []net.Address
domain string
resolved bool
dnsServer dns.Server
}
func (r *ipResolver) Resolve() []net.Address {
if r.resolved {
return r.ip
}
log.Trace(newError("looking for IP for domain: ", r.domain))
r.resolved = true
ips := r.dnsServer.Get(r.domain)
if len(ips) == 0 {
return nil
}
r.ip = make([]net.Address, len(ips))
for i, ip := range ips {
r.ip[i] = net.IPAddress(ip)
}
return r.ip
}
func (r *Router) TakeDetour(ctx context.Context) (string, error) {
resolver := &ipResolver{
dnsServer: r.dnsServer,
}
if r.domainStrategy == Config_IpOnDemand {
if dest, ok := proxy.TargetFromContext(ctx); ok && dest.Address.Family().IsDomain() {
resolver.domain = dest.Address.Domain()
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
}
}
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
@ -77,10 +112,10 @@ func (r *Router) TakeDetour(ctx context.Context) (string, error) {
}
if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
log.Trace(newError("looking up IP for ", dest))
ipDests := r.resolveIP(dest)
if ipDests != nil {
ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
resolver.domain = dest.Address.Domain()
ips := resolver.Resolve()
if len(ips) > 0 {
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule.Tag, nil

View File

@ -64,11 +64,15 @@ func InboundTagFromContext(ctx context.Context) (string, bool) {
return v, ok
}
func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
return context.WithValue(ctx, resolvedIPsKey, ips)
type IPResolver interface {
Resolve() []net.Address
}
func ResolvedIPsFromContext(ctx context.Context) ([]net.Address, bool) {
ips, ok := ctx.Value(resolvedIPsKey).([]net.Address)
func ContextWithResolveIPs(ctx context.Context, f IPResolver) context.Context {
return context.WithValue(ctx, resolvedIPsKey, f)
}
func ResolvedIPsFromContext(ctx context.Context) (IPResolver, bool) {
ips, ok := ctx.Value(resolvedIPsKey).(IPResolver)
return ips, ok
}