Append UNIX timestamp to device ID to guarantee uniqueness

This commit is contained in:
2024-07-09 16:42:47 -04:00
parent 98308782a9
commit e056dcf4ba
+4 -1
View File
@@ -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 {