3 个版本
0.1.2 | 2024 年 6 月 18 日 |
---|---|
0.1.1 | 2024 年 6 月 17 日 |
0.1.0 | 2024 年 6 月 16 日 |
#2158 在 网络编程
每月下载 43 次
88KB
2K SLoC
async-traceroute

异步 Traceroute 库/命令行工具。
什么是 Traceroute
Traceroute 允许您查看 IP 数据包从一台主机到另一台主机的路径。它使用 IP 数据包中的 TTL(生存时间) 字段从路径上的每个路由器引发一个 ICMP 生存时间超时 消息。每个处理数据包的路由器都会减少 TTL 字段,这实际上充当了一个跳数计数器。当一个路由器接收到 TTL 字段设置为 0 的 IP 数据报时,它会响应一个 ICMP 生存时间超时,从而揭示其 IP 地址。
存在几种 traceroute 探测方法。此图显示了基于 UDP 的 traceroute 方法的工作原理。
用法
Async Traceroute library/command-line tool
Usage: traceroute [OPTIONS] <HOST>
Arguments:
<HOST>
Options:
-m, --max-hops <MAX_HOPS> Set the max number of hops (max TTL to be reached) [default: 30]
-q, --queries <QUERIES> Set the number of probes per each hop [default: 3]
-w, --wait <WAIT> Wait for a probe no more than <WAIT> [default: 3s]
-N, --sim-queries <SIM_QUERIES> Set the number of probes to be tried simultaneously [default: 16]
-P, --probe-method <PROBE_METHOD> [default: udp] [possible values: udp, tcp, icmp]
-p, --port <PORT> This value changes semantics based on the probe method selected. It is either
initial udp port value for "udp" probe method (incremented by each probe,
default is 33434), or initial seq for "icmp" probe method (incremented as well,
default from 1), or destination port for "tcp" probe method (default is 80)
-n Do not resolve IP addresses to their domain names
-i, --interface <INTERFACE> Specify a network interface to operate with
-h, --help Print help
-V, --version Print version
您可以通过将其作为库使用来将 async-traceroute
集成到您的项目中。以下是一个示例代码,展示了如何对 google.com 执行 Traceroute 操作
use futures::pin_mut;
use futures_util::StreamExt;
use async_traceroute::TracerouteBuilder;
use async_traceroute::utils::dns::dns_lookup_first_ipv4_addr;
#[tokio::main]
async fn main() -> Result<(), String> {
let ip_addr = match dns_lookup_first_ipv4_addr("google.com").await {
None => return Err(String::from("Hostname not resolvable")),
Some(ip_addr) => ip_addr,
};
let traceroute = TracerouteBuilder::udp()
.destination_address(ip_addr)
.max_ttl(15)
.queries_per_hop(3)
.max_wait_probe(Duration::from_secs(3))
.simultaneous_queries(16)
.active_dns_lookup(true)
.initial_destination_port(33434)
.network_interface("eth0")
.build();
let traceroute_stream = match traceroute {
Ok(traceroute) => traceroute.trace(),
Err(error) => return Err(error),
};
pin_mut!(traceroute_stream);
while let Some(probe_result) = traceroute_stream.next().await {
println!("{:?}", probe_result);
}
Ok(())
}
安装
在您的项目目录中运行以下 Cargo 命令
cargo add async-traceroute
或将以下行添加到您的 Cargo.toml 文件中
async-traceroute = "0.1.2"
Docker
构建镜像
docker build -t async-traceroute:${VERSION} .
Docker 运行
$ docker run async-traceroute:${VERSION} --help
Async Traceroute library/command-line tool
Usage: traceroute [OPTIONS] <HOST>
...
注意
- 此库需要管理员权限来创建原始套接字
- 仅在 Linux 上进行测试
待办事项
- 添加对 Windows 的支持
依赖项
~12–24MB
~352K SLoC