Create app/panel_login.py
Browse files- app/panel_login.py +78 -0
app/panel_login.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from playwright.async_api import async_playwright
|
| 5 |
+
from playwright_stealth import stealth_async
|
| 6 |
+
|
| 7 |
+
class PanelLogin:
|
| 8 |
+
def __init__(self, username, password, server_id):
|
| 9 |
+
self.username = username
|
| 10 |
+
self.password = password
|
| 11 |
+
self.server_id = server_id
|
| 12 |
+
self.login_url = "https://cp.pikamc.vn/auth/login"
|
| 13 |
+
self.api_ws_url = f"https://cp.pikamc.vn/api/client/servers/{server_id}/websocket"
|
| 14 |
+
self.debug_path = "debug_screenshot.png"
|
| 15 |
+
|
| 16 |
+
async def fetch_ws_credentials(self):
|
| 17 |
+
async with async_playwright() as p:
|
| 18 |
+
# Khởi chạy trình duyệt (phù hợp với Docker Jammy)
|
| 19 |
+
browser = await p.chromium.launch(headless=True, args=["--no-sandbox"])
|
| 20 |
+
context = await browser.new_context(
|
| 21 |
+
viewport={'width': 1280, 'height': 720},
|
| 22 |
+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
page = await context.new_page()
|
| 26 |
+
await stealth_async(page)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Bước 1: Đăng nhập
|
| 30 |
+
print(f"[*] Đang truy cập: {self.login_url}")
|
| 31 |
+
await page.goto(self.login_url, wait_until="networkidle")
|
| 32 |
+
|
| 33 |
+
await page.fill('input[name="username"]', self.username)
|
| 34 |
+
await page.fill('input[name="password"]', self.password)
|
| 35 |
+
|
| 36 |
+
# Click Đăng nhập
|
| 37 |
+
await page.click('button[type="submit"]')
|
| 38 |
+
|
| 39 |
+
# Đợi chuyển hướng (Thường là về trang chủ / hoặc /server/...)
|
| 40 |
+
await page.wait_for_load_state("networkidle")
|
| 41 |
+
|
| 42 |
+
# Chụp ảnh debug sau khi login
|
| 43 |
+
await page.screenshot(path=self.debug_path)
|
| 44 |
+
print(f"[+] Đã chụp ảnh debug tại: {self.debug_path}")
|
| 45 |
+
|
| 46 |
+
# Bước 2: Truy cập thẳng vào API lấy Websocket Token
|
| 47 |
+
# Playwright sẽ sử dụng Cookie đã lưu sau khi login thành công
|
| 48 |
+
print(f"[*] Đang lấy Token từ: {self.api_ws_url}")
|
| 49 |
+
await page.goto(self.api_ws_url)
|
| 50 |
+
|
| 51 |
+
# Lấy nội dung JSON trả về từ trang API
|
| 52 |
+
content = await page.inner_text('pre') if await page.query_selector('pre') else await page.content()
|
| 53 |
+
|
| 54 |
+
# Nếu nội dung nằm trong body (đối với một số trình duyệt)
|
| 55 |
+
if not content.startswith('{'):
|
| 56 |
+
content = await page.locator('body').inner_text()
|
| 57 |
+
|
| 58 |
+
data = json.loads(content)
|
| 59 |
+
|
| 60 |
+
await browser.close()
|
| 61 |
+
|
| 62 |
+
if "data" in data:
|
| 63 |
+
return {
|
| 64 |
+
"ok": True,
|
| 65 |
+
"token": data["data"]["token"],
|
| 66 |
+
"socket": data["data"]["socket"]
|
| 67 |
+
}
|
| 68 |
+
else:
|
| 69 |
+
return {"ok": False, "error": "Không tìm thấy data trong JSON", "raw": content}
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
await page.screenshot(path="error_debug.png")
|
| 73 |
+
await browser.close()
|
| 74 |
+
return {"ok": False, "error": str(e)}
|
| 75 |
+
|
| 76 |
+
# Cách sử dụng nhanh để test:
|
| 77 |
+
# login_svc = PanelLogin("user", "pass", "45feca4d-3f79-4bcc-ae11-114335a1c77e")
|
| 78 |
+
# result = asyncio.run(login_svc.fetch_ws_credentials())
|