Agent: Add TCPPipeSpawner class

This commit is contained in:
Kekoa Kaaikala 2022-09-01 19:15:23 +00:00
parent 0e869462b5
commit ef39a8afd1
2 changed files with 26 additions and 0 deletions

View File

@ -1,3 +1,4 @@
from .relay_user_handler import RelayUser, RelayUserHandler
from .tcp_connection_handler import TCPConnectionHandler
from .tcp import SocketsPipe
from .tcp_pipe_spawner import TCPPipeSpawner

View File

@ -0,0 +1,25 @@
import socket
from ipaddress import IPv4Address
from typing import Callable
from .tcp import SocketsPipe
class TCPPipeSpawner:
def __init__(self, target_addr: IPv4Address, target_port: int):
self._target_addr = target_addr
self._target_port = target_port
def spawn_pipe(self, source: socket.socket) -> SocketsPipe:
dest = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
dest.connect((self._target_addr, self._target_port))
except socket.error as err:
source.close()
dest.close()
raise err
return SocketsPipe(source, dest, client_data_received=self._client_data_received)
def notify_client_data_received(self, callback: Callable[[bytes], bool]):
self._client_data_received = callback