Hana Celeste commited on
Commit
db9faca
·
verified ·
1 Parent(s): 5c7079b

Update app/byps_logic.py

Browse files
Files changed (1) hide show
  1. app/byps_logic.py +33 -25
app/byps_logic.py CHANGED
@@ -1,6 +1,7 @@
1
  import asyncio
2
  import random
3
  from playwright.async_api import async_playwright
 
4
 
5
  class BypsApp:
6
  def __init__(self):
@@ -15,60 +16,67 @@ class BypsApp:
15
  if not self.playwright:
16
  self.playwright = await async_playwright().start()
17
 
18
- async def stop(self):
19
- if self.playwright:
20
- await self.playwright.stop()
21
- self.playwright = None
22
-
23
  async def fetch(self, url: str):
24
- if not self.playwright:
25
- await self.start()
26
 
27
  browser = await self.playwright.chromium.launch(
28
- headless=False,
29
  proxy=self.proxy_config,
30
- args=[
31
- "--no-sandbox",
32
- "--disable-blink-features=AutomationControlled",
33
- "--disable-dev-shm-usage"
34
- ]
35
  )
36
 
37
  context = await browser.new_context(
38
- viewport={'width': 1280, 'height': 720},
39
  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"
40
  )
41
 
42
  page = await context.new_page()
43
-
44
- await page.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
45
 
46
  try:
47
- print(f"[BYPS] Accessing: {url}")
48
- await page.goto(url, wait_until="commit", timeout=60000)
49
 
50
- for i in range(10):
 
51
  content = await page.content()
52
  title = await page.title()
53
 
54
  if "just a moment" not in title.lower() and "cloudflare" not in content.lower():
 
55
  break
56
 
57
- print(f"[BYPS] Chờ Cloudflare... {i+1}s")
58
- await page.mouse.move(random.randint(0, 500), random.randint(0, 500))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  await asyncio.sleep(2)
60
 
61
- await asyncio.sleep(2)
 
62
 
63
  final_content = await page.content()
64
 
 
65
  try:
66
- json_content = await page.evaluate("() => JSON.parse(document.querySelector('body').innerText)")
67
- return {"ok": True, "type": "json", "data": json_content}
68
  except:
69
  return {"ok": True, "type": "html", "data": final_content}
70
 
71
  except Exception as e:
72
  return {"ok": False, "error": str(e)}
73
  finally:
74
- await browser.close()
 
1
  import asyncio
2
  import random
3
  from playwright.async_api import async_playwright
4
+ from playwright_stealth import stealth_async
5
 
6
  class BypsApp:
7
  def __init__(self):
 
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_async(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 và 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 để mô 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()