Add an "insecure session" that satisfies the ISecureConn interface

This commit is contained in:
Alex Stokes 2019-08-02 16:50:44 -07:00
parent fb43728661
commit 879cbf1abd
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
3 changed files with 63 additions and 4 deletions

View File

@ -0,0 +1,32 @@
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
from libp2p.security.base_transport import BaseSecureTransport
from libp2p.security.secure_conn_interface import ISecureConn
class BaseSession(ISecureConn, IRawConnection):
"""
``BaseSession`` is not fully instantiated from its abstract classes as it
is only meant to be used in clases that derive from it.
"""
def __init__(
self, transport: BaseSecureTransport, conn: IRawConnection, peer_id: ID
) -> None:
self.local_peer = self.transport.local_peer
self.local_private_key = self.transport.local_private_key
self.insecure_conn = conn
self.remote_peer_id = peer_id
self.remote_permanent_pubkey = b""
def get_local_peer(self) -> ID:
return self.local_peer
def get_local_private_key(self) -> bytes:
return self.local_private_key
def get_remote_peer(self) -> ID:
return self.remote_peer_id
def get_remote_public_key(self) -> bytes:
return self.remote_permanent_pubkey

View File

@ -0,0 +1,13 @@
from libp2p.peer.id import ID
from libp2p.security.secure_transport_interface import ISecureTransport
class BaseSecureTransport(ISecureTransport):
"""
``BaseSecureTransport`` is not fully instantiated from its abstract classes as it
is only meant to be used in clases that derive from it.
"""
def __init__(self, local_private_key: bytes) -> None:
self.local_private_key = local_private_key
self.local_peer = ID.from_privkey(local_private_key)

View File

@ -1,10 +1,24 @@
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
from libp2p.security.base_session import BaseSession
from libp2p.security.base_transport import BaseSecureTransport
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.security.secure_transport_interface import ISecureTransport
class InsecureTransport(ISecureTransport):
class InsecureSession(BaseSession):
def __init__(
self, transport: BaseSecureTransport, conn: IRawConnection, peer_id: ID
) -> None:
super(InsecureSession, self).__init__(transport, conn, peer_id)
async def write(self, data: bytes) -> None:
await self.insecure_conn.write(data)
async def read(self) -> bytes:
return await self.insecure_conn.read()
class InsecureTransport(BaseSecureTransport):
"""
``InsecureTransport`` provides the "identity" upgrader for a ``IRawConnection``,
i.e. the upgraded transport does not add any additional security.
@ -16,7 +30,7 @@ class InsecureTransport(ISecureTransport):
for an inbound connection (i.e. we are not the initiator)
:return: secure connection object (that implements secure_conn_interface)
"""
return conn
return InsecureSession(self, conn, ID(b""))
async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn:
"""
@ -24,4 +38,4 @@ class InsecureTransport(ISecureTransport):
for an inbound connection (i.e. we are the initiator)
:return: secure connection object (that implements secure_conn_interface)
"""
return conn
return InsecureSession(self, conn, peer_id)