Hana Celeste commited on
Commit
77a7a0b
·
verified ·
1 Parent(s): 2cd457a

Update app/aternos_api.py

Browse files
Files changed (1) hide show
  1. app/aternos_api.py +58 -39
app/aternos_api.py CHANGED
@@ -5,11 +5,12 @@ from playwright_stealth import stealth_async
5
  class AternosAPI:
6
  def __init__(self):
7
  self.playwright = None
 
8
  self.proxy_config = {
9
- "server": "http://p.webshare.io:80",
10
- "username": "szusjfnw-rotate",
11
- "password": "mftzj9sljp5t"
12
- }
13
 
14
  async def start(self):
15
  if not self.playwright:
@@ -21,58 +22,76 @@ class AternosAPI:
21
  self.playwright = None
22
 
23
  async def check_access(self):
24
- """Hàm chính để FastAPI gọi vào"""
25
  if not self.playwright:
26
  await self.start()
27
 
28
- browser = await self.playwright.chromium.launch(
29
- headless=True,
30
- proxy=self.proxy_config,
31
- args=[
32
- "--no-sandbox",
33
- "--disable-blink-features=AutomationControlled",
34
- "--disable-dev-shm-usage"
35
- ]
36
- )
 
 
37
 
38
- # Giả lập vân tay trình duyệt thật
39
- context = await browser.new_context(
40
- 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",
41
- viewport={'width': 1920, 'height': 1080}
42
- )
43
-
44
- page = await context.new_page()
45
- # Xóa dấu vết bot
46
- await stealth_async(page)
47
 
48
- try:
49
- # Bước 1: Truy cập trang chủ để lấy "vía" (cookies nền)
 
 
50
  await page.goto("https://aternos.org/go/", wait_until="networkidle", timeout=60000)
51
- await asyncio.sleep(3)
52
 
53
- # Bước 2: Vào thẳng trang server
54
- response = await page.goto("https://aternos.org/server/", wait_until="domcontentloaded", timeout=60000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Đợi thêm chút nếu gặp màn hình chờ của Cloudflare
57
- await asyncio.sleep(5)
58
 
59
- title = await page.title()
60
- content = await page.content()
61
  status = response.status if response else 0
62
 
63
- # Logic kiểm tra chuẩn xác
64
- is_cf_challenge = "just a moment" in title.lower() or "cloudflare" in content.lower()
65
- is_success = status == 200 and not is_cf_challenge
66
 
67
  return {
68
- "ok": is_success,
69
  "status": status,
70
- "title": title,
71
  "proxy_working": status != 407,
72
- "cloudflare_blocked": is_cf_challenge
 
73
  }
74
 
75
  except Exception as e:
 
76
  return {"ok": False, "error": str(e)}
77
  finally:
78
- await browser.close()
 
 
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",
12
+ "password": "mftzj9sljp5t"
13
+ }
14
 
15
  async def start(self):
16
  if not self.playwright:
 
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: 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" 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()