1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

pick IP family by local address in freedom

This commit is contained in:
Darien Raymond 2018-11-19 21:36:46 +01:00
parent 9a83561504
commit 8a82a3664c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 67 additions and 31 deletions

View File

@ -114,6 +114,14 @@ func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
}
}
// Address implements internet.Dialer.
func (h *Handler) Address() net.Address {
if h.senderSettings == nil || h.senderSettings.Via == nil {
return nil
}
return h.senderSettings.Via.AsAddress()
}
// Dial implements internet.Dialer.
func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
if h.senderSettings != nil {

View File

@ -1 +1,5 @@
package freedom
func (c *Config) useIP() bool {
return c.DomainStrategy == Config_USE_IP || c.DomainStrategy == Config_USE_IP4 || c.DomainStrategy == Config_USE_IP6
}

View File

@ -21,18 +21,24 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Config_DomainStrategy int32
const (
Config_AS_IS Config_DomainStrategy = 0
Config_USE_IP Config_DomainStrategy = 1
Config_AS_IS Config_DomainStrategy = 0
Config_USE_IP Config_DomainStrategy = 1
Config_USE_IP4 Config_DomainStrategy = 2
Config_USE_IP6 Config_DomainStrategy = 3
)
var Config_DomainStrategy_name = map[int32]string{
0: "AS_IS",
1: "USE_IP",
2: "USE_IP4",
3: "USE_IP6",
}
var Config_DomainStrategy_value = map[string]int32{
"AS_IS": 0,
"USE_IP": 1,
"AS_IS": 0,
"USE_IP": 1,
"USE_IP4": 2,
"USE_IP6": 3,
}
func (x Config_DomainStrategy) String() string {
@ -157,27 +163,28 @@ func init() {
}
var fileDescriptor_66807b6fe2cca4da = []byte{
// 340 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0x6f, 0x4b, 0x83, 0x50,
0x14, 0xc6, 0xd3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, 0x75, 0x4b, 0x73, 0xb7, 0x10, 0x79, 0x85, 0xff, 0xd1, 0x2e,
0x9d, 0xcf, 0xeb, 0x78, 0x56, 0x43, 0x85, 0xba, 0x38, 0x3e, 0xfc, 0xc0, 0x4d, 0xdc, 0x6e, 0x74,
0xe0, 0x50, 0xa7, 0x00, 0x9b, 0x0c, 0x85, 0xbf, 0xc6, 0x1c, 0xd7, 0xd6, 0xef, 0x72, 0x05, 0xb7,
0x55, 0x22, 0x77, 0x25, 0x30, 0x18, 0x41, 0x67, 0x3f, 0x00, 0x69, 0xc1, 0x9f, 0x91, 0xe7, 0xcf,
0x3d, 0xf3, 0x17, 0x01, 0x30, 0x1e, 0xbc, 0x99, 0x3f, 0x5f, 0x98, 0x1a, 0x69, 0x43, 0xb3, 0xaa,
0xaf, 0x4d, 0x7d, 0xd7, 0xdc, 0x98, 0x8d, 0xf1, 0x14, 0x7a, 0x21, 0x4f, 0x8e, 0xae, 0xba, 0xd0,
0x5e, 0x9a, 0x75, 0xf9, 0xa1, 0x5b, 0x8f, 0x8e, 0x1b, 0x14, 0x74, 0x52, 0xb2, 0x16, 0x8a, 0x75,
0x5b, 0x3d, 0xbd, 0x19, 0xea, 0xb7, 0xae, 0xbe, 0x02, 0x00, 0x00, 0xff, 0xff, 0x72, 0x98, 0x5b,
0x40, 0x67, 0x02, 0x00, 0x00,
}

View File

@ -16,6 +16,8 @@ message Config {
enum DomainStrategy {
AS_IS = 0;
USE_IP = 1;
USE_IP4 = 2;
USE_IP6 = 3;
}
DomainStrategy domain_strategy = 1;
uint32 timeout = 2 [deprecated = true];

View File

@ -57,8 +57,20 @@ func (h *Handler) policy() policy.Session {
return p
}
func (h *Handler) resolveIP(ctx context.Context, domain string) net.Address {
ips, err := h.dns.LookupIP(domain)
func (h *Handler) resolveIP(ctx context.Context, domain string, localAddr net.Address) net.Address {
var lookupFunc func(string) ([]net.IP, error) = h.dns.LookupIP
if h.config.DomainStrategy == Config_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()) {
if lookupIPv4, ok := h.dns.(dns.IPv4Lookup); ok {
lookupFunc = lookupIPv4.LookupIPv4
}
} else if h.config.DomainStrategy == Config_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()) {
if lookupIPv6, ok := h.dns.(dns.IPv6Lookup); ok {
lookupFunc = lookupIPv6.LookupIPv6
}
}
ips, err := lookupFunc(domain)
if err != nil {
newError("failed to get IP address for domain ", domain).Base(err).WriteToLog(session.ExportIDToError(ctx))
}
@ -101,8 +113,8 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
var conn internet.Connection
err := retry.ExponentialBackoff(5, 100).On(func() error {
dialDest := destination
if h.config.DomainStrategy == Config_USE_IP && dialDest.Address.Family().IsDomain() {
ip := h.resolveIP(ctx, dialDest.Address.Domain())
if h.config.useIP() && dialDest.Address.Family().IsDomain() {
ip := h.resolveIP(ctx, dialDest.Address.Domain(), dialer.Address())
if ip != nil {
dialDest = net.Destination{
Network: dialDest.Network,

View File

@ -11,6 +11,9 @@ import (
type Dialer interface {
// Dial dials a system connection to the given destination.
Dial(ctx context.Context, destination net.Destination) (Connection, error)
// Address returns the address used by this Dialer. Maybe nil if not known.
Address() net.Address
}
// dialFunc is an interface to dial network connection to a specific destination.