init
This commit is contained in:
40
main.py
Normal file
40
main.py
Normal file
@ -0,0 +1,40 @@
|
||||
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}
|
Reference in New Issue
Block a user