| 1 |
import argparse |
| 2 |
import asyncio |
| 3 |
import logging |
| 4 |
import struct |
| 5 |
from typing import Optional |
| 6 |
|
| 7 |
from aioquic.asyncio import QuicConnectionProtocol, serve |
| 8 |
from aioquic.quic.configuration import QuicConfiguration |
| 9 |
from aioquic.quic.events import QuicEvent, StreamDataReceived |
| 10 |
from aioquic.quic.logger import QuicFileLogger |
| 11 |
from aioquic.tls import SessionTicket |
| 12 |
from dnslib.dns import DNSRecord |
| 13 |
|
| 14 |
|
| 15 |
class DnsServerProtocol(QuicConnectionProtocol): |
| 16 |
def quic_event_received(self, event: QuicEvent): |
| 17 |
if isinstance(event, StreamDataReceived): |
| 18 |
|
| 19 |
length = struct.unpack("!H", bytes(event.data[:2]))[0] |
| 20 |
query = DNSRecord.parse(event.data[2 : 2 + length]) |
| 21 |
|
| 22 |
|
| 23 |
data = query.send(args.resolver, 53) |
| 24 |
data = struct.pack("!H", len(data)) + data |
| 25 |
|
| 26 |
|
| 27 |
self._quic.send_stream_data(event.stream_id, data, end_stream=True) |
| 28 |
|
| 29 |
|
| 30 |
class SessionTicketStore: |
| 31 |
""" |
| 32 |
Simple in-memory store for session tickets. |
| 33 |
""" |
| 34 |
|
| 35 |
def __init__(self) -> None: |
| 36 |
self.tickets: dict[bytes, SessionTicket] = {} |
| 37 |
|
| 38 |
def add(self, ticket: SessionTicket) -> None: |
| 39 |
self.tickets[ticket.ticket] = ticket |
| 40 |
|
| 41 |
def pop(self, label: bytes) -> Optional[SessionTicket]: |
| 42 |
return self.tickets.pop(label, None) |
| 43 |
|
| 44 |
|
| 45 |
async def main( |
| 46 |
host: str, |
| 47 |
port: int, |
| 48 |
configuration: QuicConfiguration, |
| 49 |
session_ticket_store: SessionTicketStore, |
| 50 |
retry: bool, |
| 51 |
) -> None: |
| 52 |
await serve( |
| 53 |
host, |
| 54 |
port, |
| 55 |
configuration=configuration, |
| 56 |
create_protocol=DnsServerProtocol, |
| 57 |
session_ticket_fetcher=session_ticket_store.pop, |
| 58 |
session_ticket_handler=session_ticket_store.add, |
| 59 |
retry=retry, |
| 60 |
) |
| 61 |
await asyncio.Future() |
| 62 |
|
| 63 |
|
| 64 |
if __name__ == "__main__": |
| 65 |
parser = argparse.ArgumentParser(description="DNS over QUIC server") |
| 66 |
parser.add_argument( |
| 67 |
"--host", |
| 68 |
type=str, |
| 69 |
default="::", |
| 70 |
help="listen on the specified address (defaults to ::)", |
| 71 |
) |
| 72 |
parser.add_argument( |
| 73 |
"--port", |
| 74 |
type=int, |
| 75 |
default=853, |
| 76 |
help="listen on the specified port (defaults to 853)", |
| 77 |
) |
| 78 |
parser.add_argument( |
| 79 |
"-k", |
| 80 |
"--private-key", |
| 81 |
type=str, |
| 82 |
help="load the TLS private key from the specified file", |
| 83 |
) |
| 84 |
parser.add_argument( |
| 85 |
"-c", |
| 86 |
"--certificate", |
| 87 |
type=str, |
| 88 |
required=True, |
| 89 |
help="load the TLS certificate from the specified file", |
| 90 |
) |
| 91 |
parser.add_argument( |
| 92 |
"--resolver", |
| 93 |
type=str, |
| 94 |
default="8.8.8.8", |
| 95 |
help="Upstream Classic DNS resolver to use", |
| 96 |
) |
| 97 |
parser.add_argument( |
| 98 |
"--retry", |
| 99 |
action="store_true", |
| 100 |
help="send a retry for new connections", |
| 101 |
) |
| 102 |
parser.add_argument( |
| 103 |
"-q", |
| 104 |
"--quic-log", |
| 105 |
type=str, |
| 106 |
help="log QUIC events to QLOG files in the specified directory", |
| 107 |
) |
| 108 |
parser.add_argument( |
| 109 |
"-v", "--verbose", action="store_true", help="increase logging verbosity" |
| 110 |
) |
| 111 |
|
| 112 |
args = parser.parse_args() |
| 113 |
|
| 114 |
logging.basicConfig( |
| 115 |
format="%(asctime)s %(levelname)s %(name)s %(message)s", |
| 116 |
level=logging.DEBUG if args.verbose else logging.INFO, |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
if args.quic_log: |
| 121 |
quic_logger = QuicFileLogger(args.quic_log) |
| 122 |
else: |
| 123 |
quic_logger = None |
| 124 |
|
| 125 |
configuration = QuicConfiguration( |
| 126 |
alpn_protocols=["doq"], |
| 127 |
is_client=False, |
| 128 |
quic_logger=quic_logger, |
| 129 |
) |
| 130 |
|
| 131 |
configuration.load_cert_chain(args.certificate, args.private_key) |
| 132 |
|
| 133 |
try: |
| 134 |
asyncio.run( |
| 135 |
main( |
| 136 |
host=args.host, |
| 137 |
port=args.port, |
| 138 |
configuration=configuration, |
| 139 |
session_ticket_store=SessionTicketStore(), |
| 140 |
retry=args.retry, |
| 141 |
) |
| 142 |
) |
| 143 |
except KeyboardInterrupt: |
| 144 |
pass |