idoh : Fast and secure DNS over HTTPS resolution
idoh is a lightweight, high-performance Rust library for DNS over HTTPS (DoH) resolution. It concurrently queries multiple DoH providers and returns the fastest response, ensuring both speed and reliability.
Table of Contents
Features
- Concurrent Resolution: Queries multiple DoH providers (Google, Cloudflare, Tencent, etc.) simultaneously.
- Fastest Response Wins: Returns the result from the first provider to respond successfully.
- Robust Error Handling: Automatically handles failures from individual providers without compromising the overall resolution process.
- Simple API: Easy-to-use
resolvefunction for common DNS record types. - Typed MX Records: Optional
mxfeature provides structured MX record parsing. - Customizable: Supports custom extraction logic for DNS answers.
Usage
Add idoh to your Cargo.toml:
[]
= "0.1.7"
Basic Usage
Use the generic resolve function for any DNS record type:
use ;
async
Using MX Feature
Enable the mx feature for structured MX record parsing:
[]
= { = "0.1.7", = ["mx"] }
use ;
async
Design
The core design philosophy of idoh is speed through concurrency.
- Task Spawning: When
resolveis called, it spawns a background task. - Staggered Concurrency: The task iterates through a predefined list of high-quality DoH providers (
DOH_LI). It spawns a sub-task for a provider every 500ms. This strategy balances speed and resource usage: if the first provider is fast, we don't waste resources querying others. - Race to Finish: A bounded channel (
crossfire::mpsc::bounded_async) acting as a message queue with a capacity of 1 is used to collect the result. The first successful response wins and is sent to the channel. - Cancellation: Once a result is received, the main
resolvefunction returns, and the background tasks are aborted usingdefer-liteto clean up resources.
Flowchart
graph TD
A[User calls resolve] --> B{Spawn Manager Task};
B --> C[Provider 1];
C -- Wait 500ms --> D[Provider 2];
D -- Wait 500ms --> E[Provider ...];
C -- Query --> F[DoH Server 1];
D -- Query --> G[DoH Server 2];
E -- Query --> H[DoH Server ...];
F -- Response --> I{Channel};
G -- Response --> I;
H -- Response --> I;
I -- First Success --> J[Return Result];
J --> K[Abort Pending Tasks];
Tech Stack
- tokio: Asynchronous runtime for executing concurrent tasks.
- ireq: Simple and efficient HTTP client for making DoH requests.
- sonic-rs: High-performance JSON parsing for processing DNS responses.
- crossfire: High-performance channels for task communication.
Directory Structure
src/lib.rs: The main entry point. Exports the public API and modules.src/resolve.rs: Contains the coreresolvelogic and the list of DoH providers (DOH_LI).src/post.rs: Handles the HTTP GET requests to DoH providers and defines theAnswerstruct.src/record_type.rs: Defines constants for common DNS record types (e.g.,A,MX,TXT).src/mx.rs: (Optional, requiresmxfeature) Provides themxfunction andMxstruct for structured MX record queries.
API Reference
resolve
pub async
name: The domain name to resolve.record_type: The DNS record type (e.g., "A", "AAAA", "MX", "TXT").extract: A closure to process the list ofAnswers and return the desired resultT.
mx (requires mx feature)
pub async
Queries MX records for a domain and returns structured results.
domain: The domain name to query.- Returns: A vector of
Mxstructs, sorted by the DNS server (not by priority).
Mx Struct
Represents a mail exchange record:
priority: Mail server priority (lower values indicate higher priority).server: Mail server hostname (trailing dots are automatically removed).ttl: Time to live in seconds.
Answer Struct
Represents a single DNS record returned by the DoH provider.
record_type Module
Contains constants for DNS record types:
A(1) - IPv4 addressNS(2) - Name serverCNAME(5) - Canonical nameSOA(6) - Start of authorityPTR(12) - Pointer recordMX(15) - Mail exchangeTXT(16) - Text recordAAAA(28) - IPv6 addressSRV(33) - Service locatorANY(255) - Any record type
History: The Rise of DoH
DNS over HTTPS (DoH) was introduced to address the privacy and security vulnerabilities of traditional DNS. Traditional DNS queries are sent in plaintext, allowing anyone on the network path to see which websites a user is visiting.
- 2018: The IETF standardized DoH as RFC 8484.
- Adoption: Major browsers like Firefox and Chrome began supporting DoH to protect user privacy.
- Impact: DoH encrypts DNS traffic, preventing eavesdropping and manipulation, making the internet safer for everyone.
idoh builds on this legacy by providing a tool to easily integrate secure and fast DNS resolution into Rust applications.
About
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
idoh : 极速安全的 DNS over HTTPS 解析库
idoh 是一个轻量级、高性能的 Rust 库,用于 DNS over HTTPS (DoH) 解析。它并发查询多个 DoH 提供商并返回最快的响应,确保速度和可靠性。
目录
功能特性
- 并发解析:同时查询多个 DoH 提供商(Google, Cloudflare, 腾讯等)。
- 极速响应:返回首个成功响应的提供商结果,唯快不破。
- 健壮容错:自动处理单个提供商的失败,不影响整体解析流程。
- API 简洁:提供简单易用的
resolve函数,满足常见 DNS 记录类型查询。 - 类型化 MX 记录:可选的
mx特性提供结构化的 MX 记录解析。 - 高度定制:支持自定义 DNS 结果提取逻辑。
使用指南
在 Cargo.toml 中添加 idoh:
[]
= "0.1.7"
基础用法
使用通用的 resolve 函数查询任意 DNS 记录类型:
use ;
async
使用 MX 特性
启用 mx 特性以获得结构化的 MX 记录解析:
[]
= { = "0.1.7", = ["mx"] }
use ;
async
设计思路
idoh 的核心设计理念是 通过并发实现极速。
- 任务生成:调用
resolve时,会启动一个后台任务。 - 交错并发:任务遍历预定义的优质 DoH 提供商列表 (
DOH_LI),每隔 500毫秒 启动一个新的查询子任务。这种策略在速度和资源之间取得了平衡:如果第一个提供商响应够快,就不必浪费资源查询后续提供商。 - 竞速机制:使用容量为 1 的有界通道 (
crossfire::mpsc::bounded_async) 作为消息队列来收集结果。谁先成功响应,谁的结果就会被写入通道并返回。 - 自动清理:一旦收到结果,主
resolve函数返回,并通过defer-lite自动中止剩余的后台任务,释放资源。
流程图
graph TD
A[用户调用 resolve] --> B{启动管理任务};
B --> C[提供商 1];
C -- 等待 500ms --> D[提供商 2];
D -- 等待 500ms --> E[提供商 ...];
C -- 查询 --> F[DoH 服务器 1];
D -- 查询 --> G[DoH 服务器 2];
E -- 查询 --> H[DoH 服务器 ...];
F -- 响应 --> I{通道};
G -- 响应 --> I;
H -- 响应 --> I;
I -- 首个成功 --> J[返回结果];
J --> K[中止待处理任务];
技术栈
- tokio:用于执行并发任务的异步运行时。
- ireq:简单高效的 HTTP 客户端,用于发送 DoH 请求。
- sonic-rs:高性能 JSON 解析库,用于处理 DNS 响应。
- crossfire:高性能通道,用于任务间通信。
目录结构
src/lib.rs:库入口,导出公共 API 和模块。src/resolve.rs:包含核心resolve逻辑和 DoH 提供商列表 (DOH_LI)。src/post.rs:处理向 DoH 提供商发送 HTTP GET 请求,定义Answer结构体。src/record_type.rs:定义常用 DNS 记录类型的常量(如A,MX,TXT)。src/mx.rs:(可选,需要mx特性)提供mx函数和Mx结构体,用于结构化 MX 记录查询。
API 参考
resolve
pub async
name: 要解析的域名。record_type: DNS 记录类型(如 "A", "AAAA", "MX", "TXT")。extract: 处理Answer列表并返回所需结果T的闭包。
mx(需要 mx 特性)
pub async
查询域名的 MX 记录并返回结构化结果。
domain: 要查询的域名。- 返回值:
Mx结构体的向量,按 DNS 服务器返回顺序排序(不按优先级排序)。
Mx 结构体
表示邮件交换记录:
priority: 邮件服务器优先级(数值越小优先级越高)。server: 邮件服务器主机名(自动移除末尾的点号)。ttl: 生存时间(秒)。
Answer 结构体
表示 DoH 提供商返回的单条 DNS 记录。
record_type 模块
包含 DNS 记录类型的常量:
A(1) - IPv4 地址NS(2) - 名称服务器CNAME(5) - 规范名称SOA(6) - 授权起始PTR(12) - 指针记录MX(15) - 邮件交换TXT(16) - 文本记录AAAA(28) - IPv6 地址SRV(33) - 服务定位ANY(255) - 任意记录类型
历史背景:DoH 的崛起
DNS over HTTPS (DoH) 的引入是为了解决传统 DNS 的隐私和安全漏洞。传统 DNS 查询以明文形式发送,允许网络路径上的任何人查看用户正在访问的网站。
- 2018年:IETF 将 DoH 标准化为 RFC 8484。
- 普及:Firefox 和 Chrome 等主流浏览器开始支持 DoH 以保护用户隐私。
- 影响:DoH 加密了 DNS 流量,防止窃听和篡改,让互联网对每个人都更安全。
idoh 建立在这一基础之上,为 Rust 应用提供了一种简单、快速且安全的 DNS 解析集成方案。
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: