Hana Celeste commited on
Commit
19b04ec
·
verified ·
1 Parent(s): 5c50257

Update app/byps_logic.py

Browse files
Files changed (1) hide show
  1. 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: await self.start()
 
21
 
22
- browser = await self.playwright.chromium.launch(
23
- headless=False, # Bắt buộc dùng Xvfb
24
- proxy=self.proxy_config,
25
- args=["--no-sandbox", "--disable-setuid-sandbox"]
26
- )
 
 
 
 
 
 
 
 
27
 
28
- context = await browser.new_context(
29
- viewport={'width': 1920, 'height': 1080},
30
- 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"
31
- )
32
-
33
- page = await context.new_page()
34
- # Áp dụng Stealth để giả lập trình duyệt người dùng thật
35
- await stealth(page)
 
36
 
37
- try:
38
- await page.goto(url, wait_until="domcontentloaded", timeout=60000)
 
39
 
40
- # Vòng lặp cực mạnh để xử lý Turnstile
41
- for i in range(20):
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] Đã vượt thành công!")
47
  break
48
 
49
- # THÀNH BẠI ĐÂY: Tìm Iframe của Turnstile Click
50
  try:
51
- # Cloudflare thường giấu checkbox trong một iframe
52
- frames = page.frames
53
- for frame in frames:
54
- if "turnstile" in frame.url or "challenges" in frame.url:
55
- # Thử click vào tọa độ giữa của iframe đó
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 Verify Checkbox")
60
  except:
61
  pass
62
 
63
- # Di chuyển chuột ngẫu nhiên để phỏng người thật
64
- await page.mouse.move(random.randint(100, 700), random.randint(100, 700))
65
- await asyncio.sleep(2)
 
66
 
67
- # Chờ thêm 1 chút để dữ liệu tải xong
68
- await page.wait_for_load_state("networkidle", timeout=5000)
69
 
70
  final_content = await page.content()
71
 
72
- # Check JSON
73
  try:
74
- json_data = await page.evaluate("() => JSON.parse(document.querySelector('pre').innerText)")
 
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
- await browser.close()
 
 
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 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 click vào checkbox Turnstile nếu
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()