6 个版本
0.8.1 | 2023 年 11 月 13 日 |
---|---|
0.7.2 | 2023 年 8 月 18 日 |
0.7.0 | 2023 年 7 月 10 日 |
#7 in #cidr
每月 52 次下载
31KB
527 行
fcidr
分段无类域间路由 (FCIDR)
一个库,它暴露了一个数据结构来表示一组 CIDR 范围,以及一个接口来计算 CIDR 上的集合操作。
此数据结构可以应用于,例如,配置防火墙,该防火墙使用显式表达允许和拒绝规则的规则集来隐式拒绝(AWS 安全组)。
注意 目前仅支持 IPv4。IPv6 的支持正在通过 #6 跟踪。
命令行界面 (CLI)
该项目还发布了一个二进制应用程序,用于命令行,支持通过读取标准输入来组合 CIDR 上的集合操作的链。
安装
目前,crates.io 是唯一分发此应用程序的地方。
cargo install fcidr
用法
Fragmented Classless Inter-Domain Routing (FCIDR)
Usage: fcidr [CIDR] <COMMAND>
Commands:
complement Compute the complement of the input CIDR(s) [aliases: !, not]
difference Compute the set difference between the input CIDR(s) and another CIDR [aliases: -, exclude, minus]
superset Exits successfully if the input CIDR(s) is a superset of another CIDR [aliases: >, contains]
union Compute the set union of the input CIDR(s) and another CIDR [aliases: +, include, plus]
help Print this message or the help of the given subcommand(s)
Arguments:
[CIDR] The input CIDR range and first operand to the computation. If omitted, input is taken from stdin. In this way, multiple computations can be chained together
Options:
-h, --help Print help
-V, --version Print version`
示例
计算 10.0.0.0/8 内特定的一组 CIDR,排除某些子范围,并包括一个被排除的子范围的一部分
fcidr 10.0.0.0/8 difference 10.0.64.0/20 | fcidr difference 10.0.82.0/24 | fcidr union 10.0.82.74/31
10.0.0.0/18
10.0.80.0/23
10.0.82.74/31
10.0.83.0/24
10.0.84.0/22
10.0.88.0/21
10.0.96.0/19
10.0.128.0/17
10.1.0.0/16
10.2.0.0/15
10.4.0.0/14
10.8.0.0/13
10.16.0.0/12
10.32.0.0/11
10.64.0.0/10
10.128.0.0/9
反转前面的结果
fcidr 10.0.0.0/8 difference 10.0.64.0/20 | fcidr difference 10.0.82.0/24 | fcidr union 10.0.82.74/31 | fcidr complement
0.0.0.0/5
8.0.0.0/7
10.0.64.0/20
10.0.82.0/26
10.0.82.64/29
10.0.82.72/31
10.0.82.76/30
10.0.82.80/28
10.0.82.96/27
10.0.82.128/25
11.0.0.0/8
12.0.0.0/6
16.0.0.0/4
32.0.0.0/3
64.0.0.0/2
128.0.0.0/1
备选简洁语法
注意这些符号可能与您的 shell 不兼容,因此如果您想,可以引用它们,例如 fcidr "!"
).
fcidr 10.0.0.0/8 + 127.0.0.0/16 | fcidr - 10.64.0.0/16 | fcidr !
0.0.0.0/5
8.0.0.0/7
10.64.0.0/16
11.0.0.0/8
12.0.0.0/6
16.0.0.0/4
32.0.0.0/3
64.0.0.0/3
96.0.0.0/4
112.0.0.0/5
120.0.0.0/6
124.0.0.0/7
126.0.0.0/8
127.1.0.0/16
127.2.0.0/15
127.4.0.0/14
127.8.0.0/13
127.16.0.0/12
127.32.0.0/11
127.64.0.0/10
127.128.0.0/9
128.0.0.0/1
检查 IP 是否在 CIDR 内
fcidr 255.0.0.0/16 contains "255.0.1.2/32" && echo Woohoo!
Woohoo!
echo 255.0.0.0/16 | fcidr contains "255.1.1.2/32" && echo Woohoo!
Error: "not a superset of 255.1.1.2/32"
检查 CIDR 是否在大量 CIDR 中的任何一个内
基于前面的示例,由于亚马逊发布了其公共 IP 范围的 JSON 格式列表,我们可以检查 IP 或 CIDR 是否实际上属于亚马逊。只要我们能得到由换行符分隔的范围列表,将其管道传递到 fcidr
就使任务变得简单。
curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix' | fcidr contains 52.43.76.84/30 && echo "This CIDR is within an Amazon range."
This CIDR is within an Amazon range.
curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix' | fcidr contains 62.43.76.0/24 && echo "This CIDR is within an Amazon range."
Error: "not a superset of 62.43.76.0/24"
开发
先决条件
如何操作
创建开发外壳环境。运行所有其他命令所必需的。
nix develop
使用 cargo 构建。
just build
使用 cargo 内置的快速静态分析检查代码。
just check
删除构建文件。
just clean
格式化代码。
just format
使用 clippy 进行更好的静态分析检查代码。
just lint
运行应用程序。
just run
使用 cargo 内置的测试运行器运行测试。
just test
监视代码更改并重新构建。
just watch
所有 just
命令都可以在 --
后接受额外的命令行参数。
例如:带有报告版本标志运行应用程序。
just run -- --version
技巧和建议
从开发外壳打开 IDE
为了在您的 IDE 中获取对 rust 二进制文件的链接,您应该从终端打开开发外壳,然后从该会话打开您的 IDE。这会将开发外壳的环境带入您的 IDE。
例如,如果您使用VSCode。
cd path/to/this/project
nix develop
code .
通过这样做,您可以安装rust-analyzer VSCode扩展,并且它将正常工作,因为它能够指向正确的rust二进制文件和库。您还将能够在VSCode中访问由nix flake安装的任何包。
依赖项
~1.2–1.8MB
~35K SLoC