Hana Celeste commited on
Update app/byps_logic.py
Browse files- app/byps_logic.py +55 -35
app/byps_logic.py
CHANGED
|
@@ -16,67 +16,87 @@ class BypsApp:
|
|
| 16 |
if not self.playwright:
|
| 17 |
self.playwright = await async_playwright().start()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
async def fetch(self, url: str):
|
| 20 |
-
if not self.playwright:
|
|
|
|
| 21 |
|
| 22 |
-
browser =
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
# Vòng lặp
|
| 41 |
-
for i in range(
|
| 42 |
content = await page.content()
|
| 43 |
title = await page.title()
|
| 44 |
|
|
|
|
| 45 |
if "just a moment" not in title.lower() and "cloudflare" not in content.lower():
|
| 46 |
-
print("[BYPS]
|
| 47 |
break
|
| 48 |
|
| 49 |
-
#
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
box = await page.locator('iframe[src*="challenge"]').bounding_box()
|
| 57 |
if box:
|
| 58 |
await page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
|
| 59 |
-
print("[BYPS] Đã click vào
|
| 60 |
except:
|
| 61 |
pass
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
await page.mouse.move(random.randint(100,
|
| 65 |
-
await
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
await
|
| 69 |
|
| 70 |
final_content = await page.content()
|
| 71 |
|
| 72 |
-
#
|
| 73 |
try:
|
| 74 |
-
|
|
|
|
| 75 |
return {"ok": True, "type": "json", "data": json_data}
|
| 76 |
except:
|
| 77 |
return {"ok": True, "type": "html", "data": final_content}
|
| 78 |
|
| 79 |
except Exception as e:
|
|
|
|
| 80 |
return {"ok": False, "error": str(e)}
|
| 81 |
finally:
|
| 82 |
-
|
|
|
|
|
|
| 16 |
if not self.playwright:
|
| 17 |
self.playwright = await async_playwright().start()
|
| 18 |
|
| 19 |
+
async def stop(self):
|
| 20 |
+
if self.playwright:
|
| 21 |
+
await self.playwright.stop()
|
| 22 |
+
self.playwright = None
|
| 23 |
+
|
| 24 |
async def fetch(self, url: str):
|
| 25 |
+
if not self.playwright:
|
| 26 |
+
await self.start()
|
| 27 |
|
| 28 |
+
browser = None
|
| 29 |
+
try:
|
| 30 |
+
# Khởi tạo browser với các tham số chống bot mạnh nhất
|
| 31 |
+
browser = await self.playwright.chromium.launch(
|
| 32 |
+
headless=False, # Phải dùng False vì chạy qua Xvfb
|
| 33 |
+
args=[
|
| 34 |
+
"--no-sandbox",
|
| 35 |
+
"--disable-setuid-sandbox",
|
| 36 |
+
"--disable-dev-shm-usage",
|
| 37 |
+
"--disable-blink-features=AutomationControlled",
|
| 38 |
+
"--use-gl=egl",
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
context = await browser.new_context(
|
| 43 |
+
viewport={'width': 1280, 'height': 720},
|
| 44 |
+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
page = await context.new_page()
|
| 48 |
+
|
| 49 |
+
# Kích hoạt Stealth mode chống detect Playwright
|
| 50 |
+
await stealth(page)
|
| 51 |
|
| 52 |
+
print(f"[BYPS] Đang truy cập: {url}")
|
| 53 |
+
# Tăng timeout lên 90s vì Cloudflare giải rất lâu
|
| 54 |
+
await page.goto(url, wait_until="commit", timeout=90000)
|
| 55 |
|
| 56 |
+
# Vòng lặp kiểm tra và giải Challenge
|
| 57 |
+
for i in range(15):
|
| 58 |
content = await page.content()
|
| 59 |
title = await page.title()
|
| 60 |
|
| 61 |
+
# Check nếu đã vượt qua lớp bảo vệ
|
| 62 |
if "just a moment" not in title.lower() and "cloudflare" not in content.lower():
|
| 63 |
+
print(f"[BYPS] Thành công sau {i*2}s")
|
| 64 |
break
|
| 65 |
|
| 66 |
+
# Thử tìm và click vào checkbox Turnstile nếu có
|
| 67 |
try:
|
| 68 |
+
for frame in page.frames:
|
| 69 |
+
if "challenge" in frame.url or "turnstile" in frame.url:
|
| 70 |
+
# Lấy vị trí của iframe để click vào giữa
|
| 71 |
+
target = page.locator('iframe[src*="challenge"]')
|
| 72 |
+
box = await target.bounding_box()
|
|
|
|
| 73 |
if box:
|
| 74 |
await page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
|
| 75 |
+
print("[BYPS] Đã click vào tọa độ Captcha")
|
| 76 |
except:
|
| 77 |
pass
|
| 78 |
|
| 79 |
+
# Giả lập hành vi người dùng: di chuyển chuột và cuộn
|
| 80 |
+
await page.mouse.move(random.randint(100, 600), random.randint(100, 600))
|
| 81 |
+
await page.mouse.wheel(0, 200)
|
| 82 |
+
await asyncio.sleep(3)
|
| 83 |
|
| 84 |
+
# Đợi một chút cho dữ liệu load hết sau khi bypass
|
| 85 |
+
await asyncio.sleep(2)
|
| 86 |
|
| 87 |
final_content = await page.content()
|
| 88 |
|
| 89 |
+
# Kiểm tra xem kết quả là JSON hay HTML
|
| 90 |
try:
|
| 91 |
+
# Nếu trang trả về JSON thô (thường nằm trong thẻ <pre>)
|
| 92 |
+
json_data = await page.evaluate("() => JSON.parse(document.querySelector('body').innerText)")
|
| 93 |
return {"ok": True, "type": "json", "data": json_data}
|
| 94 |
except:
|
| 95 |
return {"ok": True, "type": "html", "data": final_content}
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
print(f"[BYPS ERROR] {str(e)}")
|
| 99 |
return {"ok": False, "error": str(e)}
|
| 100 |
finally:
|
| 101 |
+
if browser:
|
| 102 |
+
await browser.close()
|