295 lines
12 KiB
Python
295 lines
12 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
import time
|
|
import subprocess
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from account_switcher import AccountSwitcher
|
|
|
|
def get_full_machine_info():
|
|
"""获取更完整的机器码相关信息"""
|
|
info = {}
|
|
|
|
# 1. 获取 package.json 中的所有信息(检查两个可能的路径)
|
|
cursor_paths = [
|
|
Path(os.path.expanduser("~")) / "AppData" / "Local" / "Programs" / "Cursor",
|
|
Path(os.path.expanduser("~")) / "AppData" / "Local" / "cursor"
|
|
]
|
|
|
|
info["package_json"] = {}
|
|
for cursor_path in cursor_paths:
|
|
package_json = cursor_path / "resources" / "app" / "package.json"
|
|
if package_json.exists():
|
|
with open(package_json, "r", encoding="utf-8") as f:
|
|
info["package_json"][str(package_json)] = json.load(f)
|
|
|
|
# 2. 获取 storage.json 的内容
|
|
storage_path = Path(os.getenv('APPDATA')) / "Cursor" / "User" / "globalStorage" / "storage.json"
|
|
info["storage_json"] = None
|
|
if storage_path.exists():
|
|
try:
|
|
with open(storage_path, "r", encoding="utf-8") as f:
|
|
info["storage_json"] = json.load(f)
|
|
except:
|
|
pass
|
|
|
|
# 3. 检查注册表项和其值
|
|
import winreg
|
|
registry_paths = {
|
|
"cryptography": r"SOFTWARE\Microsoft\Cryptography", # 系统 MachineGuid
|
|
"cursor_shell": r"Software\Classes\Directory\Background\shell\Cursor",
|
|
"cursor_command": r"Software\Classes\Directory\Background\shell\Cursor\command",
|
|
"cursor_auth": r"Software\Cursor\Auth",
|
|
"cursor_updates": r"Software\Cursor\Updates",
|
|
"cursor_main": r"Software\Cursor"
|
|
}
|
|
|
|
info["registry"] = {}
|
|
|
|
# HKEY_LOCAL_MACHINE 项
|
|
try:
|
|
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, registry_paths["cryptography"], 0, winreg.KEY_READ | winreg.KEY_WOW64_64KEY)
|
|
info["registry"]["HKLM_MachineGuid"] = {
|
|
"exists": True,
|
|
"value": winreg.QueryValueEx(key, "MachineGuid")[0]
|
|
}
|
|
except WindowsError:
|
|
info["registry"]["HKLM_MachineGuid"] = {
|
|
"exists": False,
|
|
"value": None
|
|
}
|
|
|
|
# HKEY_CURRENT_USER 项
|
|
for name, path in registry_paths.items():
|
|
if name == "cryptography":
|
|
continue
|
|
try:
|
|
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, winreg.KEY_READ)
|
|
info["registry"][f"HKCU_{name}"] = {"exists": True, "values": {}}
|
|
try:
|
|
i = 0
|
|
while True:
|
|
name, value, type = winreg.EnumValue(key, i)
|
|
info["registry"][f"HKCU_{name}"]["values"][name] = value
|
|
i += 1
|
|
except WindowsError:
|
|
pass
|
|
except WindowsError:
|
|
info["registry"][f"HKCU_{name}"] = {"exists": False, "values": {}}
|
|
|
|
# 4. 检查关键文件的存在状态
|
|
paths_to_check = {
|
|
"storage": storage_path,
|
|
"storage_backup": Path(os.getenv('APPDATA')) / "Cursor" / "User" / "globalStorage" / "backups",
|
|
"user_data": Path(os.getenv('LOCALAPPDATA')) / "Cursor" / "User",
|
|
"global_storage": Path(os.getenv('APPDATA')) / "Cursor" / "User" / "globalStorage",
|
|
"cache": Path(os.getenv('LOCALAPPDATA')) / "Cursor" / "Cache",
|
|
"updater": Path(os.getenv('LOCALAPPDATA')) / "cursor-updater"
|
|
}
|
|
|
|
info["files"] = {}
|
|
for name, path in paths_to_check.items():
|
|
if path.exists():
|
|
info["files"][name] = {
|
|
"exists": True,
|
|
"is_dir": path.is_dir(),
|
|
"size": os.path.getsize(path) if path.is_file() else None,
|
|
"modified_time": datetime.fromtimestamp(os.path.getmtime(path)).isoformat()
|
|
}
|
|
else:
|
|
info["files"][name] = {
|
|
"exists": False,
|
|
"is_dir": None,
|
|
"size": None,
|
|
"modified_time": None
|
|
}
|
|
|
|
# 5. 获取遥测相关的配置值
|
|
info["telemetry"] = {
|
|
"machineId": info["storage_json"].get("telemetry.machineId") if info["storage_json"] else None,
|
|
"macMachineId": info["storage_json"].get("telemetry.macMachineId") if info["storage_json"] else None,
|
|
"devDeviceId": info["storage_json"].get("telemetry.devDeviceId") if info["storage_json"] else None,
|
|
"sqmId": info["storage_json"].get("telemetry.sqmId") if info["storage_json"] else None
|
|
}
|
|
|
|
return info
|
|
|
|
def save_info(info, filename):
|
|
"""保存信息到文件"""
|
|
with open(filename, "w", encoding="utf-8") as f:
|
|
json.dump(info, f, indent=2, ensure_ascii=False)
|
|
|
|
def compare_info(before_file, after_file):
|
|
"""比较两个信息文件的差异"""
|
|
with open(before_file, "r", encoding="utf-8") as f:
|
|
before = json.load(f)
|
|
with open(after_file, "r", encoding="utf-8") as f:
|
|
after = json.load(f)
|
|
|
|
differences = {
|
|
"package_json_changes": {},
|
|
"registry_changes": {},
|
|
"file_changes": {}
|
|
}
|
|
|
|
# 比较 package.json 变化
|
|
if "package_json" in before and "package_json" in after:
|
|
for key in set(before["package_json"].keys()) | set(after["package_json"].keys()):
|
|
before_val = before["package_json"].get(key)
|
|
after_val = after["package_json"].get(key)
|
|
if before_val != after_val:
|
|
differences["package_json_changes"][key] = {
|
|
"before": before_val,
|
|
"after": after_val
|
|
}
|
|
|
|
# 比较注册表变化
|
|
for path in set(before["registry"].keys()) | set(after["registry"].keys()):
|
|
before_reg = before["registry"].get(path, {})
|
|
after_reg = after["registry"].get(path, {})
|
|
if before_reg != after_reg:
|
|
differences["registry_changes"][path] = {
|
|
"before": before_reg,
|
|
"after": after_reg
|
|
}
|
|
|
|
# 比较文件变化
|
|
for name in set(before["files"].keys()) | set(after["files"].keys()):
|
|
before_file = before["files"].get(name, {})
|
|
after_file = after["files"].get(name, {})
|
|
if before_file != after_file:
|
|
differences["file_changes"][name] = {
|
|
"before": before_file,
|
|
"after": after_file
|
|
}
|
|
|
|
return differences
|
|
|
|
def main():
|
|
"""主测试流程"""
|
|
# 设置日志
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
print("选择操作:")
|
|
print("1. 记录当前状态(在运行 GitHub 脚本前执行)")
|
|
print("2. 记录重置后状态并对比(在运行 GitHub 脚本后执行)")
|
|
print("3. 运行我们的重置方法并记录对比")
|
|
|
|
choice = input("\n请选择操作 (1/2/3): ").strip()
|
|
|
|
if choice == "1":
|
|
# 记录初始状态
|
|
print("\n记录初始状态...")
|
|
initial_info = get_full_machine_info()
|
|
save_info(initial_info, "before_github_reset.json")
|
|
print("初始状态已保存到 before_github_reset.json")
|
|
|
|
elif choice == "2":
|
|
# 检查是否存在初始状态文件
|
|
if not os.path.exists("before_github_reset.json"):
|
|
print("错误:找不到初始状态文件 before_github_reset.json")
|
|
print("请先执行选项 1 记录初始状态")
|
|
return
|
|
|
|
# 记录 GitHub 脚本执行后的状态
|
|
print("\n记录 GitHub 脚本执行后的状态...")
|
|
after_info = get_full_machine_info()
|
|
save_info(after_info, "after_github_reset.json")
|
|
|
|
# 对比变化
|
|
print("\n=== GitHub 脚本执行前后的变化 ===")
|
|
differences = compare_info("before_github_reset.json", "after_github_reset.json")
|
|
|
|
# 显示 package.json 的变化
|
|
if differences["package_json_changes"]:
|
|
print("\npackage.json 变化:")
|
|
for key, change in differences["package_json_changes"].items():
|
|
print(f"- {key}:")
|
|
print(f" 之前: {change['before']}")
|
|
print(f" 之后: {change['after']}")
|
|
|
|
# 显示注册表的变化
|
|
if differences["registry_changes"]:
|
|
print("\n注册表变化:")
|
|
for path, change in differences["registry_changes"].items():
|
|
print(f"\n- {path}:")
|
|
print(f" 之前: {change['before']}")
|
|
print(f" 之后: {change['after']}")
|
|
|
|
# 显示文件的变化
|
|
if differences["file_changes"]:
|
|
print("\n文件变化:")
|
|
for name, change in differences["file_changes"].items():
|
|
print(f"\n- {name}:")
|
|
before_status = "存在" if change["before"].get("exists") else "不存在"
|
|
after_status = "存在" if change["after"].get("exists") else "不存在"
|
|
print(f" 状态: {before_status} -> {after_status}")
|
|
if change["before"].get("exists") and change["after"].get("exists"):
|
|
if change["before"].get("size") != change["after"].get("size"):
|
|
print(f" 大小: {change['before'].get('size')} -> {change['after'].get('size')}")
|
|
if change["before"].get("modified_time") != change["after"].get("modified_time"):
|
|
print(f" 修改时间: {change['before'].get('modified_time')} -> {change['after'].get('modified_time')}")
|
|
|
|
# 保存差异到文件
|
|
save_info(differences, "github_script_changes.json")
|
|
print("\n详细的变化信息已保存到 github_script_changes.json")
|
|
|
|
elif choice == "3":
|
|
if not os.path.exists("github_script_changes.json"):
|
|
print("错误:找不到 GitHub 脚本的变化记录文件 github_script_changes.json")
|
|
print("请先执行选项 1 和 2 来记录 GitHub 脚本的变化")
|
|
return
|
|
|
|
# 记录运行我们的重置方法前的状态
|
|
print("\n记录当前状态...")
|
|
before_our_reset = get_full_machine_info()
|
|
save_info(before_our_reset, "before_our_reset.json")
|
|
|
|
# 运行我们的重置方法
|
|
print("\n运行我们的重置方法...")
|
|
switcher = AccountSwitcher()
|
|
original_restart = switcher.restart_cursor
|
|
try:
|
|
switcher.restart_cursor = lambda: True
|
|
switcher.reset_machine_id()
|
|
finally:
|
|
switcher.restart_cursor = original_restart
|
|
|
|
# 记录重置后的状态
|
|
print("\n记录重置后的状态...")
|
|
after_our_reset = get_full_machine_info()
|
|
save_info(after_our_reset, "after_our_reset.json")
|
|
|
|
# 对比我们的方法与 GitHub 脚本的差异
|
|
print("\n=== 对比我们的重置方法与 GitHub 脚本的差异 ===")
|
|
with open("github_script_changes.json", "r", encoding="utf-8") as f:
|
|
github_changes = json.load(f)
|
|
our_changes = compare_info("before_our_reset.json", "after_our_reset.json")
|
|
|
|
# 分析差异
|
|
print("\n=== 差异分析 ===")
|
|
print("1. GitHub 脚本改变但我们没有改变的项:")
|
|
for category in ["package_json_changes", "registry_changes", "file_changes"]:
|
|
for key in github_changes[category]:
|
|
if key not in our_changes[category]:
|
|
print(f"- {category}: {key}")
|
|
|
|
print("\n2. 我们改变但 GitHub 脚本没有改变的项:")
|
|
for category in ["package_json_changes", "registry_changes", "file_changes"]:
|
|
for key in our_changes[category]:
|
|
if key not in github_changes[category]:
|
|
print(f"- {category}: {key}")
|
|
|
|
# 保存分析结果
|
|
save_info({
|
|
"github_changes": github_changes,
|
|
"our_changes": our_changes
|
|
}, "reset_methods_comparison.json")
|
|
print("\n详细的对比结果已保存到 reset_methods_comparison.json")
|
|
|
|
else:
|
|
print("无效的选择")
|
|
|
|
if __name__ == "__main__":
|
|
main() |