67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
from fastapi import FastAPI, Depends
|
|
from pydantic import BaseModel
|
|
import json
|
|
from rcon.battleye import Client
|
|
|
|
app = FastAPI()
|
|
|
|
# Load credentials
|
|
credentials_file = 'credentials.json'
|
|
with open(credentials_file, 'r') as file:
|
|
credentials = json.load(file)
|
|
|
|
SERVER_ADDRESS = credentials.get('SERVER_ADDRESS', '')
|
|
SERVER_PORT = credentials.get('SERVER_PORT', 0)
|
|
RCON_PASSWORD = credentials.get('RCON_PASSWORD', '')
|
|
|
|
class RCONManager:
|
|
def __init__(self, address, port, password):
|
|
self.client = Client(address, port, passwd=password)
|
|
self.client.connect()
|
|
|
|
def run(self, command):
|
|
return self.client.run(command)
|
|
|
|
def close(self):
|
|
self.client.disconnect()
|
|
|
|
rcon_manager = RCONManager(SERVER_ADDRESS, SERVER_PORT, RCON_PASSWORD)
|
|
|
|
@app.on_event("shutdown")
|
|
def shutdown_event():
|
|
rcon_manager.close()
|
|
|
|
class PlayerAction(BaseModel):
|
|
steamid: str
|
|
reason: str
|
|
duration: int
|
|
|
|
def get_rcon_manager():
|
|
return rcon_manager
|
|
|
|
@app.get("/players")
|
|
def list_players(rcon: RCONManager = Depends(get_rcon_manager)):
|
|
response = rcon.run('players')
|
|
return {"response": response}
|
|
|
|
# Ähnliche Änderungen für die anderen Endpunkte anwenden
|
|
@app.post("/kick")
|
|
def kick_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)):
|
|
response = rcon.run(f'kick {action.steamid} {action.reason}')
|
|
return {"response": response}
|
|
|
|
@app.post("/ban")
|
|
def ban_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)):
|
|
response = rcon.run(f'ban {action.steamid} {action.duration} "{action.reason}"')
|
|
return {"response": response}
|
|
|
|
@app.post("/unlock")
|
|
def unlock_server(rcon: RCONManager = Depends(get_rcon_manager)):
|
|
response = rcon.run('#unlock')
|
|
return {"response": response}
|
|
|
|
@app.post("/lock")
|
|
def lock_server(rcon: RCONManager = Depends(get_rcon_manager)):
|
|
response = rcon.run('#lock')
|
|
return {"response": response}
|