Files
DayZ-rcon-python/main.py
2024-06-11 15:53:57 +02:00

52 lines
1.6 KiB
Python

from fastapi import FastAPI
from pydantic import BaseModel
from rcon.battleye import Client
import json
import os
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 PlayerAction(BaseModel):
steamid: str
reason: str
duration: int
@app.get("/players")
def list_players():
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client:
response = client.run('players')
return {"response": response}
@app.post("/kick")
def kick_player(action: PlayerAction):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client:
response = client.run(f'kick {action.steamid} {action.reason}')
return {"response": response}
@app.post("/ban")
def ban_player(action: PlayerAction):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client:
response = client.run(f'ban {action.steamid} {action.duration} "{action.reason}"')
return {"response": response}
@app.post("/unlock")
def unlock_server():
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client:
response = client.run('#unlock')
return {"response": response}
@app.post("/lock")
def lock_server():
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client:
response = client.run('#lock')
return {"response": response}