-
Notifications
You must be signed in to change notification settings - Fork 822
Expand file tree
/
Copy pathtavily_browser_solver.py
More file actions
651 lines (559 loc) · 22.9 KB
/
tavily_browser_solver.py
File metadata and controls
651 lines (559 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""
使用 Camoufox + Solver 完成注册
思路:从真实注册页开始,自动处理 Turnstile、邮箱验证码和密码设置
"""
import os
import re
import threading
import time
import requests as std_requests
from camoufox.sync_api import Camoufox
from config import (
API_KEY_TIMEOUT,
EMAIL_CODE_TIMEOUT,
LOCAL_SOLVER_URL,
REGISTER_HEADLESS,
)
from mail_provider import get_email_code, get_verification_link
TURNSTILE_SITEKEY = "0x4AAAAAAAQFNSW6xordsuIq"
_HERE = os.path.dirname(os.path.abspath(__file__))
_SAVE_FILE = os.path.join(_HERE, "accounts.txt")
_SAVE_LOCK = threading.Lock()
def extract_signup_url(html):
"""从登录密码页里提取注册入口"""
match = re.search(r'href="(/u/signup/identifier[^"]*)"', html)
if not match:
return None
return f"https://auth.tavily.com{match.group(1)}"
def has_unified_identifier_entry(page):
"""新版 Tavily 已改成登录/注册合一入口,当前页可直接作为注册起点。"""
selectors = [
'input[name="username"]',
'input[name="email"]',
'input[type="email"]',
]
has_email_input = any(page.query_selector(selector) for selector in selectors)
has_continue = any(
page.query_selector(selector)
for selector in (
'button[type="submit"]',
'button:has-text("Continue")',
'button:has-text("Sign Up")',
)
)
return has_email_input and has_continue
def fill_first_input(page, selectors, value):
"""填充第一个存在的输入框"""
for selector in selectors:
if page.query_selector(selector):
page.fill(selector, value)
return selector
return None
def close_marketing_dialog(page):
"""关闭首页营销弹窗,避免遮挡 API Key 区域"""
close_button = page.query_selector('button[aria-label="Close"]')
if close_button:
close_button.click()
time.sleep(1)
def extract_api_key(page):
"""从当前页面 HTML 里提取明文 API Key"""
html = page.content()
api_key_matches = re.findall(r'tvly-[a-zA-Z0-9_-]{20,}', html)
api_keys = [k for k in api_key_matches if k != "tvly-YOUR_API_KEY"]
if not api_keys:
return None
return max(api_keys, key=len)
def wait_for_api_key(page, timeout=20):
"""等待首页 API Key 模块渲染出明文 key"""
start_time = time.time()
while time.time() - start_time < timeout:
close_marketing_dialog(page)
api_key = extract_api_key(page)
if api_key:
return api_key
time.sleep(1)
return None
def save_account(email, password, api_key):
"""并发注册时串行写入 accounts.txt,避免落盘交叉。"""
with _SAVE_LOCK:
with open(_SAVE_FILE, 'a', encoding='utf-8') as f:
f.write(f"{email},{password},{api_key}\n")
def verify_api_key(api_key, timeout=30):
"""真实调用 Tavily API,验证新 key 可用。"""
try:
response = std_requests.post(
"https://api.tavily.com/search",
json={
"api_key": api_key,
"query": "api key verification",
"max_results": 1,
},
timeout=timeout,
)
except Exception as exc:
print(f"❌ API Key 调用测试失败: {exc}")
return False
if response.status_code == 200:
print("✅ API Key 调用测试通过")
return True
preview = response.text.strip().replace("\n", " ")[:160]
print(f"❌ API Key 调用测试失败: HTTP {response.status_code}")
if preview:
print(f" 响应: {preview}")
return False
def submit_primary_action(page, input_selector=None):
"""优先提交默认 Continue 动作,避免误点 Resend / Go back"""
button_selectors = [
'button[data-action-button-primary="true"]',
'button[type="submit"][name="action"][value="default"]:not([aria-hidden="true"])',
'button[type="submit"]:not([aria-hidden="true"])',
]
for selector in button_selectors:
if page.query_selector(selector):
try:
page.click(selector, no_wait_after=True, timeout=3000)
return True
except Exception:
continue
if input_selector and page.query_selector(input_selector):
try:
page.press(input_selector, 'Enter')
return True
except Exception:
return False
return False
def extract_page_feedback(page):
"""提取页面上的高价值提示,便于定位卡点。"""
selectors = [
'[role="alert"]',
'[data-error-visible="true"]',
'.ulp-input-error-message',
'.auth0-global-message',
'.cf-turnstile-error',
]
messages = []
for selector in selectors:
for node in page.query_selector_all(selector):
text = (node.inner_text() or "").strip()
if text and text not in messages:
messages.append(text)
return " | ".join(messages)
def print_feedback_hint(feedback):
"""针对常见失败原因补一条更直接的提示。"""
if not feedback:
return
lowered = feedback.lower()
if "suspicious activity detected" in lowered:
print(" 提示: Tavily 当前会拦截公开 DuckMail 域名,建议改用私有 DuckMail 域名/API Key 或 Cloudflare。")
elif "security challenge" in lowered:
print(" 提示: 当前卡在密码页二次安全挑战,程序已尝试自动重提,但目标站仍可触发额外风控。")
def wait_for_post_signup_target(page, timeout):
"""等待注册后跳转到 Tavily 或验证页。"""
deadline = time.time() + (timeout / 1000)
while time.time() < deadline:
current_url = page.url.lower()
if "app.tavily.com" in current_url or "/verify" in current_url or "/continue" in current_url:
return True
time.sleep(0.5)
return False
def normalize_feedback(feedback):
"""统一页面提示文案,方便做关键字判断。"""
return (feedback or "").replace("’", "'").strip().lower()
def get_turnstile_sitekey(page):
"""优先从当前页面提取 sitekey,拿不到再回退默认值。"""
try:
sitekey = page.evaluate(
"""
() => {
const node = document.querySelector(
'[data-captcha-sitekey], .cf-turnstile, [data-sitekey]'
);
if (!node) {
return '';
}
return (
node.getAttribute('data-captcha-sitekey') ||
node.getAttribute('data-sitekey') ||
''
);
}
"""
)
except Exception:
sitekey = ""
if sitekey:
return sitekey.strip()
html = page.content()
match = re.search(
r'(?:data-captcha-sitekey|data-sitekey)=["\']([^"\']+)["\']',
html,
re.IGNORECASE,
)
if match:
return match.group(1).strip()
return TURNSTILE_SITEKEY
def collect_turnstile_state(page):
"""收集密码页 challenge 相关 DOM 状态,便于判断恢复策略。"""
try:
state = page.evaluate(
"""
() => {
const passwordInput = document.querySelector('input[name="password"]');
const widget = document.querySelector(
'div[data-captcha-sitekey], .cf-turnstile, [data-sitekey]'
);
const iframe = document.querySelector(
'iframe[src*="challenges.cloudflare.com"], iframe[src*="turnstile"]'
);
const captchaInput = document.querySelector(
'input[name="captcha"], input[name="cf-turnstile-response"], textarea[name="cf-turnstile-response"]'
);
return {
hasCaptchaDiv: !!widget,
hasChallengeIframe: !!iframe,
hasCaptchaInput: !!captchaInput,
hasTurnstile: typeof window.turnstile !== 'undefined',
hasPasswordInput: !!passwordInput,
passwordValueLength: passwordInput ? passwordInput.value.length : 0,
sitekey: widget
? (widget.getAttribute('data-captcha-sitekey') || widget.getAttribute('data-sitekey') || '')
: '',
};
}
"""
)
except Exception:
state = {}
return {
"hasCaptchaDiv": bool(state.get("hasCaptchaDiv")),
"hasChallengeIframe": bool(state.get("hasChallengeIframe")),
"hasCaptchaInput": bool(state.get("hasCaptchaInput")),
"hasTurnstile": bool(state.get("hasTurnstile")),
"hasPasswordInput": bool(state.get("hasPasswordInput")),
"passwordValueLength": int(state.get("passwordValueLength") or 0),
"sitekey": (state.get("sitekey") or "").strip(),
}
def has_password_challenge_signal(feedback=None, state=None):
"""判断当前是否出现了密码页安全挑战。"""
lowered = normalize_feedback(feedback)
if any(
keyword in lowered
for keyword in (
"security challenge",
"captcha",
"turnstile",
"cloudflare",
"couldn't load the security challenge",
)
):
return True
state = state or {}
return any(
(
state.get("hasCaptchaDiv"),
state.get("hasChallengeIframe"),
state.get("hasCaptchaInput"),
state.get("hasTurnstile"),
)
)
def format_turnstile_state(state):
"""压缩输出 challenge 状态,便于终端观察。"""
return (
f"captchaDiv={'Y' if state.get('hasCaptchaDiv') else 'N'}, "
f"iframe={'Y' if state.get('hasChallengeIframe') else 'N'}, "
f"input={'Y' if state.get('hasCaptchaInput') else 'N'}, "
f"turnstile={'Y' if state.get('hasTurnstile') else 'N'}, "
f"pwdLen={state.get('passwordValueLength', 0)}"
)
def refill_password(page, password):
"""挑战失败后密码输入框经常会被清空,重填一次。"""
selector = 'input[name="password"]'
if not page.query_selector(selector):
return False
page.fill(selector, password)
return True
def refresh_password_page_if_needed(page, feedback, state):
"""如果挑战脚本没加载出来,刷新当前密码页拿一遍新的 DOM。"""
lowered = normalize_feedback(feedback)
if "couldn't load the security challenge" not in lowered:
return False
# 新版 Auth0 页面经常会先渲染占位 captcha div / hidden input,
# 但真正的 Cloudflare iframe 还没挂起来;这种情况也需要刷新。
if state.get("hasChallengeIframe") or state.get("hasTurnstile"):
return False
print("🔄 检测到安全挑战加载失败,刷新密码页后重试...")
try:
page.reload(wait_until="networkidle", timeout=30000)
page.wait_for_selector('input[name="password"]', timeout=15000)
time.sleep(2)
return True
except Exception as exc:
print(f"⚠️ 刷新密码页失败: {exc}")
return False
def wait_for_password_challenge_ready(page, timeout=8):
"""等待密码页的 Cloudflare challenge 尽量挂起。"""
deadline = time.time() + timeout
latest_state = {}
while time.time() < deadline:
latest_state = collect_turnstile_state(page)
if latest_state.get("hasChallengeIframe") or latest_state.get("hasTurnstile"):
return latest_state
time.sleep(0.5)
return latest_state
def ensure_password_challenge_ready(page):
"""密码页刚出现时先给 challenge 一个渲染窗口,必要时主动刷新一次。"""
state = wait_for_password_challenge_ready(page, timeout=6)
if not state.get("hasPasswordInput"):
return state
if state.get("hasChallengeIframe") or state.get("hasTurnstile"):
return state
if state.get("hasCaptchaDiv") or state.get("hasCaptchaInput"):
print("🔄 密码页 challenge 未完全渲染,先刷新一次页面...")
try:
page.reload(wait_until="networkidle", timeout=30000)
page.wait_for_selector('input[name="password"]', timeout=15000)
time.sleep(2)
state = wait_for_password_challenge_ready(page, timeout=6)
except Exception as exc:
print(f"⚠️ 密码页预刷新失败: {exc}")
return state
def recover_password_challenge(page, password, max_attempts=3):
"""密码页未跳转时,尝试等待 challenge 渲染并恢复提交流程。"""
print("⚠️ 密码页未完成跳转,开始处理安全挑战...")
for attempt in range(1, max_attempts + 1):
if wait_for_post_signup_target(page, timeout=5000):
return True
time.sleep(2)
feedback = extract_page_feedback(page)
state = collect_turnstile_state(page)
print(f"🔁 密码页恢复尝试 {attempt}/{max_attempts}")
print(f" DOM: {format_turnstile_state(state)}")
if feedback:
print(f" 提示: {feedback}")
if wait_for_post_signup_target(page, timeout=2000):
return True
if refresh_password_page_if_needed(page, feedback, state):
feedback = extract_page_feedback(page)
state = collect_turnstile_state(page)
print(f" 刷新后 DOM: {format_turnstile_state(state)}")
if feedback:
print(f" 刷新后提示: {feedback}")
if wait_for_post_signup_target(page, timeout=2000):
return True
if has_password_challenge_signal(feedback, state):
sitekey = state.get("sitekey") or get_turnstile_sitekey(page)
print(f"🔐 尝试恢复 Turnstile challenge (sitekey={sitekey})")
token = solve_turnstile(page.url, sitekey=sitekey)
if token:
if inject_turnstile_token(page, token):
print("✅ 已注入密码页 challenge token")
else:
print("⚠️ Token 已拿到,但页面未确认注入成功,继续重提")
else:
print("⚠️ 密码页 challenge token 获取失败,继续执行普通重提")
else:
print("⏳ 未检测到显式 challenge DOM,执行延迟重提")
if not refill_password(page, password):
if wait_for_post_signup_target(page, timeout=5000):
return True
print("❌ 密码输入框丢失,无法继续恢复")
return False
time.sleep(1)
submit_primary_action(page, 'input[name="password"]')
time.sleep(4)
return wait_for_post_signup_target(page, timeout=5000)
def submit_password_with_recovery(page, password):
"""提交密码,并在随机 challenge 场景下自动恢复。"""
initial_state = ensure_password_challenge_ready(page)
if initial_state:
print(f"🔍 密码页 challenge 状态: {format_turnstile_state(initial_state)}")
if not refill_password(page, password):
return False
time.sleep(1)
submit_primary_action(page, 'input[name="password"]')
time.sleep(5)
if wait_for_post_signup_target(page, timeout=15000):
return True
return recover_password_challenge(page, password)
def solve_turnstile(url, sitekey=TURNSTILE_SITEKEY):
"""调用 Solver 获取 token"""
try:
r = std_requests.get(
f"{LOCAL_SOLVER_URL}/turnstile",
params={"url": url, "sitekey": sitekey or TURNSTILE_SITEKEY},
timeout=10
)
if r.status_code != 200:
print(f"❌ Solver 请求失败: {r.status_code}")
return None
task_id = r.json().get("taskId")
if not task_id:
print("❌ 未获取到 Task ID")
return None
# 轮询结果
for i in range(60):
time.sleep(2)
res = std_requests.get(
f"{LOCAL_SOLVER_URL}/result",
params={"id": task_id},
timeout=10
)
if res.status_code == 200:
data = res.json()
status = data.get("status")
if status == "ready":
token = data.get("solution", {}).get("token")
if token:
return token
elif status == "CAPTCHA_FAIL":
return None
return None
except Exception as e:
print(f"❌ Solver 异常: {e}")
return None
def inject_turnstile_token(page, token):
"""注入 Turnstile token 到页面"""
safe_token = token.replace("\\", "\\\\").replace("'", "\\'")
script = f"""
(function() {{
const token = '{safe_token}';
const form = document.querySelector('form') || document.body;
const names = ['captcha', 'cf-turnstile-response'];
const ensureField = (name) => {{
let field = document.querySelector(`input[name="${{name}}"], textarea[name="${{name}}"]`);
if (field) {{
return field;
}}
field = document.createElement(name.includes('response') ? 'textarea' : 'input');
if (field.tagName === 'INPUT') {{
field.type = 'hidden';
}}
field.name = name;
form.appendChild(field);
return field;
}};
names.forEach((name) => {{
const field = ensureField(name);
field.value = token;
field.dispatchEvent(new Event('input', {{ bubbles: true }}));
field.dispatchEvent(new Event('change', {{ bubbles: true }}));
}});
if (typeof window._turnstileTokenCallback === 'function') {{
window._turnstileTokenCallback(token);
}}
if (typeof window.turnstileCallback === 'function') {{
window.turnstileCallback(token);
}}
return true;
}})();
"""
return page.evaluate(script)
def register_with_browser_solver(email, password):
"""使用浏览器 + Solver 注册"""
print(f"🌐 使用浏览器模式注册: {email}")
try:
with Camoufox(headless=REGISTER_HEADLESS) as browser:
page = browser.new_page()
# 1. 访问 Tavily 统一登录/注册入口
page.goto("https://app.tavily.com/sign-in", wait_until="networkidle", timeout=30000)
time.sleep(2)
signup_url = extract_signup_url(page.content())
if signup_url:
print("🧭 进入注册页...")
page.goto(signup_url, wait_until="networkidle", timeout=30000)
time.sleep(2)
elif has_unified_identifier_entry(page):
print("ℹ️ 检测到 Tavily 新版登录/注册合一入口,直接复用当前页面...")
else:
print(f"❌ 未找到注册入口: {page.url}")
return None
email_selector = fill_first_input(page, ['input[name="email"]', 'input[name="username"]'], email)
if not email_selector:
print("❌ 注册页未找到邮箱输入框")
return None
# 2. 注册页 Turnstile
print("🔐 处理注册页 Turnstile...")
token1 = solve_turnstile(page.url, sitekey=get_turnstile_sitekey(page))
if not token1:
print("❌ Token 获取失败")
return None
print(f"✅ Token: {token1[:50]}...")
if inject_turnstile_token(page, token1):
print("✅ Token 已注入")
else:
print("⚠️ 未找到 captcha 输入框")
# 3. 提交邮箱,进入验证码页
submit_primary_action(page, email_selector)
time.sleep(6)
try:
page.wait_for_selector('input[name="code"], input[name="password"]', timeout=15000)
except:
print("⚠️ 首次提交后未跳转,重试点击 Continue...")
submit_primary_action(page)
time.sleep(3)
try:
page.wait_for_selector('input[name="code"], input[name="password"]', timeout=20000)
except:
feedback = extract_page_feedback(page)
print(f"❌ 未进入验证码/密码页面: {page.url}")
if feedback:
print(f" 页面提示: {feedback}")
print_feedback_hint(feedback)
return None
if page.query_selector('input[name="code"]'):
print("✅ 到达邮箱验证码页")
code = get_email_code(email, timeout=EMAIL_CODE_TIMEOUT)
if not code:
return None
page.fill('input[name="code"]', code)
submit_primary_action(page, 'input[name="code"]')
time.sleep(3)
# 4. 进入密码页
try:
page.wait_for_selector('input[name="password"]', timeout=30000)
print("✅ 到达注册密码页")
except:
print(f"❌ 未到达注册密码页: {page.url}")
return None
# 5. 设置密码
if not submit_password_with_recovery(page, password):
feedback = extract_page_feedback(page)
print(f"❌ 登录失败: {page.url}")
if feedback:
print(f" 页面提示: {feedback}")
print_feedback_hint(feedback)
return None
print("✅ 登录成功")
# 7. 检查是否还需要额外验证
time.sleep(3)
if 'verify' in page.url.lower():
print("📧 需要邮件验证")
verify_url = get_verification_link(email, timeout=60)
if not verify_url:
return None
page.goto(verify_url, wait_until="networkidle", timeout=60000)
page.wait_for_url("**/app.tavily.com/**", timeout=60000)
time.sleep(3)
# 8. 获取 API Key
print("🔑 获取 API Key...")
time.sleep(3)
api_key = wait_for_api_key(page, timeout=API_KEY_TIMEOUT)
if not api_key:
print("⚠️ 未找到 API Key")
return None
print("🧪 验证 API Key 可用性...")
if not verify_api_key(api_key):
return None
save_account(email, password, api_key)
print(f"🎉 注册成功")
print(f" 邮箱: {email}")
print(f" 密码: {password}")
print(f" Key : {api_key}")
return api_key
except Exception as e:
print(f"❌ 注册失败: {e}")
return None
if __name__ == "__main__":
from tavily_core import create_email
email, password = create_email()
register_with_browser_solver(email, password)