Hana Celeste commited on
Update app/aternos_api.py
Browse files- 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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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 =
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# Xóa dấu vết bot
|
| 46 |
-
await stealth_async(page)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
await page.goto("https://aternos.org/go/", wait_until="networkidle", timeout=60000)
|
| 51 |
-
await asyncio.sleep(3)
|
| 52 |
|
| 53 |
-
# Bước 2:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Đợi thêm
|
| 57 |
-
await asyncio.sleep(
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
status = response.status if response else 0
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
return {
|
| 68 |
-
"ok":
|
| 69 |
"status": status,
|
| 70 |
-
"title":
|
| 71 |
"proxy_working": status != 407,
|
| 72 |
-
"cloudflare_blocked":
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
except Exception as e:
|
|
|
|
| 76 |
return {"ok": False, "error": str(e)}
|
| 77 |
finally:
|
| 78 |
-
|
|
|
|
|
|
| 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: 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()
|