xxx
This commit is contained in:
@@ -8,6 +8,7 @@ import winreg
|
||||
import ctypes
|
||||
import shutil
|
||||
import json
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
from typing import Dict, Tuple, Any, Optional
|
||||
from logger import logger
|
||||
@@ -86,6 +87,42 @@ class MachineResetter:
|
||||
self.logger.info(f"配置已备份到: {backup_path}")
|
||||
return backup_path
|
||||
|
||||
def check_cursor_version(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
检查 Cursor 版本是否支持
|
||||
:return: (是否支持, 版本号)
|
||||
"""
|
||||
try:
|
||||
# 主要检测路径
|
||||
package_paths = [
|
||||
os.path.join(os.getenv('LOCALAPPDATA'), 'Programs', 'cursor', 'resources', 'app', 'package.json'),
|
||||
os.path.join(os.getenv('LOCALAPPDATA'), 'cursor', 'resources', 'app', 'package.json')
|
||||
]
|
||||
|
||||
for path in package_paths:
|
||||
if os.path.exists(path):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
version = data.get('version', '')
|
||||
# 检查版本号是否低于 0.44.0
|
||||
version_parts = list(map(int, version.split('.')))
|
||||
if version_parts[0] == 0 and version_parts[1] < 44:
|
||||
return False, version
|
||||
return True, version
|
||||
|
||||
return True, "未知版本"
|
||||
except Exception as e:
|
||||
self.logger.warning(f"版本检测失败: {str(e)}")
|
||||
return True, "版本检测失败"
|
||||
|
||||
def generate_secure_random(self, length: int) -> str:
|
||||
"""
|
||||
生成安全的随机十六进制字符串
|
||||
:param length: 字节长度
|
||||
:return: 十六进制字符串
|
||||
"""
|
||||
return secrets.token_hex(length)
|
||||
|
||||
def generate_new_ids(self) -> Dict[str, str]:
|
||||
"""
|
||||
生成新的机器码系列
|
||||
@@ -93,8 +130,8 @@ class MachineResetter:
|
||||
"""
|
||||
# 生成 auth0|user_ 前缀的十六进制
|
||||
prefix = "auth0|user_".encode('utf-8').hex()
|
||||
# 生成32字节(64个十六进制字符)的随机数
|
||||
random_part = uuid.uuid4().hex + uuid.uuid4().hex
|
||||
# 使用安全的随机数生成方法
|
||||
random_part = self.generate_secure_random(32) # 生成64个十六进制字符
|
||||
|
||||
return {
|
||||
"machineId": f"{prefix}{random_part}",
|
||||
@@ -159,6 +196,11 @@ class MachineResetter:
|
||||
:return: (新ID字典, 新GUID)
|
||||
"""
|
||||
try:
|
||||
# 检查 Cursor 版本
|
||||
version_supported, version = self.check_cursor_version()
|
||||
if not version_supported:
|
||||
raise Exception(f"当前 Cursor 版本 ({version}) 不支持重置,请使用 v0.44.0 及以上版本")
|
||||
|
||||
# 检查管理员权限
|
||||
self._update_progress("checking", "检查管理员权限...")
|
||||
if not self.is_admin():
|
||||
@@ -174,23 +216,34 @@ class MachineResetter:
|
||||
backup_path = self.backup_file()
|
||||
self.logger.info(f"配置已备份到: {backup_path}")
|
||||
|
||||
# 生成新机器码
|
||||
self._update_progress("generating", "生成新的机器码...")
|
||||
new_ids = self.generate_new_ids()
|
||||
|
||||
# 更新配置文件
|
||||
self._update_progress("updating", "更新配置文件...")
|
||||
self.update_config(new_ids)
|
||||
|
||||
# 更新注册表
|
||||
self._update_progress("registry", "更新注册表...")
|
||||
new_guid = self.update_machine_guid()
|
||||
|
||||
self._update_progress("complete", "重置完成")
|
||||
self.logger.info("机器码重置成功")
|
||||
|
||||
return new_ids, new_guid
|
||||
|
||||
try:
|
||||
# 生成新机器码
|
||||
self._update_progress("generating", "生成新的机器码...")
|
||||
new_ids = self.generate_new_ids()
|
||||
|
||||
# 更新配置文件
|
||||
self._update_progress("updating", "更新配置文件...")
|
||||
self.update_config(new_ids)
|
||||
|
||||
# 更新注册表
|
||||
self._update_progress("registry", "更新注册表...")
|
||||
new_guid = self.update_machine_guid()
|
||||
|
||||
self._update_progress("complete", "重置完成")
|
||||
self.logger.info("机器码重置成功")
|
||||
|
||||
return new_ids, new_guid
|
||||
|
||||
except Exception as e:
|
||||
# 如果出错,尝试恢复备份
|
||||
try:
|
||||
if os.path.exists(backup_path):
|
||||
shutil.copy2(backup_path, self.storage_file)
|
||||
self.logger.info("已恢复配置文件备份")
|
||||
except Exception as restore_error:
|
||||
self.logger.error(f"恢复备份失败: {str(restore_error)}")
|
||||
raise
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"重置失败: {str(e)}")
|
||||
self._update_progress("error", f"操作失败: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user