mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-09-05 08:37:25 -04:00
Append UNIX timestamp to device ID to guarantee uniqueness
This commit is contained in:
+4
-1
@@ -5,15 +5,18 @@ import (
|
|||||||
"github.com/rwinkhart/MUTN/src/backend"
|
"github.com/rwinkhart/MUTN/src/backend"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeviceIDGen generates a new client device ID and registers it with the server
|
// DeviceIDGen generates a new client device ID and registers it with the server
|
||||||
// device IDs are only needed for online synchronization
|
// 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)
|
// returns the remote EntryRoot and OS type (OS type is a bool: backend.IsWindows)
|
||||||
func DeviceIDGen() (string, string) {
|
func DeviceIDGen() (string, string) {
|
||||||
deviceIDPrefix, _ := os.Hostname()
|
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
|
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)
|
_, 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 {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user