Improve logging of dead connections that didn't make a request

This commit is contained in:
Juhani Krekelä 2020-05-28 20:43:36 +03:00
parent ee1bc97159
commit 0a8e10db90
1 changed files with 23 additions and 10 deletions

View File

@ -123,8 +123,8 @@ def drop_privileges():
class CommandError(OneArgumentException):
text = 'Error with command: %s'
class SocketReadError(OneArgumentException):
text = 'Error reading socket: %s'
class SocketTimeoutError(Exception):
pass
class ReaderCommands(enum.Enum):
stop = range(1)
@ -147,8 +147,8 @@ def SocketReader(sock):
try:
chunk = sock.recv(1024)
except socket.timeout:
raise SocketReadError('Error reading socket: Remote end timed out')
except socket.timeout as err:
raise SocketTimeoutError('Remote end timed out') from err
if not chunk:
break
@ -243,7 +243,10 @@ def normalize_path(path, *, config):
return '/'.join(normalized_components)
class RequestError(OneArgumentException):
text = 'Error with handling request: %s'
text = 'Error when handling request: %s'
class EmptyRequestError(OneArgumentException):
text = 'Got an empty request: %s'
class Protocol(enum.Enum):
gopher, gopherplus, http = range(3)
@ -262,7 +265,17 @@ def get_request(sockreader, *, config):
try:
request.append(next(sockreader))
except StopIteration: # Other end hung up before sending a full header
raise RequestError('Remote end hung up unexpectedly')
if len(request) == 0:
raise EmptyRequestError('Remote end hung up unexpectedly')
else:
print('request:', request)#debg
raise RequestError('Remote end hung up unexpectedly')
except SocketTimeoutError as err:
if len(request) == 0:
raise EmptyRequestError('Remote end timed out') from err
else:
print('request:', request)#debg
raise err
if len(request) >= config.request_max_size:
raise RequestError('Request too long')
@ -798,8 +811,8 @@ def read_blacklist(blacklist_file):
try:
ip_range = ipaddress.ip_network(line)
except ValueError:
raise IPParseError('Invalid format: ' + line)
except ValueError as err:
raise IPParseError('Invalid format: ' + line) from err
blacklist.append(ip_range)
@ -810,8 +823,8 @@ def read_blacklist(blacklist_file):
def ip_in_ranges(ip, ip_ranges):
try:
ip = ipaddress.ip_address(ip)
except ValueError:
raise IPParseError('Invalid format: ' + line)
except ValueError as err:
raise IPParseError('Invalid format: ' + line) from err
for ip_range in ip_ranges:
if ip in ip_range: