diff --git a/libv2ray_support.go b/libv2ray_support.go index 4ad63e9..44672e0 100644 --- a/libv2ray_support.go +++ b/libv2ray_support.go @@ -135,18 +135,27 @@ func (d *ProtectedDialer) lookupAddr(addr string) (*resolved, error) { return nil, fmt.Errorf("domain %s Failed to resolve", addr) } - IPs := make([]net.IP, len(addrs)) - for i, ia := range addrs { - IPs[i] = ia.IP + IPs := make([]net.IP, 0) + //ipv6 is prefer, append ipv6 then ipv4 + //ipv6 is not prefer, append ipv4 then ipv6 + if(d.preferIPv6) { + for _, ia := range addrs { + if(ia.IP.To4() == nil) { + IPs = append(IPs, ia.IP) + } + } } - // LookupIPAddr returns a slice of IPs with IPv6 addrs in front, - // if user perfer not IPv6, revert the result so that IPv4 addr comes first - if !d.preferIPv6 && len(IPs) > 1 && IPs[0].To4() == nil && IPs[len(IPs)-1].To4() != nil { - for i := len(IPs)/2 - 1; i >= 0; i-- { - opp := len(IPs) - 1 - i - IPs[i], IPs[opp] = IPs[opp], IPs[i] + for _, ia := range addrs { + if(ia.IP.To4() != nil) { + IPs = append(IPs, ia.IP) } - log.Printf("PrepareDomain Prefer NOT IPv6 %v\n", IPs) + } + if(!d.preferIPv6) { + for _, ia := range addrs { + if(ia.IP.To4() == nil) { + IPs = append(IPs, ia.IP) + } + } } rs := &resolved{