Browse Source

custom rcon 4 ignore encode errors

master
gsd 2 years ago
parent
commit
7ee8ca8d50
  1. 47
      ext/python-a2s-rcon-api/custom_rcon.py
  2. 3
      ext/python-a2s-rcon-api/service.py

47
ext/python-a2s-rcon-api/custom_rcon.py

@ -0,0 +1,47 @@
from asyncio import StreamReader, StreamWriter, open_connection
from rcon.exceptions import SessionTimeout, WrongPassword
from rcon.source.proto import Packet, Type
from rcon.source.async_rcon import close, communicate
# https://github.com/conqp/rcon/blob/929f358f1a7d155141704866b736c6f9666b4bd1/rcon/source/async_rcon.py#L45
async def rcon(
command: str,
*arguments: str,
host: str,
port: int,
passwd: str,
encoding: str = 'utf-8',
frag_threshold: int = 4096,
frag_detect_cmd: str = ''
) -> str:
"""Run a command asynchronously."""
reader, writer = await open_connection(host, port)
response = await communicate(
reader,
writer,
Packet.make_login(passwd, encoding=encoding),
frag_threshold=frag_threshold,
frag_detect_cmd=frag_detect_cmd
)
# Wait for SERVERDATA_AUTH_RESPONSE according to:
# https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
while response.type != Type.SERVERDATA_AUTH_RESPONSE:
response = await Packet.aread(reader)
if response.id == -1:
await close(writer)
raise WrongPassword()
request = Packet.make_command(command, *arguments, encoding=encoding)
response = await communicate(reader, writer, request)
await close(writer)
if response.id != request.id:
raise SessionTimeout()
try:
return response.payload.decode(encoding)
except UnicodeDecodeError:
return response.payload.decode(encoding, errors="ignore")

3
ext/python-a2s-rcon-api/service.py

@ -3,7 +3,8 @@ from fastapi.responses import JSONResponse, Response
import os, argparse
from a2s import ainfo as VALVE_SERVER_INFO
from a2s import aplayers as VALVE_SERVER_PLAYERS
from rcon.source import rcon as VALVE_SERVER_RCON
#from rcon.source import rcon as VALVE_SERVER_RCON
from custom_rcon import rcon as VALVE_SERVER_RCON
from time import time
from pydantic import BaseModel
from RCONPlayerModel import *

Loading…
Cancel
Save