reset_machine.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(
  31. os.path.expanduser(
  32. "~/Library/Application Support/Cursor/User/globalStorage/storage.json"
  33. )
  34. )
  35. elif sys.platform == "linux": # Linux 和其他类Unix系统
  36. self.db_path = os.path.abspath(
  37. os.path.expanduser("~/.config/Cursor/User/globalStorage/storage.json")
  38. )
  39. else:
  40. raise NotImplementedError(f"不支持的操作系统: {sys.platform}")
  41. def generate_new_ids(self):
  42. """生成新的机器ID"""
  43. # 生成新的UUID
  44. dev_device_id = str(uuid.uuid4())
  45. # 生成新的machineId (64个字符的十六进制)
  46. machine_id = hashlib.sha256(os.urandom(32)).hexdigest()
  47. # 生成新的macMachineId (128个字符的十六进制)
  48. mac_machine_id = hashlib.sha512(os.urandom(64)).hexdigest()
  49. # 生成新的sqmId
  50. sqm_id = "{" + str(uuid.uuid4()).upper() + "}"
  51. return {
  52. "telemetry.devDeviceId": dev_device_id,
  53. "telemetry.macMachineId": mac_machine_id,
  54. "telemetry.machineId": machine_id,
  55. "telemetry.sqmId": sqm_id,
  56. }
  57. def reset_machine_ids(self):
  58. """重置机器ID并备份原文件"""
  59. try:
  60. print(f"{Fore.CYAN}{EMOJI['INFO']} 正在检查配置文件...{Style.RESET_ALL}")
  61. # 检查文件是否存在
  62. if not os.path.exists(self.db_path):
  63. print(
  64. f"{Fore.RED}{EMOJI['ERROR']} 配置文件不存在: {self.db_path}{Style.RESET_ALL}"
  65. )
  66. return False
  67. # 检查文件权限
  68. if not os.access(self.db_path, os.R_OK | os.W_OK):
  69. print(
  70. f"{Fore.RED}{EMOJI['ERROR']} 无法读写配置文件,请检查文件权限!{Style.RESET_ALL}"
  71. )
  72. print(
  73. f"{Fore.RED}{EMOJI['ERROR']} 如果你使用过 go-cursor-help 来修改 ID; 请修改文件只读权限 {self.db_path} {Style.RESET_ALL}"
  74. )
  75. return False
  76. # 读取现有配置
  77. print(f"{Fore.CYAN}{EMOJI['FILE']} 读取当前配置...{Style.RESET_ALL}")
  78. with open(self.db_path, "r", encoding="utf-8") as f:
  79. config = json.load(f)
  80. # 生成新的ID
  81. print(f"{Fore.CYAN}{EMOJI['RESET']} 生成新的机器标识...{Style.RESET_ALL}")
  82. new_ids = self.generate_new_ids()
  83. # 更新配置
  84. config.update(new_ids)
  85. # 保存新配置
  86. print(f"{Fore.CYAN}{EMOJI['FILE']} 保存新配置...{Style.RESET_ALL}")
  87. with open(self.db_path, "w", encoding="utf-8") as f:
  88. json.dump(config, f, indent=4)
  89. print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识重置成功!{Style.RESET_ALL}")
  90. print(f"\n{Fore.CYAN}新的机器标识:{Style.RESET_ALL}")
  91. for key, value in new_ids.items():
  92. print(f"{EMOJI['INFO']} {key}: {Fore.GREEN}{value}{Style.RESET_ALL}")
  93. return True
  94. except PermissionError as e:
  95. print(f"{Fore.RED}{EMOJI['ERROR']} 权限错误: {str(e)}{Style.RESET_ALL}")
  96. print(
  97. f"{Fore.YELLOW}{EMOJI['INFO']} 请尝试以管理员身份运行此程序{Style.RESET_ALL}"
  98. )
  99. return False
  100. except Exception as e:
  101. print(f"{Fore.RED}{EMOJI['ERROR']} 重置过程出错: {str(e)}{Style.RESET_ALL}")
  102. return False
  103. if __name__ == "__main__":
  104. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  105. print(f"{Fore.CYAN}{EMOJI['RESET']} Cursor 机器标识重置工具{Style.RESET_ALL}")
  106. print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  107. resetter = MachineIDResetter()
  108. resetter.reset_machine_ids()
  109. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  110. input(f"{EMOJI['INFO']} 按回车键退出...")