Skip to content

Commit

Permalink
feat: 删除修改机器id
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng zhen committed Jan 5, 2025
1 parent b2661bc commit 88c3f37
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 228 deletions.
110 changes: 69 additions & 41 deletions cursor_pro_keep_alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

# 定义emoji和颜色常量
EMOJI = {
'START': '🚀',
'FORM': '📝',
'VERIFY': '🔄',
'PASSWORD': '🔑',
'CODE': '📱',
'DONE': '✨',
'ERROR': '❌',
'WAIT': '⏳',
'SUCCESS': '✅',
'MAIL': '📧'
"START": "🚀",
"FORM": "📝",
"VERIFY": "🔄",
"PASSWORD": "🔑",
"CODE": "📱",
"DONE": "✨",
"ERROR": "❌",
"WAIT": "⏳",
"SUCCESS": "✅",
"MAIL": "📧",
}

os.environ["PYTHONVERBOSE"] = "0"
Expand All @@ -31,17 +31,19 @@
from logo import print_logo
from reset_machine import MachineIDResetter


# 在文件开头设置日志
class ColoredFormatter(logging.Formatter):
"""自定义彩色日志格式器"""

def format(self, record):
if record.levelno == logging.INFO:
record.msg = f"{Fore.CYAN}{record.msg}{Style.RESET_ALL}"
elif record.levelno == logging.ERROR:
record.msg = f"{Fore.RED}{EMOJI['ERROR']} {record.msg}{Style.RESET_ALL}"
return super().format(record)


# 修改日志配置
logging.basicConfig(
level=logging.INFO,
Expand All @@ -55,14 +57,20 @@ def format(self, record):
# 为控制台处理器设置彩色格式器
for handler in logging.getLogger().handlers:
if isinstance(handler, logging.StreamHandler):
handler.setFormatter(ColoredFormatter("%(asctime)s - %(levelname)s - %(message)s"))
handler.setFormatter(
ColoredFormatter("%(asctime)s - %(levelname)s - %(message)s")
)


def show_progress(progress, total, prefix='Progress:', suffix='Complete', length=50):
def show_progress(progress, total, prefix="Progress:", suffix="Complete", length=50):
filled_length = int(length * progress / total)
bar = Fore.GREEN + '█' * filled_length + Fore.WHITE + '░' * (length - filled_length)
bar = Fore.GREEN + "█" * filled_length + Fore.WHITE + "░" * (length - filled_length)
percent = f"{100.0 * progress / total:.1f}"
print(f'\r{Fore.CYAN}{prefix} |{bar}| {Fore.YELLOW}{percent}%{Fore.BLUE} {suffix}{Style.RESET_ALL}', end='', flush=True)
print(
f"\r{Fore.CYAN}{prefix} |{bar}| {Fore.YELLOW}{percent}%{Fore.BLUE} {suffix}{Style.RESET_ALL}",
end="",
flush=True,
)
if progress == total:
print()

Expand All @@ -81,11 +89,15 @@ def handle_turnstile(tab):
)

if challengeCheck:
print(f"{Fore.YELLOW}{EMOJI['WAIT']} 检测到验证请求,开始处理...{Style.RESET_ALL}")
print(
f"{Fore.YELLOW}{EMOJI['WAIT']} 检测到验证请求,开始处理...{Style.RESET_ALL}"
)
time.sleep(random.uniform(1, 3))
challengeCheck.click()
time.sleep(2)
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 验证突破成功!{Style.RESET_ALL}")
print(
f"{Fore.GREEN}{EMOJI['SUCCESS']} 验证突破成功!{Style.RESET_ALL}"
)
return True
except:
pass
Expand Down Expand Up @@ -114,15 +126,19 @@ def get_cursor_session_token(tab, max_attempts=3, retry_interval=2):
:param retry_interval: 重试间隔(秒)
:return: session token 或 None
"""
print(f"{Fore.CYAN}{EMOJI['WAIT']} 开始获取 Cursor Session Token...{Style.RESET_ALL}")
print(
f"{Fore.CYAN}{EMOJI['WAIT']} 开始获取 Cursor Session Token...{Style.RESET_ALL}"
)
attempts = 0

while attempts < max_attempts:
try:
cookies = tab.cookies()
for cookie in cookies:
if cookie.get("name") == "WorkosCursorSessionToken":
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} Token 获取成功{Style.RESET_ALL}")
print(
f"{Fore.GREEN}{EMOJI['SUCCESS']} Token 获取成功{Style.RESET_ALL}"
)
return cookie["value"].split("%3A%3A")[1]

attempts += 1
Expand All @@ -132,13 +148,19 @@ def get_cursor_session_token(tab, max_attempts=3, retry_interval=2):
)
time.sleep(retry_interval)
else:
print(f"{Fore.RED}{EMOJI['ERROR']} 已达到最大尝试次数({max_attempts}),获取 Token 失败{Style.RESET_ALL}")
print(
f"{Fore.RED}{EMOJI['ERROR']} 已达到最大尝试次数({max_attempts}),获取 Token 失败{Style.RESET_ALL}"
)

except Exception as e:
print(f"{Fore.RED}{EMOJI['ERROR']} 获取 Token 失败: {str(e)}{Style.RESET_ALL}")
print(
f"{Fore.RED}{EMOJI['ERROR']} 获取 Token 失败: {str(e)}{Style.RESET_ALL}"
)
attempts += 1
if attempts < max_attempts:
print(f"{Fore.YELLOW}{EMOJI['WAIT']} 将在 {retry_interval} 秒后重试...{Style.RESET_ALL}")
print(
f"{Fore.YELLOW}{EMOJI['WAIT']} 将在 {retry_interval} 秒后重试...{Style.RESET_ALL}"
)
time.sleep(retry_interval)

return None
Expand All @@ -150,18 +172,18 @@ def update_cursor_auth(email=None, access_token=None, refresh_token=None):
"""
print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
print(f"{Fore.YELLOW}{EMOJI['WAIT']} 正在更新 Cursor 认证信息...{Style.RESET_ALL}")

