Pure MoonBit authoritative DNS server (RFC 1035) with robust compression pointer defense.
Dependencies
纯 MoonBit 实现的权威 DNS 服务器 (RFC 1035 Authoritative DNS Server) 零外部 FFI 依赖 | 恶意压缩指针防御 (无 Panic/死循环) | 严格权威语义 (AA=1/NXDOMAIN/NODATA) | UDP & TCP 双传输 | 内置迷你 dig
+-------------------+
| moondns CLI |
| (serve / query) |
+---------+---------+
|
+----------------------+----------------------+
| |
v v
+-------------------+ +-------------------+
| UDP Transport | | TCP Transport |
| (async / socket) | | (async / socket) |
+---------+---------+ +---------+---------+
| |
+----------------------+----------------------+
| (Raw Request Bytes)
v
+-------------------------+
| transport/pipeline | (Memory Pipeline)
| handle_raw_query |
+------------+------------+
|
v
+-------------------------+
| moondns/codec |
| parse / encode Message |
+------------+------------+
| (Parsed Query Message)
v
+-------------------------+
| moondns/resolver | <---+ (Loads Zone File)
| build_response (Pure) | |
+------------+------------+ +--+--------------+
| | moondns/zone |
v | Zone & Lookup |
(Authoritative Response) +-----------------+# 构建项目 (无警告、无错误)
moon build --target native
# 运行完整 8 组规范测试集 (60 个测试用例全部通过)
moon testpowershell -ExecutionPolicy Bypass -File examples/demo.ps1chmod +x examples/demo.sh
./examples/demo.sh# 以 1053 端口启动权威 DNS 服务器,加载示例 Zone 文件
moon run cmd/main -- serve --zone examples/example.zone --port 1053
# 支持同时监听 TCP 端口
moon run cmd/main -- serve --zone examples/example.zone --port 1053 --tcp# 1. 查询 Apex A 记录
moon run cmd/main -- query --name example.com --type A --port 1053
# 2. 查询 CNAME 别名记录 (自动展示 CNAME 与跟随的 A 记录)
moon run cmd/main -- query --name www.example.com --type A --port 1053
# 3. 查询不存在的域名 (展示 NXDOMAIN 状态与 Authority 节 SOA 负缓存)
moon run cmd/main -- query --name ghost.example.com --type A --port 1053
# 4. 查询多字符串 TXT 记录
moon run cmd/main -- query --name text.example.com --type TXT --port 1053
# 5. 查询 AAAA IPv6 记录
moon run cmd/main -- query --name ns1.example.com --type AAAA --port 1053
# 6. 使用 TCP 查询
moon run cmd/main -- query --name example.com --type A --port 1053 --tcpdig @127.0.0.1 -p 1053 example.com A
dig @127.0.0.1 -p 1053 www.example.com A
dig @127.0.0.1 -p 1053 nonexistent.example.com ATotal tests: 60, passed: 60, failed: 0. (通过率: 100%)| # | 测试套件 | 对应源文件 | 测试范围与 RFC 规范依据 | 测试用例数 | 状态 |
|---|---|---|---|---|---|
| 1 | 编解码黄金测试 | tests/suite1_golden_test.mbt | RFC 1035 §4.1: 多组手工构造的真实报文字节向量,执行 parse -> encode 逐字节完全等价断言 | 3 | ✅ 通过 |
| 2 | 压缩指针解析 | tests/suite2_pointer_test.mbt | RFC 1035 §4.1.4: 共享后缀域名解析、多跳跨指针链(Multi-hop chain)、根域名指针解析 | 3 | ✅ 通过 |
| 3 | 恶意指针防护 | tests/suite3_malicious_test.mbt | 指针自环(A->A)、互相循环(AB)、偏移越界、超长跳转深度(>128 跳)、截断报文、零长度输入、非法标签类型全部返回 Result::Err,绝不 panic/死循环 | 8 | ✅ 通过 |
| 4 | 域名大小写不敏感 | tests/suite4_case_test.mbt | RFC 1035 §2.3.3: 表驱动 ASCII 大小写等价测试,wWw.ExAmPlE.tEsT 与 www.example.test 查询返回相同记录 | 2 | ✅ 通过 |
| 5 | 各记录类型往返 | tests/suite5_records_test.mbt | RFC 1035 §3.2/§3.3, RFC 3596, RFC 6891: A, AAAA, CNAME, NS, MX, TXT (含多 character-string), SOA, OPT 逐类构造、编码、解析、字段全等验证 | 8 | ✅ 通过 |
| 6 | TC 截断机制 | tests/suite6_truncation_test.mbt | RFC 1035 §4.1.1, §4.2.1: UDP 报文超过 512 字节触发 TC=1 截断且长度 ≤512;TCP 及 EDNS0 (4096) 模式下保留完整报文 | 2 | ✅ 通过 |
| 7 | 权威语义正确性 | tests/suite7_semantics_test.mbt | RFC 1035 §4.1.1, RFC 2308: 正常命中 (AA=1, NOERROR)、NXDOMAIN (RCODE=3 + Authority SOA)、NODATA (RCODE=0 + Authority SOA)、未知类型、未知 Class、CNAME 链式追踪 | 6 | ✅ 通过 |
| 8 | 端到端测试 | tests/suite8_e2e_test.mbt | 纯内存端到端管道 handle_raw_query: Zone 文件加载 -> 真实请求线缆字节 -> 管道处理 -> 真实响应线缆字节 -> 客户端解包完整验证 | 3 | ✅ 通过 |
| + | 模块单元测试 | codec/, records/, zone/, resolver/ | 模块内部辅助功能(IP 解析器、Zone 文本标记化、通配符匹配、序列化) | 25 | ✅ 通过 |
| 总计 | 全项目测试集 | — | 覆盖 RFC 1035 所有核心规范与防护要求 | 60 | ✅ 100% |
$ moon run cmd/main -- serve --zone examples/example.zone --port 1053
[MoonDNS] Loaded zone for origin 'example.com' with 16 records.$ moon run cmd/main -- query --name example.com --type A --port 1053
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4951
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 3600 IN A 93.184.216.34
;; Query time: 2 msec
;; SERVER: 127.0.0.1:1053 (UDP)$ moon run cmd/main -- query --name www.example.com --type A --port 1053
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4951
;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.example.com. IN A
;; ANSWER SECTION:
www.example.com. 3600 IN CNAME example.com.
example.com. 3600 IN A 93.184.216.34
;; Query time: 4 msec
;; SERVER: 127.0.0.1:1053 (UDP)$ moon run cmd/main -- query --name ghost.example.com --type A --port 1053
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 4951
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;ghost.example.com. IN A
;; AUTHORITY SECTION:
example.com. 3600 IN SOA ns1.example.com. hostmaster.example.com. 2026092001 7200 3600 1209600 3600
;; Query time: 2 msec
;; SERVER: 127.0.0.1:1053 (UDP)fn domain_equal(a : String, b : String) -> Boolfn normalize_domain(name : String) -> Stringfn to_ascii_lower(s : String) -> StringInstall
Download zipPure MoonBit authoritative DNS server (RFC 1035) with robust compression pointer defense.
Dependencies