2018-09-24 11:37:21 +02:00
|
|
|
from importlib import reload
|
|
|
|
|
|
|
|
import socket
|
2019-12-14 20:08:34 +01:00
|
|
|
import logging
|
|
|
|
import struct
|
|
|
|
|
|
|
|
from network.compression import uncompress
|
|
|
|
|
|
|
|
from handlers import serverQueryHandler, mainHandler
|
|
|
|
|
|
|
|
from helpers.byteFormater import readable_bytes
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-09-24 11:37:21 +02:00
|
|
|
|
|
|
|
class Client:
|
|
|
|
BUFF_SIZE = 65535
|
|
|
|
HANDLERS = {
|
2019-12-14 20:08:34 +01:00
|
|
|
b"SAMP": serverQueryHandler.handle
|
2018-09-24 11:37:21 +02:00
|
|
|
}
|
|
|
|
|
2019-12-14 20:08:34 +01:00
|
|
|
def __init__(self, server: "Server", addr: "Address"):
|
2018-09-24 11:37:21 +02:00
|
|
|
self.server = server
|
2019-12-14 20:08:34 +01:00
|
|
|
|
|
|
|
self.ip_uint = struct.unpack(b"<I", bytes(int(x) for x in addr[0].split(".")))[0]
|
|
|
|
|
2018-09-24 11:37:21 +02:00
|
|
|
self.addr = addr
|
|
|
|
self.state = 0
|
2019-12-14 20:08:34 +01:00
|
|
|
self.__writebuffer = bytearray()
|
2018-09-24 11:37:21 +02:00
|
|
|
|
2019-12-14 20:08:34 +01:00
|
|
|
def handle_data(self, data: bytes):
|
2018-09-24 11:37:21 +02:00
|
|
|
found = False
|
2019-12-14 20:08:34 +01:00
|
|
|
for pattern, handle in self.HANDLERS.items(): # SAMP query
|
2018-10-02 00:38:25 +02:00
|
|
|
if data.startswith(pattern):
|
2019-12-14 20:08:34 +01:00
|
|
|
queue = handle(self, data)
|
2018-09-24 11:37:21 +02:00
|
|
|
if queue:
|
|
|
|
self.write_to_buffer(queue)
|
|
|
|
found = True
|
|
|
|
|
|
|
|
if not found:
|
2019-12-14 20:08:34 +01:00
|
|
|
# deobfuscate
|
|
|
|
try:
|
|
|
|
data: bytearray = uncompress(data)
|
|
|
|
logger.debug("[%s] -> %s" % (self.addr, readable_bytes(data)))
|
|
|
|
queue = mainHandler.handle(self, data)
|
|
|
|
except Exception as err:
|
|
|
|
logger.error(err)
|
|
|
|
finally:
|
|
|
|
if queue:
|
2018-09-24 11:37:21 +02:00
|
|
|
self.write_to_buffer(queue)
|
|
|
|
|
2019-12-14 20:08:34 +01:00
|
|
|
def write_to_buffer(self, data: bytes):
|
|
|
|
if type(data) == int:
|
2018-09-24 11:37:21 +02:00
|
|
|
self.__writebuffer.append(data)
|
2019-12-14 20:08:34 +01:00
|
|
|
elif type(data) == list:
|
|
|
|
for item in data:
|
|
|
|
self.write_to_buffer(item)
|
2018-09-24 11:37:21 +02:00
|
|
|
else:
|
|
|
|
self.__writebuffer += data
|
|
|
|
|
2019-12-14 20:08:34 +01:00
|
|
|
def get_buffer_size(self) -> int:
|
2018-09-24 11:37:21 +02:00
|
|
|
return len(self.__writebuffer)
|
2019-12-14 20:08:34 +01:00
|
|
|
|
2018-09-24 11:37:21 +02:00
|
|
|
def write_socket(self):
|
2019-12-14 20:08:34 +01:00
|
|
|
if self.get_buffer_size() > 0:
|
|
|
|
#write_chunk = self.__writebuffer.pop(0)
|
|
|
|
#logger.debug("[%s] <- %s" % (self.addr, write_chunk))
|
|
|
|
logger.debug("[%s] <- %s" % (self.addr, readable_bytes(self.__writebuffer)))
|
|
|
|
self.server.socket.sendto(self.__writebuffer, self.addr)
|
|
|
|
self.__writebuffer.clear()
|
|
|
|
|
|
|
|
def send_immediate(self, data: bytes):
|
|
|
|
logger.debug("[%s] <- %s" % (self.addr, readable_bytes(data)))
|
|
|
|
self.server.socket.sendto(data, self.addr)
|
2018-09-24 11:37:21 +02:00
|
|
|
|
|
|
|
def restart():
|
2019-12-14 20:08:34 +01:00
|
|
|
reload(serverQueryHandler)
|
|
|
|
reload(mainHandler)
|
|
|
|
mainHandler.restart()
|