Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnyIP capabilities to randomly bind to any IPv6 in the assigned prefix #48

Merged
merged 15 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type HTTPClientSettings struct {
RandomLocalIP bool
DisableIPv4 bool
DisableIPv6 bool
IPv6AnyIP bool
}

type CustomHTTPClient struct {
Expand Down Expand Up @@ -89,7 +90,7 @@ func NewWARCWritingHTTPClient(HTTPClientSettings HTTPClientSettings) (httpClient
if httpClient.randomLocalIP {
httpClient.interfacesWatcherStop = make(chan bool)
httpClient.interfacesWatcherStarted = make(chan bool)
go httpClient.getAvailableIPs()
go httpClient.getAvailableIPs(HTTPClientSettings.IPv6AnyIP)
<-httpClient.interfacesWatcherStarted
}

Expand Down
120 changes: 88 additions & 32 deletions random_local_ip.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package warc

import (
"crypto/rand"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
)
Expand All @@ -14,16 +15,18 @@ var (
)

type availableIPs struct {
IPs []net.IP
sync.Mutex
Index uint32
IPs atomic.Pointer[[]net.IPNet]
Index atomic.Uint64
AnyIP bool
}

func (c *CustomHTTPClient) getAvailableIPs() (IPs []net.IP, err error) {
func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err error) {
var first = true

if IPv6 == nil {
IPv6 = &availableIPs{}
IPv6 = &availableIPs{
AnyIP: IPv6AnyIP,
}
}

if IPv4 == nil {
Expand All @@ -43,8 +46,8 @@ func (c *CustomHTTPClient) getAvailableIPs() (IPs []net.IP, err error) {
}

// Iterate over the interfaces
var newIPv4 []net.IP
var newIPv6 []net.IP
newIPv4 := make([]net.IPNet, 0)
newIPv6 := make([]net.IPNet, 0)
for _, iface := range interfaces {
if strings.Contains(iface.Name, "docker") {
continue
Expand All @@ -53,34 +56,36 @@ func (c *CustomHTTPClient) getAvailableIPs() (IPs []net.IP, err error) {
// Get the addresses associated with the interface
addrs, err := iface.Addrs()
if err != nil {
time.Sleep(time.Second)
continue
}

// Iterate over the addresses
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() {
// Add IPv6 addresses to the list
if ipNet.IP.IsGlobalUnicast() {
if ipNet.IP.To4() == nil && !strings.HasPrefix(ipNet.IP.String(), "fe80") {
newIPv6 = append(newIPv6, ipNet.IP)
} else if ipNet.IP.To4() != nil {
// Add IPv4 addresses to the list
newIPv4 = append(newIPv4, ipNet.IP)
}
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)
}
}
}
}

// Add the new addresses to the list
IPv6.Lock()
IPv6.IPs = newIPv6
IPv6.Unlock()

IPv4.Lock()
IPv4.IPs = newIPv4
IPv4.Unlock()
IPv6.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)

if first {
c.interfacesWatcherStarted <- true
Expand All @@ -94,21 +99,33 @@ func (c *CustomHTTPClient) getAvailableIPs() (IPs []net.IP, err error) {
}

func getNextIP(availableIPs *availableIPs) net.IP {
availableIPs.Lock()
defer availableIPs.Unlock()
IPsPtr := availableIPs.IPs.Load()
if IPsPtr == nil {
return nil
}

if len(availableIPs.IPs) == 0 {
IPs := *IPsPtr
if len(IPs) == 0 {
return nil
}

currentIndex := atomic.AddUint32(&availableIPs.Index, 1) - 1
ip := availableIPs.IPs[int(currentIndex)%len(availableIPs.IPs)]
currentIndex := availableIPs.Index.Add(1) - 1
ipNet := IPs[currentIndex%uint64(len(IPs))]

return ip
if availableIPs.AnyIP && ipNet.IP.To4() == nil && ipNet.IP.To16() != nil {
ip, err := generateRandomIPv6(ipNet)
if err == nil {
return ip
}
}

return ipNet.IP
}

func getLocalAddr(network, IP string) any {
destIP := net.ParseIP(strings.Trim(IP, "[]"))
IP = strings.Trim(IP, "[]")

destIP := net.ParseIP(IP)
if destIP == nil {
return nil
}
Expand All @@ -129,3 +146,42 @@ func getLocalAddr(network, IP string) any {
return nil
}
}

func generateRandomIPv6(baseIPv6Net net.IPNet) (net.IP, error) {
baseIP := baseIPv6Net.IP.To16()
if baseIP == nil || len(baseIPv6Net.Mask) != net.IPv6len {
return nil, fmt.Errorf("invalid base IPv6 address or mask")
}

ones, bits := baseIPv6Net.Mask.Size()
if bits != 128 || ones < 0 || ones > bits {
return nil, fmt.Errorf("invalid network mask length")
}

hostBits := bits - ones

// Generate random host bits
nBytes := (hostBits + 7) / 8 // Number of bytes needed for host bits
randomBytes := make([]byte, nBytes)
_, err := rand.Read(randomBytes)
if err != nil {
return nil, fmt.Errorf("failed to generate random bits: %v", err)
}

// Mask the random bytes if hostBits is not a multiple of 8
if hostBits%8 != 0 {
extraBits := 8 - (hostBits % 8)
randomBytes[0] = randomBytes[0] & (0xFF >> extraBits)
}

// Construct the randomized IP address
randomizedIP := make(net.IP, net.IPv6len)
copy(randomizedIP, baseIP.Mask(baseIPv6Net.Mask)) // Copy the network prefix

// Apply the random host bits to the randomized IP
for i := 0; i < nBytes; i++ {
randomizedIP[16-nBytes+i] |= randomBytes[i]
}

return randomizedIP, nil
}
Loading
Loading