auth_manager = CursorAuthManager()
result = auth_manager.update_auth(email, access_token, refresh_token)

if result:
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 邮箱信息更新成功{Style.RESET_ALL}")
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} Access Token 更新成功{Style.RESET_ALL}")
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} Refresh Token 更新成功{Style.RESET_ALL}")
print(f"{Fore.GREEN}{EMOJI['DONE']} 认证信息更新完成{Style.RESET_ALL}")
else:
print(f"{Fore.RED}{EMOJI['ERROR']} 认证信息更新失败{Style.RESET_ALL}")

print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
return result

Expand All @@ -173,16 +195,20 @@ def sign_up_account(browser, tab):
print(f"{Fore.YELLOW}[信息]{Style.RESET_ALL}")
print(f"{EMOJI['FORM']} 邮箱服务商: {Fore.GREEN}mailto.plus{Style.RESET_ALL}")
print(f"{EMOJI['FORM']} 临时邮箱地址: {Fore.GREEN}{account}{Style.RESET_ALL}")
print(f"{EMOJI['FORM']} 注册名称: {Fore.GREEN}{first_name} {last_name}{Style.RESET_ALL}")
print(
f"{EMOJI['FORM']} 注册名称: {Fore.GREEN}{first_name} {last_name}{Style.RESET_ALL}"
)
print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}\n")

total_steps = 5
show_progress(0, total_steps)
tab.get(sign_up_url)

try:
if tab.ele("@name=first_name"):
print(f"\n{Fore.YELLOW}{EMOJI['FORM']} [1/5] 填写注册信息...{Style.RESET_ALL}")
print(
f"\n{Fore.YELLOW}{EMOJI['FORM']} [1/5] 填写注册信息...{Style.RESET_ALL}"
)
tab.actions.click("@name=first_name").input(first_name)
time.sleep(random.uniform(1, 3))

Expand All @@ -205,7 +231,9 @@ def sign_up_account(browser, tab):

try:
if tab.ele("@name=password"):
print(f"\n{Fore.YELLOW}{EMOJI['PASSWORD']} [3/5] 设置密码...{Style.RESET_ALL}")
print(
f"\n{Fore.YELLOW}{EMOJI['PASSWORD']} [3/5] 设置密码...{Style.RESET_ALL}"
)
tab.ele("@name=password").input(password)
time.sleep(random.uniform(1, 3))

Expand Down Expand Up @@ -250,7 +278,7 @@ def sign_up_account(browser, tab):
for i in range(wait_time):
print(f"{Fore.CYAN}{EMOJI['WAIT']} 等待中... {wait_time-i}{Style.RESET_ALL}")
time.sleep(1)

# 获取可用额度
total_usage = "未知"
tab.get(settings_url)
Expand All @@ -265,7 +293,9 @@ def sign_up_account(browser, tab):
usage_info = usage_ele.text
total_usage = usage_info.split("/")[-1].strip()
except Exception as e:
print(f"\n{Fore.RED}{EMOJI['ERROR']} 获取可用额度失败: {str(e)}{Style.RESET_ALL}")
print(
f"\n{Fore.RED}{EMOJI['ERROR']} 获取可用额度失败: {str(e)}{Style.RESET_ALL}"
)

# 显示最终信息
print(f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}")
Expand All @@ -276,7 +306,7 @@ def sign_up_account(browser, tab):
print(f"{EMOJI['SUCCESS']} 密码: {Fore.GREEN}{password}{Style.RESET_ALL}")
print(f"{EMOJI['SUCCESS']} 可用额度: {Fore.GREEN}{total_usage}{Style.RESET_ALL}")
print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}")

# 记录到日志
account_info = (
f"\n{Fore.CYAN}{'='*50}{Style.RESET_ALL}\n"
Expand Down Expand Up @@ -364,14 +394,11 @@ def get_account_info(self):
update_cursor_auth(
email=account, access_token=token, refresh_token=token
)

# 在认证更新成功后重置机器ID
print(f"\n{Fore.CYAN}{EMOJI['WAIT']} 开始重置机器标识...{Style.RESET_ALL}")
resetter = MachineIDResetter()
if resetter.reset_machine_ids():
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识重置完成{Style.RESET_ALL}")
else:
print(f"{Fore.RED}{EMOJI['ERROR']} 机器标识重置失败{Style.RESET_ALL}")

# 提示如果需要修改机器id 去 https://github.com/yuaotian/go-cursor-help
print(
f"{Fore.YELLOW}如果需要修改机器id 去 https://github.com/yuaotian/go-cursor-help{Style.RESET_ALL}"
)
else:
print(f"{Fore.RED}{EMOJI['ERROR']} 账户注册失败{Style.RESET_ALL}")

Expand All @@ -380,8 +407,9 @@ def get_account_info(self):
except Exception as e:
logging.error(f"程序执行出错: {str(e)}")
import traceback

logging.error(traceback.format_exc())
finally:
if browser_manager:
browser_manager.quit()
input(f"\n{Fore.CYAN}{EMOJI['WAIT']} 按回车键退出...{Style.RESET_ALL}")
input(f"\n{Fore.CYAN}{EMOJI['WAIT']} 按回车键退出...{Style.RESET_ALL}")
Loading

0 comments on commit 88c3f37

Please sign in to comment.