Skip to content

Commit

Permalink
reconciliate after fork sync + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
equals215 committed Sep 26, 2024
1 parent f2ba784 commit 0f5deaf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
117 changes: 50 additions & 67 deletions random_local_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,67 +38,67 @@ func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err er
case <-c.interfacesWatcherStop:
return nil, nil
default:
// Get all network interfaces
interfaces, err := net.Interfaces()
if err != nil {
time.Sleep(time.Second)
continue
}

// Iterate over the interfaces
newIPv4 := make([]net.IPNet, 0)
newIPv6 := make([]net.IPNet, 0)
for _, iface := range interfaces {
if strings.Contains(iface.Name, "docker") {
continue
}

// Get the addresses associated with the interface
addrs, err := iface.Addrs()
// Get all network interfaces
interfaces, err := net.Interfaces()
if err != nil {
time.Sleep(time.Second)
continue
}

// Iterate over the addresses
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
ip := ipNet.IP
// Iterate over the interfaces
newIPv4 := make([]net.IPNet, 0)
newIPv6 := make([]net.IPNet, 0)
for _, iface := range interfaces {
if strings.Contains(iface.Name, "docker") {
continue
}

if ip.IsLoopback() {
continue
}
// Get the addresses associated with the interface
addrs, err := iface.Addrs()
if err != nil {
time.Sleep(time.Second)
continue
}

// Process Global Unicast IPv6 addresses
if ip.IsGlobalUnicast() && ip.To16() != nil && ip.To4() == nil {
newIPv6 = append(newIPv6, *ipNet)
// Iterate over the addresses
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
ip := ipNet.IP

if ip.IsLoopback() {
continue
}

// Process Global Unicast IPv6 addresses
if ip.IsGlobalUnicast() && ip.To16() != nil && ip.To4() == nil {
newIPv6 = append(newIPv6, *ipNet)
}

// Process Global Unicast IPv4 addresses
if ip.IsGlobalUnicast() && ip.To16() == nil && ip.To4() != nil {
// Add IPv4 addresses to the list
newIPv4 = append(newIPv4, *ipNet)
}
}
}

// Process Global Unicast IPv4 addresses
if ip.IsGlobalUnicast() && ip.To16() == nil && ip.To4() != nil {
// Add IPv4 addresses to the list
newIPv4 = append(newIPv4, *ipNet)
}
if first {
c.interfacesWatcherStarted <- true
close(c.interfacesWatcherStarted)
first = false
}
}

if first {
c.interfacesWatcherStarted <- true
close(c.interfacesWatcherStarted)
first = false
time.Sleep(time.Second)
}

// Add the new addresses to the list
IPv6.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)

time.Sleep(time.Second)
}

// Add the new addresses to the list
IPv6.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)

time.Sleep(time.Second)
}
}
}

func getNextIP(availableIPs *availableIPs) net.IP {
IPsPtr := availableIPs.IPs.Load()
Expand All @@ -124,49 +124,32 @@ func getNextIP(availableIPs *availableIPs) net.IP {
return ipNet.IP
}

<<<<<<< HEAD
func getLocalAddr(destNetwork, destAddress string) any {
lastColon := strings.LastIndex(destAddress, ":")
func getLocalAddr(network, IP string) any {
lastColon := strings.LastIndex(IP, ":")
if lastColon == -1 {
return nil
}
destAddr := destAddress[:lastColon]

destAddr = strings.TrimPrefix(destAddr, "[")
destAddr = strings.TrimSuffix(destAddr, "]")
ip := IP[:lastColon]
ip = strings.TrimPrefix(ip, "[")
ip = strings.TrimSuffix(ip, "]")

destIP := net.ParseIP(destAddr)
=======
func getLocalAddr(network, IP string) any {
destIP := net.ParseIP(strings.Trim(IP, "[]"))
>>>>>>> upstream/master
destIP := net.ParseIP(ip)
if destIP == nil {
return nil
}

if destIP.To4() != nil {
<<<<<<< HEAD
if strings.Contains(destNetwork, "tcp") {
return &net.TCPAddr{IP: getNextIP(IPv4)}
} else if strings.Contains(destNetwork, "udp") {
=======
if strings.Contains(network, "tcp") {
return &net.TCPAddr{IP: getNextIP(IPv4)}
} else if strings.Contains(network, "udp") {
>>>>>>> upstream/master
return &net.UDPAddr{IP: getNextIP(IPv4)}
}
return nil
} else {
<<<<<<< HEAD
if strings.Contains(destNetwork, "tcp") {
return &net.TCPAddr{IP: getNextIP(IPv6)}
} else if strings.Contains(destNetwork, "udp") {
=======
if strings.Contains(network, "tcp") {
return &net.TCPAddr{IP: getNextIP(IPv6)}
} else if strings.Contains(network, "udp") {
>>>>>>> upstream/master
return &net.UDPAddr{IP: getNextIP(IPv6)}
}
return nil
Expand Down
19 changes: 18 additions & 1 deletion random_local_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestGetLocalAddrIPv6TCP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

addr := getLocalAddr("tcp", "[2001:db8::2]:80")
addr := getLocalAddr("tcp6", "[2001:db8::2]:80")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
Expand All @@ -238,6 +238,23 @@ func TestGetLocalAddrIPv6TCP(t *testing.T) {
}
}

func TestGetLocalAddrIPv6TCPAnyIP(t *testing.T) {
IPv6 = &availableIPs{AnyIP: true}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

addr := getLocalAddr("tcp6", "[2001:db12::20]:80")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
}
if !ipNet1.Contains(tcpAddr.IP) {
t.Errorf("Expected IP within %v, got %v", ipNet1, tcpAddr.IP)
}
}

// TestGetLocalAddrIPv4UDP tests local address selection for IPv4 UDP connections.
func TestGetLocalAddrIPv4UDP(t *testing.T) {
IPv4 = &availableIPs{}
Expand Down

0 comments on commit 0f5deaf

Please sign in to comment.