Files
popa_minecraft_launcher_api/main.py
2025-07-16 20:24:26 +05:00

40 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI, HTTPException
from aiomcrcon import Client, RCONConnectionError, IncorrectPasswordError
import asyncio
app = FastAPI()
# Конфигурация RCON (замените на свои данные)
RCON_CONFIG = {
"hub": {"host": "minecraft.hub.popa-popa.ru", "port": 29001, "password": "2006siT_"},
"survival": {"host": "minecraft.survival.popa-popa.ru", "port": 25575, "password": "пароль_survival"},
"pillars": {"host": "minecraft.pillars.popa-popa.ru", "port": 29003, "password": "2006siT_"},
"velocity": {"host": "minecraft.velocity.popa-popa.ru", "port": 25575, "password": "пароль_velocity"}
}
async def send_rcon_command(server_type: str, command: str) -> str:
"""Отправляет RCON-команду на указанный сервер."""
config = RCON_CONFIG.get(server_type)
if not config:
raise HTTPException(status_code=400, detail="Неверный тип сервера")
try:
async with Client(config["host"], config["port"], config["password"]) as client:
response = await client.send_cmd(command)
return response
except RCONConnectionError:
raise HTTPException(status_code=503, detail="Не удалось подключиться к серверу")
except IncorrectPasswordError:
raise HTTPException(status_code=403, detail="Неверный пароль RCON")
@app.get("/rcon/")
async def execute_rcon(server_type: str, command: str):
"""Выполняет RCON-команду на указанном сервере."""
result = await send_rcon_command(server_type, command)
return {"server": server_type, "command": command, "response": result}
@app.get("/players/online/")
async def get_online_players(server_type: str):
"""Возвращает список игроков онлайн на сервере."""
players = await send_rcon_command(server_type, "list")
return {"server": server_type, "online_players": players}