feat: debugging server update

This commit is contained in:
Strix 2023-10-15 18:06:27 +02:00
parent b456391e62
commit a108ce5226
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774

View file

@ -1,13 +1,21 @@
import socket import socket, zlib
HOST = "127.0.0.1" # Standard loopback interface address (localhost) HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 2000 # Port to listen on (non-privileged ports are > 1023) PORT = 2000 # Port to listen on (non-privileged ports are > 1023)
def calculate_crc(data):
# Calculate the CRC32 checksum of the data
crc = zlib.crc32(data)
# Convert the CRC32 value to a 4-byte big-endian byte array
crc_bytes = crc.to_bytes(4, byteorder='big')
return crc_bytes
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT)) s.bind((HOST, PORT))
s.listen() s.listen()
while True: while True:
try:
conn, addr = s.accept() conn, addr = s.accept()
with conn: with conn:
print(f"Connected by {addr}") print(f"Connected by {addr}")
@ -15,4 +23,23 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
data = conn.recv(1024) data = conn.recv(1024)
if not data: if not data:
break break
print(f'{data.hex()} [{len(data)}]') print(f'RECV: {data.hex()} [{len(data)}]')
if data[0x11] == 0x01:
pckt = bytearray([0x01])
pckt.extend([0x00, 0x00, 0x00, 0x00]) # src
pckt.extend([0x00, 0x00, 0x00, 0xFF]) # dest
pckt.extend([0x00, 0x00, 0x00, 0x00]) # packet_id
pckt.extend(data[0x09:0x0D]) # reply_to
pckt.extend([0x01]) # command
pckt.extend([0x00, 0x04]) # data_length
pckt.extend([0x00, 0x00, 0x00, 0xFF])
# Calculate CRC for the received data
crc = calculate_crc(pckt)
# Append the CRC value to the data
data_with_crc = pckt + crc
# Send the modified data (with CRC) back to the client
conn.sendall(data_with_crc)
print(f'SEND: {data_with_crc.hex()} [{len(data_with_crc)}]')
except:
s.close()