reset_machine.py 4.9 KB

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