mirror of
https://github.com/go-i2p/go-sam-go.git
synced 2025-06-15 21:28:46 -04:00
Simplify ID
This commit is contained in:
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user