idoq 0.1.4

DNS over QUIC (DoQ) client / DNS over QUIC (DoQ) 客户端
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
[English]#en | [中文]#zh

---

<a id="en"></a>

# idoq : DNS over QUIC Client for Rust

Based on [idns](https://crates.io/crates/idns). See idns for `DnsRace`, `Parse` trait, caching, and more.

## Table of Contents

- [Features]#features
- [Installation]#installation
- [Usage]#usage
- [API Reference]#api-reference
- [Architecture]#architecture
- [Tech Stack]#tech-stack
- [Directory Structure]#directory-structure
- [History]#history

## Features

- RFC 9250 compliant DoQ implementation
- Built-in DoQ server list (AdGuard, ControlD, Alibaba DNS)
- Async/await with Tokio
- TLS 1.3 over QUIC
- A, AAAA, MX, TXT, NS, CNAME, PTR, SRV record types
- Zero-copy DNS message parsing
- Lazy-initialized TLS config
- Connection reuse with auto-reconnect

## Installation

```toml
[dependencies]
idoq = "0.1"
```

## Usage

Basic query:

```rust
use idoq::{Doq, DOQ_LI, QType};
use idns::Query;

#[tokio::main]
async fn main() {
  let client = Doq::new(DOQ_LI[0].clone());

  if let Ok(Some(answers)) = client.answer_li(QType::A, "example.com").await {
    for a in answers {
      println!("{} TTL={}", a.val, a.ttl);
    }
  }
}
```

Custom server:

```rust
use idoq::{Doq, host_ip, QType};
use idns::Query;

#[tokio::main]
async fn main() {
  let client = Doq::new(host_ip("dns.alidns.com", 223, 5, 5, 5));

  if let Ok(Some(answers)) = client.answer_li(QType::AAAA, "google.com").await {
    for a in answers {
      println!("{}", a.val);
    }
  }
}
```

MX records with Parse trait:

```rust
use idoq::{Doq, host_ip};
use idns::{Mx, Query};

#[tokio::main]
async fn main() {
  let client = Doq::new(host_ip("dns.alidns.com", 223, 5, 5, 5));

  if let Ok(Some(mx_list)) = Query::query::<Mx>(&client, "gmail.com").await {
    for mx in mx_list {
      println!("{} {} TTL={}", mx.priority, mx.server, mx.ttl);
    }
  }
}
```

Race multiple servers:

```rust
use idoq::{DOQ_LI, doq_li, QType};
use idns::{DnsRace, Query};

#[tokio::main]
async fn main() {
  let race = DnsRace::new(doq_li(DOQ_LI));

  if let Ok(Some(answers)) = race.answer_li(QType::A, "github.com").await {
    for a in answers {
      println!("{}", a.val);
    }
  }
}
```

## API Reference

### Types

#### `QType`

DNS query types (re-exported from `idns`):

| Type | Value | Description |
|------|-------|-------------|
| A | 1 | IPv4 address |
| NS | 2 | Name server |
| CNAME | 5 | Canonical name |
| PTR | 12 | Pointer record |
| MX | 15 | Mail exchange |
| TXT | 16 | Text record |
| AAAA | 28 | IPv6 address |
| SRV | 33 | Service record |

### Structs

#### `Doq`

DoQ client with connection reuse. Implements `idns::Query` trait.

```rust
pub struct Doq {
  pub server: HostIp,
}
```

Methods:
- `new(server: HostIp) -> Self`
- `query(&self, domain: &str, qtype: QType) -> Result<Option<Vec<Answer>>>`

#### `HostIp`

Server configuration.

```rust
pub struct HostIp {
  pub host: SmolStr,  // TLS SNI hostname
  pub ip: IpAddr,
}
```

### Functions

#### `host_ip`

```rust
pub const fn host_ip(host: &'static str, a: u8, b: u8, c: u8, d: u8) -> HostIp
```

#### `doq_li`

```rust
pub fn doq_li(li: &[HostIp]) -> Vec<Doq>
```

### Constants

#### `DOQ_LI`

Pre-configured DoQ servers:

| Server | IP |
|--------|-----|
| AdGuard DNS | 94.140.14.140, 94.140.14.141 |
| ControlD | 76.76.2.11 |
| Alibaba DNS | 223.5.5.5, 223.6.6.6 |

#### `site` module

- `site::ADGUARD` - `"unfiltered.adguard-dns.com"`
- `site::CONTROLD` - `"p0.freedns.controld.com"`
- `site::ALIDNS` - `"dns.alidns.com"`

## Architecture

```mermaid
graph TD
    A[Client] --> B[Doq.query]
    B --> C[Doq.conn]
    C --> D{Alive?}
    D -->|Yes| E[Reuse]
    D -->|No| F[Doq.dial]
    F --> G[QUIC Endpoint]
    G --> H[TLS Handshake]
    H --> I[Connection]
    E --> J[Doq.send]
    I --> J
    J --> K[parser.build]
    K --> L[Send]
    L --> M[Receive]
    M --> N[parser.parse]
    N --> O[Answers]
```

### Query Flow

1. `Doq.query()` - Entry point
2. `Doq.conn()` - Double-checked locking for connection reuse
3. `Doq.dial()` - QUIC endpoint with TLS 1.3, ALPN "doq"
4. `Doq.send()` - Bidirectional stream, 2-byte length prefix
5. `parser.build()` - DNS message (ID=0 per RFC 9250) with EDNS
6. `parser.parse()` - Extract answer records

### Implementation Details

- DNS message ID = 0 (RFC 9250)
- 2-byte length prefix for framing
- EDNS OPT with 4096 byte payload
- `LazyLock` for `ClientConfig`
- `RwLock<Option<Connection>>` for caching
- Auto-reconnect on error
- 7s timeout

## Tech Stack

| Component | Library |
|-----------|---------|
| QUIC | quinn |
| TLS | rustls + ring |
| Async | tokio |
| Buffer | bytes |
| Error | thiserror |

## Directory Structure

```
idoq/
├── src/
│   ├── lib.rs      # API, HostIp, DOQ_LI
│   ├── doq.rs      # Doq client
│   ├── parser.rs   # DNS build/parse
│   └── error.rs    # Error types
├── tests/
│   └── main.rs
└── Cargo.toml
```

## History

DNS over QUIC (DoQ) was standardized in RFC 9250 (May 2022), following DNS over HTTPS (DoH, RFC 8484, 2018) and DNS over TLS (DoT, RFC 7858, 2016).

QUIC, developed by Google in 2012, became RFC 9000 in 2021. It provides TLS 1.3 at transport layer with 0-RTT handshakes.

DoQ combines encrypted DNS privacy with QUIC performance: multiplexed streams prevent head-of-line blocking, connection migration handles network changes. Unlike DoH over HTTP/2 or HTTP/3, DoQ runs directly on QUIC with less overhead.

AdGuard deployed public DoQ servers in 2020, followed by Alibaba DNS and ControlD. Adoption grows as QUIC becomes the foundation for HTTP/3.

---

## About

This project is an open-source component of [js0.site ⋅ Refactoring the Internet Plan](https://js0.site).

We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:

* [Google Group]https://groups.google.com/g/js0-site
* [js0site.bsky.social]https://bsky.app/profile/js0site.bsky.social

---

<a id="zh"></a>

# idoq : Rust DNS over QUIC 客户端

基于 [idns](https://crates.io/crates/idns)。`DnsRace`、`Parse` trait、缓存等更多功能请查看 idns。

## 目录

- [特性]#特性
- [安装]#安装
- [使用]#使用
- [API 参考]#api-参考
- [架构]#架构
- [技术栈]#技术栈
- [目录结构]#目录结构
- [历史]#历史

## 特性

- 符合 RFC 9250 的 DoQ 实现
- 内置 DoQ 服务器列表 (AdGuard、ControlD、阿里 DNS)
- 基于 Tokio 异步
- TLS 1.3 + QUIC
- 支持 A、AAAA、MX、TXT、NS、CNAME、PTR、SRV 记录
- 零拷贝 DNS 消息解析
- 延迟初始化 TLS 配置
- 连接复用,自动重连

## 安装

```toml
[dependencies]
idoq = "0.1"
```

## 使用

基本查询:

```rust
use idoq::{Doq, DOQ_LI, QType};
use idns::Query;

#[tokio::main]
async fn main() {
  let client = Doq::new(DOQ_LI[0].clone());

  if let Ok(Some(answers)) = client.answer_li(QType::A, "example.com").await {
    for a in answers {
      println!("{} TTL={}", a.val, a.ttl);
    }
  }
}
```

自定义服务器:

```rust
use idoq::{Doq, host_ip, QType};
use idns::Query;

#[tokio::main]
async fn main() {
  let client = Doq::new(host_ip("dns.alidns.com", 223, 5, 5, 5));

  if let Ok(Some(answers)) = client.answer_li(QType::AAAA, "google.com").await {
    for a in answers {
      println!("{}", a.val);
    }
  }
}
```

使用 Parse trait 查询 MX 记录:

```rust
use idoq::{Doq, host_ip};
use idns::{Mx, Query};

#[tokio::main]
async fn main() {
  let client = Doq::new(host_ip("dns.alidns.com", 223, 5, 5, 5));

  if let Ok(Some(mx_list)) = Query::query::<Mx>(&client, "gmail.com").await {
    for mx in mx_list {
      println!("{} {} TTL={}", mx.priority, mx.server, mx.ttl);
    }
  }
}
```

竞速查询多个服务器:

```rust
use idoq::{DOQ_LI, doq_li, QType};
use idns::{DnsRace, Query};

#[tokio::main]
async fn main() {
  let race = DnsRace::new(doq_li(DOQ_LI));

  if let Ok(Some(answers)) = race.answer_li(QType::A, "github.com").await {
    for a in answers {
      println!("{}", a.val);
    }
  }
}
```

## API 参考

### 类型

#### `QType`

DNS 查询类型 (从 `idns` 重导出):

| 类型 || 说明 |
|------|-----|------|
| A | 1 | IPv4 地址 |
| NS | 2 | 域名服务器 |
| CNAME | 5 | 别名 |
| PTR | 12 | 指针记录 |
| MX | 15 | 邮件交换 |
| TXT | 16 | 文本记录 |
| AAAA | 28 | IPv6 地址 |
| SRV | 33 | 服务记录 |

### 结构体

#### `Doq`

DoQ 客户端,支持连接复用。实现 `idns::Query` trait。

```rust
pub struct Doq {
  pub server: HostIp,
}
```

方法:
- `new(server: HostIp) -> Self`
- `query(&self, domain: &str, qtype: QType) -> Result<Option<Vec<Answer>>>`

#### `HostIp`

服务器配置。

```rust
pub struct HostIp {
  pub host: SmolStr,  // TLS SNI 主机名
  pub ip: IpAddr,
}
```

### 函数

#### `host_ip`

```rust
pub const fn host_ip(host: &'static str, a: u8, b: u8, c: u8, d: u8) -> HostIp
```

#### `doq_li`

```rust
pub fn doq_li(li: &[HostIp]) -> Vec<Doq>
```

### 常量

#### `DOQ_LI`

预配置 DoQ 服务器:

| 服务器 | IP |
|--------|-----|
| AdGuard DNS | 94.140.14.140, 94.140.14.141 |
| ControlD | 76.76.2.11 |
| 阿里 DNS | 223.5.5.5, 223.6.6.6 |

#### `site` 模块

- `site::ADGUARD` - `"unfiltered.adguard-dns.com"`
- `site::CONTROLD` - `"p0.freedns.controld.com"`
- `site::ALIDNS` - `"dns.alidns.com"`

## 架构

```mermaid
graph TD
    A[客户端] --> B[Doq.query]
    B --> C[Doq.conn]
    C --> D{存活?}
    D -->|是| E[复用]
    D -->|否| F[Doq.dial]
    F --> G[QUIC Endpoint]
    G --> H[TLS 握手]
    H --> I[连接]
    E --> J[Doq.send]
    I --> J
    J --> K[parser.build]
    K --> L[发送]
    L --> M[接收]
    M --> N[parser.parse]
    N --> O[应答]
```

### 查询流程

1. `Doq.query()` - 入口
2. `Doq.conn()` - 双重检查锁定,复用连接
3. `Doq.dial()` - QUIC 端点,TLS 1.3,ALPN "doq"
4. `Doq.send()` - 双向流,2 字节长度前缀
5. `parser.build()` - DNS 消息 (ID=0,RFC 9250) + EDNS
6. `parser.parse()` - 提取应答记录

### 实现细节

- DNS 消息 ID = 0 (RFC 9250)
- 2 字节长度前缀分帧
- EDNS OPT 4096 字节负载
- `LazyLock` 延迟初始化 `ClientConfig`
- `RwLock<Option<Connection>>` 缓存连接
- 错误时自动重连
- 7 秒超时

## 技术栈

| 组件 ||
|------|-----|
| QUIC | quinn |
| TLS | rustls + ring |
| 异步 | tokio |
| 缓冲 | bytes |
| 错误 | thiserror |

## 目录结构

```
idoq/
├── src/
│   ├── lib.rs      # API、HostIp、DOQ_LI
│   ├── doq.rs      # Doq 客户端
│   ├── parser.rs   # DNS 构建/解析
│   └── error.rs    # 错误类型
├── tests/
│   └── main.rs
└── Cargo.toml
```

## 历史

DNS over QUIC (DoQ) 于 2022 年 5 月在 RFC 9250 中标准化,继 DNS over HTTPS (DoH, RFC 8484, 2018) 和 DNS over TLS (DoT, RFC 7858, 2016) 之后。

QUIC 由 Google 于 2012 年开发,2021 年成为 RFC 9000。它在传输层提供 TLS 1.3 加密,支持 0-RTT 握手。

DoQ 结合加密 DNS 的隐私优势与 QUIC 的性能优势:多路复用避免队头阻塞,连接迁移处理网络切换。与运行在 HTTP/2 或 HTTP/3 上的 DoH 不同,DoQ 直接运行在 QUIC 上,开销更低。

AdGuard 于 2020 年率先部署公共 DoQ 服务器,随后阿里 DNS、ControlD 跟进。随着 QUIC 成为 HTTP/3 基础,DoQ 采用率持续增长。

---

## 关于

本项目为 [js0.site ⋅ 重构互联网计划](https://js0.site) 的开源组件。

我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注:

* [谷歌邮件列表]https://groups.google.com/g/js0-site
* [js0site.bsky.social]https://bsky.app/profile/js0site.bsky.social