py-libp2p/libp2p/security/security_multistream.py

102 lines
4.2 KiB
Python
Raw Normal View History

2019-05-02 01:54:19 +08:00
from abc import ABC
from collections import OrderedDict
2019-04-28 07:42:05 +08:00
2019-08-07 18:02:30 +08:00
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
from libp2p.protocol_muxer.multiselect import Multiselect
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator
2019-08-03 05:49:54 +08:00
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.security.secure_transport_interface import ISecureTransport
from libp2p.transport.typing import TSecurityOptions
2019-08-07 18:02:30 +08:00
from libp2p.typing import TProtocol
2019-08-01 19:12:11 +08:00
2019-05-02 01:54:19 +08:00
2019-04-28 07:42:05 +08:00
"""
Represents a secured connection object, which includes a connection and details about the security
involved in the secured connection
Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go
"""
2019-08-01 06:00:12 +08:00
class SecurityMultistream(ABC):
"""SSMuxer is a multistream stream security transport multiplexer.
Go implementation: github.com/libp2p/go-conn-security-multistream/ssms.go
"""
# NOTE: Can be changed to `typing.OrderedDict` since Python 3.7.2.
transports: "OrderedDict[TProtocol, ISecureTransport]"
2019-08-08 14:22:06 +08:00
multiselect: Multiselect
multiselect_client: MultiselectClient
2019-08-01 19:12:11 +08:00
def __init__(self, secure_transports_by_protocol: TSecurityOptions) -> None:
self.transports = OrderedDict()
2019-04-28 07:42:05 +08:00
self.multiselect = Multiselect()
self.multiselect_client = MultiselectClient()
for protocol, transport in secure_transports_by_protocol.items():
self.add_transport(protocol, transport)
2019-08-08 14:22:06 +08:00
def add_transport(self, protocol: TProtocol, transport: ISecureTransport) -> None:
"""Add a protocol and its corresponding transport to multistream-
select(multiselect). The order that a protocol is added is exactly the
precedence it is negotiated in multiselect.
:param protocol: the protocol name, which is negotiated in multiselect.
:param transport: the corresponding transportation to the ``protocol``.
"""
# If protocol is already added before, remove it and add it again.
if protocol in self.transports:
del self.transports[protocol]
2019-04-28 07:42:05 +08:00
self.transports[protocol] = transport
# Note: None is added as the handler for the given protocol since
# we only care about selecting the protocol, not any handler function
self.multiselect.add_handler(protocol, None)
2019-08-08 14:22:06 +08:00
async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:
"""Secure the connection, either locally or by communicating with
opposing node via conn, for an inbound connection (i.e. we are not the
initiator)
2019-04-28 07:42:05 +08:00
:return: secure connection object (that implements secure_conn_interface)
"""
transport = await self.select_transport(conn, False)
secure_conn = await transport.secure_inbound(conn)
2019-04-28 07:42:05 +08:00
return secure_conn
2019-08-08 14:22:06 +08:00
async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn:
"""Secure the connection, either locally or by communicating with
opposing node via conn, for an inbound connection (i.e. we are the
initiator)
2019-04-28 07:42:05 +08:00
:return: secure connection object (that implements secure_conn_interface)
"""
transport = await self.select_transport(conn, True)
secure_conn = await transport.secure_outbound(conn, peer_id)
2019-04-28 07:42:05 +08:00
return secure_conn
async def select_transport(
self, conn: IRawConnection, initiator: bool
) -> ISecureTransport:
"""Select a transport that both us and the node on the other end of
conn support and agree on.
2019-04-28 07:42:05 +08:00
:param conn: conn to choose a transport over
:param initiator: true if we are the initiator, false otherwise
:return: selected secure transport
"""
protocol: TProtocol
communicator = MultiselectCommunicator(conn)
2019-04-28 07:42:05 +08:00
if initiator:
# Select protocol if initiator
2019-08-01 06:00:12 +08:00
protocol = await self.multiselect_client.select_one_of(
list(self.transports.keys()), communicator
2019-08-01 06:00:12 +08:00
)
2019-04-28 07:42:05 +08:00
else:
# Select protocol if non-initiator
protocol, _ = await self.multiselect.negotiate(communicator)
2019-04-28 07:42:05 +08:00
# Return transport from protocol
return self.transports[protocol]