Log ClientHellos nicely

This commit is contained in:
Juhani Krekelä 2020-06-08 16:30:04 +03:00
parent 97d01a088c
commit 3abe65a551
1 changed files with 11 additions and 1 deletions

View File

@ -248,6 +248,8 @@ class RequestError(OneArgumentException):
class EmptyRequestError(OneArgumentException):
text = 'Got an empty request: %s'
class TLSClientHelloError(Exception): pass
class Protocol(enum.Enum):
gopher, gopherplus, http = range(3)
@ -280,6 +282,10 @@ def get_request(sockreader, *, config):
if len(request) >= config.request_max_size:
raise RequestError('Request too long')
# We have enough data to recognise a TLS 1.1+ ClientHello
if len(request) >= 3 and request[:3] == b'\x16\x03\x01':
raise TLSClientHelloError('Got a TLS ClientHello')
# We have enough data to recognise a HTTP request
if protocol is None and len(request) >= 5:
# Does it look like a HTTP GET request?
@ -354,7 +360,11 @@ def get_request(sockreader, *, config):
# Found the end of the path
path_end = index
path = request[:path_end].decode('utf-8')
try:
path = request[:path_end].decode('utf-8')
except UnicodeDecodeError as err:
print(request)#debg
raise err
# If another field was present, check to see if it marks a Gopher+ request
if chr(request[index]) == '\t':