From 9548eab4398a4dc3cedb9bc684ea1cfa6b18a640 Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Tue, 12 Jul 2016 17:33:59 +0300 Subject: [PATCH] Some anti-DOS protection --- neomi.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/neomi.py b/neomi.py index 2240df3..a6ecb9f 100644 --- a/neomi.py +++ b/neomi.py @@ -7,9 +7,11 @@ import time class config: None +config.max_threads = 8192 config.port = 7777 -config.max_threads = 1024 config.recognised_selectors = ['0', '1', '5', '9', 'g', 'h', 'I', 's'] +config.request_max_size = 8192 +config.socket_timeout = 1 # error(message) # Print error message to stderr @@ -99,9 +101,14 @@ def extract_selector_path(selector_path): def get_request(sock): request = b'' while True: - data = sock.recv(1024) + try: + data = sock.recv(1024) + except socket.timeout: + raise RequestEerror('Remote end timed out') if not data: # Other end hung up before sending a header raise RequestEerror('Remote end hung up unexpectedly') + if len(data) >= config.request_max_size: + raise RequestEerror('Request too long') request += data @@ -201,6 +208,9 @@ def listen(port): # Accept and handle the connection conn, addr = s.accept() + # Set timeout for socket + sock.settimeout(config.socket_timeout) + spawn_thread(conn, addr[0]) listen(config.port)