From 2a69ffa663cea93c523a793df4ea7d01d3e14bc0 Mon Sep 17 00:00:00 2001 From: MickaelK Date: Tue, 23 Jan 2024 00:45:26 +1100 Subject: [PATCH] fix (id): work with multiple containers --- common/random.go | 22 ++++++++++++++++++++++ ctrl/tunnel.go | 18 ------------------ webfleet/model/machine.go | 6 +++--- 3 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 common/random.go diff --git a/common/random.go b/common/random.go new file mode 100644 index 0000000..a4f9719 --- /dev/null +++ b/common/random.go @@ -0,0 +1,22 @@ +package common + +import ( + "crypto/rand" + "math/big" +) + +var Letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + +func RandomString(n int) string { + b := make([]rune, n) + for i := range b { + max := *big.NewInt(int64(len(Letters))) + r, err := rand.Int(rand.Reader, &max) + if err != nil { + b[i] = Letters[0] + } else { + b[i] = Letters[r.Int64()] + } + } + return string(b) +} diff --git a/ctrl/tunnel.go b/ctrl/tunnel.go index 0cff354..4deb98a 100644 --- a/ctrl/tunnel.go +++ b/ctrl/tunnel.go @@ -2,11 +2,9 @@ package ctrl import ( "context" - "crypto/rand" "crypto/tls" "fmt" "io/ioutil" - "math/big" "net/http" "time" @@ -131,19 +129,3 @@ func setup(url string, tenant string, jsonInfo []byte, retry int) error { ws.Close() return nil } - -var Letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789") - -func RandomString(n int) string { - b := make([]rune, n) - for i := range b { - max := *big.NewInt(int64(len(Letters))) - r, err := rand.Int(rand.Reader, &max) - if err != nil { - b[i] = Letters[0] - } else { - b[i] = Letters[r.Int64()] - } - } - return string(b) -} diff --git a/webfleet/model/machine.go b/webfleet/model/machine.go index 64fe19d..dfec07f 100644 --- a/webfleet/model/machine.go +++ b/webfleet/model/machine.go @@ -44,10 +44,10 @@ func GetMachineInfo() []byte { content, err := os.ReadFile("/etc/machine-id") if err != nil { return "" - } else if string(content) == "" { - content, _ = os.ReadFile("/proc/sys/kernel/random/uuid") + } else if string(content) != "" { + return strings.TrimSpace(string(content)) } - return strings.TrimSpace(string(content)) + return RandomString(5) }(), Device: func() string { if isMac() {