Skip to content

Commit

Permalink
new: make random more random in parallel use
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jul 17, 2024
1 parent 6c534fd commit 4f3122e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion iputils/iputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"time"
)

func RandomSeed() int64 {
var seed = rand.Int63()
seed ^= time.Now().UnixNano()
return seed
}

// RandomIPFromPrefix returns a random IP from the provided CIDR prefix.
// Supports IPv4 and IPv6. Does not support mapped inputs.
func RandomIPFromPrefix(cidr netip.Prefix) (netip.Addr, error) {
Expand All @@ -25,7 +31,7 @@ func RandomIPFromPrefix(cidr netip.Prefix) (netip.Addr, error) {
}

// Initialise rand number generator
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
rng := rand.New(rand.NewSource(RandomSeed()))

// Find the bit length of the Host portion of the provided CIDR
// prefix
Expand Down
2 changes: 1 addition & 1 deletion warp/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func RandomWarpPrefix(v4, v6 bool) netip.Prefix {
}

cidrs := WarpPrefixes()
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
rng := rand.New(rand.NewSource(iputils.RandomSeed()))
for {
cidr := cidrs[rng.Intn(len(cidrs))]

Expand Down

0 comments on commit 4f3122e

Please sign in to comment.