diff --git a/random_local_ip.go b/random_local_ip.go index 1b84e16..8c4f31d 100644 --- a/random_local_ip.go +++ b/random_local_ip.go @@ -49,7 +49,7 @@ func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err er newIPv4 := make([]net.IPNet, 0) newIPv6 := make([]net.IPNet, 0) for _, iface := range interfaces { - if strings.Contains(iface.Name, "docker") || !strings.Contains(iface.Flags.String(), "broadcast") || !strings.Contains(iface.Flags.String(), "up") { + if strings.Contains(iface.Name, "docker") || strings.Contains(iface.Flags.String(), "pointopoint") || !strings.Contains(iface.Flags.String(), "up") { continue } @@ -98,7 +98,7 @@ func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err er } } -func getNextIP(availableIPs *availableIPs) net.IP { +func GetNextIP(availableIPs *availableIPs) net.IP { IPsPtr := availableIPs.IPs.Load() if IPsPtr == nil { return nil @@ -132,16 +132,16 @@ func getLocalAddr(network, IP string) any { if destIP.To4() != nil { if strings.Contains(network, "tcp") { - return &net.TCPAddr{IP: getNextIP(IPv4)} + return &net.TCPAddr{IP: GetNextIP(IPv4)} } else if strings.Contains(network, "udp") { - return &net.UDPAddr{IP: getNextIP(IPv4)} + return &net.UDPAddr{IP: GetNextIP(IPv4)} } return nil } else { if strings.Contains(network, "tcp") { - return &net.TCPAddr{IP: getNextIP(IPv6)} + return &net.TCPAddr{IP: GetNextIP(IPv6)} } else if strings.Contains(network, "udp") { - return &net.UDPAddr{IP: getNextIP(IPv6)} + return &net.UDPAddr{IP: GetNextIP(IPv6)} } return nil } diff --git a/random_local_ip_test.go b/random_local_ip_test.go index 020dea7..a7b2f98 100644 --- a/random_local_ip_test.go +++ b/random_local_ip_test.go @@ -110,15 +110,15 @@ func TestGetNextIP(t *testing.T) { availableIPs := &availableIPs{} availableIPs.IPs.Store(&ipList) - ip := getNextIP(availableIPs) + ip := GetNextIP(availableIPs) if !ip.Equal(ip1) { t.Errorf("Expected %v, got %v", ip1, ip) } - ip = getNextIP(availableIPs) + ip = GetNextIP(availableIPs) if !ip.Equal(ip2) { t.Errorf("Expected %v, got %v", ip2, ip) } - ip = getNextIP(availableIPs) + ip = GetNextIP(availableIPs) if !ip.Equal(ip1) { t.Errorf("Expected %v, got %v", ip1, ip) } @@ -128,7 +128,7 @@ func TestGetNextIP(t *testing.T) { func TestGetNextIPEmptyIPs(t *testing.T) { availableIPs := &availableIPs{} availableIPs.IPs.Store(&[]net.IPNet{}) - ip := getNextIP(availableIPs) + ip := GetNextIP(availableIPs) if ip != nil { t.Errorf("Expected nil, got %v", ip) } @@ -142,7 +142,7 @@ func TestGetNextIPAnyIP(t *testing.T) { availableIPs := &availableIPs{AnyIP: true} availableIPs.IPs.Store(&ipList) - ip := getNextIP(availableIPs) + ip := GetNextIP(availableIPs) if ip == nil { t.Error("Expected non-nil IP, got nil") } @@ -162,7 +162,7 @@ func TestGetNextIPAnyIPMultipleIPv6(t *testing.T) { availableIPs.IPs.Store(&ipList) for i := 0; i < 5; i++ { - ip := getNextIP(availableIPs) + ip := GetNextIP(availableIPs) if ip == nil { t.Error("Expected non-nil IP, got nil") } @@ -180,8 +180,8 @@ func TestTestGetNextIPAnyIPv6Randomness(t *testing.T) { availableIPs := &availableIPs{AnyIP: true} availableIPs.IPs.Store(&ipList) - ip1 := getNextIP(availableIPs) - ip2 := getNextIP(availableIPs) + ip1 := GetNextIP(availableIPs) + ip2 := GetNextIP(availableIPs) if ip1.Equal(ip2) { t.Errorf("Expected different IPs, got %v", ip1) } @@ -196,7 +196,7 @@ func TestGetNextIPHighIndex(t *testing.T) { availableIPs.IPs.Store(&ipList) availableIPs.Index.Store(9999999) - ip := getNextIP(availableIPs) + ip := GetNextIP(availableIPs) if !ip.Equal(ip1) { t.Errorf("Expected %v, got %v", ip1, ip) } @@ -361,8 +361,11 @@ func TestGetAvailableIPsAnyIP(t *testing.T) { newIPv4 := make([]net.IPNet, 0) newIPv6 := make([]net.IPNet, 0) for _, iface := range interfaces { - if strings.Contains(iface.Name, "docker") { + if strings.Contains(iface.Name, "docker") || !strings.Contains(iface.Flags.String(), "broadcast") || !strings.Contains(iface.Flags.String(), "up") { continue + } else { + t.Logf("Interface: %v", iface.Name) + t.Logf("Flags: %v", iface.Flags) } // Get the addresses associated with the interface @@ -374,13 +377,14 @@ func TestGetAvailableIPsAnyIP(t *testing.T) { // Iterate over the addresses for _, addr := range addrs { if ipNet, ok := addr.(*net.IPNet); ok { - t.Logf("IPNet: %v", ipNet) ip := ipNet.IP if ip.IsLoopback() { continue } + t.Logf("IPNet: %v", ipNet) + // Process Global Unicast IPv6 addresses if ip.IsGlobalUnicast() && ip.To16() != nil && ip.To4() == nil && ip.IsGlobalUnicast() { newIPv6 = append(newIPv6, *ipNet)