Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_init_client_peer_wait(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
valid = datetime.utcnow() + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert client.is_connected
async def test_fernet_token(peer_listener, peer_manager, test_endpoint):
"""Test fernet token created by server."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = server.generate_client_token(
FERNET_TOKENS, valid, hostname, aes_key, aes_iv
)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
async def test_close_client_peer(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer, test flow - close it."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = datetime.utcnow() + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
peer = peer_manager.get_peer("localhost")
async def test_init_client_peer(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
valid = datetime.utcnow() + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert client.is_connected
assert client._multiplexer._throttling is None
async def test_init_client_peer_invalid_token(
peer_listener, peer_manager, test_endpoint
):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = datetime.utcnow() + timedelta(days=-1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
with pytest.raises(SniTunConnectionError):
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert not peer_manager.peer_available("localhost")
async def test_fernet_token_date(peer_listener, peer_manager, test_endpoint):
"""Test fernet token created by server as invalid."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = timedelta(days=-1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = server.generate_client_token(
FERNET_TOKENS, valid, hostname, aes_key, aes_iv
)
with pytest.raises(SniTunConnectionError):
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
def __init__(
self,
runner: AppRunner,
context: ssl.SSLContext,
snitun_server: str,
snitun_port=None,
):
"""Initialize SniTunClient with aiohttp."""
self._connector = None
self._client = ClientPeer(snitun_server, snitun_port)
self._socket = socket.socket()
self._server_name = "{}:{}".format(snitun_server, snitun_port)
# Init interface
self._socket.setblocking(False)
self._socket.bind(("127.0.0.1", 0))
self._site = SockSite(runner, self._socket, ssl_context=context)