mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
feat: implement get_remote_address via delegation pattern
This commit is contained in:
@ -2,6 +2,9 @@ from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
|
||||
class Closer(ABC):
|
||||
@ -35,7 +38,14 @@ class ReadWriter(Reader, Writer):
|
||||
|
||||
|
||||
class ReadWriteCloser(Reader, Writer, Closer):
|
||||
pass
|
||||
@abstractmethod
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
"""
|
||||
Return the remote address of the connected peer.
|
||||
|
||||
:return: A tuple of (host, port) or None if not available
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class MsgReader(ABC):
|
||||
@ -66,3 +76,9 @@ class Encrypter(ABC):
|
||||
|
||||
class EncryptedMsgReadWriter(MsgReadWriteCloser, Encrypter):
|
||||
"""Read/write message with encryption/decryption."""
|
||||
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
"""Get remote address if supported by the underlying connection."""
|
||||
if hasattr(self, "conn") and hasattr(self.conn, "get_remote_address"):
|
||||
return self.conn.get_remote_address()
|
||||
return None
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import logging
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
import trio
|
||||
|
||||
@ -42,3 +45,11 @@ class TrioTCPStream(ReadWriteCloser):
|
||||
|
||||
async def close(self) -> None:
|
||||
await self.stream.aclose()
|
||||
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
"""Return the remote address as (host, port) tuple."""
|
||||
try:
|
||||
return self.stream.socket.getpeername()
|
||||
except (AttributeError, OSError) as e:
|
||||
logger.error("Error getting remote address: %s", e)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user