Some anti-DOS protection

This commit is contained in:
Juhani Haverinen 2016-07-12 17:33:59 +03:00
parent 841f7025da
commit 9548eab439
1 changed files with 12 additions and 2 deletions

View File

@ -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)