-
Notifications
You must be signed in to change notification settings - Fork 4
/
random_local_ip.go
187 lines (155 loc) · 4.05 KB
/
random_local_ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package warc
import (
"crypto/rand"
"fmt"
"net"
"strings"
"sync/atomic"
"time"
)
var (
IPv6 *availableIPs
IPv4 *availableIPs
)
type availableIPs struct {
IPs atomic.Pointer[[]net.IPNet]
Index atomic.Uint64
AnyIP bool
}
func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err error) {
var first = true
if IPv6 == nil {
IPv6 = &availableIPs{
AnyIP: IPv6AnyIP,
}
}
if IPv4 == nil {
IPv4 = &availableIPs{}
}
for {
select {
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") || strings.Contains(iface.Flags.String(), "pointtopoint") || !strings.Contains(iface.Flags.String(), "up") {
continue
}
// 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 {
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.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)
if first {
c.interfacesWatcherStarted <- true
close(c.interfacesWatcherStarted)
first = false
}
time.Sleep(time.Second)
}
}
}
func GetNextIP(availableIPs *availableIPs) net.IP {
IPsPtr := availableIPs.IPs.Load()
if IPsPtr == nil {
return nil
}
IPs := *IPsPtr
if len(IPs) == 0 {
return nil
}
currentIndex := availableIPs.Index.Add(1) - 1
ipNet := IPs[currentIndex%uint64(len(IPs))]
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 {
IP = strings.Trim(IP, "[]")
destIP := net.ParseIP(IP)
if destIP == nil {
return nil
}
if destIP.To4() != nil {
if strings.Contains(network, "tcp") {
return &net.TCPAddr{IP: GetNextIP(IPv4)}
} else if strings.Contains(network, "udp") {
return &net.UDPAddr{IP: GetNextIP(IPv4)}
}
return nil
} else {
if strings.Contains(network, "tcp") {
return &net.TCPAddr{IP: GetNextIP(IPv6)}
} else if strings.Contains(network, "udp") {
return &net.UDPAddr{IP: GetNextIP(IPv6)}
}
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
}