From 5d3c80a1929ccec4125e7045b99c2ec5f7199e44 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sun, 21 Feb 2021 22:11:27 +0100 Subject: [PATCH] Seed the random IP address generator --- utils.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/utils.go b/utils.go index 178d5e8d..e787b1d7 100644 --- a/utils.go +++ b/utils.go @@ -13,6 +13,7 @@ import ( "fmt" "io" "net" + "time" mathrand "math/rand" @@ -95,17 +96,17 @@ func (h *Headscale) getAvailableIP() (*net.IP, error) { } func getRandomIP() (*net.IP, error) { + mathrand.Seed(time.Now().Unix()) ipo, ipnet, err := net.ParseCIDR("100.64.0.0/10") - if err == nil { ip := ipo.To4() fmt.Println("In Randomize IPAddr: IP ", ip, " IPNET: ", ipnet) fmt.Println("Final address is ", ip) // fmt.Println("Broadcast address is ", ipb) // fmt.Println("Network address is ", ipn) - + r := mathrand.Uint32() ipRaw := make([]byte, 4) - binary.LittleEndian.PutUint32(ipRaw, mathrand.Uint32()) + binary.LittleEndian.PutUint32(ipRaw, r) // ipRaw[3] = 254 // fmt.Println("ipRaw is ", ipRaw) for i, v := range ipRaw { @@ -115,7 +116,7 @@ func getRandomIP() (*net.IP, error) { } fmt.Println("FINAL IP: ", ip.String()) return &ip, nil - } else { - return nil, err } + + return nil, err }