Skip to content

Commit

Permalink
add: export GetNextIP method
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB committed Dec 6, 2024
1 parent 963f065 commit 2aa9310
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
12 changes: 6 additions & 6 deletions random_local_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 15 additions & 11 deletions random_local_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 2aa9310

Please sign in to comment.