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

use ip if already resolved

This commit is contained in:
Darien Raymond 2017-11-15 12:55:54 +01:00
parent 26f005e822
commit 23f209075f
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -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 {