Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
client.writer.write(crypto.encrypt(token))
await client.writer.drain()
await asyncio.sleep(0.1)
assert init_task.exception() is None
assert init_task.done()
assert peer.is_ready
assert peer.is_connected
ping_task = loop.create_task(peer.multiplexer.ping())
await asyncio.sleep(0.1)
ping_data = await client.reader.read(1024)
ping = crypto.decrypt(ping_data)
assert ping[16] == CHANNEL_FLOW_PING
assert int.from_bytes(ping[17:21], "big") == 0
assert ping[21:25] == b"ping"
ping_task.cancel()
client.writer.close()
client.close.set()
await asyncio.sleep(0.1)
assert peer.multiplexer.wait().done()
async def test_multiplexer_ping_error(loop, test_server, multiplexer_client):
"""Test a ping between peers."""
from snitun.multiplexer import core as multi_core
multi_core.PEER_TCP_TIMEOUT = 0.2
client = test_server[0]
ping_task = loop.create_task(multiplexer_client.ping())
await asyncio.sleep(0.3)
data = await client.reader.read(60)
data = multiplexer_client._crypto.decrypt(data)
assert data[16] == CHANNEL_FLOW_PING
assert int.from_bytes(data[17:21], "big") == 0
assert data[21:25] == b"ping"
assert ping_task.done()
with pytest.raises(MultiplexerTransportError):
raise ping_task.exception()
multi_core.PEER_TCP_TIMEOUT = 90
async def ping(self):
"""Send a ping flow message to hold the connection open."""
self._healthy.clear()
try:
self._write_message(
MultiplexerMessage(uuid.uuid4(), CHANNEL_FLOW_PING, b"", b"ping")
)
# Wait until pong is received
async with async_timeout.timeout(PEER_TCP_TIMEOUT):
await self._healthy.wait()
except (OSError, asyncio.TimeoutError):
_LOGGER.error("Ping fails, no response from peer")
self._loop.call_soon(self.shutdown)
raise MultiplexerTransportError() from None
throttling=self._throttling,
)
self._channels[channel.uuid] = channel
self._loop.create_task(self._new_connections(self, channel))
# Close
elif message.flow_type == CHANNEL_FLOW_CLOSE:
# check if message exists
if message.channel_id not in self._channels:
_LOGGER.debug("Receive close from unknown channel")
return
channel = self._channels.pop(message.channel_id)
channel.close()
# Ping
elif message.flow_type == CHANNEL_FLOW_PING:
if message.extra.startswith(b"pong"):
_LOGGER.debug("Receive pong from peer / reset healthy")
self._healthy.set()
else:
_LOGGER.debug("Receive ping from peer / send pong")
self._write_message(
MultiplexerMessage(
message.channel_id, CHANNEL_FLOW_PING, b"", b"pong"
)
)
else:
_LOGGER.warning("Receive unknown message type")