From 3abe65a551a2084f52516d8387e95b8c3b6a295b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Mon, 8 Jun 2020 16:30:04 +0300 Subject: [PATCH] Log ClientHellos nicely --- neomi.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/neomi.py b/neomi.py index 8d8fb9a..00c69ca 100644 --- a/neomi.py +++ b/neomi.py @@ -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':