Hana Celeste commited on
Update app/aternos_api.py
Browse files1234565 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import asynciofrom playwright.async_api import async_playwrightfrom playwright_stealth import stealth_asyncclass AternosAPI:def __init__(self):5 unmodified linesasync def stop(self):if self.playwright:await self.playwright.stop()async def check_with_proxy(self, proxy: str):# Cấu hình trình duyệt cực kỳ cẩn thậnbrowser = await self.playwright.chromium.launch(headless=True, # Nếu chạy Docker/Linux thì để True, nhưng tốt nhất là False nếu có thểproxy={"server": proxy},args=["--disable-blink-features=AutomationControlled","--no-sandbox","--disable-dev-shm-usage","--disable-infobars","--window-position=0,0","--ignore-certificate-errors",])# Tạo context với Fingerprint giả lập người dùng thậtcontext = await browser.new_context(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",viewport={'width': 1920, 'height': 1080},device_scale_factor=1,)page = await context.new_page()# Áp dụng Stealth để xóa sạch dấu vết automationawait stealth_async(page)try:# Truy cập trang chủ trước để lấy cookie nền (mẹo nhỏ)await page.goto("https://aternos.org/", wait_until="networkidle", timeout=30000)await asyncio.sleep(2) # Chờ CF xử lý nhẹ# Mới bắt đầu vào trang serverresp = await page.goto("https://aternos.org/server/",wait_until="networkidle",timeout=40000)# Chờ thêm 5 giây nếu thấy vòng xoay của Cloudflareif "just a moment" in (await page.title()).lower():print("Detected Cloudflare Challenge... waiting 10s")await asyncio.sleep(10)title = await page.title()content = await page.content()# Kiểm tra xem có bị lỗi Internal từ phía Proxy khôngstatus = resp.status if resp else 500blocked = "just a moment" in title.lower() or "cloudflare" in content.lower()return {"ok": not blocked and status == 200,"status": status,"title": title,"blocked": blocked}except Exception as e:return {"ok": False, "error": str(e), "blocked": True}finally:await browser.close()1234565 unmodified lines121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import asynciofrom playwright.async_api import async_playwrightfrom playwright_stealth import stealth_asyncclass AternosAPI:def __init__(self):5 unmodified linesasync def stop(self):if self.playwright:await self.playwright.stop()self.playwright = Noneasync def check_with_proxy(self, proxy: str):if not self.playwright:await self.start()browser = Nonetry:# Dùng các args để giảm thiểu việc bị phát hiệnbrowser = await self.playwright.chromium.launch(headless=True,proxy={"server": proxy},args=["--disable-blink-features=AutomationControlled","--no-sandbox","--disable-dev-shm-usage",])context = await browser.new_context(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")page = await context.new_page()# Sử dụng stealth để ẩn danh# Nếu vẫn báo lỗi ImportError, hãy thay dòng dưới bằng:# from playwright_stealth import stealth_async -> import trực tiếp trong hàmawait stealth_async(page)# Thử truy cậpresp = await page.goto("https://aternos.org/server/",wait_until="networkidle", # Chờ cho network ổn địnhtimeout=30000)# Đợi thêm một chút để Cloudflare challenge (nếu có) tự giải quyếtawait asyncio.sleep(5)title = await page.title()content = await page.content()is_blocked = "just a moment" in title.lower() or "cloudflare" in content.lower()return {"ok": not is_blocked,"status": resp.status if resp else None,"title": title,"blocked": is_blocked}except Exception as e:return {"ok": False, "error": str(e), "blocked": True}finally:if browser:await browser.close()async def check_access(self):# Hàm này để gọi từ FastAPIPROXIES = ["http://szusjfnw:mftzj9sljp5t@31.59.20.176:6754",# ... bỏ danh sách proxy của bạn vào đây]for p in PROXIES:res = await self.check_with_proxy(p)if res["ok"]:return resreturn {"ok": False, "message": "All proxies failed"}
app/aternos_api.py+49-44