mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Add an "insecure session" that satisfies the ISecureConn interface
This commit is contained in:
32
libp2p/security/base_session.py
Normal file
32
libp2p/security/base_session.py
Normal 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
|
||||||
13
libp2p/security/base_transport.py
Normal file
13
libp2p/security/base_transport.py
Normal 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)
|
||||||
@ -1,10 +1,24 @@
|
|||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
from libp2p.peer.id import ID
|
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_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``,
|
``InsecureTransport`` provides the "identity" upgrader for a ``IRawConnection``,
|
||||||
i.e. the upgraded transport does not add any additional security.
|
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)
|
for an inbound connection (i.e. we are not the initiator)
|
||||||
:return: secure connection object (that implements secure_conn_interface)
|
: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:
|
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)
|
for an inbound connection (i.e. we are the initiator)
|
||||||
:return: secure connection object (that implements secure_conn_interface)
|
:return: secure connection object (that implements secure_conn_interface)
|
||||||
"""
|
"""
|
||||||
return conn
|
return InsecureSession(self, conn, peer_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user