-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPserver.py
583 lines (462 loc) · 20.9 KB
/
TCPserver.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import socket
import threading
import logging
import time
import inspect
from concurrent.futures import ThreadPoolExecutor
from typing import Optional, Callable, Any
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
import os
import Event as event
EventHandler = event.EventHandler
# Convert the byte string to a list of integers for logging
def byte_string_to_int_list(byte_string: bytes) -> list[int]:
return [byte for byte in byte_string]
class Client:
"""Class representing a client."""
def __init__(
self,
on_remove: Callable[['Client'], None],
_connected_callbacks: EventHandler,
conn: socket.socket,
addr: tuple[str, int],
timeout: int = 5,
encryption: int = 0,
public_key: Optional[rsa.RSAPublicKey] = None,
private_key: Optional[rsa.RSAPrivateKey] = None,
secretToken: str = "",
buffer_size: int = 1024,
) -> None:
logging.debug("Initializing Client.")
self._on_remove: Callable[['Client'], None] = on_remove
self._connected_callbacks: EventHandler = _connected_callbacks
self.conn: Optional[socket.socket] = conn
self.conn.settimeout(timeout)
self._ping_timeout_time: int = timeout
self.addr: tuple[str, int] = addr
self._disconnected_callbacks: EventHandler = EventHandler()
self._timeout_callbacks: EventHandler = EventHandler()
self._message_callbacks: EventHandler = EventHandler()
self._running: bool = False
self._encryption: int = encryption
self._last_ping: float = 0
self._ping_callbacks: EventHandler = EventHandler()
self.server_publickey: Optional[rsa.RSAPublicKey] = public_key
self.server_privatekey: Optional[rsa.RSAPrivateKey] = private_key
self.client_key: Optional[bytes] = None
self.client_initkey: Optional[bytes] = None
self._ping_message: bytes = b"PING"
self.secretToken: str = secretToken
self.buffer_size: int = buffer_size
def address(self) -> tuple[str, int]:
"""Return the client's address."""
return self.addr
def start(self) -> None:
"""Start the client listener."""
logging.debug(f"Client[{self.addr}] Starting client.")
if not self.conn or self._running:
return
self._running = True
self._reset_ping()
if self._encryption:
self._send_server_publickey()
if not self._listen_for_clientkey(): # if returns false = error
return
self.send(b"OK")
self._reset_ping()
if not self._validate_token():
logging.warning(f"Invalid token from {self.addr}. Closing connection.")
self.stop()
return
logging.info(f"Valid token received from {self.addr}. Connection authorized.")
self._connected_callbacks.emit(self)
self._listen()
def _validate_token(self) -> bool:
"""Validate the token sent by the client."""
logging.debug(f"Client[{self.addr}] Validating token.")
if self.conn is None:
return False
while self._running:
try:
current_time = time.time()
if current_time - self._last_ping > self._ping_timeout_time:
self._ping_timeout()
return False
data = self.conn.recv(self.buffer_size)
if data:
if self._encryption:
logging.debug(f"Client[{self.addr}] Received encrypted data: {byte_string_to_int_list(data)}")
data = self._decrypt(data)
logging.debug(f"Client[{self.addr}] Received data: {byte_string_to_int_list(data)}")
if data.decode('utf-8') != self.secretToken:
return False
else:
return True
except (socket.timeout, socket.error, OSError) as e:
if isinstance(e, socket.timeout):
self._ping_timeout()
else:
self._handle_socket_errors(e)
return False
return False
def _handle_ping(self) -> None:
"""Handle the ping message."""
logging.debug(f"Received ping from {self.addr}")
self._reset_ping()
self._ping_callbacks.emit(self)
self.send(b"PONG")
def _reset_ping(self) -> None:
"""Reset the ping timer."""
logging.debug(f"Client[{self.addr}] Resetting ping timer.")
self._last_ping = time.time()
def _ping_timeout(self) -> None:
"""Emit timeout and stop connection."""
logging.warning(f"Client[{self.addr}] Ping interval exceeded. Closing connection.")
self._timeout_callbacks.emit(self)
self.stop()
def _listen_for_clientkey(self) -> bool:
"""Listen for the client's key."""
logging.debug(f"Client[{self.addr}] Listening for client key.")
if self.conn is None or self.server_privatekey is None:
return False
while self._running:
try:
current_time = time.time()
if current_time - self._last_ping > self._ping_timeout_time:
self._ping_timeout()
return False
data = self.conn.recv(self.buffer_size)
if data:
logging.debug(f"Client[{self.addr}] Received client key: {byte_string_to_int_list(data)}")
init_and_key = self.server_privatekey.decrypt(
data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
self.client_initkey = init_and_key[:16] # the first 16 bytes are the init vector
self.client_key = init_and_key[16:] # the rest is the key
logging.debug(f"Client[{self.addr}] Decrypted AES Key: {byte_string_to_int_list(self.client_key)}")
logging.debug(f"Client[{self.addr}] Decrypted AES IV: {byte_string_to_int_list(self.client_initkey)}")
return True
else:
logging.debug(f"Client[{self.addr}] No data received. Closing connection.")
self.stop()
return False
except (socket.timeout, socket.error, OSError, Exception) as e:
if isinstance(e, socket.timeout):
self._ping_timeout()
else:
self._handle_socket_errors(e)
return False
return False
def _send_server_publickey(self) -> None:
"""Send the server's public key to the client."""
logging.debug(f"Client[{self.addr}] Sending server public key.")
if self.server_publickey is None:
return
server_publickey = self.server_publickey.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
self.send(server_publickey)
def _decrypt(self, encrypted_data: bytes) -> bytes:
"""Decrypt the received data."""
logging.debug(f"Client[{self.addr}] Decrypting data")
if self.client_key is None or self.client_initkey is None:
return b""
cipher = Cipher(algorithms.AES(self.client_key), modes.CFB(self.client_initkey), backend=default_backend())
decryptor = cipher.decryptor()
plaintext = decryptor.update(encrypted_data) + decryptor.finalize()
return plaintext
def _encrypt(self, data: bytes) -> bytes:
"""Encrypt the data to be sent."""
logging.debug(f"Client[{self.addr}] Encrypting data: {len(data)}")
if self.client_key is None or self.client_initkey is None:
return b""
cipher = Cipher(algorithms.AES(self.client_key), modes.CFB(self.client_initkey), backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(data) + encryptor.finalize()
return ciphertext
def _handle_socket_errors(self, error: Exception) -> None:
"""Centralize error handling for socket-related errors."""
logging.debug(f"Client[{self.addr}] Socket error: {error}")
self.stop()
def _listen(self) -> None:
"""Private method to listen for incoming data from the client."""
logging.debug(f"Client[{self.addr}] Listening for data.")
if self.conn is None:
return
while self._running:
try:
current_time = time.time()
if current_time - self._last_ping > self._ping_timeout_time:
self._ping_timeout()
return
data = self.conn.recv(self.buffer_size)
if data:
if self._encryption:
logging.debug(f"Client[{self.addr}] Received encrypted data: {byte_string_to_int_list(data)}")
data = self._decrypt(data)
logging.debug(f"Client[{self.addr}] Received data: {byte_string_to_int_list(data)}")
if data == self._ping_message:
self._handle_ping()
else:
self._message_callbacks.emit(self, data)
except (socket.timeout, socket.error, OSError) as e:
if isinstance(e, socket.timeout):
self._ping_timeout()
else:
self._handle_socket_errors(e)
def stop(self) -> None:
"""Stop the client and close its connection."""
if not self._running:
logging.warning(f"Client[{self.addr}] already stopped.")
return
logging.debug(f"Client[{self.addr}] Stopping client.")
self._running = False
if not self.conn:
return
self._disconnected_callbacks.emit(self)
try:
self.conn.shutdown(socket.SHUT_RDWR)
self.conn.close()
except Exception as e:
logging.error(f"Error while closing client connection: {e}")
self.conn = None
self._on_remove(self)
logging.debug(f"Thread: Stopped for client: {self.addr}")
def send(self, data: bytes) -> None:
"""Send data to the client."""
try:
logging.debug(f"Client[{self.addr}] Sending data: {len(data)}")
if self.conn is None:
raise Exception("Connection is None.")
if self._encryption and self.client_key and self.client_initkey:
data = self._encrypt(data)
self.conn.sendall(data)
except (OSError, Exception) as e:
self._handle_socket_errors(e)
def on_event(self, event_type: str, callback: Callable) -> Optional[int]:
"""Register an event callback based on the event type."""
num_params = len(inspect.signature(callback).parameters)
if event_type == "disconnected":
if num_params == 1:
return self._disconnected_callbacks.add_event(callback)
else:
logging.error(f"Invalid number of parameters for 'disconnected' event. Expected 1, got {num_params}.")
elif event_type == "timeout":
if num_params == 1:
return self._timeout_callbacks.add_event(callback)
else:
logging.error(f"Invalid number of parameters for 'timeout' event. Expected 1, got {num_params}.")
elif event_type == "message":
if num_params == 2:
return self._message_callbacks.add_event(callback)
else:
logging.error(f"Invalid number of parameters for 'message' event. Expected 2, got {num_params}.")
elif event_type == "ping":
if num_params == 1:
return self._ping_callbacks.add_event(callback)
else:
logging.error(f"Invalid number of parameters for 'ping' event. Expected 1, got {num_params}.")
else:
logging.warning(f"Unsupported event type: {event_type}")
return None
def remove_event(self, event_type: str, event_id: int) -> None:
"""Remove an event callback based on the event type."""
if event_type == "disconnected":
self._disconnected_callbacks.remove_event(event_id)
elif event_type == "timeout":
self._timeout_callbacks.remove_event(event_id)
elif event_type == "message":
self._message_callbacks.remove_event(event_id)
elif event_type == "ping":
self._ping_callbacks.remove_event(event_id)
else:
logging.warning(f"Unsupported event type: {event_type}")
class Server:
"""Class representing a TCP server."""
def __init__(
self,
host: str,
port: int,
timeout: int = 5,
encryption: int = 0,
backlog: int = 5,
max_threads: int = 10,
secretToken: str = "",
buffer_size: int = 1024,
) -> None:
logging.debug("Initializing Server.")
self.host: str = host
self.port: int = port
self.backlog: int = backlog
self.timeout: int = timeout
self._connected_callbacks: EventHandler = EventHandler()
self._clients: list[Client] = []
self._clients_lock: threading.Lock = threading.Lock()
self._socket: Optional[socket.socket] = None
self._running: bool = False
self.max_threads: int = max_threads
self.main_accept_clients_thread: Optional[threading.Thread] = None
self._thread_pool: Optional[ThreadPoolExecutor] = None
self.active_clients_count: int = 0
self._encryption: int = encryption
self.public_key: Optional[rsa.RSAPublicKey] = None
self.private_key: Optional[rsa.RSAPrivateKey] = None
self.secretToken: str = secretToken
self.buffer_size: int = buffer_size
def generate_keys(self) -> None:
"""Generate RSA keys."""
logging.debug("Generating RSA keys.")
self.private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
self.public_key = self.private_key.public_key()
def start(self) -> None:
"""Start the server."""
if self._encryption:
self.generate_keys()
logging.debug("Starting server.")
if self._socket:
logging.warning("Server already started.")
return
self._running = True
self.main_accept_clients_thread = threading.Thread(target=self._accept_clients)
self.main_accept_clients_thread.daemon = True
self._thread_pool = ThreadPoolExecutor(self.max_threads)
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._socket.settimeout(self.timeout)
self._socket.bind((self.host, self.port))
self._socket.listen(self.backlog)
logging.debug("Thread: Starting client acceptance.")
self.main_accept_clients_thread.start()
except Exception as e:
logging.error(f"Failed to start server: {e}")
self._running = False
logging.info(f"Server started: {self.host}:{self.port}")
def _accept_clients(self) -> None:
"""Private method to accept incoming clients."""
logging.debug("Accepting clients.")
while True:
if not self._running:
break
try:
logging.debug("Waiting for client...")
if self._socket is None:
logging.error("Server socket is None. Stopping client acceptance.")
break
conn, addr = self._socket.accept()
if conn and self._running:
logging.debug(f"Accepted client: {addr}")
client = Client(
self._remove_client,
self._connected_callbacks,
conn,
addr,
self.timeout,
self._encryption,
self.public_key,
self.private_key,
self.secretToken,
self.buffer_size,
)
with self._clients_lock:
self._clients.append(client)
if self._thread_pool is None:
logging.error("Thread pool is None. Stopping client acceptance.")
break
logging.debug(f"Thread: Starting for client: {addr}")
self._thread_pool.submit(client.start)
self.active_clients_count += 1
except socket.timeout:
logging.debug("Main socket timeout. But can be ignored.")
pass
except socket.error as e:
if e.errno == 10038:
logging.info("Server socket closed. Stopping client acceptance.")
else:
logging.error(f"Error accepting clients: {e}")
def _remove_client(self, client: Client) -> None:
"""Private method to remove a client from the server's client list."""
with self._clients_lock:
logging.debug(f"Removing TCP client: {client.addr}")
self._clients.remove(client)
self.active_clients_count -= 1
def stop(self) -> None:
"""Stop the server."""
if not self._running:
logging.warning("Server already stopped.")
return
logging.debug("Stopping server.")
self._running = False
logging.debug("Stopping clients.")
for client in self._clients[:]:
client.stop()
logging.debug("Stopping server socket.")
if self._socket:
self._socket.close()
self._socket = None
logging.debug("Thread: Stopping client acceptance.")
if self.main_accept_clients_thread and self.main_accept_clients_thread.is_alive():
self.main_accept_clients_thread.join()
logging.debug("Thread: Shutting down thread pool.")
if self._thread_pool:
self._thread_pool.shutdown(wait=True)
def on_connected(self, callback: Callable[[Client], Any]) -> int:
"""Register a callback for when a client connects."""
logging.debug("Registering 'connected' event.")
num_params = len(inspect.signature(callback).parameters)
if num_params != 1:
logging.error(f"Invalid number of parameters for 'connected' event. Expected 1, got {num_params}.")
return -1
return self._connected_callbacks.add_event(callback)
def remove_connected_event(self, event_id: int) -> None:
"""Remove the connected event callback using its ID."""
logging.debug("Removing 'connected' event.")
self._connected_callbacks.remove_event(event_id)
# EXAMPLE USAGE
SECRET_TOKEN = "your_secret_token"
def handle_client_message(client: Client, data: bytes) -> None:
"""Handle received message after token validation."""
logging.info(f"Received from {client.addr}: {data.decode('utf-8')}")
client.send(b"OK")
def on_connected(client: Client) -> None:
"""Handle new client connection."""
logging.info(f"Connected by {client.addr}")
client.on_event("disconnected", lambda c: logging.info(f"Disconnected by {c.addr}"))
client.on_event("timeout", lambda c: logging.info(f"Timeout by {c.addr}"))
client.on_event("message", handle_client_message)
client.on_event("ping", lambda c: logging.info(f"Ping from {c.addr}"))
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
srv = Server('localhost', 5000, 5, True, 5, 10, SECRET_TOKEN)
srv.on_connected(on_connected)
logging.info("Starting server: 127.0.0.1:5000...")
srv.start()
logging.info("Waiting for connections...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
logging.info("Stopping server...")
logging.info(f"Disconnecting from {srv.active_clients_count} clients...")
srv.stop()
while srv.active_clients_count > 0:
logging.info(f"Waiting for {srv.active_clients_count} clients to disconnect...")
time.sleep(1)
logging.info("Server stopped.")
logging.info("THE END")
if __name__ == '__main__':
main()