-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved base class for Packet in mojang.api.net.common
- Loading branch information
Showing
9 changed files
with
124 additions
and
107 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .client import RconClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import struct | ||
import socket | ||
import select | ||
|
||
from ..common.packet import Packet | ||
from .packets.login import LoginRequest, LoginResponse | ||
from .packets.command import CommandRequest, CommandResponse | ||
|
||
class RconClient: | ||
|
||
def __init__(self, host: str, port: int, password: str): | ||
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
self.__host = (host, port) | ||
self.__password = password | ||
|
||
self.__id__ = 0 | ||
|
||
@property | ||
def _new_id(self): | ||
self.__id__ += 1 | ||
return self.__id__ | ||
|
||
def _recv(self, cls: Packet = None): | ||
if select.select([self.__sock], [], [], 1)[0]: | ||
with self.__sock.makefile('rb') as stream: | ||
packet_length = struct.unpack('<i', stream.read(4))[0] | ||
|
||
data = b'' | ||
while len(data) < packet_length: | ||
_data = stream.read(packet_length - len(data)) | ||
if _data: | ||
data += _data | ||
if cls: | ||
return cls.create_from(data) | ||
else: | ||
return data | ||
|
||
def _send(self, packet: Packet): | ||
if select.select([], [self.__sock], [], 1)[1]: | ||
with self.__sock.makefile('wb') as stream: | ||
data = bytes(packet) | ||
stream.write(struct.pack('<i', len(data))) | ||
stream.write(data) | ||
|
||
def connect(self): | ||
self.__sock.connect(self.__host) | ||
self.__sock.setblocking(False) | ||
self._authenticate() | ||
|
||
def _authenticate(self): | ||
packet = LoginRequest(id=self._new_id, payload=self.__password) | ||
self._send(packet) | ||
|
||
r_packet = self._recv(LoginResponse) | ||
|
||
if r_packet.type != 2 or r_packet.id == -1: | ||
raise Exception('Authentication Failed !') | ||
|
||
def run_cmd(self, cmd: str): | ||
packet = CommandRequest(id=self._new_id, payload=cmd) | ||
self._send(packet) | ||
|
||
response = '' | ||
try: | ||
while 1: | ||
r_packet = self._recv(CommandResponse) | ||
if r_packet is None: | ||
break | ||
|
||
if r_packet.type != 0 or r_packet.id != packet.id: | ||
raise Exception('Error while getting the response') | ||
|
||
response += r_packet.payload | ||
finally: | ||
return response | ||
|
||
def close(self): | ||
self.__sock.close() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import struct | ||
|
||
from ...common.packet import Packet | ||
|
||
class CommandRequest(Packet): | ||
type: int = 2 | ||
id: int | ||
payload: str | ||
|
||
def _bytes(self): | ||
encoded_payload = self.payload.encode('ascii') | ||
return struct.pack('<ii{}s2s'.format(len(encoded_payload)), self.id, self.type, encoded_payload, b'\0\0') | ||
|
||
class CommandResponse(Packet): | ||
type: int | ||
id: int | ||
payload: str | ||
|
||
@classmethod | ||
def _parse(cls, buffer: bytes): | ||
request_id, _type, payload = struct.unpack_from('<ii{}s'.format(len(buffer) - 10), buffer) | ||
return dict(type=_type,id=request_id,payload=payload.decode('ascii')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import struct | ||
|
||
from ...common.packet import Packet | ||
|
||
class LoginRequest(Packet): | ||
type: int = 3 | ||
id: int | ||
payload: str | ||
|
||
def _bytes(self): | ||
encoded_payload = self.payload.encode('ascii') | ||
return struct.pack('<ii{}s2s'.format(len(encoded_payload)), self.id, self.type, encoded_payload, b'\0\0') | ||
|
||
class LoginResponse(Packet): | ||
type: int | ||
id: int | ||
|
||
@classmethod | ||
def _parse(cls, buffer: bytes): | ||
request_id, _type, _ = struct.unpack_from('<ii{}s'.format(len(buffer) - 10), buffer) | ||
return dict(type=_type,id=request_id) |