Some anti-DOS protection
This commit is contained in:
parent
841f7025da
commit
9548eab439
12
neomi.py
12
neomi.py
|
@ -7,9 +7,11 @@ import time
|
||||||
|
|
||||||
class config: None
|
class config: None
|
||||||
|
|
||||||
|
config.max_threads = 8192
|
||||||
config.port = 7777
|
config.port = 7777
|
||||||
config.max_threads = 1024
|
|
||||||
config.recognised_selectors = ['0', '1', '5', '9', 'g', 'h', 'I', 's']
|
config.recognised_selectors = ['0', '1', '5', '9', 'g', 'h', 'I', 's']
|
||||||
|
config.request_max_size = 8192
|
||||||
|
config.socket_timeout = 1
|
||||||
|
|
||||||
# error(message)
|
# error(message)
|
||||||
# Print error message to stderr
|
# Print error message to stderr
|
||||||
|
@ -99,9 +101,14 @@ def extract_selector_path(selector_path):
|
||||||
def get_request(sock):
|
def get_request(sock):
|
||||||
request = b''
|
request = b''
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
data = sock.recv(1024)
|
data = sock.recv(1024)
|
||||||
|
except socket.timeout:
|
||||||
|
raise RequestEerror('Remote end timed out')
|
||||||
if not data: # Other end hung up before sending a header
|
if not data: # Other end hung up before sending a header
|
||||||
raise RequestEerror('Remote end hung up unexpectedly')
|
raise RequestEerror('Remote end hung up unexpectedly')
|
||||||
|
if len(data) >= config.request_max_size:
|
||||||
|
raise RequestEerror('Request too long')
|
||||||
|
|
||||||
request += data
|
request += data
|
||||||
|
|
||||||
|
@ -201,6 +208,9 @@ def listen(port):
|
||||||
# Accept and handle the connection
|
# Accept and handle the connection
|
||||||
conn, addr = s.accept()
|
conn, addr = s.accept()
|
||||||
|
|
||||||
|
# Set timeout for socket
|
||||||
|
sock.settimeout(config.socket_timeout)
|
||||||
|
|
||||||
spawn_thread(conn, addr[0])
|
spawn_thread(conn, addr[0])
|
||||||
|
|
||||||
listen(config.port)
|
listen(config.port)
|
||||||
|
|
Loading…
Reference in New Issue