Skip to content

Commit

Permalink
refactor: update ID generation methods and remove obsolete test file
Browse files Browse the repository at this point in the history
- Renamed the GenerateMacMachineID method to GenerateSQMID and updated its implementation to generate a new SQM ID in UUID format.
- Enhanced the ID generation logic by introducing a buffer pool for efficient memory management.
- Removed the generator_test.go file as it was no longer needed, streamlining the codebase.
- Updated the generateNewConfig function to utilize the new SQMID generation method, ensuring consistency across ID generation processes.
  • Loading branch information
yuaotian committed Jan 1, 2025
1 parent 15d8210 commit 4683460
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 81 deletions.
2 changes: 1 addition & 1 deletion cmd/cursor-id-modifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func generateNewConfig(display *ui.Display, generator *idgen.Generator, oldConfi

if oldConfig != nil && oldConfig.TelemetrySqmId != "" {
newConfig.TelemetrySqmId = oldConfig.TelemetrySqmId
} else if sqmID, err := generator.GenerateMacMachineID(); err != nil {
} else if sqmID, err := generator.GenerateSQMID(); err != nil {
log.Fatal("Failed to generate SQM ID:", err)
} else {
newConfig.TelemetrySqmId = sqmID
Expand Down
108 changes: 75 additions & 33 deletions pkg/idgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,113 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
"sync"
)

// Generator handles secure ID generation for machines and devices
type Generator struct{}
type Generator struct {
bufferPool sync.Pool
}

// NewGenerator creates a new ID generator
func NewGenerator() *Generator {
return &Generator{}
return &Generator{
bufferPool: sync.Pool{
New: func() interface{} {
return make([]byte, 64)
},
},
}
}

// Helper methods
// -------------

// simulateWork adds a small delay to make progress visible
func (g *Generator) simulateWork() {
time.Sleep(800 * time.Millisecond)
}
// Constants for ID generation
const (
machineIDPrefix = "auth0|user_"
uuidFormat = "%s-%s-%s-%s-%s"
)

// generateRandomHex generates a random hex string of specified length
func (g *Generator) generateRandomHex(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
buffer := g.bufferPool.Get().([]byte)
defer g.bufferPool.Put(buffer)

if _, err := rand.Read(buffer[:length]); err != nil {
return "", fmt.Errorf("failed to generate random bytes: %w", err)
}
return hex.EncodeToString(bytes), nil
return hex.EncodeToString(buffer[:length]), nil
}

// Public methods
// -------------

// GenerateMachineID generates a new machine ID with auth0|user_ prefix
func (g *Generator) GenerateMachineID() (string, error) {
g.simulateWork()

// 生成随机部分 (25字节,将产生50个十六进制字符)
randomPart, err := g.generateRandomHex(25)
randomPart, err := g.generateRandomHex(32) // 生成64字符的十六进制
if err != nil {
return "", err
}

// 构建完整的ID: "auth0|user_" + random
prefix := "auth0|user_"
fullID := fmt.Sprintf("%x%x%s",
[]byte(prefix), // 转换前缀为十六进制
[]byte("0"), // 添加一个字符
randomPart, // 随机部分
)

return fullID, nil
return fmt.Sprintf("%x%s", []byte(machineIDPrefix), randomPart), nil
}

// GenerateMacMachineID generates a new 64-byte MAC machine ID
func (g *Generator) GenerateMacMachineID() (string, error) {
g.simulateWork()
return g.generateRandomHex(64)
return g.generateRandomHex(32) // 生成64字符的十六进制
}

// GenerateDeviceID generates a new device ID in UUID format
func (g *Generator) GenerateDeviceID() (string, error) {
g.simulateWork()
id, err := g.generateRandomHex(16)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s-%s-%s-%s",
return fmt.Sprintf(uuidFormat,
id[0:8], id[8:12], id[12:16], id[16:20], id[20:32]), nil
}

// GenerateSQMID generates a new SQM ID in UUID format (with braces)
func (g *Generator) GenerateSQMID() (string, error) {
id, err := g.GenerateDeviceID()
if err != nil {
return "", err
}
return fmt.Sprintf("{%s}", id), nil
}

// ValidateID validates the format of various ID types
func (g *Generator) ValidateID(id string, idType string) bool {
switch idType {
case "machineID", "macMachineID":
return len(id) == 64 && isHexString(id)
case "deviceID":
return isValidUUID(id)
case "sqmID":
if len(id) < 2 || id[0] != '{' || id[len(id)-1] != '}' {
return false
}
return isValidUUID(id[1 : len(id)-1])
default:
return false
}
}

// Helper functions
func isHexString(s string) bool {
_, err := hex.DecodeString(s)
return err == nil
}

func isValidUUID(uuid string) bool {
if len(uuid) != 36 {
return false
}
for i, r := range uuid {
if i == 8 || i == 13 || i == 18 || i == 23 {
if r != '-' {
return false
}
continue
}
if !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'f') || (r >= 'A' && r <= 'F')) {
return false
}
}
return true
}
47 changes: 0 additions & 47 deletions pkg/idgen/generator_test.go

This file was deleted.

0 comments on commit 4683460

Please sign in to comment.