Hana Celeste commited on
Rename app/aternos_api.py to app/byps_logic.py
Browse files- app/aternos_api.py +0 -111
- app/byps_logic.py +74 -0
app/aternos_api.py
DELETED
|
@@ -1,111 +0,0 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import random
|
| 3 |
-
from playwright.async_api import async_playwright
|
| 4 |
-
|
| 5 |
-
class AternosAPI:
|
| 6 |
-
def __init__(self):
|
| 7 |
-
self.playwright = None
|
| 8 |
-
self.proxy_config = {
|
| 9 |
-
"server": "http://p.webshare.io:80",
|
| 10 |
-
"username": "szusjfnw-rotate",
|
| 11 |
-
"password": "mftzj9sljp5t"
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
async def start(self):
|
| 15 |
-
if not self.playwright:
|
| 16 |
-
self.playwright = await async_playwright().start()
|
| 17 |
-
|
| 18 |
-
async def stop(self):
|
| 19 |
-
if self.playwright:
|
| 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"
|
| 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)
|
| 80 |
-
final_url = page.url
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/byps_logic.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import random
|
| 3 |
+
from playwright.async_api import async_playwright
|
| 4 |
+
|
| 5 |
+
class BypsApp:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.playwright = None
|
| 8 |
+
self.proxy_config = {
|
| 9 |
+
"server": "http://p.webshare.io:80",
|
| 10 |
+
"username": "szusjfnw-rotate",
|
| 11 |
+
"password": "mftzj9sljp5t"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
async def start(self):
|
| 15 |
+
if not self.playwright:
|
| 16 |
+
self.playwright = await async_playwright().start()
|
| 17 |
+
|
| 18 |
+
async def stop(self):
|
| 19 |
+
if self.playwright:
|
| 20 |
+
await self.playwright.stop()
|
| 21 |
+
self.playwright = None
|
| 22 |
+
|
| 23 |
+
async def fetch(self, url: str):
|
| 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=[
|
| 31 |
+
"--no-sandbox",
|
| 32 |
+
"--disable-blink-features=AutomationControlled",
|
| 33 |
+
"--disable-dev-shm-usage"
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
context = await browser.new_context(
|
| 38 |
+
viewport={'width': 1280, 'height': 720},
|
| 39 |
+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
page = await context.new_page()
|
| 43 |
+
|
| 44 |
+
await page.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
print(f"[BYPS] Accessing: {url}")
|
| 48 |
+
await page.goto(url, wait_until="commit", timeout=60000)
|
| 49 |
+
|
| 50 |
+
for i in range(10):
|
| 51 |
+
content = await page.content()
|
| 52 |
+
title = await page.title()
|
| 53 |
+
|
| 54 |
+
if "just a moment" not in title.lower() and "cloudflare" not in content.lower():
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
print(f"[BYPS] Chờ Cloudflare... {i+1}s")
|
| 58 |
+
await page.mouse.move(random.randint(0, 500), random.randint(0, 500))
|
| 59 |
+
await asyncio.sleep(2)
|
| 60 |
+
|
| 61 |
+
await asyncio.sleep(2)
|
| 62 |
+
|
| 63 |
+
final_content = await page.content()
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
json_content = await page.evaluate("() => JSON.parse(document.querySelector('body').innerText)")
|
| 67 |
+
return {"ok": True, "type": "json", "data": json_content}
|
| 68 |
+
except:
|
| 69 |
+
return {"ok": True, "type": "html", "data": final_content}
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return {"ok": False, "error": str(e)}
|
| 73 |
+
finally:
|
| 74 |
+
await browser.close()
|