reset_machine.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import os
  2. import json
  3. import uuid
  4. import hashlib
  5. import shutil
  6. from colorama import Fore, Style, init
  7. # 初始化colorama
  8. init()
  9. # 定义emoji和颜色常量
  10. EMOJI = {
  11. "FILE": "📄",
  12. "BACKUP": "💾",
  13. "SUCCESS": "✅",
  14. "ERROR": "❌",
  15. "INFO": "ℹ️",
  16. "RESET": "🔄",
  17. }
  18. class MachineIDResetter:
  19. def __init__(self):
  20. # 判断操作系统
  21. if os.name == "nt": # Windows
  22. self.db_path = os.path.join(
  23. os.getenv("APPDATA"), "Cursor", "User", "globalStorage", "storage.json"
  24. )
  25. else: # macOS
  26. self.db_path = os.path.expanduser(
  27. "~/Library/Application Support/Cursor/User/globalStorage/storage.json"
  28. )
  29. def generate_new_ids(self):
  30. """生成新的机器ID"""
  31. # 生成新的UUID
  32. dev_device_id = str(uuid.uuid4())
  33. # 生成新的machineId (64个字符的十六进制)
  34. machine_id = hashlib.sha256(os.urandom(32)).hexdigest()
  35. # 生成新的macMachineId (128个字符的十六进制)
  36. mac_machine_id = hashlib.sha512(os.urandom(64)).hexdigest()
  37. # 生成新的sqmId
  38. sqm_id = "{" + str(uuid.uuid4()).upper() + "}"
  39. return {
  40. "telemetry.devDeviceId": dev_device_id,
  41. "telemetry.macMachineId": mac_machine_id,
  42. "telemetry.machineId": machine_id,
  43. "telemetry.sqmId": sqm_id,
  44. }
  45. def reset_machine_ids(self):
  46. """重置机器ID并备份原文件"""
  47. try:
  48. print(f"{Fore.CYAN}{EMOJI['INFO']} 正在检查配置文件...{Style.RESET_ALL}")
  49. # 检查文件是否存在
  50. if not os.path.exists(self.db_path):
  51. print(
  52. f"{Fore.RED}{EMOJI['ERROR']} 配置文件不存在: {self.db_path}{Style.RESET_ALL}"
  53. )
  54. return False
  55. # 检查文件权限
  56. if not os.access(self.db_path, os.R_OK | os.W_OK):
  57. print(
  58. f"{Fore.RED}{EMOJI['ERROR']} 无法读写配置文件,请检查文件权限!{Style.RESET_ALL}"
  59. )
  60. return False
  61. # 读取现有配置
  62. print(f"{Fore.CYAN}{EMOJI['FILE']} 读取当前配置...{Style.RESET_ALL}")
  63. with open(self.db_path, "r", encoding="utf-8") as f:
  64. config = json.load(f)
  65. # 只在没有备份文件时创建备份
  66. backup_path = self.db_path + ".bak"
  67. if not os.path.exists(backup_path):
  68. print(
  69. f"{Fore.YELLOW}{EMOJI['BACKUP']} 创建配置备份: {backup_path}{Style.RESET_ALL}"
  70. )
  71. shutil.copy2(self.db_path, backup_path)
  72. else:
  73. print(
  74. f"{Fore.YELLOW}{EMOJI['INFO']} 已存在备份文件,跳过备份步骤{Style.RESET_ALL}"
  75. )
  76. # 生成新的ID
  77. print(f"{Fore.CYAN}{EMOJI['RESET']} 生成新的机器标识...{Style.RESET_ALL}")
  78. new_ids = self.generate_new_ids()
  79. # 更新配置
  80. config.update(new_ids)
  81. # 保存新配置
  82. print(f"{Fore.CYAN}{EMOJI['FILE']} 保存新配置...{Style.RESET_ALL}")
  83. with open(self.db_path, "w", encoding="utf-8") as f:
  84. json.dump(config, f, indent=4)
  85. print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识重置成功!{Style.RESET_ALL}")
  86. print(f"\n{Fore.CYAN}新的机器标识:{Style.RESET_ALL}")
  87. for key, value in new_ids.items():
  88. print(f"{EMOJI['INFO']} {key}: {Fore.GREEN}{value}{Style.RESET_ALL}")
  89. return True
  90. except PermissionError as e:
  91. print(f"{Fore.RED}{EMOJI['ERROR']} 权限错误: {str(e)}{Style.RESET_ALL}")
  92. print(
  93. f"{Fore.YELLOW}{EMOJI['INFO']} 请尝试以管理员身份运行此程序{Style.RESET_ALL}"
  94. )
  95. return False
  96. except Exception as e:
  97. print(f"{Fore.RED}{EMOJI['ERROR']} 重置过程出错: {str(e)}{Style.RESET_ALL}")
  98. return False
  99. if __name__ == "__main__":
  100. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  101. print(f"{Fore.CYAN}{EMOJI['RESET']} Cursor 机器标识重置工具{Style.RESET_ALL}")
  102. print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  103. resetter = MachineIDResetter()
  104. resetter.reset_machine_ids()
  105. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  106. input(f"{EMOJI['INFO']} 按回车键退出...")