| 1 | import argparse |
| 2 | import asyncio |
| 3 | import logging |
| 4 | import ssl |
| 5 | from typing import Optional, cast |
| 6 | |
| 7 | from aioquic.asyncio.client import connect |
| 8 | from aioquic.asyncio.protocol import QuicConnectionProtocol |
| 9 | from aioquic.quic.configuration import QuicConfiguration |
| 10 | from aioquic.quic.events import DatagramFrameReceived, QuicEvent |
| 11 | from aioquic.quic.logger import QuicFileLogger |
| 12 | |
| 13 | logger = logging.getLogger("client") |
| 14 | |
| 15 | |
| 16 | class SiduckClient(QuicConnectionProtocol): |
| 17 | def __init__(self, *args, **kwargs) -> None: |
| 18 | super().__init__(*args, **kwargs) |
| 19 | self._ack_waiter: Optional[asyncio.Future[None]] = None |
| 20 | |
| 21 | async def quack(self) -> None: |
| 22 | assert self._ack_waiter is None, "Only one quack at a time." |
| 23 | self._quic.send_datagram_frame(b"quack") |
| 24 | |
| 25 | waiter = self._loop.create_future() |
| 26 | self._ack_waiter = waiter |
| 27 | self.transmit() |
| 28 | |
| 29 | return await asyncio.shield(waiter) |
| 30 | |
| 31 | def quic_event_received(self, event: QuicEvent) -> None: |
| 32 | if self._ack_waiter is not None: |
| 33 | if isinstance(event, DatagramFrameReceived) and event.data == b"quack-ack": |
| 34 | waiter = self._ack_waiter |
| 35 | self._ack_waiter = None |
| 36 | waiter.set_result(None) |
| 37 | |
| 38 | |
| 39 | async def main(configuration: QuicConfiguration, host: str, port: int) -> None: |
| 40 | async with connect( |
| 41 | host, port, configuration=configuration, create_protocol=SiduckClient |
| 42 | ) as client: |
| 43 | client = cast(SiduckClient, client) |
| 44 | logger.info("sending quack") |
| 45 | await client.quack() |
| 46 | logger.info("received quack-ack") |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | parser = argparse.ArgumentParser(description="SiDUCK client") |
| 51 | parser.add_argument( |
| 52 | "host", type=str, help="The remote peer's host name or IP address" |
| 53 | ) |
| 54 | parser.add_argument("port", type=int, help="The remote peer's port number") |
| 55 | parser.add_argument( |
| 56 | "-k", |
| 57 | "--insecure", |
| 58 | action="store_true", |
| 59 | help="do not validate server certificate", |
| 60 | ) |
| 61 | parser.add_argument( |
| 62 | "-q", |
| 63 | "--quic-log", |
| 64 | type=str, |
| 65 | help="log QUIC events to QLOG files in the specified directory", |
| 66 | ) |
| 67 | parser.add_argument( |
| 68 | "-l", |
| 69 | "--secrets-log", |
| 70 | type=str, |
| 71 | help="log secrets to a file, for use with Wireshark", |
| 72 | ) |
| 73 | parser.add_argument( |
| 74 | "-v", "--verbose", action="store_true", help="increase logging verbosity" |
| 75 | ) |
| 76 | |
| 77 | args = parser.parse_args() |
| 78 | |
| 79 | logging.basicConfig( |
| 80 | format="%(asctime)s %(levelname)s %(name)s %(message)s", |
| 81 | level=logging.DEBUG if args.verbose else logging.INFO, |
| 82 | ) |
| 83 | |
| 84 | configuration = QuicConfiguration( |
| 85 | alpn_protocols=["siduck"], is_client=True, max_datagram_frame_size=65536 |
| 86 | ) |
| 87 | if args.insecure: |
| 88 | configuration.verify_mode = ssl.CERT_NONE |
| 89 | if args.quic_log: |
| 90 | configuration.quic_logger = QuicFileLogger(args.quic_log) |
| 91 | if args.secrets_log: |
| 92 | configuration.secrets_log_file = open(args.secrets_log, "a") |
| 93 | |
| 94 | asyncio.run( |
| 95 | main( |
| 96 | configuration=configuration, |
| 97 | host=args.host, |
| 98 | port=args.port, |
| 99 | ) |
| 100 | ) |