From e056dcf4ba0f839095d4449190c56b7e79f60fe7 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Tue, 9 Jul 2024 16:42:47 -0400 Subject: [PATCH] Append UNIX timestamp to device ID to guarantee uniqueness --- src/sync/init.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sync/init.go b/src/sync/init.go index 7efbbe9..4c91539 100644 --- a/src/sync/init.go +++ b/src/sync/init.go @@ -5,15 +5,18 @@ import ( "github.com/rwinkhart/MUTN/src/backend" "math/rand" "os" + "strconv" "strings" + "time" ) // DeviceIDGen generates a new client device ID and registers it with the server // device IDs are only needed for online synchronization +// device IDs are guaranteed unique as the current UNIX time is appended to them // returns the remote EntryRoot and OS type (OS type is a bool: backend.IsWindows) func DeviceIDGen() (string, string) { deviceIDPrefix, _ := os.Hostname() - deviceIDSuffix := backend.StringGen(rand.Intn(48)+48, false, 0) // TODO consider using complex string generator and removing unsafe characters manually + deviceIDSuffix := backend.StringGen(rand.Intn(32)+48, false, 0) + "-" + strconv.FormatInt(time.Now().Unix(), 10) // TODO consider using complex string generator and removing unsafe characters manually deviceID := deviceIDPrefix + "-" + deviceIDSuffix _, err := os.Create(backend.ConfigDir + backend.PathSeparator + "devices" + backend.PathSeparator + deviceID) // TODO remove existing device ID file if it exists (from both client and server) if err != nil {