Hana Celeste commited on
Commit
e681d15
·
verified ·
1 Parent(s): a25ff06

Update app/aternos_api.py

Browse files
Files changed (1) hide show
  1. app/aternos_api.py +41 -66
app/aternos_api.py CHANGED
@@ -1,11 +1,9 @@
1
  import asyncio
2
- from playwright.async_api import async_playwright
3
- from playwright_stealth import stealth_async
4
 
5
  class AternosAPI:
6
  def __init__(self):
7
  self.playwright = None
8
- # Dùng giao thức HTTP để tránh lỗi "SOCKS5 auth support"
9
  self.proxy_config = {
10
  "server": "http://p.webshare.io:80",
11
  "username": "szusjfnw-rotate",
@@ -16,82 +14,59 @@ class AternosAPI:
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 check_access(self):
25
- if not self.playwright:
26
- await self.start()
 
 
 
 
 
 
27
 
28
- browser = None
29
- try:
30
- browser = await self.playwright.chromium.launch(
31
- headless=True,
32
- proxy=self.proxy_config,
33
- args=[
34
- "--disable-blink-features=AutomationControlled",
35
- "--no-sandbox",
36
- "--disable-dev-shm-usage"
37
- ]
38
- )
39
 
40
- context = await browser.new_context(
41
- user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
42
- )
43
-
44
- page = await context.new_page()
45
- # Bật chế độ tàng hình
46
- await stealth_async(page)
47
 
48
- # --- BẮT ĐẦU QUY TRÌNH VƯỢT CLOUDFLARE ---
49
-
50
- # Bước 1: Vào trang chủ để "nhử" Cloudflare hiện thử thách
51
- print("[INFO] Đang truy cập Aternos...")
52
- await page.goto("https://aternos.org/go/", wait_until="networkidle", timeout=60000)
53
 
54
- # Bước 2: Cơ chế "kiên nhẫn" - Đợi tối đa 15 giây để Cloudflare tự giải quyết
55
- for i in range(15):
56
- content = await page.content()
57
- title = (await page.title()).lower()
58
-
59
- # Nếu không còn thấy dấu hiệu của Cloudflare thì thoát vòng lặp
60
- if "just a moment" not in title and "cloudflare" not in content.lower():
61
- print(f"[INFO] Cloudflare đã thông qua sau {i} giây!")
 
62
  break
63
-
64
- print(f"[INFO] Đang đợi Cloudflare giải quyết... ({i}s)")
65
- # Giả lập di chuyển chuột nhẹ để trông giống người thật
66
- await page.mouse.move(100 + i*10, 100 + i*10)
67
  await asyncio.sleep(1)
68
-
69
- # Bước 3: Sau khi đã lọt qua lớp bảo vệ, tiến vào trang đích
70
- response = await page.goto("https://aternos.org/server/", wait_until="networkidle", timeout=60000)
71
 
72
- # Đợi thêm 2 giây để trang render xong hoàn toàn
 
73
  await asyncio.sleep(2)
74
 
75
- final_title = await page.title()
76
- final_content = await page.content()
77
- status = response.status if response else 0
78
-
79
- # Kiểm tra xem thực sự đã vào được trang quản lý server chưa
80
- # Nếu title chứa "Login" hoặc "Sign up" là vẫn chưa đăng nhập, nhưng ít nhất đã vượt được CF
81
- is_blocked = "just a moment" in final_title.lower() or "cloudflare" in final_content.lower()
82
-
83
  return {
84
- "ok": status == 200 and not is_blocked,
85
- "status": status,
86
- "title": final_title,
87
- "proxy_working": status != 407,
88
- "cloudflare_blocked": is_blocked,
89
- "url": page.url
90
  }
91
 
92
  except Exception as e:
93
- print(f"[ERROR] Có lỗi xảy ra: {str(e)}")
94
  return {"ok": False, "error": str(e)}
95
  finally:
96
- if browser:
97
- await browser.close()
 
1
  import asyncio
2
+ from undetected_playwright.async_api import async_playwright # Lưu ý dùng undetected_playwright
 
3
 
4
  class AternosAPI:
5
  def __init__(self):
6
  self.playwright = None
 
7
  self.proxy_config = {
8
  "server": "http://p.webshare.io:80",
9
  "username": "szusjfnw-rotate",
 
14
  if not self.playwright:
15
  self.playwright = await async_playwright().start()
16
 
 
 
 
 
 
17
  async def check_access(self):
18
+ if not self.playwright: await self.start()
19
+
20
+ # Dùng trình duyệt ẩn danh hoàn toàn
21
+ browser = await self.playwright.chromium.launch(
22
+ headless=True,
23
+ proxy=self.proxy_config,
24
+ args=["--no-sandbox", "--disable-setuid-sandbox", "--disable-blink-features=AutomationControlled"]
25
+ )
26
 
27
+ context = await browser.new_context(
28
+ 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",
29
+ viewport={'width': 1920, 'height': 1080}
30
+ )
 
 
 
 
 
 
 
31
 
32
+ # MẸO CỐT LÕI: Ghi đè webdriver để qua mặt Cloudflare
33
+ await context.add_init_script("""
34
+ Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
35
+ window.chrome = { runtime: {} };
36
+ """)
 
 
37
 
38
+ page = await context.new_page()
 
 
 
 
39
 
40
+ try:
41
+ # Truy cập trang chủ trước
42
+ await page.goto("https://aternos.org/go/", wait_until="commit", timeout=60000)
43
+
44
+ # Đợi thử thách Cloudflare biến mất
45
+ print("[INFO] Đang đợi thử thách Cloudflare...")
46
+ for _ in range(20): # Tăng lên 20 giây cho chắc
47
+ title = await page.title()
48
+ if "Just a moment" not in title:
49
  break
 
 
 
 
50
  await asyncio.sleep(1)
 
 
 
51
 
52
+ # Thử click vào một chỗ trống để trigger tương tác người thật
53
+ await page.mouse.click(100, 100)
54
  await asyncio.sleep(2)
55
 
56
+ # Lấy thông tin cuối cùng
57
+ title = await page.title()
58
+ content = await page.content()
59
+
60
+ # Nếu title là "Login..." nghĩa là đã VƯỢT QUA CLOUDFLARE thành công
61
+ is_cf_gone = "Just a moment" not in title
62
+
 
63
  return {
64
+ "ok": is_cf_gone,
65
+ "title": title,
66
+ "msg": "Vượt Cloudflare thành công!" if is_cf_gone else "Vẫn bị kẹt"
 
 
 
67
  }
68
 
69
  except Exception as e:
 
70
  return {"ok": False, "error": str(e)}
71
  finally:
72
+ await browser.close()