From 51806362f93be185e9d5f51ef34f3cb324a05d17 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Tue, 27 May 2025 20:08:55 -0400 Subject: [PATCH] Simplify ID --- common/config.go | 14 ++------------ common/util.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/common/config.go b/common/config.go index 7590950..02d4fa3 100644 --- a/common/config.go +++ b/common/config.go @@ -2,10 +2,8 @@ package common import ( "fmt" - "math/rand" "strconv" "strings" - "time" "github.com/sirupsen/logrus" ) @@ -69,17 +67,9 @@ func (f *I2PConfig) SetSAMAddress(addr string) { // ID returns the tunnel name as a formatted string. If no tunnel name is set, // generates a random 12-character name using lowercase letters. func (f *I2PConfig) ID() string { - generator := rand.New(rand.NewSource(time.Now().UnixNano())) - // If no tunnel name set, generate random one + // Ensure tunnel name is set, generating if needed if f.TunName == "" { - // Generate 12 random lowercase letters - b := make([]byte, 12) - for i := range b { - b[i] = "abcdefghijklmnopqrstuvwxyz"[generator.Intn(26)] - } - f.TunName = string(b) - - // Log the generated name + f.TunName = f.generateRandomTunnelName() log.WithField("TunName", f.TunName).Debug("Generated random tunnel name") } diff --git a/common/util.go b/common/util.go index d55f942..e8dc5b5 100644 --- a/common/util.go +++ b/common/util.go @@ -97,3 +97,21 @@ func RandPort() (portNumber string, err error) { return "", oops.Errorf("unable to find a pair of available tcp and udp ports in %v attempts", maxAttempts) } + +// generateRandomTunnelName creates a random 12-character tunnel name using lowercase letters. +// This function is deterministic for testing when a fixed seed is used. +func (f *I2PConfig) generateRandomTunnelName() string { + const ( + nameLength = 12 + letters = "abcdefghijklmnopqrstuvwxyz" + ) + + generator := rand.New(rand.NewSource(time.Now().UnixNano())) + name := make([]byte, nameLength) + + for i := range name { + name[i] = letters[generator.Intn(len(letters))] + } + + return string(name) +}