# Buggy code
def sni(self):
    for extension in self._client_hello.extensions.extensions:
        is_valid_sni_extension = (
            extension.type == 0x00
            and len(extension.server_names) == 1
            and extension.server_names[0].name_type == 0
            and check.is_valid_host(extension.server_names[0].host_name)
        )
        if is_valid_sni_extension:
            return extension.server_names[0].host_name.decode("idna")


# Traceback (construct.core.FieldError)
⇩ [0/0]    [showhost]                                                                                                                                                                        ?:help [*:8001]
Traceback (most recent call last):
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 460, in _parse
⇩ [0/0]    [showhost]                                                                                                                                                                        ?:help [*:8001]
Error: 192.168.2.28:54194: Traceback (most recent call last):
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 460, in _parse
return packer.unpack(self.fmtstr, _read_stream(stream, self.sizeof()))[0]
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 73, in _read_stream
raise FieldError("could not read enough bytes, expected %d, found %d" % (length, len(data)))
construct.core.FieldError: could not read enough bytes, expected 2, found 0
Traceback (most recent call last):
During handling of the above exception, another exception occurred:
return self.subcon._parse(stream, context, path)
Traceback (most recent call last):
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 2696, in _parse
return self.subcon._parse(stream, context, path)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 859, in _parse
subobj = sc._parse(stream, context, path)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 462, in _parse
raise FieldError("packer %r error during parsing" % self.fmtstr)
construct.core.FieldError: packer '>H' error during parsing
Traceback (most recent call last):
During handling of the above exception, another exception occurred:
return cls(raw_client_hello)
Traceback (most recent call last):
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 293, in from_client_conn
return cls(raw_client_hello)
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 251, in __init__
self._client_hello = _constructs.ClientHello.parse(raw_client_hello)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 175, in parse
return self.parse_stream(BytesIO(data), context, **kw)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 186, in parse_stream
return self._parse(stream, context, "parsing")
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 2696, in _parse
return self.subcon._parse(stream, context, path)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 859, in _parse
subobj = sc._parse(stream, context, path)
File "/Users/yifanlu/Downloads/mitmproxy/venv3.5/lib/python3.5/site-packages/construct/core.py", line 2700, in _parse
raise e.__class__("%s\n    %s" % (e, path))
construct.core.FieldError: packer '>H' error during parsing
parsing -> ClientHello -> extensions
Traceback (most recent call last):
During handling of the above exception, another exception occurred:
root_layer()
Traceback (most recent call last):
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/server.py", line 119, in handle
root_layer()
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/modes/transparent_proxy.py", line 19, in __call__
layer()
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 338, in __call__
self._client_hello = TlsClientHello.from_client_conn(self.client_conn)
File "/Users/yifanlu/Downloads/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 297, in from_client_conn
(repr(e), raw_client_hello.encode("hex"))
AttributeError: 'bytes' object has no attribute 'encode'

# Fixed code
def sni(self):
    if self._client_hello.extensions:
        for extension in self._client_hello.extensions.extensions:
            is_valid_sni_extension = (
                extension.type == 0x00
                and len(extension.server_names) == 1
                and extension.server_names[0].name_type == 0
                and check.is_valid_host(extension.server_names[0].host_name)
            )
            if is_valid_sni_extension:
                return extension.server_names[0].host_name.decode("idna")
    return None