2023-08-24 15:29:57 +03:00
|
|
|
import asyncio
|
|
|
|
import socket
|
|
|
|
from asyncio.streams import StreamReader, StreamWriter
|
|
|
|
|
|
|
|
from .debug import debug_client, debug_server
|
|
|
|
|
|
|
|
MAX_SIZE = 0x400000
|
|
|
|
|
|
|
|
async def handle_server(writer: StreamWriter, server: socket.socket, fp):
|
2023-08-24 17:40:23 +03:00
|
|
|
try:
|
|
|
|
while (packet := await loop.sock_recv(server, MAX_SIZE)):
|
|
|
|
try:
|
|
|
|
debug_server(packet, fp)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'[S] error: {e}')
|
|
|
|
writer.write(packet)
|
|
|
|
await writer.drain()
|
|
|
|
except Exception as e:
|
|
|
|
print(f'handle_server(): {e}')
|
2023-08-24 15:29:57 +03:00
|
|
|
|
|
|
|
async def handle_client(reader: StreamReader, writer: StreamWriter):
|
|
|
|
print(reader, writer)
|
2023-08-24 17:40:23 +03:00
|
|
|
try:
|
|
|
|
server = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
|
|
server.connect(('201:4f8c:4ea:0:71ec:6d7:6f1b:a4f9', 25565))
|
|
|
|
server.setblocking(False)
|
|
|
|
|
|
|
|
with open("packets.txt", "w") as fp:
|
|
|
|
loop.create_task(handle_server(writer, server, fp))
|
|
|
|
|
|
|
|
while (packet := await reader.read(MAX_SIZE)):
|
|
|
|
try:
|
|
|
|
debug_client(packet, fp)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'[C] error: {e}')
|
|
|
|
await loop.sock_sendall(server, packet)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'handle_client(): {e}')
|
2023-08-24 15:29:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
def main():
|
|
|
|
loop.create_task(asyncio.start_server(handle_client, 'localhost', 25565))
|
|
|
|
loop.run_forever()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|