| 1 |
import argparse |
| 2 |
import asyncio |
| 3 |
import logging |
| 4 |
import pickle |
| 5 |
import ssl |
| 6 |
import struct |
| 7 |
from typing import Optional, cast |
| 8 |
|
| 9 |
from aioquic.asyncio.client import connect |
| 10 |
from aioquic.asyncio.protocol import QuicConnectionProtocol |
| 11 |
from aioquic.quic.configuration import QuicConfiguration |
| 12 |
from aioquic.quic.events import QuicEvent, StreamDataReceived |
| 13 |
from aioquic.quic.logger import QuicFileLogger |
| 14 |
from dnslib.dns import QTYPE, DNSHeader, DNSQuestion, DNSRecord |
| 15 |
|
| 16 |
logger = logging.getLogger("client") |
| 17 |
|
| 18 |
|
| 19 |
class DnsClientProtocol(QuicConnectionProtocol): |
| 20 |
def __init__(self, *args, **kwargs) -> None: |
| 21 |
super().__init__(*args, **kwargs) |
| 22 |
self._ack_waiter: Optional[asyncio.Future[DNSRecord]] = None |
| 23 |
|
| 24 |
async def query(self, query_name: str, query_type: str) -> DNSRecord: |
| 25 |
|
| 26 |
query = DNSRecord( |
| 27 |
header=DNSHeader(id=0), |
| 28 |
q=DNSQuestion(query_name, getattr(QTYPE, query_type)), |
| 29 |
) |
| 30 |
data = bytes(query.pack()) |
| 31 |
data = struct.pack("!H", len(data)) + data |
| 32 |
|
| 33 |
|
| 34 |
stream_id = self._quic.get_next_available_stream_id() |
| 35 |
self._quic.send_stream_data(stream_id, data, end_stream=True) |
| 36 |
waiter = self._loop.create_future() |
| 37 |
self._ack_waiter = waiter |
| 38 |
self.transmit() |
| 39 |
|
| 40 |
return await asyncio.shield(waiter) |
| 41 |
|
| 42 |
def quic_event_received(self, event: QuicEvent) -> None: |
| 43 |
if self._ack_waiter is not None: |
| 44 |
if isinstance(event, StreamDataReceived): |
| 45 |
|
| 46 |
length = struct.unpack("!H", bytes(event.data[:2]))[0] |
| 47 |
answer = DNSRecord.parse(event.data[2 : 2 + length]) |
| 48 |
|
| 49 |
|
| 50 |
waiter = self._ack_waiter |
| 51 |
self._ack_waiter = None |
| 52 |
waiter.set_result(answer) |
| 53 |
|
| 54 |
|
| 55 |
def save_session_ticket(ticket): |
| 56 |
""" |
| 57 |
Callback which is invoked by the TLS engine when a new session ticket |
| 58 |
is received. |
| 59 |
""" |
| 60 |
logger.info("New session ticket received") |
| 61 |
if args.session_ticket: |
| 62 |
with open(args.session_ticket, "wb") as fp: |
| 63 |
pickle.dump(ticket, fp) |
| 64 |
|
| 65 |
|
| 66 |
async def main( |
| 67 |
configuration: QuicConfiguration, |
| 68 |
host: str, |
| 69 |
port: int, |
| 70 |
query_name: str, |
| 71 |
query_type: str, |
| 72 |
) -> None: |
| 73 |
logger.debug(f"Connecting to {host}:{port}") |
| 74 |
async with connect( |
| 75 |
host, |
| 76 |
port, |
| 77 |
configuration=configuration, |
| 78 |
session_ticket_handler=save_session_ticket, |
| 79 |
create_protocol=DnsClientProtocol, |
| 80 |
) as client: |
| 81 |
client = cast(DnsClientProtocol, client) |
| 82 |
logger.debug("Sending DNS query") |
| 83 |
answer = await client.query(query_name, query_type) |
| 84 |
logger.info("Received DNS answer\n%s" % answer) |
| 85 |
|
| 86 |
|
| 87 |
if __name__ == "__main__": |
| 88 |
parser = argparse.ArgumentParser(description="DNS over QUIC client") |
| 89 |
parser.add_argument( |
| 90 |
"--host", |
| 91 |
type=str, |
| 92 |
default="localhost", |
| 93 |
help="The remote peer's host name or IP address", |
| 94 |
) |
| 95 |
parser.add_argument( |
| 96 |
"--port", type=int, default=853, help="The remote peer's port number" |
| 97 |
) |
| 98 |
parser.add_argument( |
| 99 |
"-k", |
| 100 |
"--insecure", |
| 101 |
action="store_true", |
| 102 |
help="do not validate server certificate", |
| 103 |
) |
| 104 |
parser.add_argument( |
| 105 |
"--ca-certs", type=str, help="load CA certificates from the specified file" |
| 106 |
) |
| 107 |
parser.add_argument("--query-name", required=True, help="Domain to query") |
| 108 |
parser.add_argument("--query-type", default="A", help="The DNS query type to send") |
| 109 |
parser.add_argument( |
| 110 |
"-q", |
| 111 |
"--quic-log", |
| 112 |
type=str, |
| 113 |
help="log QUIC events to QLOG files in the specified directory", |
| 114 |
) |
| 115 |
parser.add_argument( |
| 116 |
"-l", |
| 117 |
"--secrets-log", |
| 118 |
type=str, |
| 119 |
help="log secrets to a file, for use with Wireshark", |
| 120 |
) |
| 121 |
parser.add_argument( |
| 122 |
"-s", |
| 123 |
"--session-ticket", |
| 124 |
type=str, |
| 125 |
help="read and write session ticket from the specified file", |
| 126 |
) |
| 127 |
parser.add_argument( |
| 128 |
"-v", "--verbose", action="store_true", help="increase logging verbosity" |
| 129 |
) |
| 130 |
|
| 131 |
args = parser.parse_args() |
| 132 |
|
| 133 |
logging.basicConfig( |
| 134 |
format="%(asctime)s %(levelname)s %(name)s %(message)s", |
| 135 |
level=logging.DEBUG if args.verbose else logging.INFO, |
| 136 |
) |
| 137 |
|
| 138 |
configuration = QuicConfiguration(alpn_protocols=["doq"], is_client=True) |
| 139 |
if args.ca_certs: |
| 140 |
configuration.load_verify_locations(args.ca_certs) |
| 141 |
if args.insecure: |
| 142 |
configuration.verify_mode = ssl.CERT_NONE |
| 143 |
if args.quic_log: |
| 144 |
configuration.quic_logger = QuicFileLogger(args.quic_log) |
| 145 |
if args.secrets_log: |
| 146 |
configuration.secrets_log_file = open(args.secrets_log, "a") |
| 147 |
if args.session_ticket: |
| 148 |
try: |
| 149 |
with open(args.session_ticket, "rb") as fp: |
| 150 |
configuration.session_ticket = pickle.load(fp) |
| 151 |
except FileNotFoundError: |
| 152 |
logger.debug(f"Unable to read {args.session_ticket}") |
| 153 |
pass |
| 154 |
else: |
| 155 |
logger.debug("No session ticket defined...") |
| 156 |
|
| 157 |
asyncio.run( |
| 158 |
main( |
| 159 |
configuration=configuration, |
| 160 |
host=args.host, |
| 161 |
port=args.port, |
| 162 |
query_name=args.query_name, |
| 163 |
query_type=args.query_type, |
| 164 |
) |
| 165 |
) |