How to use the snitun.exceptions.ParseSNIError function in snitun

To help you get started, we’ve selected a few snitun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NabuCasa / snitun / tests / server / test_sni.py View on Github external
def test_bad_client_hello(test_package: bytes):
    """Test bad client hello."""
    with pytest.raises(ParseSNIError):
        sni.parse_tls_sni(test_package)
github NabuCasa / snitun / snitun / server / listener_sni.py View on Github external
except OSError:
                return
        else:
            client_hello = data

        # Connection closed before data received
        if not client_hello:
            with suppress(OSError):
                writer.close()
            return

        try:
            # Read Hostname
            try:
                hostname = parse_tls_sni(client_hello)
            except ParseSNIError:
                _LOGGER.warning("Receive invalid ClientHello on public Interface")
                return

            # Peer available?
            if not self._peer_manager.peer_available(hostname):
                _LOGGER.debug("Hostname %s not connected", hostname)
                return
            peer = self._peer_manager.get_peer(hostname)

            # Proxy data over mutliplexer to client
            _LOGGER.debug("Processing for hostname % started", hostname)
            await self._proxy_peer(peer.multiplexer, client_hello, reader, writer)

        finally:
            if not writer.transport.is_closing():
                with suppress(OSError):
github NabuCasa / snitun / snitun / server / sni.py View on Github external
size = (data[pos + 1] << 8) + data[pos + 2]

        # Unknown server name type
        if data[pos] != 0x00:
            _LOGGER.debug("Unknown ServerName type")
            pos += 3 + size
            continue

        try:
            return bytes(data[pos + 3 : pos + 3 + size]).decode()
        except IndexError:
            _LOGGER.debug("Wrong host length")
            raise ParseSNIError() from None

    _LOGGER.debug("Not found any valid ServerName")
    raise ParseSNIError()
github NabuCasa / snitun / snitun / server / sni.py View on Github external
def parse_tls_sni(data: bytes) -> str:
    """Parse TLS SNI extention."""

    if len(data) < TLS_HEADER_LEN:
        _LOGGER.debug("Invalid TLS header")
        raise ParseSNIError()

    # If TLS handshake
    if data[0] != TLS_HANDSHAKE_CONTENT_TYPE:
        _LOGGER.debug("Not TLS handshake received")
        raise ParseSNIError()

    # Check compatible ClientHello
    if int(data[1]) < 3:
        _LOGGER.debug("Received ClientHello without SNI support")
        raise ParseSNIError()

    # Calculate TLS record size
    tls_size = (data[3] << 8) + data[4] + TLS_HEADER_LEN
    if len(data) < tls_size:
        _LOGGER.debug("Can't calculate the TLS record size")
        raise ParseSNIError()

    # Check if handshake is a ClientHello
    pos = TLS_HEADER_LEN
    if data[pos] != TLS_HANDSHAKE_TYPE_CLIENT_HELLO:
        _LOGGER.debug("Invalid ClientHello type")