From 23f209075f7d78bbb97737448ac1cef2214d654c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 15 Nov 2017 12:55:54 +0100 Subject: [PATCH] use ip if already resolved --- proxy/freedom/freedom.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index ea28a8d1..c249c646 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -49,25 +49,20 @@ func New(ctx context.Context, config *Config) (*Handler, error) { return f, nil } -func (v *Handler) ResolveIP(destination net.Destination) net.Destination { - if !destination.Address.Family().IsDomain() { - return destination +func (v *Handler) ResolveIP(ctx context.Context, domain string) net.Address { + if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok { + ips := resolver.Resolve() + if len(ips) == 0 { + return nil + } + return ips[dice.Roll(len(ips))] } - ips := v.dns.Get(destination.Address.Domain()) + ips := v.dns.Get(domain) if len(ips) == 0 { - log.Trace(newError("DNS returns nil answer. Keep domain as is.")) - return destination + return nil } - - ip := ips[dice.Roll(len(ips))] - newDest := net.Destination{ - Network: destination.Network, - Address: net.IPAddress(ip), - Port: destination.Port, - } - log.Trace(newError("changing destination from ", destination, " to ", newDest)) - return newDest + return net.IPAddress(ips[dice.Roll(len(ips))]) } func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error { @@ -87,7 +82,15 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial var conn internet.Connection if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() { - destination = v.ResolveIP(destination) + ip := v.ResolveIP(ctx, destination.Address.Domain()) + if ip != nil { + destination = net.Destination{ + Network: destination.Network, + Address: ip, + Port: destination.Port, + } + log.Trace(newError("changing destination to ", destination)) + } } err := retry.ExponentialBackoff(5, 100).On(func() error {