Improve logging of dead connections that didn't make a request
This commit is contained in:
parent
ee1bc97159
commit
0a8e10db90
1 changed files with 23 additions and 10 deletions
33
neomi.py
33
neomi.py
|
@ -123,8 +123,8 @@ def drop_privileges():
|
||||||
class CommandError(OneArgumentException):
|
class CommandError(OneArgumentException):
|
||||||
text = 'Error with command: %s'
|
text = 'Error with command: %s'
|
||||||
|
|
||||||
class SocketReadError(OneArgumentException):
|
class SocketTimeoutError(Exception):
|
||||||
text = 'Error reading socket: %s'
|
pass
|
||||||
|
|
||||||
class ReaderCommands(enum.Enum):
|
class ReaderCommands(enum.Enum):
|
||||||
stop = range(1)
|
stop = range(1)
|
||||||
|
@ -147,8 +147,8 @@ def SocketReader(sock):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
chunk = sock.recv(1024)
|
chunk = sock.recv(1024)
|
||||||
except socket.timeout:
|
except socket.timeout as err:
|
||||||
raise SocketReadError('Error reading socket: Remote end timed out')
|
raise SocketTimeoutError('Remote end timed out') from err
|
||||||
|
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
|
@ -243,7 +243,10 @@ def normalize_path(path, *, config):
|
||||||
return '/'.join(normalized_components)
|
return '/'.join(normalized_components)
|
||||||
|
|
||||||
class RequestError(OneArgumentException):
|
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):
|
class Protocol(enum.Enum):
|
||||||
gopher, gopherplus, http = range(3)
|
gopher, gopherplus, http = range(3)
|
||||||
|
@ -262,7 +265,17 @@ def get_request(sockreader, *, config):
|
||||||
try:
|
try:
|
||||||
request.append(next(sockreader))
|
request.append(next(sockreader))
|
||||||
except StopIteration: # Other end hung up before sending a full header
|
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:
|
if len(request) >= config.request_max_size:
|
||||||
raise RequestError('Request too long')
|
raise RequestError('Request too long')
|
||||||
|
@ -798,8 +811,8 @@ def read_blacklist(blacklist_file):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ip_range = ipaddress.ip_network(line)
|
ip_range = ipaddress.ip_network(line)
|
||||||
except ValueError:
|
except ValueError as err:
|
||||||
raise IPParseError('Invalid format: ' + line)
|
raise IPParseError('Invalid format: ' + line) from err
|
||||||
|
|
||||||
blacklist.append(ip_range)
|
blacklist.append(ip_range)
|
||||||
|
|
||||||
|
@ -810,8 +823,8 @@ def read_blacklist(blacklist_file):
|
||||||
def ip_in_ranges(ip, ip_ranges):
|
def ip_in_ranges(ip, ip_ranges):
|
||||||
try:
|
try:
|
||||||
ip = ipaddress.ip_address(ip)
|
ip = ipaddress.ip_address(ip)
|
||||||
except ValueError:
|
except ValueError as err:
|
||||||
raise IPParseError('Invalid format: ' + line)
|
raise IPParseError('Invalid format: ' + line) from err
|
||||||
|
|
||||||
for ip_range in ip_ranges:
|
for ip_range in ip_ranges:
|
||||||
if ip in ip_range:
|
if ip in ip_range:
|
||||||
|
|
Loading…
Reference in a new issue