-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
80 lines (63 loc) · 2.59 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import socket
import traceback
from threading import *
from Packets.MessageFactory import *
from Logic.Device import Device
class ServerThread():
def __init__(self, ip, port):
self.address = str(ip)
self.port = int(port)
self.client = socket.socket()
def start(self):
self.client.bind((self.address, self.port))
print(f'Server is listening on {self.address}:{self.port}'.format(self.address, self.port))
while True:
self.client.listen(5)
client, address = self.client.accept()
print('New connection from {}'.format(address[0]))
clientThread = ClientThread(client).start()
class ClientThread(Thread):
def __init__(self, client):
Thread.__init__(self)
self.client = client
self.device = Device(self.client)
def recvall(self, size):
data = []
while size > 0:
self.client.settimeout(5.0)
s = self.client.recv(size)
self.client.settimeout(None)
if not s:
raise EOFError
data.append(s)
size -= len(s)
return b''.join(data)
def run(self):
while True:
header = self.client.recv(7)
packetid = int.from_bytes(header[:2], 'big')
length = int.from_bytes(header[2:5], 'big')
version = int.from_bytes(header[5:], 'big')
data = self.recvall(length)
if len(header) >= 7:
if length == len(data):
print('[*] {} received'.format(packetid))
try:
decrypted = self.device.decrypt(data)
if packetid in availablePackets:
Message = availablePackets[packetid](decrypted, self.device)
Message.decode()
Message.process()
else:
print('[*] {} not handled'.format(packetid))
except:
print('[*] Error while decrypting / handling {}'.format(packetid))
traceback.print_exc()
else:
print('[*] Incorrect Length for packet {} (header length: {}, data length: {})'.format(packetid, length, len(data)))
else:
print('[*] Received an invalid packet from client')
self.client.close()
if __name__ == '__main__':
server = ServerThread("0.0.0.0", 9339)
server.start()