Hana Celeste commited on
Update app/aternos_api.py
Browse files- app/aternos_api.py +50 -42
app/aternos_api.py
CHANGED
|
@@ -20,14 +20,16 @@ class AternosAPI:
|
|
| 20 |
await self.playwright.stop()
|
| 21 |
self.playwright = None
|
| 22 |
|
| 23 |
-
|
| 24 |
-
if not self.playwright:
|
|
|
|
| 25 |
|
| 26 |
browser = await self.playwright.chromium.launch(
|
| 27 |
headless=False,
|
| 28 |
proxy=self.proxy_config,
|
| 29 |
args=["--no-sandbox", "--disable-blink-features=AutomationControlled"]
|
| 30 |
)
|
|
|
|
| 31 |
context = await browser.new_context(
|
| 32 |
viewport={'width': 1366, 'height': 768},
|
| 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"
|
|
@@ -35,39 +37,43 @@ class AternosAPI:
|
|
| 35 |
page = await context.new_page()
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
# 1. Vượt Cloudflare
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
await page.mouse.move(random.randint(100, 600), random.randint(100, 600))
|
| 43 |
await asyncio.sleep(2)
|
| 44 |
|
| 45 |
-
# 2.
|
| 46 |
-
print("[INFO]
|
| 47 |
user_input = await page.wait_for_selector("input.username", state="visible", timeout=20000)
|
| 48 |
-
|
| 49 |
-
# Xóa sạch nếu có chữ cũ và điền mới
|
| 50 |
-
await user_input.click(click_count=3)
|
| 51 |
await page.keyboard.press("Backspace")
|
| 52 |
-
await page.type("input.username",
|
| 53 |
|
| 54 |
-
await page.type("input.password",
|
| 55 |
|
| 56 |
-
# 3.
|
| 57 |
-
print("[INFO]
|
| 58 |
-
login_btn = await page.wait_for_selector("button.login-button"
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
await asyncio.sleep(2) # Đợi trang mới ổn định
|
| 68 |
final_url = page.url
|
| 69 |
|
| 70 |
-
#
|
| 71 |
if "/servers/" in final_url:
|
| 72 |
await page.click(".server-body")
|
| 73 |
await page.wait_for_url("**/server/**", timeout=10000)
|
|
@@ -75,29 +81,31 @@ class AternosAPI:
|
|
| 75 |
|
| 76 |
is_success = "/server" in final_url and "go" not in final_url
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
msg = "
|
| 80 |
-
if is_success and action_type
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
return {
|
| 93 |
"ok": is_success,
|
|
|
|
| 94 |
"url": final_url,
|
| 95 |
-
"title": await page.title()
|
| 96 |
-
"msg": msg if is_success else "Vẫn kẹt ở trang login (Sai pass hoặc lỗi click)"
|
| 97 |
}
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
return {"ok": False, "error": str(e)
|
| 101 |
finally:
|
| 102 |
-
await browser.close()
|
| 103 |
-
|
|
|
|
| 20 |
await self.playwright.stop()
|
| 21 |
self.playwright = None
|
| 22 |
|
| 23 |
+
async def run_action(self, user, pwd, action_type="login"):
|
| 24 |
+
if not self.playwright:
|
| 25 |
+
await self.start()
|
| 26 |
|
| 27 |
browser = await self.playwright.chromium.launch(
|
| 28 |
headless=False,
|
| 29 |
proxy=self.proxy_config,
|
| 30 |
args=["--no-sandbox", "--disable-blink-features=AutomationControlled"]
|
| 31 |
)
|
| 32 |
+
|
| 33 |
context = await browser.new_context(
|
| 34 |
viewport={'width': 1366, 'height': 768},
|
| 35 |
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"
|
|
|
|
| 37 |
page = await context.new_page()
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# 1. Vượt Cloudflare
|
| 41 |
+
print("[INFO] Truy cập Aternos...")
|
| 42 |
+
await page.goto("https://aternos.org/go", wait_until="domcontentloaded", timeout=60000)
|
| 43 |
+
|
| 44 |
+
for i in range(15):
|
| 45 |
+
title = await page.title()
|
| 46 |
+
if "just a moment" not in title.lower():
|
| 47 |
+
break
|
| 48 |
await page.mouse.move(random.randint(100, 600), random.randint(100, 600))
|
| 49 |
await asyncio.sleep(2)
|
| 50 |
|
| 51 |
+
# 2. Nhập liệu
|
| 52 |
+
print("[INFO] Đang điền form...")
|
| 53 |
user_input = await page.wait_for_selector("input.username", state="visible", timeout=20000)
|
| 54 |
+
await user_input.click(click_count=3)
|
|
|
|
|
|
|
| 55 |
await page.keyboard.press("Backspace")
|
| 56 |
+
await page.type("input.username", user, delay=100)
|
| 57 |
|
| 58 |
+
await page.type("input.password", pwd, delay=100)
|
| 59 |
|
| 60 |
+
# 3. Click Đăng nhập & Đợi chuyển hướng
|
| 61 |
+
print("[INFO] Click Login...")
|
| 62 |
+
login_btn = await page.wait_for_selector("button.login-button")
|
| 63 |
|
| 64 |
+
# Kết hợp vừa bấm vừa đợi URL thay đổi
|
| 65 |
+
try:
|
| 66 |
+
await asyncio.gather(
|
| 67 |
+
page.wait_for_url("**/server*", timeout=20000),
|
| 68 |
+
login_btn.click(force=True)
|
| 69 |
+
)
|
| 70 |
+
except:
|
| 71 |
+
print("[WARN] Timeout hoặc kẹt chuyển hướng")
|
| 72 |
|
| 73 |
+
await asyncio.sleep(3)
|
|
|
|
| 74 |
final_url = page.url
|
| 75 |
|
| 76 |
+
# Xử lý nếu có danh sách server
|
| 77 |
if "/servers/" in final_url:
|
| 78 |
await page.click(".server-body")
|
| 79 |
await page.wait_for_url("**/server/**", timeout=10000)
|
|
|
|
| 81 |
|
| 82 |
is_success = "/server" in final_url and "go" not in final_url
|
| 83 |
|
| 84 |
+
# 4. Thực hiện Action Start/Stop
|
| 85 |
+
msg = "Login thành công" if is_success else "Thất bại"
|
| 86 |
+
if is_success and action_type in ["start", "stop"]:
|
| 87 |
+
target_btn = f"#{action_type}"
|
| 88 |
+
btn = await page.query_selector(target_btn)
|
| 89 |
+
if btn and await btn.is_visible():
|
| 90 |
+
await btn.click()
|
| 91 |
+
if action_type == "start":
|
| 92 |
+
try:
|
| 93 |
+
confirm = await page.wait_for_selector(".btn-success", timeout=5000)
|
| 94 |
+
await confirm.click()
|
| 95 |
+
msg = "Server đang khởi động (EULA accepted)"
|
| 96 |
+
except:
|
| 97 |
+
msg = "Server đang khởi động"
|
| 98 |
+
else:
|
| 99 |
+
msg = "Server đã bấm dừng"
|
| 100 |
|
| 101 |
return {
|
| 102 |
"ok": is_success,
|
| 103 |
+
"msg": msg,
|
| 104 |
"url": final_url,
|
| 105 |
+
"title": await page.title()
|
|
|
|
| 106 |
}
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
+
return {"ok": False, "error": str(e)}
|
| 110 |
finally:
|
| 111 |
+
await browser.close()
|
|
|