A working plaintext 2.0 without validation
This commit is contained in:
parent
a0923d202a
commit
22b1a5395d
|
@ -98,7 +98,7 @@ def initialize_default_swarm(
|
|||
|
||||
muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex}
|
||||
security_transports_by_protocol = sec_opt or {
|
||||
PLAINTEXT_PROTOCOL_ID: InsecureTransport(key_pair)
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
||||
}
|
||||
upgrader = TransportUpgrader(
|
||||
security_transports_by_protocol, muxer_transports_by_protocol
|
||||
|
|
|
@ -184,12 +184,6 @@ class Swarm(INetwork):
|
|||
async def conn_handler(
|
||||
reader: asyncio.StreamReader, writer: asyncio.StreamWriter
|
||||
) -> None:
|
||||
# Read in first message (should be peer_id of initiator) and ack
|
||||
peer_id = ID.from_base58((await reader.read(1024)).decode())
|
||||
|
||||
writer.write("received peer id".encode())
|
||||
await writer.drain()
|
||||
|
||||
# Upgrade reader/write to a net_stream and pass \
|
||||
# to appropriate stream handler (using multiaddr)
|
||||
raw_conn = RawConnection(
|
||||
|
@ -202,9 +196,11 @@ class Swarm(INetwork):
|
|||
|
||||
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
||||
# the conn and then mux the conn
|
||||
# FIXME: This dummy `ID(b"")` for the remote peer is useless.
|
||||
secured_conn = await self.upgrader.upgrade_security(
|
||||
raw_conn, peer_id, False
|
||||
raw_conn, ID(b""), False
|
||||
)
|
||||
peer_id = secured_conn.get_remote_peer()
|
||||
muxed_conn = await self.upgrader.upgrade_connection(
|
||||
secured_conn, self.generic_protocol_handler, peer_id
|
||||
)
|
||||
|
|
2
libp2p/security/insecure/exceptions.py
Normal file
2
libp2p/security/insecure/exceptions.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
class UpgradeFailure(Exception):
|
||||
pass
|
|
@ -1,15 +1,34 @@
|
|||
from libp2p.crypto.keys import PublicKey
|
||||
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.typing import TProtocol
|
||||
from libp2p.utils import encode_varint_prefixed, read_varint_prefixed_bytes
|
||||
|
||||
PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/1.0.0")
|
||||
from .exceptions import UpgradeFailure
|
||||
from .pb import plaintext_pb2
|
||||
|
||||
# Reference: https://github.com/libp2p/go-libp2p-core/blob/master/sec/insecure/insecure.go
|
||||
|
||||
|
||||
PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/2.0.0")
|
||||
|
||||
|
||||
class InsecureSession(BaseSession):
|
||||
pass
|
||||
# FIXME: Update the read/write to `BaseSession`
|
||||
async def run_handshake(self):
|
||||
msg = make_exchange_message(self.local_private_key.get_public_key())
|
||||
self.writer.write(encode_varint_prefixed(msg.SerializeToString()))
|
||||
await self.writer.drain()
|
||||
|
||||
msg_bytes_other_side = await read_varint_prefixed_bytes(self.reader)
|
||||
msg_other_side = plaintext_pb2.Exchange()
|
||||
msg_other_side.ParseFromString(msg_bytes_other_side)
|
||||
# TODO: Verify public key with peer id
|
||||
# TODO: Store public key
|
||||
self.remote_peer_id = ID(msg_other_side.id)
|
||||
|
||||
|
||||
class InsecureTransport(BaseSecureTransport):
|
||||
|
@ -24,7 +43,9 @@ class InsecureTransport(BaseSecureTransport):
|
|||
for an inbound connection (i.e. we are not the initiator)
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
return InsecureSession(self, conn, ID(b""))
|
||||
session = InsecureSession(self, conn, ID(b""))
|
||||
await session.run_handshake()
|
||||
return session
|
||||
|
||||
async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn:
|
||||
"""
|
||||
|
@ -32,4 +53,19 @@ class InsecureTransport(BaseSecureTransport):
|
|||
for an inbound connection (i.e. we are the initiator)
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
return InsecureSession(self, conn, peer_id)
|
||||
session = InsecureSession(self, conn, peer_id)
|
||||
await session.run_handshake()
|
||||
# TODO: Check if `remote_public_key is not None`. If so, check if `session.remote_peer`
|
||||
received_peer_id = session.get_remote_peer()
|
||||
if session.get_remote_peer() != peer_id:
|
||||
raise UpgradeFailure(
|
||||
"remote peer sent unexpected peer ID. "
|
||||
f"expected={peer_id} received={received_peer_id}"
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
def make_exchange_message(pubkey: PublicKey) -> plaintext_pb2.Exchange:
|
||||
pubkey_pb = pubkey.serialize_to_protobuf()
|
||||
id_bytes = ID.from_pubkey(pubkey).to_bytes()
|
||||
return plaintext_pb2.Exchange(id=id_bytes, pubkey=pubkey_pb)
|
||||
|
|
|
@ -9,11 +9,11 @@ from libp2p.peer.id import ID
|
|||
from libp2p.security.secure_conn_interface import ISecureConn
|
||||
from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream
|
||||
from libp2p.typing import TProtocol
|
||||
from libp2p.utils import decode_uvarint_from_stream, encode_uvarint
|
||||
|
||||
from .constants import HeaderTags
|
||||
from .exceptions import StreamNotFound
|
||||
from .mplex_stream import MplexStream
|
||||
from libp2p.utils import decode_uvarint_from_stream, encode_uvarint
|
||||
|
||||
MPLEX_PROTOCOL_ID = TProtocol("/mplex/6.7.0")
|
||||
|
||||
|
|
|
@ -75,18 +75,6 @@ class TCP(ITransport):
|
|||
|
||||
reader, writer = await asyncio.open_connection(host, int(port))
|
||||
|
||||
# TODO: Change this `sending peer id` process to `/plaintext/2.0.0`
|
||||
# First: send our peer ID so receiver knows it
|
||||
writer.write(self_id.to_base58().encode())
|
||||
await writer.drain()
|
||||
|
||||
# Await ack for peer id
|
||||
expected_ack_str = "received peer id"
|
||||
ack = (await reader.read(len(expected_ack_str))).decode()
|
||||
|
||||
if ack != expected_ack_str:
|
||||
raise Exception("Receiver did not receive peer id")
|
||||
|
||||
return RawConnection(host, port, reader, writer, True)
|
||||
|
||||
def create_listener(self, handler_function: THandler) -> TCPListener:
|
||||
|
|
|
@ -4,8 +4,7 @@ from typing import Tuple
|
|||
|
||||
from libp2p.typing import StreamReader
|
||||
|
||||
|
||||
TIMEOUT = 10
|
||||
TIMEOUT = 1
|
||||
|
||||
|
||||
def encode_uvarint(number: int) -> bytes:
|
||||
|
|
Loading…
Reference in New Issue
Block a user