Security: SecureSession

Make security sessions(secio, noise) share the same implementation
`BaseSession` to avoid duplicate implementation of buffered read.
This commit is contained in:
mhchia
2020-02-17 23:33:45 +08:00
parent 2df47a943c
commit 3c2e835725
8 changed files with 150 additions and 196 deletions

View File

@ -2,6 +2,7 @@ from abc import ABC, abstractmethod
class Closer(ABC):
@abstractmethod
async def close(self) -> None:
...
@ -39,10 +40,6 @@ class MsgReader(ABC):
async def read_msg(self) -> bytes:
...
# @abstractmethod
# async def next_msg_len(self) -> int:
# ...
class MsgWriter(ABC):
@abstractmethod
@ -50,7 +47,7 @@ class MsgWriter(ABC):
...
class MsgReadWriter(MsgReader, MsgWriter):
class MsgReadWriteCloser(MsgReader, MsgWriter, Closer):
pass
@ -64,5 +61,5 @@ class Encrypter(ABC):
...
class EncryptedMsgReadWriter(MsgReadWriter, Encrypter):
pass
class EncryptedMsgReadWriter(MsgReadWriteCloser, Encrypter):
"""Read/write message with encryption/decryption."""

View File

@ -8,10 +8,9 @@ NOTE: currently missing the capability to indicate lengths by "varint" method.
# TODO unify w/ https://github.com/libp2p/py-libp2p/blob/1aed52856f56a4b791696bbcbac31b5f9c2e88c9/libp2p/utils.py#L85-L99 # noqa: E501
from typing import Optional
from libp2p.io.abc import MsgReadWriter, Reader, ReadWriteCloser
from libp2p.io.abc import MsgReadWriteCloser, Reader, ReadWriteCloser
from libp2p.io.utils import read_exactly
BYTE_ORDER = "big"
@ -26,12 +25,12 @@ def encode_msg_with_length(msg_bytes: bytes, size_len_bytes: int) -> bytes:
except OverflowError:
raise ValueError(
"msg_bytes is too large for `size_len_bytes` bytes length: "
f"msg_bytes={msg_bytes}, size_len_bytes={size_len_bytes}"
f"msg_bytes={msg_bytes!r}, size_len_bytes={size_len_bytes}"
)
return len_prefix + msg_bytes
class BaseMsgReadWriter(MsgReadWriter):
class BaseMsgReadWriter(MsgReadWriteCloser):
next_length: Optional[int]
read_write_closer: ReadWriteCloser
size_len_bytes: int