reset_machine.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. print(f"{Fore.CYAN}{EMOJI['FILE']} 读取当前配置...{Style.RESET_ALL}")
  57. with open(self.db_path, "r", encoding="utf-8") as f:
  58. config = json.load(f)
  59. # 备份原文件
  60. backup_path = self.db_path + ".bak"
  61. print(
  62. f"{Fore.YELLOW}{EMOJI['BACKUP']} 创建配置备份: {backup_path}{Style.RESET_ALL}"
  63. )
  64. shutil.copy2(self.db_path, backup_path)
  65. # 生成新的ID
  66. print(f"{Fore.CYAN}{EMOJI['RESET']} 生成新的机器标识...{Style.RESET_ALL}")
  67. new_ids = self.generate_new_ids()
  68. # 更新配置
  69. config.update(new_ids)
  70. # 保存新配置
  71. print(f"{Fore.CYAN}{EMOJI['FILE']} 保存新配置...{Style.RESET_ALL}")
  72. with open(self.db_path, "w", encoding="utf-8") as f:
  73. json.dump(config, f, indent=4)
  74. print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识重置成功!{Style.RESET_ALL}")
  75. print(f"\n{Fore.CYAN}新的机器标识:{Style.RESET_ALL}")
  76. for key, value in new_ids.items():
  77. print(f"{EMOJI['INFO']} {key}: {Fore.GREEN}{value}{Style.RESET_ALL}")
  78. return True
  79. except Exception as e:
  80. print(f"{Fore.RED}{EMOJI['ERROR']} 重置过程出错: {str(e)}{Style.RESET_ALL}")
  81. return False
  82. if __name__ == "__main__":
  83. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  84. print(f"{Fore.CYAN}{EMOJI['RESET']} Cursor 机器标识重置工具{Style.RESET_ALL}")
  85. print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  86. resetter = MachineIDResetter()
  87. resetter.reset_machine_ids()
  88. print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
  89. input(f"{EMOJI['INFO']} 按回车键退出...")