Seed the random IP address generator
This commit is contained in:
parent
272eee79fd
commit
5d3c80a192
1 changed files with 6 additions and 5 deletions
11
utils.go
11
utils.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
|
|
||||||
|
@ -95,17 +96,17 @@ func (h *Headscale) getAvailableIP() (*net.IP, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRandomIP() (*net.IP, error) {
|
func getRandomIP() (*net.IP, error) {
|
||||||
|
mathrand.Seed(time.Now().Unix())
|
||||||
ipo, ipnet, err := net.ParseCIDR("100.64.0.0/10")
|
ipo, ipnet, err := net.ParseCIDR("100.64.0.0/10")
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ip := ipo.To4()
|
ip := ipo.To4()
|
||||||
fmt.Println("In Randomize IPAddr: IP ", ip, " IPNET: ", ipnet)
|
fmt.Println("In Randomize IPAddr: IP ", ip, " IPNET: ", ipnet)
|
||||||
fmt.Println("Final address is ", ip)
|
fmt.Println("Final address is ", ip)
|
||||||
// fmt.Println("Broadcast address is ", ipb)
|
// fmt.Println("Broadcast address is ", ipb)
|
||||||
// fmt.Println("Network address is ", ipn)
|
// fmt.Println("Network address is ", ipn)
|
||||||
|
r := mathrand.Uint32()
|
||||||
ipRaw := make([]byte, 4)
|
ipRaw := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(ipRaw, mathrand.Uint32())
|
binary.LittleEndian.PutUint32(ipRaw, r)
|
||||||
// ipRaw[3] = 254
|
// ipRaw[3] = 254
|
||||||
// fmt.Println("ipRaw is ", ipRaw)
|
// fmt.Println("ipRaw is ", ipRaw)
|
||||||
for i, v := range ipRaw {
|
for i, v := range ipRaw {
|
||||||
|
@ -115,7 +116,7 @@ func getRandomIP() (*net.IP, error) {
|
||||||
}
|
}
|
||||||
fmt.Println("FINAL IP: ", ip.String())
|
fmt.Println("FINAL IP: ", ip.String())
|
||||||
return &ip, nil
|
return &ip, nil
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue