| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from functools import partial |
| 4 |
from typing import Awaitable, Callable, Dict, Optional, Tuple |
| 5 |
|
| 6 |
from aioquic.buffer import Buffer |
| 7 |
from aioquic.h3.connection import H3_ALPN |
| 8 |
from aioquic.quic.configuration import QuicConfiguration |
| 9 |
from aioquic.quic.connection import QuicConnection |
| 10 |
from aioquic.quic.events import ( |
| 11 |
ConnectionIdIssued, |
| 12 |
ConnectionIdRetired, |
| 13 |
ConnectionTerminated, |
| 14 |
ProtocolNegotiated, |
| 15 |
) |
| 16 |
from aioquic.quic.packet import ( |
| 17 |
encode_quic_version_negotiation, |
| 18 |
PACKET_TYPE_INITIAL, |
| 19 |
pull_quic_header, |
| 20 |
) |
| 21 |
|
| 22 |
from .h3 import H3Protocol |
| 23 |
from ..config import Config |
| 24 |
from ..events import Closed, Event, RawData |
| 25 |
from ..typing import AppWrapper, TaskGroup, WorkerContext |
| 26 |
|
| 27 |
|
| 28 |
class QuicProtocol: |
| 29 |
def __init__( |
| 30 |
self, |
| 31 |
app: AppWrapper, |
| 32 |
config: Config, |
| 33 |
context: WorkerContext, |
| 34 |
task_group: TaskGroup, |
| 35 |
server: Optional[Tuple[str, int]], |
| 36 |
send: Callable[[Event], Awaitable[None]], |
| 37 |
) -> None: |
| 38 |
self.app = app |
| 39 |
self.config = config |
| 40 |
self.context = context |
| 41 |
self.connections: Dict[bytes, QuicConnection] = {} |
| 42 |
self.http_connections: Dict[QuicConnection, H3Protocol] = {} |
| 43 |
self.send = send |
| 44 |
self.server = server |
| 45 |
self.task_group = task_group |
| 46 |
|
| 47 |
self.quic_config = QuicConfiguration(alpn_protocols=H3_ALPN, is_client=False) |
| 48 |
self.quic_config.load_cert_chain(certfile=config.certfile, keyfile=config.keyfile) |
| 49 |
|
| 50 |
@property |
| 51 |
def idle(self) -> bool: |
| 52 |
return len(self.connections) == 0 and len(self.http_connections) == 0 |
| 53 |
|
| 54 |
async def handle(self, event: Event) -> None: |
| 55 |
if isinstance(event, RawData): |
| 56 |
try: |
| 57 |
header = pull_quic_header(Buffer(data=event.data), host_cid_length=8) |
| 58 |
except ValueError: |
| 59 |
return |
| 60 |
if ( |
| 61 |
header.version is not None |
| 62 |
and header.version not in self.quic_config.supported_versions |
| 63 |
): |
| 64 |
data = encode_quic_version_negotiation( |
| 65 |
source_cid=header.destination_cid, |
| 66 |
destination_cid=header.source_cid, |
| 67 |
supported_versions=self.quic_config.supported_versions, |
| 68 |
) |
| 69 |
await self.send(RawData(data=data, address=event.address)) |
| 70 |
return |
| 71 |
|
| 72 |
connection = self.connections.get(header.destination_cid) |
| 73 |
if ( |
| 74 |
connection is None |
| 75 |
and len(event.data) >= 1200 |
| 76 |
and header.packet_type == PACKET_TYPE_INITIAL |
| 77 |
and not self.context.terminated.is_set() |
| 78 |
): |
| 79 |
connection = QuicConnection( |
| 80 |
configuration=self.quic_config, |
| 81 |
original_destination_connection_id=header.destination_cid, |
| 82 |
) |
| 83 |
self.connections[header.destination_cid] = connection |
| 84 |
self.connections[connection.host_cid] = connection |
| 85 |
|
| 86 |
if connection is not None: |
| 87 |
connection.receive_datagram(event.data, event.address, now=self.context.time()) |
| 88 |
await self._handle_events(connection, event.address) |
| 89 |
elif isinstance(event, Closed): |
| 90 |
pass |
| 91 |
|
| 92 |
async def send_all(self, connection: QuicConnection) -> None: |
| 93 |
for data, address in connection.datagrams_to_send(now=self.context.time()): |
| 94 |
await self.send(RawData(data=data, address=address)) |
| 95 |
|
| 96 |
async def _handle_events( |
| 97 |
self, connection: QuicConnection, client: Optional[Tuple[str, int]] = None |
| 98 |
) -> None: |
| 99 |
event = connection.next_event() |
| 100 |
while event is not None: |
| 101 |
if isinstance(event, ConnectionTerminated): |
| 102 |
pass |
| 103 |
elif isinstance(event, ProtocolNegotiated): |
| 104 |
self.http_connections[connection] = H3Protocol( |
| 105 |
self.app, |
| 106 |
self.config, |
| 107 |
self.context, |
| 108 |
self.task_group, |
| 109 |
client, |
| 110 |
self.server, |
| 111 |
connection, |
| 112 |
partial(self.send_all, connection), |
| 113 |
) |
| 114 |
elif isinstance(event, ConnectionIdIssued): |
| 115 |
self.connections[event.connection_id] = connection |
| 116 |
elif isinstance(event, ConnectionIdRetired): |
| 117 |
del self.connections[event.connection_id] |
| 118 |
|
| 119 |
if connection in self.http_connections: |
| 120 |
await self.http_connections[connection].handle(event) |
| 121 |
|
| 122 |
event = connection.next_event() |
| 123 |
|
| 124 |
await self.send_all(connection) |
| 125 |
|
| 126 |
timer = connection.get_timer() |
| 127 |
if timer is not None: |
| 128 |
self.task_group.spawn(self._handle_timer, timer, connection) |
| 129 |
|
| 130 |
async def _handle_timer(self, timer: float, connection: QuicConnection) -> None: |
| 131 |
wait = max(0, timer - self.context.time()) |
| 132 |
await self.context.sleep(wait) |
| 133 |
if connection._close_at is not None: |
| 134 |
connection.handle_timer(now=self.context.time()) |
| 135 |
await self._handle_events(connection, None) |