114 lines
4.4 KiB
Python
114 lines
4.4 KiB
Python
import os
|
||
import subprocess
|
||
import logging
|
||
from colorama import Fore, Style
|
||
|
||
def print_status(message, status="info"):
|
||
"""打印状态信息"""
|
||
prefix = {
|
||
"info": f"{Fore.CYAN}[信息]{Style.RESET_ALL}",
|
||
"success": f"{Fore.GREEN}[成功]{Style.RESET_ALL}",
|
||
"error": f"{Fore.RED}[错误]{Style.RESET_ALL}",
|
||
"warning": f"{Fore.YELLOW}[警告]{Style.RESET_ALL}"
|
||
}
|
||
print(f"\n{prefix.get(status, prefix['info'])} {message}")
|
||
|
||
def deep_reset():
|
||
"""执行深度重置,包括设备ID和系统MachineGuid"""
|
||
try:
|
||
print_status("开始执行深度重置...", "info")
|
||
|
||
# 获取路径信息
|
||
storage_path = os.path.expandvars(r"%APPDATA%\Cursor\User\globalStorage\storage.json")
|
||
backup_path = os.path.expandvars(r"%APPDATA%\Cursor\User\globalStorage\backups")
|
||
|
||
# 检查并创建备份目录
|
||
if not os.path.exists(backup_path):
|
||
os.makedirs(backup_path)
|
||
|
||
# 准备 PowerShell 命令
|
||
cmd = f'''
|
||
# 设置输出编码为 UTF-8
|
||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||
$OutputEncoding = [System.Text.Encoding]::UTF8
|
||
|
||
# 配置文件路径
|
||
$STORAGE_FILE = "{storage_path}"
|
||
$BACKUP_DIR = "{backup_path}"
|
||
|
||
# 备份现有配置
|
||
if (Test-Path $STORAGE_FILE) {{
|
||
$backupName = "storage.json.backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
|
||
Copy-Item $STORAGE_FILE "$BACKUP_DIR\\$backupName"
|
||
}}
|
||
|
||
# 生成新的 ID
|
||
function New-StandardMachineId {{
|
||
$template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
||
$result = $template -replace '[xy]', {{
|
||
param($match)
|
||
$r = [Random]::new().Next(16)
|
||
$v = if ($match.Value -eq "x") {{ $r }} else {{ ($r -band 0x3) -bor 0x8 }}
|
||
return $v.ToString("x")
|
||
}}
|
||
return $result
|
||
}}
|
||
|
||
$MAC_MACHINE_ID = New-StandardMachineId
|
||
$UUID = [System.Guid]::NewGuid().ToString()
|
||
$prefixBytes = [System.Text.Encoding]::UTF8.GetBytes("auth0|user_")
|
||
$prefixHex = -join ($prefixBytes | ForEach-Object {{ [System.Convert]::ToString($_, 16).PadLeft(2, '0') }})
|
||
$randomPart = -join (1..32 | ForEach-Object {{ [Convert]::ToString((Get-Random -Minimum 0 -Maximum 256), 16).PadLeft(2, '0') }})
|
||
$MACHINE_ID = "$prefixHex$randomPart"
|
||
$SQM_ID = "{{$([System.Guid]::NewGuid().ToString().ToUpper())}}"
|
||
|
||
# 更新配置文件
|
||
$originalContent = Get-Content $STORAGE_FILE -Raw -Encoding UTF8
|
||
$config = $originalContent | ConvertFrom-Json
|
||
|
||
# 更新特定的值
|
||
$config.'telemetry.machineId' = $MACHINE_ID
|
||
$config.'telemetry.macMachineId' = $MAC_MACHINE_ID
|
||
$config.'telemetry.devDeviceId' = $UUID
|
||
$config.'telemetry.sqmId' = $SQM_ID
|
||
|
||
# 保存更新后的配置
|
||
$updatedJson = $config | ConvertTo-Json -Depth 10
|
||
[System.IO.File]::WriteAllText($STORAGE_FILE, $updatedJson, [System.Text.Encoding]::UTF8)
|
||
|
||
# 更新系统 MachineGuid
|
||
$newMachineGuid = [System.Guid]::NewGuid().ToString()
|
||
$registryPath = "HKLM:\\SOFTWARE\\Microsoft\\Cryptography"
|
||
|
||
# 备份原始值
|
||
$originalGuid = (Get-ItemProperty -Path $registryPath -Name "MachineGuid").MachineGuid
|
||
$backupFile = "$BACKUP_DIR\\MachineGuid.backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
|
||
$originalGuid | Out-File $backupFile -Encoding UTF8
|
||
|
||
# 更新注册表
|
||
Set-ItemProperty -Path $registryPath -Name "MachineGuid" -Value $newMachineGuid
|
||
'''
|
||
|
||
# 执行 PowerShell 命令
|
||
process = subprocess.run(
|
||
["powershell", "-NoProfile", "-NonInteractive", "-Command", cmd],
|
||
capture_output=True,
|
||
text=True,
|
||
encoding='utf-8'
|
||
)
|
||
|
||
if process.returncode == 0:
|
||
print_status("深度重置完成", "success")
|
||
return True
|
||
else:
|
||
print_status("深度重置失败", "error")
|
||
if process.stderr:
|
||
print_status(f"错误信息: {process.stderr}", "error")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print_status(f"深度重置出错: {str(e)}", "error")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
deep_reset() |