Keep connection open instead of reconnecting every time

This commit is contained in:
2024-06-11 16:12:39 +02:00
parent d7c3cc3cd9
commit 7bada37794
2 changed files with 39 additions and 24 deletions

Binary file not shown.

51
main.py
View File

@@ -1,8 +1,7 @@
from fastapi import FastAPI from fastapi import FastAPI, Depends
from pydantic import BaseModel from pydantic import BaseModel
from rcon.battleye import Client
import json import json
import os from rcon.battleye import Client
app = FastAPI() app = FastAPI()
@@ -15,37 +14,53 @@ SERVER_ADDRESS = credentials.get('SERVER_ADDRESS', '')
SERVER_PORT = credentials.get('SERVER_PORT', 0) SERVER_PORT = credentials.get('SERVER_PORT', 0)
RCON_PASSWORD = credentials.get('RCON_PASSWORD', '') 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): class PlayerAction(BaseModel):
steamid: str steamid: str
reason: str reason: str
duration: int duration: int
def get_rcon_manager():
return rcon_manager
@app.get("/players") @app.get("/players")
def list_players(): def list_players(rcon: RCONManager = Depends(get_rcon_manager)):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: response = rcon.run('players')
response = client.run('players')
return {"response": response} return {"response": response}
# Ähnliche Änderungen für die anderen Endpunkte anwenden
@app.post("/kick") @app.post("/kick")
def kick_player(action: PlayerAction): def kick_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: response = rcon.run(f'kick {action.steamid} {action.reason}')
response = client.run(f'kick {action.steamid} {action.reason}')
return {"response": response} return {"response": response}
@app.post("/ban") @app.post("/ban")
def ban_player(action: PlayerAction): def ban_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: response = rcon.run(f'ban {action.steamid} {action.duration} "{action.reason}"')
response = client.run(f'ban {action.steamid} {action.duration} "{action.reason}"')
return {"response": response} return {"response": response}
@app.post("/unlock") @app.post("/unlock")
def unlock_server(): def unlock_server(rcon: RCONManager = Depends(get_rcon_manager)):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: response = rcon.run('#unlock')
response = client.run('#unlock')
return {"response": response} return {"response": response}
@app.post("/lock") @app.post("/lock")
def lock_server(): def lock_server(rcon: RCONManager = Depends(get_rcon_manager)):
with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: response = rcon.run('#lock')
response = client.run('#lock')
return {"response": response} return {"response": response}