| 1 |
"""BattlEye RCon client.""" |
| 2 |
|
| 3 |
from logging import getLogger |
| 4 |
from socket import SOCK_DGRAM |
| 5 |
from typing import Callable |
| 6 |
|
| 7 |
from rcon.battleye.proto import HEADER_SIZE |
| 8 |
from rcon.battleye.proto import RESPONSE_TYPES |
| 9 |
from rcon.battleye.proto import CommandRequest |
| 10 |
from rcon.battleye.proto import CommandResponse |
| 11 |
from rcon.battleye.proto import Header |
| 12 |
from rcon.battleye.proto import LoginRequest |
| 13 |
from rcon.battleye.proto import LoginResponse |
| 14 |
from rcon.battleye.proto import Request |
| 15 |
from rcon.battleye.proto import Response |
| 16 |
from rcon.battleye.proto import ServerMessage |
| 17 |
from rcon.battleye.proto import ServerMessageAck |
| 18 |
from rcon.client import BaseClient |
| 19 |
from rcon.exceptions import WrongPassword |
| 20 |
|
| 21 |
|
| 22 |
__all__ = ["Client"] |
| 23 |
|
| 24 |
|
| 25 |
MessageHandler = Callable[[ServerMessage], None] |
| 26 |
|
| 27 |
|
| 28 |
def log_message(server_message: ServerMessage) -> None: |
| 29 |
"""Default handler, logging the server message.""" |
| 30 |
|
| 31 |
getLogger("Server message").info(server_message.message) |
| 32 |
|
| 33 |
|
| 34 |
class Client(BaseClient, socket_type=SOCK_DGRAM): |
| 35 |
"""BattlEye RCon client.""" |
| 36 |
|
| 37 |
def __init__( |
| 38 |
self, |
| 39 |
*args, |
| 40 |
max_length: int = 4096, |
| 41 |
message_handler: MessageHandler = log_message, |
| 42 |
**kwargs, |
| 43 |
): |
| 44 |
super().__init__(*args, **kwargs) |
| 45 |
self.max_length = max_length |
| 46 |
self.message_handler = message_handler |
| 47 |
|
| 48 |
def handle_server_message(self, message: ServerMessage) -> None: |
| 49 |
"""Handle the respective server message.""" |
| 50 |
with self._socket.makefile("wb") as file: |
| 51 |
file.write(bytes(ServerMessageAck(message.seq))) |
| 52 |
|
| 53 |
self.message_handler(message) |
| 54 |
|
| 55 |
def receive(self) -> Response: |
| 56 |
"""Receive a packet.""" |
| 57 |
return RESPONSE_TYPES[ |
| 58 |
( |
| 59 |
header := Header.from_bytes( |
| 60 |
(data := self._socket.recv(self.max_length))[:HEADER_SIZE] |
| 61 |
) |
| 62 |
).type |
| 63 |
].from_bytes(header, data[HEADER_SIZE:]) |
| 64 |
|
| 65 |
def receive_transaction(self): |
| 66 |
command_responses = [] |
| 67 |
login_response = None |
| 68 |
seq = 0 |
| 69 |
|
| 70 |
while True: |
| 71 |
response = self.receive() |
| 72 |
|
| 73 |
if isinstance(response, LoginResponse) and login_response is None: |
| 74 |
login_response = response |
| 75 |
continue |
| 76 |
|
| 77 |
if isinstance(response, CommandResponse): |
| 78 |
command_responses.append(response) |
| 79 |
seq = response.seq |
| 80 |
continue |
| 81 |
|
| 82 |
if isinstance(response, ServerMessage): |
| 83 |
self.handle_server_message(response) |
| 84 |
|
| 85 |
if login_response is not None: |
| 86 |
return login_response |
| 87 |
|
| 88 |
if len(command_responses) >= seq: |
| 89 |
break |
| 90 |
|
| 91 |
return "".join( |
| 92 |
command_response.message |
| 93 |
for command_response in sorted(command_responses, key=lambda cr: cr.seq) |
| 94 |
) |
| 95 |
|
| 96 |
def communicate(self, request: Request) -> Response | str: |
| 97 |
"""Send a request and receive a response.""" |
| 98 |
with self._socket.makefile("wb") as file: |
| 99 |
file.write(bytes(request)) |
| 100 |
|
| 101 |
return self.receive_transaction() |
| 102 |
|
| 103 |
def login(self, passwd: str) -> bool: |
| 104 |
"""Log-in the user.""" |
| 105 |
if not self.communicate(LoginRequest(passwd)).success: |
| 106 |
raise WrongPassword() |
| 107 |
|
| 108 |
return True |
| 109 |
|
| 110 |
def run(self, command: str, *args: str) -> str: |
| 111 |
"""Execute a command and return the text message.""" |
| 112 |
return self.communicate(CommandRequest.from_command(command, *args)) |