Hana Celeste commited on
Commit
2e58ab2
·
verified ·
1 Parent(s): 46ed182

Update app/aternos_api.py

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