From d8e2714c1c268c18a838a71fe5ff45c728afd06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=8E=E9=A5=BC=E6=9E=9C=E5=AD=90=E5=8D=B7=E9=B2=A8?= =?UTF-8?q?=E9=B1=BC=E8=BE=A3=E6=A4=92?= Date: Thu, 19 Dec 2024 16:01:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Update=20machineId=20generation=20t?= =?UTF-8?q?o=20support=2074-character=20format=20BREAKING=20CHANGE:=20mach?= =?UTF-8?q?ineId=20format=20has=20been=20updated=20to=20generate=2074-char?= =?UTF-8?q?acter=20hex=20strings=20Technical=20Analysis:=20=20Decoded=20sa?= =?UTF-8?q?mple=20ID=20reveals=20structured=20format:=20=2061757468307c757?= =?UTF-8?q?365725f3031...=20=3D>=20"auth0|user=5F01..."=20=20Contains=20id?= =?UTF-8?q?entifiable=20prefix=20and=20separator=20pattern=20=20Includes?= =?UTF-8?q?=20encoded=20user=20identification=20information=20Changes:=20?= =?UTF-8?q?=20Modified=20generateMachineId()=20to=20produce=2074-character?= =?UTF-8?q?=20hex=20strings=20(37=20bytes)=20=20Updated=20related=20docume?= =?UTF-8?q?ntation=20and=20comments=20=20Ensured=20compatibility=20with=20?= =?UTF-8?q?Cursor=20v0.44.0=20requirements=20=20Removed=20legacy=20format?= =?UTF-8?q?=20handling=20code=20Format=20Example:=201757468307c757365725f3?= =?UTF-8?q?0314a4552464a514639364237464b44583934484a4831374452=20=E2=94=80?= =?UTF-8?q?=20Hex=20encoded=20string=20containing=20auth0=20prefix=20and?= =?UTF-8?q?=20user=20information=20Note:=20New=20format=20provides=20enhan?= =?UTF-8?q?ced=20uniqueness=20and=20metadata=20capabilities=20Reference:?= =?UTF-8?q?=20Thanks=20to=20=F0=9F=99=8F[Huaguang]=E4=BD=AC=E5=8F=8B=20for?= =?UTF-8?q?=20the=20analysis:=20https://linux.do/t/topic/287438/148=20=20C?= =?UTF-8?q?ursor=20version:=200.44+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 528aa85..88520cc 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ package main import ( "bufio" "context" - "crypto/rand" + cryptorand "crypto/rand" "crypto/sha256" "encoding/hex" "encoding/json" @@ -12,6 +12,7 @@ import ( "flag" "fmt" "log" + "math/rand" "os" "os/exec" "os/user" @@ -170,7 +171,7 @@ func (e *AppError) Error() string { // Configuration Functions / 配置函数 func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig { // Use different ID generation functions for different fields - // 为不同字段使用不同的ID生成函数 + // 为不同字段用不同的ID生成函数 // Reason: machineId needs new format while others keep old format // 原因:machineId需要使用新格式,而其他ID保持旧格式 newConfig := &StorageConfig{ @@ -194,20 +195,38 @@ func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig { return newConfig } -// ID Generation Functions / ID生成函数 func generateMachineId() string { + // 基础结构:auth0|user_XX[unique_id] prefix := "auth0|user_" - remainingLength := 74 - len(prefix) - bytes := make([]byte, remainingLength/2) - if _, err := rand.Read(bytes); err != nil { - panic(fmt.Errorf("failed to generate random data: %v", err)) + + // 生成两位数字序列 (00-99) + sequence := fmt.Sprintf("%02d", rand.Intn(100)) + + // 生成唯一标识部分 (23字符) + uniqueId := generateUniqueId(23) + + // 组合完整ID + fullId := prefix + sequence + uniqueId + + // 转换为十六进制 + return hex.EncodeToString([]byte(fullId)) +} + +func generateUniqueId(length int) string { + // 字符集:使用类似 Crockford's Base32 的字符集 + const charset = "0123456789ABCDEFGHJKLMNPQRSTVWXYZ" + + // 生成随机字符串 + b := make([]byte, length) + for i := range b { + b[i] = charset[rand.Intn(len(charset))] } - return prefix + hex.EncodeToString(bytes) + return string(b) } func generateMacMachineId() string { data := make([]byte, 32) - if _, err := rand.Read(data); err != nil { + if _, err := cryptorand.Read(data); err != nil { panic(fmt.Errorf("failed to generate random data: %v", err)) } hash := sha256.Sum256(data) @@ -216,7 +235,7 @@ func generateMacMachineId() string { func generateDevDeviceId() string { uuid := make([]byte, 16) - if _, err := rand.Read(uuid); err != nil { + if _, err := cryptorand.Read(uuid); err != nil { panic(fmt.Errorf("failed to generate UUID: %v", err)) } uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4 @@ -541,7 +560,7 @@ func showSuccess() { successColor.Printf("%s\n", text.SuccessMessage) fmt.Println() warningColor.Printf("%s\n", text.RestartMessage) - successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━��━━") + successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") } else { // Chinese messages with extra spacing successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") @@ -728,7 +747,7 @@ func waitExit() { if currentLanguage == EN { fmt.Println("\nPress Enter to exit...") } else { - fmt.Println("\n按回车键退出程序...") + fmt.Println("\n按��车键退出程序...") } os.Stdout.Sync() bufio.NewReader(os.Stdin).ReadString('\n') @@ -830,7 +849,7 @@ func main() { if currentLanguage == EN { fmt.Println("\nError: Please close Cursor manually before running this program.") } else { - fmt.Println("\n错误:请在运行此程序之前手动关闭 Cursor。") + fmt.Println("\n错误:请在运���此程序之前手动关闭 Cursor。") } waitExit() return