feat: 1.1版本更新 - 优化UI界面和激活流程
This commit is contained in:
@@ -15,12 +15,14 @@ from exit_cursor import ExitCursor
|
||||
import go_cursor_help
|
||||
from logo import print_logo
|
||||
from typing import Tuple, Dict, Optional
|
||||
import time
|
||||
import requests.adapters
|
||||
|
||||
class CursorTokenUpdater:
|
||||
def __init__(self):
|
||||
self.auth_manager = CursorAuthManager()
|
||||
self._hardware_id = None # 延迟初始化硬件ID
|
||||
|
||||
|
||||
@property
|
||||
def hardware_id(self) -> str:
|
||||
"""获取硬件ID(延迟初始化)"""
|
||||
@@ -30,68 +32,68 @@ class CursorTokenUpdater:
|
||||
|
||||
def _get_hardware_id(self) -> str:
|
||||
"""获取硬件唯一标识
|
||||
方案1: CPU ID + 主板序列号 + BIOS序列号
|
||||
方案2: 系统盘序列号 + Windows安装时间
|
||||
方案3: 计算机名(最后的备选方案)
|
||||
macOS: 使用系统序列号和硬件UUID
|
||||
Windows: CPU ID + 主板序列号 + BIOS序列号
|
||||
其他: 计算机名(最后的备选方案)
|
||||
"""
|
||||
try:
|
||||
# 创建startupinfo对象来隐藏命令行窗口
|
||||
startupinfo = None
|
||||
if sys.platform == "win32":
|
||||
system = platform.system()
|
||||
|
||||
if system == "Darwin": # macOS
|
||||
try:
|
||||
# 获取系统序列号
|
||||
serial_number = subprocess.check_output(['system_profiler', 'SPHardwareDataType']).decode()
|
||||
serial = ""
|
||||
for line in serial_number.split('\n'):
|
||||
if 'Serial Number' in line:
|
||||
serial = line.split(':')[1].strip()
|
||||
break
|
||||
|
||||
# 获取硬件UUID
|
||||
ioreg_output = subprocess.check_output(['ioreg', '-d2', '-c', 'IOPlatformExpertDevice']).decode()
|
||||
uuid = ""
|
||||
for line in ioreg_output.split('\n'):
|
||||
if 'IOPlatformUUID' in line:
|
||||
uuid = line.split('=')[1].strip().replace('"', '').replace(' ', '')
|
||||
break
|
||||
|
||||
if serial and uuid:
|
||||
combined = f"{serial}:{uuid}"
|
||||
hardware_id = hashlib.md5(combined.encode()).hexdigest()
|
||||
logging.info("使用macOS硬件信息生成ID成功")
|
||||
return hardware_id
|
||||
except Exception as e:
|
||||
logging.warning(f"获取macOS硬件信息失败: {str(e)}")
|
||||
|
||||
elif system == "Windows":
|
||||
# 创建startupinfo对象来隐藏命令行窗口
|
||||
startupinfo = subprocess.STARTUPINFO()
|
||||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||
startupinfo.wShowWindow = subprocess.SW_HIDE
|
||||
|
||||
# 方案1: 尝试获取硬件信息
|
||||
try:
|
||||
# 获取CPU ID
|
||||
cpu_info = subprocess.check_output('wmic cpu get ProcessorId', startupinfo=startupinfo).decode()
|
||||
cpu_id = cpu_info.split('\n')[1].strip()
|
||||
|
||||
# 获取主板序列号
|
||||
board_info = subprocess.check_output('wmic baseboard get SerialNumber', startupinfo=startupinfo).decode()
|
||||
board_id = board_info.split('\n')[1].strip()
|
||||
|
||||
# 获取BIOS序列号
|
||||
bios_info = subprocess.check_output('wmic bios get SerialNumber', startupinfo=startupinfo).decode()
|
||||
bios_id = bios_info.split('\n')[1].strip()
|
||||
|
||||
# 如果所有信息都获取成功且有效
|
||||
if all([cpu_id, board_id, bios_id]) and not all(x in ['', '0', 'None', 'To be filled by O.E.M.'] for x in [cpu_id, board_id, bios_id]):
|
||||
combined = f"{cpu_id}:{board_id}:{bios_id}"
|
||||
hardware_id = hashlib.md5(combined.encode()).hexdigest()
|
||||
logging.info("使用硬件信息生成ID成功")
|
||||
return hardware_id
|
||||
try:
|
||||
# 获取CPU ID
|
||||
cpu_info = subprocess.check_output('wmic cpu get ProcessorId', startupinfo=startupinfo).decode()
|
||||
cpu_id = cpu_info.split('\n')[1].strip()
|
||||
|
||||
except Exception as e:
|
||||
logging.warning(f"方案1失败: {str(e)}")
|
||||
|
||||
# 方案2: 系统盘序列号 + Windows安装时间
|
||||
try:
|
||||
backup_info = []
|
||||
|
||||
# 获取系统盘序列号
|
||||
volume_info = subprocess.check_output('wmic logicaldisk where "DeviceID=\'C:\'" get VolumeSerialNumber', startupinfo=startupinfo).decode()
|
||||
volume_serial = volume_info.split('\n')[1].strip()
|
||||
if volume_serial and volume_serial not in ['', '0']:
|
||||
backup_info.append(("volume", volume_serial))
|
||||
|
||||
# 获取Windows安装时间
|
||||
os_info = subprocess.check_output('wmic os get InstallDate', startupinfo=startupinfo).decode()
|
||||
install_date = os_info.split('\n')[1].strip()
|
||||
if install_date:
|
||||
backup_info.append(("install", install_date))
|
||||
|
||||
if backup_info:
|
||||
combined = "|".join(f"{k}:{v}" for k, v in sorted(backup_info))
|
||||
hardware_id = hashlib.md5(combined.encode()).hexdigest()
|
||||
logging.info("使用系统信息生成ID成功")
|
||||
return hardware_id
|
||||
# 获取主板序列号
|
||||
board_info = subprocess.check_output('wmic baseboard get SerialNumber', startupinfo=startupinfo).decode()
|
||||
board_id = board_info.split('\n')[1].strip()
|
||||
|
||||
except Exception as e:
|
||||
logging.warning(f"方案2失败: {str(e)}")
|
||||
# 获取BIOS序列号
|
||||
bios_info = subprocess.check_output('wmic bios get SerialNumber', startupinfo=startupinfo).decode()
|
||||
bios_id = bios_info.split('\n')[1].strip()
|
||||
|
||||
# 如果所有信息都获取成功且有效
|
||||
if all([cpu_id, board_id, bios_id]) and not all(x in ['', '0', 'None', 'To be filled by O.E.M.'] for x in [cpu_id, board_id, bios_id]):
|
||||
combined = f"{cpu_id}:{board_id}:{bios_id}"
|
||||
hardware_id = hashlib.md5(combined.encode()).hexdigest()
|
||||
logging.info("使用Windows硬件信息生成ID成功")
|
||||
return hardware_id
|
||||
except Exception as e:
|
||||
logging.warning(f"获取Windows硬件信息失败: {str(e)}")
|
||||
|
||||
# 方案3: 使用计算机名(最后的备选方案)
|
||||
# 最后的备选方案:使用计算机名
|
||||
computer_name = platform.node()
|
||||
if computer_name:
|
||||
hardware_id = hashlib.md5(computer_name.encode()).hexdigest()
|
||||
@@ -227,17 +229,23 @@ class CursorTokenUpdater:
|
||||
"""
|
||||
try:
|
||||
logging.info("开始重置机器码...")
|
||||
resetter = MachineIDResetter()
|
||||
result = resetter.reset(greater_than_0_45)
|
||||
|
||||
if result:
|
||||
logging.info("机器码重置成功")
|
||||
# 重置后更新硬件ID缓存
|
||||
self._hardware_id = None
|
||||
if greater_than_0_45:
|
||||
# 对于0.45以上版本,使用go_cursor_help
|
||||
go_cursor_help.go_cursor_help()
|
||||
logging.info("已调用go_cursor_help重置机器码")
|
||||
return True
|
||||
else:
|
||||
logging.error("机器码重置失败")
|
||||
return False
|
||||
# 对于0.45及以下版本,使用传统方式
|
||||
resetter = MachineIDResetter()
|
||||
result = resetter.reset_machine_ids()
|
||||
if result:
|
||||
logging.info("机器码重置成功")
|
||||
# 重置后更新硬件ID缓存
|
||||
self._hardware_id = None
|
||||
return True
|
||||
else:
|
||||
logging.error("机器码重置失败")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"重置机器码时发生错误: {str(e)}")
|
||||
@@ -268,10 +276,8 @@ class CursorTokenUpdater:
|
||||
"""
|
||||
try:
|
||||
logging.info("正在退出Cursor进程...")
|
||||
exit_handler = ExitCursor()
|
||||
exit_handler.exit()
|
||||
logging.info("Cursor进程已退出")
|
||||
return True
|
||||
result = ExitCursor()
|
||||
return result
|
||||
except Exception as e:
|
||||
logging.error(f"退出Cursor进程时发生错误: {str(e)}")
|
||||
return False
|
||||
@@ -291,34 +297,175 @@ class CursorTokenUpdater:
|
||||
print_logo()
|
||||
logging.info("=== 开始完整更新流程 ===")
|
||||
|
||||
# 1. 退出Cursor进程
|
||||
if not self.exit_cursor():
|
||||
return False
|
||||
|
||||
# 2. 修补机器码获取方法
|
||||
if not self.patch_machine_id():
|
||||
return False
|
||||
|
||||
# 3. 重置机器码
|
||||
if not self.reset_machine_id(greater_than_0_45=True):
|
||||
return False
|
||||
|
||||
# 4. 如果没有提供认证信息,从API获取
|
||||
if not all([email, access_token]):
|
||||
success, error_msg, account_info = self.get_unused_account()
|
||||
if not success:
|
||||
logging.error(f"无法获取账号信息: {error_msg}")
|
||||
try:
|
||||
# 1. 退出Cursor进程
|
||||
if not self.exit_cursor():
|
||||
logging.error("退出Cursor进程失败")
|
||||
return False
|
||||
email = account_info["email"]
|
||||
access_token = account_info["access_token"]
|
||||
refresh_token = account_info["refresh_token"]
|
||||
|
||||
# 2. 修补机器码获取方法
|
||||
if not self.patch_machine_id():
|
||||
logging.error("修补机器码获取方法失败")
|
||||
return False
|
||||
|
||||
# 3. 重置机器码
|
||||
# 对于0.45以上版本,使用go_cursor_help
|
||||
try:
|
||||
go_cursor_help.go_cursor_help()
|
||||
logging.info("已调用go_cursor_help重置机器码")
|
||||
except Exception as e:
|
||||
logging.error(f"使用go_cursor_help重置机器码失败: {str(e)}")
|
||||
# 如果go_cursor_help失败,尝试使用传统方式
|
||||
if not self.reset_machine_id(greater_than_0_45=False):
|
||||
logging.error("重置机器码失败")
|
||||
return False
|
||||
|
||||
# 4. 如果没有提供认证信息,从API获取
|
||||
if not all([email, access_token]):
|
||||
success, error_msg, account_info = self.get_unused_account()
|
||||
if not success:
|
||||
logging.error(f"无法获取账号信息: {error_msg}")
|
||||
return False
|
||||
email = account_info["email"]
|
||||
access_token = account_info["access_token"]
|
||||
refresh_token = account_info["refresh_token"]
|
||||
|
||||
# 5. 更新认证信息
|
||||
if not self.update_auth_info(email, access_token, refresh_token):
|
||||
logging.error("更新认证信息失败")
|
||||
return False
|
||||
|
||||
logging.info("=== 所有操作已完成 ===")
|
||||
return True
|
||||
|
||||
# 5. 更新认证信息
|
||||
if not self.update_auth_info(email, access_token, refresh_token):
|
||||
except Exception as e:
|
||||
logging.error(f"更新流程发生错误: {str(e)}")
|
||||
return False
|
||||
|
||||
def _get_network_error_message(self, error: Exception) -> str:
|
||||
"""获取网络错误的友好提示信息"""
|
||||
if isinstance(error, requests.exceptions.ConnectTimeout):
|
||||
return "连接服务器超时,请检查网络连接"
|
||||
elif isinstance(error, requests.exceptions.ConnectionError):
|
||||
return "无法连接到服务器,请检查网络连接"
|
||||
elif isinstance(error, requests.exceptions.ReadTimeout):
|
||||
return "读取服务器响应超时,请重试"
|
||||
else:
|
||||
return str(error)
|
||||
|
||||
logging.info("=== 所有操作已完成 ===")
|
||||
return True
|
||||
def get_device_info(self) -> dict:
|
||||
"""获取设备信息"""
|
||||
return {
|
||||
"os": platform.system(),
|
||||
"os_version": platform.version(),
|
||||
"machine": platform.machine(),
|
||||
"hostname": platform.node(),
|
||||
"hardware_id": self.hardware_id
|
||||
}
|
||||
|
||||
def check_activation_code(self, code: str) -> tuple:
|
||||
"""检查激活码
|
||||
|
||||
Args:
|
||||
code: 激活码
|
||||
|
||||
Returns:
|
||||
tuple: (成功标志, 消息, 账号信息)
|
||||
"""
|
||||
max_retries = 3 # 最大重试次数
|
||||
retry_delay = 1 # 重试间隔(秒)
|
||||
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
data = {
|
||||
"machine_id": self.hardware_id,
|
||||
"code": code
|
||||
}
|
||||
|
||||
# 禁用SSL警告
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
# 设置请求参数
|
||||
request_kwargs = {
|
||||
"json": data,
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
||||
"Accept": "*/*",
|
||||
"Connection": "keep-alive"
|
||||
},
|
||||
"timeout": 10, # 增加超时时间
|
||||
"verify": False # 禁用SSL验证
|
||||
}
|
||||
|
||||
# 创建session
|
||||
session = requests.Session()
|
||||
session.verify = False
|
||||
|
||||
# 设置重试策略
|
||||
retry_strategy = urllib3.Retry(
|
||||
total=3, # 总重试次数
|
||||
backoff_factor=0.5, # 重试间隔
|
||||
status_forcelist=[500, 502, 503, 504] # 需要重试的HTTP状态码
|
||||
)
|
||||
adapter = requests.adapters.HTTPAdapter(max_retries=retry_strategy)
|
||||
session.mount("http://", adapter)
|
||||
session.mount("https://", adapter)
|
||||
|
||||
try:
|
||||
# 尝试发送请求
|
||||
response = session.post(
|
||||
"https://cursorapi.nosqli.com/admin/api.member/activate",
|
||||
**request_kwargs
|
||||
)
|
||||
response.raise_for_status() # 检查HTTP状态码
|
||||
|
||||
result = response.json()
|
||||
# 激活成功
|
||||
if result["code"] == 200:
|
||||
api_data = result["data"]
|
||||
# 构造标准的返回数据结构
|
||||
account_info = {
|
||||
"status": "active",
|
||||
"expire_time": api_data.get("expire_time", ""),
|
||||
"total_days": api_data.get("total_days", 0),
|
||||
"days_left": api_data.get("days_left", 0),
|
||||
"device_info": self.get_device_info()
|
||||
}
|
||||
return True, result["msg"], account_info
|
||||
# 激活码无效或已被使用
|
||||
elif result["code"] == 400:
|
||||
logging.warning(f"激活码无效或已被使用: {result.get('msg', '未知错误')}")
|
||||
return False, result.get("msg", "激活码无效或已被使用"), None
|
||||
# 其他错误情况
|
||||
else:
|
||||
error_msg = result.get("msg", "未知错误")
|
||||
if attempt < max_retries - 1: # 如果还有重试机会
|
||||
logging.warning(f"第{attempt + 1}次尝试失败: {error_msg}, 准备重试...")
|
||||
time.sleep(retry_delay)
|
||||
continue
|
||||
logging.error(f"激活失败: {error_msg}")
|
||||
return False, error_msg, None
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
if attempt < max_retries - 1: # 如果还有重试机会
|
||||
logging.warning(f"第{attempt + 1}次网络请求失败: {str(e)}, 准备重试...")
|
||||
time.sleep(retry_delay)
|
||||
continue
|
||||
error_msg = self._get_network_error_message(e)
|
||||
logging.error(f"网络请求失败: {error_msg}")
|
||||
return False, f"网络连接失败: {error_msg}", None
|
||||
|
||||
except Exception as e:
|
||||
if attempt < max_retries - 1: # 如果还有重试机会
|
||||
logging.warning(f"第{attempt + 1}次请求发生错误: {str(e)}, 准备重试...")
|
||||
time.sleep(retry_delay)
|
||||
continue
|
||||
logging.error(f"激活失败: {str(e)}")
|
||||
return False, f"激活失败: {str(e)}", None
|
||||
|
||||
# 如果所有重试都失败了
|
||||
return False, "多次尝试后激活失败,请检查网络连接或稍后重试", None
|
||||
|
||||
def main():
|
||||
updater = CursorTokenUpdater()
|
||||
|
||||
Reference in New Issue
Block a user