first commit
This commit is contained in:
54
backup/.env.example
Normal file
54
backup/.env.example
Normal file
@@ -0,0 +1,54 @@
|
||||
# 临时邮箱配置
|
||||
DOMAIN=nosqli.com
|
||||
TEMP_MAIL=awegko
|
||||
TEMP_MAIL_EXT=@mailto.plus
|
||||
BROWSER_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36
|
||||
MAIL_SERVER=https://tempmail.plus
|
||||
|
||||
|
||||
DOMAIN=586vip.cn
|
||||
TEMP_MAIL=ademyyk
|
||||
TEMP_MAIL_EXT=@mailto.plus
|
||||
BROWSER_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36
|
||||
MAIL_SERVER=https://tempmail.plus
|
||||
|
||||
|
||||
DOMAIN=wuen.site
|
||||
TEMP_MAIL=actoke
|
||||
TEMP_MAIL_EXT=@mailto.plus
|
||||
BROWSER_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36
|
||||
MAIL_SERVER=https://tempmail.plus
|
||||
|
||||
|
||||
DOMAIN=jxyweb.site
|
||||
TEMP_MAIL=exvet
|
||||
TEMP_MAIL_EXT=@mailto.plus
|
||||
BROWSER_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36
|
||||
MAIL_SERVER=https://tempmail.plus
|
||||
TEMP_MAIL_EPIN=889944
|
||||
|
||||
|
||||
[
|
||||
{
|
||||
"DOMAIN": "nosqli.com",
|
||||
"TEMP_MAIL": "ozamdyw",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus"
|
||||
},
|
||||
{
|
||||
"DOMAIN": "nosqli.com",
|
||||
"TEMP_MAIL": "xkwmvp",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus"
|
||||
},
|
||||
{
|
||||
"DOMAIN": "nosqli.com",
|
||||
"TEMP_MAIL": "yzqnlr",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus"
|
||||
}
|
||||
]
|
||||
|
||||
212
backup/config.py
Normal file
212
backup/config.py
Normal file
@@ -0,0 +1,212 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import random
|
||||
from logger import logging
|
||||
|
||||
# 预定义的配置列表
|
||||
CONFIGS = [
|
||||
|
||||
# {
|
||||
# "DOMAIN": "nosqli.com",
|
||||
# "TEMP_MAIL": "awegko",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "586vip.cn",
|
||||
# "TEMP_MAIL": "emufu",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "wuen.site",
|
||||
# "TEMP_MAIL": "actoke",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "jxyweb.site",
|
||||
# "TEMP_MAIL": "exvet",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# # "TEMP_MAIL_EPIN": "889944"
|
||||
# },
|
||||
|
||||
|
||||
|
||||
# {
|
||||
# "DOMAIN": "cursorpro.asia",
|
||||
# "TEMP_MAIL": "ybeudu",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "cursorpro.xyz",
|
||||
# "TEMP_MAIL": "neyxwub",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "cursorpro.xin",
|
||||
# "TEMP_MAIL": "qeznosa",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
{
|
||||
"DOMAIN": "cursorpro.com.cn",
|
||||
"TEMP_MAIL": "oducbum",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus",
|
||||
"TEMP_MAIL_EPIN": ""
|
||||
},
|
||||
# {
|
||||
# "DOMAIN": "jpgoogle.online",
|
||||
# "TEMP_MAIL": "oahpuza",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
# {
|
||||
# "DOMAIN": "jpgoogle.asia",
|
||||
# "TEMP_MAIL": "emufu",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": ""
|
||||
# },
|
||||
{
|
||||
"DOMAIN": "jpgoogle.xyz",
|
||||
"TEMP_MAIL": "ybeudu",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus",
|
||||
"TEMP_MAIL_EPIN": ""
|
||||
},
|
||||
|
||||
{
|
||||
"DOMAIN": "jpgoogle.xin",
|
||||
"TEMP_MAIL": "actoke",
|
||||
"TEMP_MAIL_EXT": "@mailto.plus",
|
||||
"BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
"MAIL_SERVER": "https://tempmail.plus",
|
||||
"TEMP_MAIL_EPIN": ""
|
||||
},
|
||||
# {
|
||||
# "DOMAIN": "tengxu2024.xyz",
|
||||
# "TEMP_MAIL": "exvet",
|
||||
# "TEMP_MAIL_EXT": "@mailto.plus",
|
||||
# "BROWSER_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36",
|
||||
# "MAIL_SERVER": "https://tempmail.plus",
|
||||
# "TEMP_MAIL_EPIN": "889944"
|
||||
# },
|
||||
]
|
||||
|
||||
class Config:
|
||||
def __init__(self, config_index=None):
|
||||
# 获取应用程序的根目录路径
|
||||
if getattr(sys, "frozen", False):
|
||||
# 如果是打包后的可执行文件
|
||||
application_path = os.path.dirname(sys.executable)
|
||||
else:
|
||||
# 如果是开发环境
|
||||
application_path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# 如果指定了索引,使用指定配置,否则随机选择
|
||||
if config_index is not None and 0 <= config_index < len(CONFIGS):
|
||||
self.current_config = CONFIGS[config_index]
|
||||
else:
|
||||
self.current_config = random.choice(CONFIGS)
|
||||
|
||||
# 设置当前配置
|
||||
self.domain = self.current_config.get("DOMAIN", "")
|
||||
self.temp_mail = self.current_config.get("TEMP_MAIL", "").strip()
|
||||
self.temp_mail_ext = self.current_config.get("TEMP_MAIL_EXT", "").strip()
|
||||
self.temp_mail_epin = self.current_config.get("TEMP_MAIL_EPIN", "").strip()
|
||||
self.browser_user_agent = self.current_config.get("BROWSER_USER_AGENT", "")
|
||||
self.mail_server = self.current_config.get("MAIL_SERVER", "")
|
||||
|
||||
# IMAP相关配置
|
||||
self.imap = False
|
||||
if self.temp_mail == "null":
|
||||
self.imap = True
|
||||
self.imap_server = self.current_config.get("IMAP_SERVER", "")
|
||||
self.imap_port = self.current_config.get("IMAP_PORT", "")
|
||||
self.imap_user = self.current_config.get("IMAP_USER", "")
|
||||
self.imap_pass = self.current_config.get("IMAP_PASS", "")
|
||||
self.imap_dir = self.current_config.get("IMAP_DIR", "inbox")
|
||||
|
||||
def get_config_count(self):
|
||||
"""获取配置总数"""
|
||||
return len(CONFIGS)
|
||||
|
||||
def get_temp_mail(self):
|
||||
return self.temp_mail
|
||||
|
||||
def get_temp_mail_ext(self):
|
||||
return self.temp_mail_ext
|
||||
|
||||
def get_temp_mail_epin(self):
|
||||
return self.temp_mail_epin
|
||||
|
||||
def get_domain(self):
|
||||
return self.domain
|
||||
|
||||
def get_browser_user_agent(self):
|
||||
return self.browser_user_agent
|
||||
|
||||
def get_mail_server(self):
|
||||
return self.mail_server
|
||||
|
||||
def get_imap(self):
|
||||
"""获取IMAP配置"""
|
||||
if not self.imap:
|
||||
return False
|
||||
return {
|
||||
"imap_server": self.imap_server,
|
||||
"imap_port": self.imap_port,
|
||||
"imap_user": self.imap_user,
|
||||
"imap_pass": self.imap_pass,
|
||||
"imap_dir": self.imap_dir,
|
||||
}
|
||||
|
||||
def print_config(self):
|
||||
"""打印当前使用的配置"""
|
||||
logging.info("\n=== 当前使用的配置 ===")
|
||||
if self.imap:
|
||||
logging.info(f"IMAP服务器: {self.imap_server}")
|
||||
logging.info(f"IMAP端口: {self.imap_port}")
|
||||
logging.info(f"IMAP用户名: {self.imap_user}")
|
||||
logging.info(f"IMAP密码: {'*' * len(self.imap_pass)}")
|
||||
logging.info(f"IMAP收件箱目录: {self.imap_dir}")
|
||||
else:
|
||||
logging.info(f"域名: {self.domain}")
|
||||
logging.info(f"临时邮箱: {self.temp_mail}{self.temp_mail_ext}")
|
||||
logging.info(f"邮件服务器: {self.mail_server}")
|
||||
logging.info("=" * 20)
|
||||
|
||||
|
||||
# 使用示例
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
config = Config()
|
||||
print("环境变量加载成功!")
|
||||
config.print_config()
|
||||
except ValueError as e:
|
||||
print(f"错误: {e}")
|
||||
86
backup/cursor_auth_manager.py
Normal file
86
backup/cursor_auth_manager.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import sqlite3
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
class CursorAuthManager:
|
||||
"""Cursor认证信息管理器"""
|
||||
|
||||
def __init__(self):
|
||||
# 判断操作系统
|
||||
if sys.platform == "win32": # Windows
|
||||
appdata = os.getenv("APPDATA")
|
||||
if appdata is None:
|
||||
raise EnvironmentError("APPDATA 环境变量未设置")
|
||||
self.db_path = os.path.join(
|
||||
appdata, "Cursor", "User", "globalStorage", "state.vscdb"
|
||||
)
|
||||
elif sys.platform == "darwin": # macOS
|
||||
self.db_path = os.path.abspath(os.path.expanduser(
|
||||
"~/Library/Application Support/Cursor/User/globalStorage/state.vscdb"
|
||||
))
|
||||
elif sys.platform == "linux" : # Linux 和其他类Unix系统
|
||||
self.db_path = os.path.abspath(os.path.expanduser(
|
||||
"~/.config/Cursor/User/globalStorage/state.vscdb"
|
||||
))
|
||||
else:
|
||||
raise NotImplementedError(f"不支持的操作系统: {sys.platform}")
|
||||
|
||||
def update_auth(self, email=None, access_token=None, refresh_token=None):
|
||||
"""
|
||||
更新Cursor的认证信息
|
||||
:param email: 新的邮箱地址
|
||||
:param access_token: 新的访问令牌
|
||||
:param refresh_token: 新的刷新令牌
|
||||
:return: bool 是否成功更新
|
||||
"""
|
||||
updates = []
|
||||
# 登录状态
|
||||
updates.append(("cursorAuth/cachedSignUpType", "Auth_0"))
|
||||
|
||||
if email is not None:
|
||||
updates.append(("cursorAuth/cachedEmail", email))
|
||||
if access_token is not None:
|
||||
updates.append(("cursorAuth/accessToken", access_token))
|
||||
if refresh_token is not None:
|
||||
updates.append(("cursorAuth/refreshToken", refresh_token))
|
||||
|
||||
if not updates:
|
||||
print("没有提供任何要更新的值")
|
||||
return False
|
||||
|
||||
conn = None
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
for key, value in updates:
|
||||
|
||||
# 如果没有更新任何行,说明key不存在,执行插入
|
||||
# 检查 accessToken 是否存在
|
||||
check_query = f"SELECT COUNT(*) FROM itemTable WHERE key = ?"
|
||||
cursor.execute(check_query, (key,))
|
||||
if cursor.fetchone()[0] == 0:
|
||||
insert_query = "INSERT INTO itemTable (key, value) VALUES (?, ?)"
|
||||
cursor.execute(insert_query, (key, value))
|
||||
else:
|
||||
update_query = "UPDATE itemTable SET value = ? WHERE key = ?"
|
||||
cursor.execute(update_query, (value, key))
|
||||
|
||||
if cursor.rowcount > 0:
|
||||
print(f"成功更新 {key.split('/')[-1]}")
|
||||
else:
|
||||
print(f"未找到 {key.split('/')[-1]} 或值未变化")
|
||||
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
except sqlite3.Error as e:
|
||||
print("数据库错误:", str(e))
|
||||
return False
|
||||
except Exception as e:
|
||||
print("发生错误:", str(e))
|
||||
return False
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
504
backup/cursor_pro_keep_alive.py
Normal file
504
backup/cursor_pro_keep_alive.py
Normal file
@@ -0,0 +1,504 @@
|
||||
import os
|
||||
import platform
|
||||
import json
|
||||
import sys
|
||||
from colorama import Fore, Style
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
from exit_cursor import ExitCursor
|
||||
import go_cursor_help
|
||||
import patch_cursor_get_machine_id
|
||||
from reset_machine import MachineIDResetter
|
||||
|
||||
os.environ["PYTHONVERBOSE"] = "0"
|
||||
os.environ["PYINSTALLER_VERBOSE"] = "0"
|
||||
|
||||
import time
|
||||
import random
|
||||
from cursor_auth_manager import CursorAuthManager
|
||||
import os
|
||||
from logger import logging
|
||||
from browser_utils import BrowserManager
|
||||
from get_email_code import EmailVerificationHandler
|
||||
from logo import print_logo
|
||||
from config import Config
|
||||
from datetime import datetime
|
||||
|
||||
# 定义 EMOJI 字典
|
||||
EMOJI = {"ERROR": "❌", "WARNING": "⚠️", "INFO": "ℹ️"}
|
||||
|
||||
|
||||
class VerificationStatus(Enum):
|
||||
"""验证状态枚举"""
|
||||
|
||||
PASSWORD_PAGE = "@name=password"
|
||||
CAPTCHA_PAGE = "@data-index=0"
|
||||
ACCOUNT_SETTINGS = "Account Settings"
|
||||
|
||||
|
||||
class TurnstileError(Exception):
|
||||
"""Turnstile 验证相关异常"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def save_screenshot(tab, stage: str, timestamp: bool = True) -> None:
|
||||
"""
|
||||
保存页面截图
|
||||
|
||||
Args:
|
||||
tab: 浏览器标签页对象
|
||||
stage: 截图阶段标识
|
||||
timestamp: 是否添加时间戳
|
||||
"""
|
||||
try:
|
||||
# 创建 screenshots 目录
|
||||
screenshot_dir = "screenshots"
|
||||
if not os.path.exists(screenshot_dir):
|
||||
os.makedirs(screenshot_dir)
|
||||
|
||||
# 生成文件名
|
||||
if timestamp:
|
||||
filename = f"turnstile_{stage}_{int(time.time())}.png"
|
||||
else:
|
||||
filename = f"turnstile_{stage}.png"
|
||||
|
||||
filepath = os.path.join(screenshot_dir, filename)
|
||||
|
||||
# 保存截图
|
||||
tab.get_screenshot(filepath)
|
||||
logging.debug(f"截图已保存: {filepath}")
|
||||
except Exception as e:
|
||||
logging.warning(f"截图保存失败: {str(e)}")
|
||||
|
||||
|
||||
def check_verification_success(tab) -> Optional[VerificationStatus]:
|
||||
"""
|
||||
检查验证是否成功
|
||||
|
||||
Returns:
|
||||
VerificationStatus: 验证成功时返回对应状态,失败返回 None
|
||||
"""
|
||||
for status in VerificationStatus:
|
||||
if tab.ele(status.value):
|
||||
logging.info(f"验证成功 - 已到达{status.name}页面")
|
||||
return status
|
||||
return None
|
||||
|
||||
|
||||
def handle_turnstile(tab, max_retries: int = 2, retry_interval: tuple = (1, 2)) -> bool:
|
||||
"""
|
||||
处理 Turnstile 验证
|
||||
|
||||
Args:
|
||||
tab: 浏览器标签页对象
|
||||
max_retries: 最大重试次数
|
||||
retry_interval: 重试间隔时间范围(最小值, 最大值)
|
||||
|
||||
Returns:
|
||||
bool: 验证是否成功
|
||||
|
||||
Raises:
|
||||
TurnstileError: 验证过程中出现异常
|
||||
"""
|
||||
logging.info("正在检测 Turnstile 验证...")
|
||||
save_screenshot(tab, "start")
|
||||
|
||||
retry_count = 0
|
||||
|
||||
try:
|
||||
while retry_count < max_retries:
|
||||
retry_count += 1
|
||||
logging.debug(f"第 {retry_count} 次尝试验证")
|
||||
|
||||
try:
|
||||
# 定位验证框元素
|
||||
challenge_check = (
|
||||
tab.ele("@id=cf-turnstile", timeout=2)
|
||||
.child()
|
||||
.shadow_root.ele("tag:iframe")
|
||||
.ele("tag:body")
|
||||
.sr("tag:input")
|
||||
)
|
||||
|
||||
if challenge_check:
|
||||
logging.info("检测到 Turnstile 验证框,开始处理...")
|
||||
# 随机延时后点击验证
|
||||
time.sleep(random.uniform(1, 3))
|
||||
challenge_check.click()
|
||||
time.sleep(2)
|
||||
|
||||
# 保存验证后的截图
|
||||
save_screenshot(tab, "clicked")
|
||||
|
||||
# 检查验证结果
|
||||
if check_verification_success(tab):
|
||||
logging.info("Turnstile 验证通过")
|
||||
save_screenshot(tab, "success")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logging.debug(f"当前尝试未成功: {str(e)}")
|
||||
|
||||
# 检查是否已经验证成功
|
||||
if check_verification_success(tab):
|
||||
return True
|
||||
|
||||
# 随机延时后继续下一次尝试
|
||||
time.sleep(random.uniform(*retry_interval))
|
||||
|
||||
# 超出最大重试次数
|
||||
logging.error(f"验证失败 - 已达到最大重试次数 {max_retries}")
|
||||
logging.error(
|
||||
"请前往开源项目查看更多信息:https://github.com/chengazhen/cursor-auto-free"
|
||||
)
|
||||
save_screenshot(tab, "failed")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Turnstile 验证过程发生异常: {str(e)}"
|
||||
logging.error(error_msg)
|
||||
save_screenshot(tab, "error")
|
||||
raise TurnstileError(error_msg)
|
||||
|
||||
|
||||
def get_cursor_session_token(tab, max_attempts=3, retry_interval=2):
|
||||
"""
|
||||
获取Cursor会话token,带有重试机制
|
||||
:param tab: 浏览器标签页
|
||||
:param max_attempts: 最大尝试次数
|
||||
:param retry_interval: 重试间隔(秒)
|
||||
:return: session token 或 None
|
||||
"""
|
||||
logging.info("开始获取cookie")
|
||||
attempts = 0
|
||||
|
||||
while attempts < max_attempts:
|
||||
try:
|
||||
cookies = tab.cookies()
|
||||
for cookie in cookies:
|
||||
if cookie.get("name") == "WorkosCursorSessionToken":
|
||||
return cookie["value"].split("%3A%3A")[1]
|
||||
|
||||
attempts += 1
|
||||
if attempts < max_attempts:
|
||||
logging.warning(
|
||||
f"第 {attempts} 次尝试未获取到CursorSessionToken,{retry_interval}秒后重试..."
|
||||
)
|
||||
time.sleep(retry_interval)
|
||||
else:
|
||||
logging.error(
|
||||
f"已达到最大尝试次数({max_attempts}),获取CursorSessionToken失败"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"获取cookie失败: {str(e)}")
|
||||
attempts += 1
|
||||
if attempts < max_attempts:
|
||||
logging.info(f"将在 {retry_interval} 秒后重试...")
|
||||
time.sleep(retry_interval)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def update_cursor_auth(email=None, access_token=None, refresh_token=None):
|
||||
"""
|
||||
更新Cursor的认证信息的便捷函数
|
||||
"""
|
||||
auth_manager = CursorAuthManager()
|
||||
return auth_manager.update_auth(email, access_token, refresh_token)
|
||||
|
||||
|
||||
def sign_up_account(browser, tab):
|
||||
logging.info("=== 开始注册账号流程 ===")
|
||||
logging.info(f"正在访问注册页面: {sign_up_url}")
|
||||
tab.get(sign_up_url)
|
||||
|
||||
try:
|
||||
if tab.ele("@name=first_name"):
|
||||
logging.info("正在填写个人信息...")
|
||||
tab.actions.click("@name=first_name").input(first_name)
|
||||
logging.info(f"已输入名字: {first_name}")
|
||||
time.sleep(random.uniform(1, 3))
|
||||
|
||||
tab.actions.click("@name=last_name").input(last_name)
|
||||
logging.info(f"已输入姓氏: {last_name}")
|
||||
time.sleep(random.uniform(1, 3))
|
||||
|
||||
tab.actions.click("@name=email").input(account)
|
||||
logging.info(f"已输入邮箱: {account}")
|
||||
time.sleep(random.uniform(1, 3))
|
||||
|
||||
logging.info("提交个人信息...")
|
||||
tab.actions.click("@type=submit")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"注册页面访问失败: {str(e)}")
|
||||
return False
|
||||
|
||||
handle_turnstile(tab)
|
||||
|
||||
try:
|
||||
if tab.ele("@name=password"):
|
||||
logging.info("正在设置密码...")
|
||||
tab.ele("@name=password").input(password)
|
||||
time.sleep(random.uniform(1, 3))
|
||||
|
||||
logging.info("提交密码...")
|
||||
tab.ele("@type=submit").click()
|
||||
logging.info("密码设置完成,等待系统响应...")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"密码设置失败: {str(e)}")
|
||||
return False
|
||||
|
||||
if tab.ele("This email is not available."):
|
||||
logging.error("注册失败:邮箱已被使用")
|
||||
return False
|
||||
|
||||
handle_turnstile(tab)
|
||||
|
||||
while True:
|
||||
try:
|
||||
if tab.ele("Account Settings"):
|
||||
logging.info("注册成功 - 已进入账户设置页面")
|
||||
break
|
||||
if tab.ele("@data-index=0"):
|
||||
logging.info("正在获取邮箱验证码...")
|
||||
code = email_handler.get_verification_code()
|
||||
if not code:
|
||||
logging.error("获取验证码失败")
|
||||
return False
|
||||
|
||||
logging.info(f"成功获取验证码: {code}")
|
||||
logging.info("正在输入验证码...")
|
||||
i = 0
|
||||
for digit in code:
|
||||
tab.ele(f"@data-index={i}").input(digit)
|
||||
time.sleep(random.uniform(0.1, 0.3))
|
||||
i += 1
|
||||
logging.info("验证码输入完成")
|
||||
break
|
||||
except Exception as e:
|
||||
logging.error(f"验证码处理过程出错: {str(e)}")
|
||||
|
||||
handle_turnstile(tab)
|
||||
wait_time = random.randint(3, 6)
|
||||
for i in range(wait_time):
|
||||
logging.info(f"等待系统处理中... 剩余 {wait_time-i} 秒")
|
||||
time.sleep(1)
|
||||
|
||||
logging.info("正在获取账户信息...")
|
||||
tab.get(settings_url)
|
||||
try:
|
||||
usage_selector = (
|
||||
"css:div.col-span-2 > div > div > div > div > "
|
||||
"div:nth-child(1) > div.flex.items-center.justify-between.gap-2 > "
|
||||
"span.font-mono.text-sm\\/\\[0\\.875rem\\]"
|
||||
)
|
||||
usage_ele = tab.ele(usage_selector)
|
||||
if usage_ele:
|
||||
usage_info = usage_ele.text
|
||||
total_usage = usage_info.split("/")[-1].strip()
|
||||
logging.info(f"账户可用额度上限: {total_usage}")
|
||||
logging.info(
|
||||
"请前往开源项目查看更多信息:https://github.com/chengazhen/cursor-auto-free"
|
||||
)
|
||||
except Exception as e:
|
||||
logging.error(f"获取账户额度信息失败: {str(e)}")
|
||||
|
||||
logging.info("\n=== 注册完成 ===")
|
||||
account_info = f"Cursor 账号信息:\n邮箱: {account}\n密码: {password}"
|
||||
logging.info(account_info)
|
||||
time.sleep(5)
|
||||
return True
|
||||
|
||||
|
||||
class EmailGenerator:
|
||||
def __init__(
|
||||
self,
|
||||
password="".join(
|
||||
random.choices(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*",
|
||||
k=12,
|
||||
)
|
||||
),
|
||||
):
|
||||
configInstance = Config()
|
||||
configInstance.print_config()
|
||||
self.domain = configInstance.get_domain()
|
||||
self.default_password = password
|
||||
self.default_first_name = self.generate_random_name()
|
||||
self.default_last_name = self.generate_random_name()
|
||||
|
||||
def generate_random_name(self, length=6):
|
||||
"""生成随机用户名"""
|
||||
first_letter = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
rest_letters = "".join(
|
||||
random.choices("abcdefghijklmnopqrstuvwxyz", k=length - 1)
|
||||
)
|
||||
return first_letter + rest_letters
|
||||
|
||||
def generate_email(self, length=8):
|
||||
"""生成随机邮箱地址"""
|
||||
random_str = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=length))
|
||||
timestamp = str(int(time.time()))[-6:] # 使用时间戳后6位
|
||||
return f"{random_str}{timestamp}@{self.domain}"
|
||||
|
||||
def get_account_info(self):
|
||||
"""获取完整的账号信息"""
|
||||
return {
|
||||
"email": self.generate_email(),
|
||||
"password": self.default_password,
|
||||
"first_name": self.default_first_name,
|
||||
"last_name": self.default_last_name,
|
||||
}
|
||||
|
||||
|
||||
def get_user_agent():
|
||||
"""获取user_agent"""
|
||||
try:
|
||||
# 使用JavaScript获取user agent
|
||||
browser_manager = BrowserManager()
|
||||
browser = browser_manager.init_browser()
|
||||
user_agent = browser.latest_tab.run_js("return navigator.userAgent")
|
||||
browser_manager.quit()
|
||||
return user_agent
|
||||
except Exception as e:
|
||||
logging.error(f"获取user agent失败: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
def check_cursor_version():
|
||||
"""检查cursor版本"""
|
||||
pkg_path, main_path = patch_cursor_get_machine_id.get_cursor_paths()
|
||||
with open(pkg_path, "r", encoding="utf-8") as f:
|
||||
version = json.load(f)["version"]
|
||||
return patch_cursor_get_machine_id.version_check(version, min_version="0.45.0")
|
||||
|
||||
|
||||
def reset_machine_id(greater_than_0_45):
|
||||
if greater_than_0_45:
|
||||
# 提示请手动执行脚本 https://github.com/chengazhen/cursor-auto-free/blob/main/patch_cursor_get_machine_id.py
|
||||
go_cursor_help.go_cursor_help()
|
||||
else:
|
||||
MachineIDResetter().reset_machine_ids()
|
||||
|
||||
|
||||
def print_end_message():
|
||||
logging.info("\n\n\n\n\n")
|
||||
logging.info("=" * 30)
|
||||
logging.info("所有操作已完成")
|
||||
logging.info("\n=== 获取更多信息 ===")
|
||||
logging.info("🔥 QQ交流群: 1034718338")
|
||||
logging.info("📺 B站UP主: 想回家的前端")
|
||||
logging.info("=" * 30)
|
||||
logging.info(
|
||||
"请前往开源项目查看更多信息:https://github.com/chengazhen/cursor-auto-free"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print_logo()
|
||||
greater_than_0_45 = 8.8
|
||||
browser_manager = None
|
||||
try:
|
||||
logging.info("\n=== 初始化程序 ===")
|
||||
# ExitCursor()
|
||||
|
||||
# 提示用户选择操作模式
|
||||
print("\n请选择操作模式:")
|
||||
print("1. 仅重置机器码")
|
||||
print("2. 完整注册流程")
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = int(input("请输入选项 (1 或 2): ").strip())
|
||||
if choice in [1, 2]:
|
||||
break
|
||||
else:
|
||||
print("无效的选项,请重新输入")
|
||||
except ValueError:
|
||||
print("请输入有效的数字")
|
||||
|
||||
if choice == 1:
|
||||
# 仅执行重置机器码
|
||||
reset_machine_id(greater_than_0_45)
|
||||
logging.info("机器码重置完成")
|
||||
print_end_message()
|
||||
sys.exit(0)
|
||||
|
||||
logging.info("正在初始化浏览器...")
|
||||
|
||||
# 获取user_agent
|
||||
user_agent = get_user_agent()
|
||||
if not user_agent:
|
||||
logging.error("获取user agent失败,使用默认值")
|
||||
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
|
||||
# 剔除user_agent中的"HeadlessChrome"
|
||||
user_agent = user_agent.replace("HeadlessChrome", "Chrome")
|
||||
|
||||
browser_manager = BrowserManager()
|
||||
browser = browser_manager.init_browser(user_agent)
|
||||
|
||||
# 获取并打印浏览器的user-agent
|
||||
user_agent = browser.latest_tab.run_js("return navigator.userAgent")
|
||||
|
||||
logging.info("正在初始化邮箱验证模块...")
|
||||
email_handler = EmailVerificationHandler()
|
||||
logging.info(
|
||||
"请前往开源项目查看更多信息:https://github.com/chengazhen/cursor-auto-free"
|
||||
)
|
||||
logging.info("\n=== 配置信息 ===")
|
||||
login_url = "https://authenticator.cursor.sh"
|
||||
sign_up_url = "https://authenticator.cursor.sh/sign-up"
|
||||
settings_url = "https://www.cursor.com/settings"
|
||||
mail_url = "https://tempmail.plus"
|
||||
|
||||
logging.info("正在生成随机账号信息...")
|
||||
email_generator = EmailGenerator()
|
||||
account = email_generator.generate_email()
|
||||
password = email_generator.default_password
|
||||
first_name = email_generator.default_first_name
|
||||
last_name = email_generator.default_last_name
|
||||
|
||||
logging.info(f"生成的邮箱账号: {account}")
|
||||
auto_update_cursor_auth = True
|
||||
|
||||
tab = browser.latest_tab
|
||||
|
||||
tab.run_js("try { turnstile.reset() } catch(e) { }")
|
||||
|
||||
logging.info("\n=== 开始注册流程 ===")
|
||||
logging.info(f"正在访问登录页面: {login_url}")
|
||||
tab.get(login_url)
|
||||
|
||||
if sign_up_account(browser, tab):
|
||||
logging.info("正在获取会话令牌...")
|
||||
token = get_cursor_session_token(tab)
|
||||
if token:
|
||||
logging.info("更新认证信息...")
|
||||
update_cursor_auth(
|
||||
email=account, access_token=token, refresh_token=token
|
||||
)
|
||||
logging.info(
|
||||
"请前往开源项目查看更多信息:https://github.com/chengazhen/cursor-auto-free"
|
||||
)
|
||||
logging.info("重置机器码...")
|
||||
reset_machine_id(greater_than_0_45)
|
||||
logging.info("所有操作已完成")
|
||||
print_end_message()
|
||||
else:
|
||||
logging.error("获取会话令牌失败,注册流程未完成")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"程序执行出现错误: {str(e)}")
|
||||
import traceback
|
||||
|
||||
logging.error(traceback.format_exc())
|
||||
finally:
|
||||
if browser_manager:
|
||||
browser_manager.quit()
|
||||
input("\n程序执行完毕,按回车键退出...")
|
||||
68
backup/exit_cursor.py
Normal file
68
backup/exit_cursor.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import psutil
|
||||
from logger import logging
|
||||
import time
|
||||
|
||||
def ExitCursor(timeout=5):
|
||||
"""
|
||||
温和地关闭 Cursor 进程
|
||||
|
||||
Args:
|
||||
timeout (int): 等待进程自然终止的超时时间(秒)
|
||||
Returns:
|
||||
bool: 是否成功关闭所有进程
|
||||
"""
|
||||
try:
|
||||
logging.info("开始退出Cursor...")
|
||||
cursor_processes = []
|
||||
# 收集所有 Cursor 进程
|
||||
for proc in psutil.process_iter(['pid', 'name']):
|
||||
try:
|
||||
if proc.info['name'].lower() in ['cursor.exe', 'cursor']:
|
||||
cursor_processes.append(proc)
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
if not cursor_processes:
|
||||
logging.info("未发现运行中的 Cursor 进程")
|
||||
return True
|
||||
|
||||
# 温和地请求进程终止
|
||||
for proc in cursor_processes:
|
||||
try:
|
||||
if proc.is_running():
|
||||
proc.terminate() # 发送终止信号
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
# 等待进程自然终止
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
still_running = []
|
||||
for proc in cursor_processes:
|
||||
try:
|
||||
if proc.is_running():
|
||||
still_running.append(proc)
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
if not still_running:
|
||||
logging.info("所有 Cursor 进程已正常关闭")
|
||||
return True
|
||||
|
||||
# 等待一小段时间再检查
|
||||
time.sleep(0.5)
|
||||
|
||||
# 如果超时后仍有进程在运行
|
||||
if still_running:
|
||||
process_list = ", ".join([str(p.pid) for p in still_running])
|
||||
logging.warning(f"以下进程未能在规定时间内关闭: {process_list}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"关闭 Cursor 进程时发生错误: {str(e)}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
ExitCursor()
|
||||
126
backup/get_veri_code_test.py
Normal file
126
backup/get_veri_code_test.py
Normal file
@@ -0,0 +1,126 @@
|
||||
from DrissionPage import ChromiumOptions, Chromium
|
||||
from DrissionPage.common import Keys
|
||||
import time
|
||||
import re
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
def get_extension_path():
|
||||
"""获取插件路径"""
|
||||
root_dir = os.getcwd()
|
||||
extension_path = os.path.join(root_dir, "turnstilePatch")
|
||||
|
||||
if hasattr(sys, "_MEIPASS"):
|
||||
print("运行在打包环境中")
|
||||
extension_path = os.path.join(sys._MEIPASS, "turnstilePatch")
|
||||
|
||||
print(f"尝试加载插件路径: {extension_path}")
|
||||
|
||||
if not os.path.exists(extension_path):
|
||||
raise FileNotFoundError(
|
||||
f"插件不存在: {extension_path}\n请确保 turnstilePatch 文件夹在正确位置"
|
||||
)
|
||||
|
||||
return extension_path
|
||||
|
||||
|
||||
def get_browser_options():
|
||||
co = ChromiumOptions()
|
||||
try:
|
||||
extension_path = get_extension_path()
|
||||
co.add_extension(extension_path)
|
||||
except FileNotFoundError as e:
|
||||
print(f"警告: {e}")
|
||||
|
||||
co.set_user_agent(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36"
|
||||
)
|
||||
co.set_pref("credentials_enable_service", False)
|
||||
co.set_argument("--hide-crash-restore-bubble")
|
||||
co.auto_port()
|
||||
|
||||
# Mac 系统特殊处理
|
||||
if sys.platform == "darwin":
|
||||
co.set_argument("--no-sandbox")
|
||||
co.set_argument("--disable-gpu")
|
||||
|
||||
return co
|
||||
|
||||
|
||||
def get_veri_code(username):
|
||||
# 使用相同的浏览器配置
|
||||
co = get_browser_options()
|
||||
browser = Chromium(co)
|
||||
code = None
|
||||
|
||||
try:
|
||||
# 获取当前标签页
|
||||
tab = browser.latest_tab
|
||||
tab.run_js("try { turnstile.reset() } catch(e) { }")
|
||||
|
||||
# 打开临时邮箱网站
|
||||
tab.get("https://tempmail.plus/zh")
|
||||
time.sleep(2)
|
||||
|
||||
# 设置邮箱用户名
|
||||
while True:
|
||||
if tab.ele("@id=pre_button"):
|
||||
# 点击输入框
|
||||
tab.actions.click("@id=pre_button")
|
||||
time.sleep(1)
|
||||
# 删除之前的内容
|
||||
tab.run_js('document.getElementById("pre_button").value = ""')
|
||||
|
||||
# 输入新用户名并回车
|
||||
tab.actions.input(username).key_down(Keys.ENTER).key_up(Keys.ENTER)
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
# 等待并获取新邮件
|
||||
while True:
|
||||
new_mail = tab.ele("@class=mail")
|
||||
if new_mail:
|
||||
if new_mail.text:
|
||||
print("最新的邮件:", new_mail.text)
|
||||
tab.actions.click("@class=mail")
|
||||
break
|
||||
else:
|
||||
print(new_mail)
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
# 提取验证码
|
||||
if tab.ele("@class=overflow-auto mb-20"):
|
||||
email_content = tab.ele("@class=overflow-auto mb-20").text
|
||||
verification_code = re.search(
|
||||
r"verification code is (\d{6})", email_content
|
||||
)
|
||||
if verification_code:
|
||||
code = verification_code.group(1)
|
||||
print("验证码:", code)
|
||||
else:
|
||||
print("未找到验证码")
|
||||
|
||||
# 删除邮件
|
||||
if tab.ele("@id=delete_mail"):
|
||||
tab.actions.click("@id=delete_mail")
|
||||
time.sleep(1)
|
||||
|
||||
if tab.ele("@id=confirm_mail"):
|
||||
tab.actions.click("@id=confirm_mail")
|
||||
print("删除邮件")
|
||||
|
||||
except Exception as e:
|
||||
print(f"发生错误: {str(e)}")
|
||||
finally:
|
||||
browser.quit()
|
||||
|
||||
return code
|
||||
|
||||
|
||||
# 测试运行
|
||||
if __name__ == "__main__":
|
||||
test_username = "test_user" # 替换为你要测试的用户名
|
||||
code = get_veri_code(test_username)
|
||||
print(f"获取到的验证码: {code}")
|
||||
29
backup/go_cursor_help.py
Normal file
29
backup/go_cursor_help.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import platform
|
||||
import os
|
||||
import subprocess
|
||||
from logger import logging
|
||||
|
||||
def go_cursor_help():
|
||||
system = platform.system()
|
||||
logging.info(f"当前操作系统: {system}")
|
||||
|
||||
base_url = "https://aizaozao.com/accelerate.php/https://raw.githubusercontent.com/yuaotian/go-cursor-help/refs/heads/master/scripts/run"
|
||||
|
||||
if system == "Darwin": # macOS
|
||||
cmd = f'curl -fsSL {base_url}/cursor_mac_id_modifier.sh | sudo bash'
|
||||
logging.info("执行macOS命令")
|
||||
os.system(cmd)
|
||||
elif system == "Linux":
|
||||
cmd = f'curl -fsSL {base_url}/cursor_linux_id_modifier.sh | sudo bash'
|
||||
logging.info("执行Linux命令")
|
||||
os.system(cmd)
|
||||
elif system == "Windows":
|
||||
cmd = f'irm {base_url}/cursor_win_id_modifier.ps1 | iex'
|
||||
logging.info("执行Windows命令")
|
||||
# 在Windows上使用PowerShell执行命令
|
||||
subprocess.run(["powershell", "-Command", cmd], shell=True)
|
||||
else:
|
||||
logging.error(f"不支持的操作系统: {system}")
|
||||
return False
|
||||
|
||||
return True
|
||||
302
backup/patch_cursor_get_machine_id.py
Normal file
302
backup/patch_cursor_get_machine_id.py
Normal file
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
# 配置日志
|
||||
def setup_logging() -> logging.Logger:
|
||||
"""配置并返回logger实例"""
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.INFO)
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter(
|
||||
"%(asctime)s - %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
return logger
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
def get_cursor_paths() -> Tuple[str, str]:
|
||||
"""
|
||||
根据不同操作系统获取 Cursor 相关路径
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: (package.json路径, main.js路径)的元组
|
||||
|
||||
Raises:
|
||||
OSError: 当找不到有效路径或系统不支持时抛出
|
||||
"""
|
||||
system = platform.system()
|
||||
|
||||
paths_map = {
|
||||
"Darwin": {
|
||||
"base": "/Applications/Cursor.app/Contents/Resources/app",
|
||||
"package": "package.json",
|
||||
"main": "out/main.js",
|
||||
},
|
||||
"Windows": {
|
||||
"base": os.path.join(
|
||||
os.getenv("LOCALAPPDATA", ""), "Programs", "Cursor", "resources", "app"
|
||||
),
|
||||
"package": "package.json",
|
||||
"main": "out/main.js",
|
||||
},
|
||||
"Linux": {
|
||||
"bases": ["/opt/Cursor/resources/app", "/usr/share/cursor/resources/app"],
|
||||
"package": "package.json",
|
||||
"main": "out/main.js",
|
||||
},
|
||||
}
|
||||
|
||||
if system not in paths_map:
|
||||
raise OSError(f"不支持的操作系统: {system}")
|
||||
|
||||
if system == "Linux":
|
||||
for base in paths_map["Linux"]["bases"]:
|
||||
pkg_path = os.path.join(base, paths_map["Linux"]["package"])
|
||||
if os.path.exists(pkg_path):
|
||||
return (pkg_path, os.path.join(base, paths_map["Linux"]["main"]))
|
||||
raise OSError("在 Linux 系统上未找到 Cursor 安装路径")
|
||||
|
||||
base_path = paths_map[system]["base"]
|
||||
return (
|
||||
os.path.join(base_path, paths_map[system]["package"]),
|
||||
os.path.join(base_path, paths_map[system]["main"]),
|
||||
)
|
||||
|
||||
|
||||
def check_system_requirements(pkg_path: str, main_path: str) -> bool:
|
||||
"""
|
||||
检查系统要求
|
||||
|
||||
Args:
|
||||
pkg_path: package.json 文件路径
|
||||
main_path: main.js 文件路径
|
||||
|
||||
Returns:
|
||||
bool: 检查是否通过
|
||||
"""
|
||||
for file_path in [pkg_path, main_path]:
|
||||
if not os.path.isfile(file_path):
|
||||
logger.error(f"文件不存在: {file_path}")
|
||||
return False
|
||||
|
||||
if not os.access(file_path, os.W_OK):
|
||||
logger.error(f"没有文件写入权限: {file_path}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def version_check(version: str, min_version: str = "", max_version: str = "") -> bool:
|
||||
"""
|
||||
版本号检查
|
||||
|
||||
Args:
|
||||
version: 当前版本号
|
||||
min_version: 最小版本号要求
|
||||
max_version: 最大版本号要求
|
||||
|
||||
Returns:
|
||||
bool: 版本号是否符合要求
|
||||
"""
|
||||
version_pattern = r"^\d+\.\d+\.\d+$"
|
||||
try:
|
||||
if not re.match(version_pattern, version):
|
||||
logger.error(f"无效的版本号格式: {version}")
|
||||
return False
|
||||
|
||||
def parse_version(ver: str) -> Tuple[int, ...]:
|
||||
return tuple(map(int, ver.split(".")))
|
||||
|
||||
current = parse_version(version)
|
||||
|
||||
if min_version and current < parse_version(min_version):
|
||||
logger.error(f"版本号 {version} 小于最小要求 {min_version}")
|
||||
return False
|
||||
|
||||
if max_version and current > parse_version(max_version):
|
||||
logger.error(f"版本号 {version} 大于最大要求 {max_version}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"版本检查失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def modify_main_js(main_path: str) -> bool:
|
||||
"""
|
||||
修改 main.js 文件
|
||||
|
||||
Args:
|
||||
main_path: main.js 文件路径
|
||||
|
||||
Returns:
|
||||
bool: 修改是否成功
|
||||
"""
|
||||
try:
|
||||
# 获取原始文件的权限和所有者信息
|
||||
original_stat = os.stat(main_path)
|
||||
original_mode = original_stat.st_mode
|
||||
original_uid = original_stat.st_uid
|
||||
original_gid = original_stat.st_gid
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp_file:
|
||||
with open(main_path, "r", encoding="utf-8") as main_file:
|
||||
content = main_file.read()
|
||||
|
||||
# 执行替换
|
||||
patterns = {
|
||||
r"async getMachineId\(\)\{return [^??]+\?\?([^}]+)\}": r"async getMachineId(){return \1}",
|
||||
r"async getMacMachineId\(\)\{return [^??]+\?\?([^}]+)\}": r"async getMacMachineId(){return \1}",
|
||||
}
|
||||
|
||||
for pattern, replacement in patterns.items():
|
||||
content = re.sub(pattern, replacement, content)
|
||||
|
||||
tmp_file.write(content)
|
||||
tmp_path = tmp_file.name
|
||||
|
||||
# 使用 shutil.copy2 保留文件权限
|
||||
shutil.copy2(main_path, main_path + ".old")
|
||||
shutil.move(tmp_path, main_path)
|
||||
|
||||
# 恢复原始文件的权限和所有者
|
||||
os.chmod(main_path, original_mode)
|
||||
if os.name != "nt": # 在非Windows系统上设置所有者
|
||||
os.chown(main_path, original_uid, original_gid)
|
||||
|
||||
logger.info("文件修改成功")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"修改文件时发生错误: {str(e)}")
|
||||
if "tmp_path" in locals():
|
||||
os.unlink(tmp_path)
|
||||
return False
|
||||
|
||||
|
||||
def backup_files(pkg_path: str, main_path: str) -> bool:
|
||||
"""
|
||||
备份原始文件
|
||||
|
||||
Args:
|
||||
pkg_path: package.json 文件路径(未使用)
|
||||
main_path: main.js 文件路径
|
||||
|
||||
Returns:
|
||||
bool: 备份是否成功
|
||||
"""
|
||||
try:
|
||||
# 只备份 main.js
|
||||
if os.path.exists(main_path):
|
||||
backup_main = f"{main_path}.bak"
|
||||
shutil.copy2(main_path, backup_main)
|
||||
logger.info(f"已备份 main.js: {backup_main}")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"备份文件失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def restore_backup_files(pkg_path: str, main_path: str) -> bool:
|
||||
"""
|
||||
恢复备份文件
|
||||
|
||||
Args:
|
||||
pkg_path: package.json 文件路径(未使用)
|
||||
main_path: main.js 文件路径
|
||||
|
||||
Returns:
|
||||
bool: 恢复是否成功
|
||||
"""
|
||||
try:
|
||||
# 只恢复 main.js
|
||||
backup_main = f"{main_path}.bak"
|
||||
if os.path.exists(backup_main):
|
||||
shutil.copy2(backup_main, main_path)
|
||||
logger.info(f"已恢复 main.js")
|
||||
return True
|
||||
|
||||
logger.error("未找到备份文件")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"恢复备份失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def patch_cursor_get_machine_id(restore_mode=False) -> None:
|
||||
"""
|
||||
主函数
|
||||
|
||||
Args:
|
||||
restore_mode: 是否为恢复模式
|
||||
"""
|
||||
logger.info("开始执行脚本...")
|
||||
|
||||
try:
|
||||
# 获取路径
|
||||
pkg_path, main_path = get_cursor_paths()
|
||||
|
||||
# 检查系统要求
|
||||
if not check_system_requirements(pkg_path, main_path):
|
||||
sys.exit(1)
|
||||
|
||||
if restore_mode:
|
||||
# 恢复备份
|
||||
if restore_backup_files(pkg_path, main_path):
|
||||
logger.info("备份恢复完成")
|
||||
else:
|
||||
logger.error("备份恢复失败")
|
||||
return
|
||||
|
||||
# 获取版本号
|
||||
try:
|
||||
with open(pkg_path, "r", encoding="utf-8") as f:
|
||||
version = json.load(f)["version"]
|
||||
logger.info(f"当前 Cursor 版本: {version}")
|
||||
except Exception as e:
|
||||
logger.error(f"无法读取版本号: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
# 检查版本
|
||||
if not version_check(version, min_version="0.45.0"):
|
||||
logger.error("版本不符合要求(需 >= 0.45.x)")
|
||||
sys.exit(1)
|
||||
|
||||
logger.info("版本检查通过,准备修改文件")
|
||||
|
||||
# 备份文件
|
||||
if not backup_files(pkg_path, main_path):
|
||||
logger.error("文件备份失败,终止操作")
|
||||
sys.exit(1)
|
||||
|
||||
# 修改文件
|
||||
if not modify_main_js(main_path):
|
||||
sys.exit(1)
|
||||
|
||||
logger.info("脚本执行完成")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"执行过程中发生错误: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
patch_cursor_get_machine_id()
|
||||
Reference in New Issue
Block a user