Hana Celeste commited on
Commit
ec99db2
·
verified ·
1 Parent(s): 9d4a8b5

Update app/aternos_api.py

Browse files
Files changed (1) hide show
  1. app/aternos_api.py +49 -44
app/aternos_api.py CHANGED
@@ -1,6 +1,6 @@
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):
@@ -12,67 +12,72 @@ class AternosAPI:
12
  async def stop(self):
13
  if self.playwright:
14
  await self.playwright.stop()
 
15
 
16
  async def check_with_proxy(self, proxy: str):
17
- # Cấu hình trình duyệt cực kỳ cẩn thận
18
- browser = await self.playwright.chromium.launch(
19
- headless=True, # Nếu chạy Docker/Linux thì để True, nhưng tốt nhất là False nếu có thể
20
- proxy={"server": proxy},
21
- args=[
22
- "--disable-blink-features=AutomationControlled",
23
- "--no-sandbox",
24
- "--disable-dev-shm-usage",
25
- "--disable-infobars",
26
- "--window-position=0,0",
27
- "--ignore-certificate-errors",
28
- ]
29
- )
30
-
31
- # Tạo context với Fingerprint giả lập người dùng thật
32
- context = await browser.new_context(
33
- 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",
34
- viewport={'width': 1920, 'height': 1080},
35
- device_scale_factor=1,
36
- )
37
-
38
- page = await context.new_page()
39
-
40
- # Áp dụng Stealth để xóa sạch dấu vết automation
41
- await stealth_async(page)
42
 
 
43
  try:
44
- # Truy cập trang chủ trước để lấy cookie nền (mẹo nhỏ)
45
- await page.goto("https://aternos.org/", wait_until="networkidle", timeout=30000)
46
- await asyncio.sleep(2) # Chờ CF xử lý nhẹ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # Mới bắt đầu vào trang server
49
  resp = await page.goto(
50
  "https://aternos.org/server/",
51
- wait_until="networkidle",
52
- timeout=40000
53
  )
54
 
55
- # Chờ thêm 5 giây nếu thấy vòng xoay của Cloudflare
56
- if "just a moment" in (await page.title()).lower():
57
- print("Detected Cloudflare Challenge... waiting 10s")
58
- await asyncio.sleep(10)
59
 
60
  title = await page.title()
61
  content = await page.content()
62
 
63
- # Kiểm tra xem có bị lỗi Internal từ phía Proxy không
64
- status = resp.status if resp else 500
65
-
66
- blocked = "just a moment" in title.lower() or "cloudflare" in content.lower()
67
 
68
  return {
69
- "ok": not blocked and status == 200,
70
- "status": status,
71
  "title": title,
72
- "blocked": blocked
73
  }
74
 
75
  except Exception as e:
76
  return {"ok": False, "error": str(e), "blocked": True}
77
  finally:
78
- await browser.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
 
12
  async def stop(self):
13
  if self.playwright:
14
  await self.playwright.stop()
15
+ self.playwright = None
16
 
17
  async def check_with_proxy(self, proxy: str):
18
+ if not self.playwright:
19
+ await self.start()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ browser = None
22
  try:
23
+ # Dùng các args để giảm thiểu việc bị phát hiện
24
+ browser = await self.playwright.chromium.launch(
25
+ headless=True,
26
+ proxy={"server": proxy},
27
+ args=[
28
+ "--disable-blink-features=AutomationControlled",
29
+ "--no-sandbox",
30
+ "--disable-dev-shm-usage",
31
+ ]
32
+ )
33
+
34
+ context = await browser.new_context(
35
+ user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
36
+ )
37
+
38
+ page = await context.new_page()
39
+
40
+ # Sử dụng stealth để ẩn danh
41
+ # Nếu vẫn báo lỗi ImportError, hãy thay dòng dưới bằng:
42
+ # from playwright_stealth import stealth_async -> import trực tiếp trong hàm
43
+ await stealth_async(page)
44
 
45
+ # Thử truy cập
46
  resp = await page.goto(
47
  "https://aternos.org/server/",
48
+ wait_until="networkidle", # Chờ cho network ổn định
49
+ timeout=30000
50
  )
51
 
52
+ # Đợi thêm một chút để Cloudflare challenge (nếu có) tự giải quyết
53
+ await asyncio.sleep(5)
 
 
54
 
55
  title = await page.title()
56
  content = await page.content()
57
 
58
+ is_blocked = "just a moment" in title.lower() or "cloudflare" in content.lower()
 
 
 
59
 
60
  return {
61
+ "ok": not is_blocked,
62
+ "status": resp.status if resp else None,
63
  "title": title,
64
+ "blocked": is_blocked
65
  }
66
 
67
  except Exception as e:
68
  return {"ok": False, "error": str(e), "blocked": True}
69
  finally:
70
+ if browser:
71
+ await browser.close()
72
+
73
+ async def check_access(self):
74
+ # Hàm này để gọi từ FastAPI
75
+ PROXIES = [
76
+ "http://szusjfnw:mftzj9sljp5t@31.59.20.176:6754",
77
+ # ... bỏ danh sách proxy của bạn vào đây
78
+ ]
79
+ for p in PROXIES:
80
+ res = await self.check_with_proxy(p)
81
+ if res["ok"]:
82
+ return res
83
+ return {"ok": False, "message": "All proxies failed"}