4个版本 (破坏性更新)

0.4.0 2022年1月10日
0.3.0 2022年1月9日
0.2.0 2022年1月8日
0.1.0 2021年6月7日

#2 in #idna

Download history 250/week @ 2024-03-13 122/week @ 2024-03-20 175/week @ 2024-03-27 108/week @ 2024-04-03 101/week @ 2024-04-10 97/week @ 2024-04-17 133/week @ 2024-04-24 149/week @ 2024-05-01 197/week @ 2024-05-08 150/week @ 2024-05-15 470/week @ 2024-05-22 221/week @ 2024-05-29 148/week @ 2024-06-05 191/week @ 2024-06-12 139/week @ 2024-06-19 84/week @ 2024-06-26

602 每月下载量
dns-ptr-resolver中使用

Apache-2.0

195KB
3.5K SLoC

Crates.io Documentation Build Status

rustdns

rustdns

rustdns是一个简单、快速且功能全面的DNS库,可以用于与域名服务进行高或低层次交互。

特性

用法(低级库)

use rustdns::Message;
use rustdns::types::*;
use std::net::UdpSocket;
use std::time::Duration;

fn udp_example() -> std::io::Result<()> {
    // A DNS Message can be easily constructed
    let mut m = Message::default();
    m.add_question("bramp.net", Type::A, Class::Internet);
    m.add_extension(Extension {   // Optionally add a EDNS extension
        payload_size: 4096,       // which supports a larger payload size.
        ..Default::default()
    });

    // Setup a UDP socket for sending to a DNS server.
    let socket = UdpSocket::bind("0.0.0.0:0")?;
    socket.set_read_timeout(Some(Duration::new(5, 0)))?;
    socket.connect("8.8.8.8:53")?; // Google's Public DNS Servers

    // Encode the DNS Message as a Vec<u8>.
    let question = m.to_vec()?;

    // Send to the server.
    socket.send(&question)?;

    // Wait for a response from the DNS server.
    let mut resp = [0; 4096];
    let len = socket.recv(&mut resp)?;

    // Take the response bytes and turn it into another DNS Message.
    let answer = Message::from_slice(&resp[0..len])?;

    // Now do something with `answer`, in this case print it!
    println!("DNS Response:\n{}", answer);

    Ok(())
}

如果成功,将打印出如下内容

;; ->>HEADER<<- opcode: Query, status: NoError, id: 44857
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
; bramp.net.              IN   A

; ANSWER SECTION:
bramp.net.            299 IN   A      104.21.62.200
bramp.net.            299 IN   A      172.67.138.196

特性

以下为可选功能

  • clients:启用以下客户端
    • doh:DNS over HTTPS (DoH) 客户端(rfc8484)。
    • json:DNS over HTTPS JSON 客户端
    • tcp:启用DNS over TCP客户端
    • udp:启用DNS over UDP客户端
  • zones:启用区域文件解析器

用法(命令行)

要使用演示CLI

$ cargo run -p dig -- A www.google.com
...
;; ->>HEADER<<- opcode: Query, status: NoError, id: 34327
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
; www.google.com.         IN   A

; ANSWER SECTION:
www.google.com.       110 IN   A      142.250.72.196

# More examples
$ cargo run -p dig -- AAAA www.google.com
$ cargo run -p dig -- ANY www.google.com
$ cargo run -p dig -- CNAME code.google.com
$ cargo run -p dig -- MX google.com
$ cargo run -p dig -- PTR 4.4.8.8.in-addr.arpa
$ cargo run -p dig -- SOA google.com
$ cargo run -p dig -- SRV _ldap._tcp.google.com
$ cargo run -p dig -- TXT google.com

测试

$ cargo test --all

# or the handy
$ cargo watch -- cargo test --all -- --nocapture

测试套件包含许多存储的真实示例,包括查询真实的DNS记录。这是通过以下命令生成的:cargo run -p generate_tests

模糊测试

该库已经进行了广泛的模糊测试。自己尝试一下吧

$ cargo fuzz run from_slice

测试数据

为了帮助测试功能,我设置了一系列预配置的记录

域名 描述
a.bramp.net 指向127.0.0.1的单个A记录
aaaa.bramp.net 指向::1的单个AAAA记录
aaaaa.bramp.net 一个A记录和一个AAAA记录解析到127.0.0.1和::1
cname.bramp.net 指向 a.bramp.net 的单个 CNAME 记录
cname-loop1.bramp.net 指向 cname-loop2.bramp.net 的单个 CNAME 记录
cname-loop2.bramp.net 指向 cname-loop1.bramp.net 的单个 CNAME 记录
mx.bramp.net 指向 a.bramp.net 的单个 MX 记录
ns.bramp.net 指向 a.bramp.net 的单个 NS 记录
txt.bramp.net 单个 TXT 记录 "A TXT record!"

释放

# Bump version number
$ cargo test-all-features
$ cargo readme > README.md
$ cargo publish --dry-run
$ cargo publish

待办事项(按优先级排序)

  • 记录 UDP/TCP 库
  • 客户端示例
  • 服务器端示例
  • DNSSEC:DSA、RSA、ECDSA 和 Ed25519 的签名、验证和密钥生成
  • NSID、Cookies、AXFR/IXFR、TSIG、SIG(0)
  • 运行时独立性
  • 修改 API 以包含获取器和设置器。
  • 修改 hyper-alpn 以支持 tokio-native-tls,以满足需要的人。
  • 实现更多的 dig 功能,例如 +trace
  • 可能将二进制解析转换为 Nom 格式。
  • 我可以解析这些 https://www.iana.org/domains/root/files 吗?

参考

许可证:Apache-2.0

Copyright 2021 Andrew Brampton (bramp.net)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

依赖关系

~5–23MB
~312K SLoC