工具简介
nftables-nat-rust 是一个基于 Rust 编写的 nftables 端口转发管理工具。它通过配置文件和 nftables 规则实现 NAT 转发、过滤和本地重定向,支持 IPv4/IPv6,并可通过 WebUI 进行可视化配置。
核心特性
- 动态 NAT 转发:自动监测配置文件和目标域名 IP 变化,更新转发规则
- 防火墙过滤:支持 Drop 功能,可作用于 INPUT/FORWARD 链
- IPv4/IPv6 双栈:支持 NAT 转发和过滤
- 配置格式:支持传统配置文件和 TOML 格式
- 规则控制:支持单端口、端口段、TCP/UDP 协议选择、IP 地址和网段过滤
- 本地重定向:支持端口重定向到本机其他端口
- Docker 兼容:与 Docker 网络兼容
- 轻量实现:基于 Rust 编写,依赖标准库和少量核心库
- 服务管理:支持 systemd 开机自启
- 域名解析:支持域名和 IP 地址,自动 DNS 解析和缓存
- WebUI:提供可视化管理界面,可切换后端地址
系统准备
适用于 Debian / Ubuntu。安装 nftables:
apt update && apt install -y nftables
systemctl disable --now iptables
systemctl disable --now iptables 为可选,用于禁用 iptables。
安装
升级时使用相同的安装命令。
方法一:TOML 配置文件版本(推荐)
bash <(curl -sSLf https://us.arloor.dev/https://github.com/arloor/nftables-nat-rust/releases/download/v2.0.0/setup.sh) toml
方法二:传统配置文件版本
bash <(curl -sSLf https://us.arloor.dev/https://github.com/arloor/nftables-nat-rust/releases/download/v2.0.0/setup.sh) legacy
WebUI 管理界面(可选):
bash <(curl -sSLf https://us.arloor.dev/https://github.com/arloor/nftables-nat-rust/releases/download/v2.0.0/setup-console.sh) # --host 0.0.0.0 -p 5533 -k /root/.acme.sh/arloor.dev/arloor.dev.key -c /root/.acme.sh/arloor.dev/fullchain.cer
配置文件
- TOML 配置文件:
/etc/nat.toml - 传统配置文件:
/etc/nat.conf
TOML 配置支持配置验证、注释、WebUI 可视化编辑与结构化配置。
规则示例
下面为 TOML 格式的示例配置,涵盖常见规则类型。
# 单端口转发:HTTPS 流量转发
[[rules]]
type = "single"
sport = 10443
dport = 443
domain = "example.com"
protocol = "all"
ip_version = "ipv4"
comment = "转发 HTTPS 到 example.com"
# 端口段转发:批量游戏端口(目标端口与本机端口相同)
[[rules]]
type = "range"
port_start = 20000
port_end = 20100
domain = "game.example.com"
protocol = "tcp"
ip_version = "all"
comment = "游戏服务器端口段"
# 端口段平移:本机 20001-20100 转发到目标 10001-10100
[[rules]]
type = "range"
port_start = 20001
port_end = 20100
dport = 10001
domain = "b.example.com"
protocol = "all"
ip_version = "ipv4"
comment = "等宽平移到另一公网端口段"
# UDP 专用转发:DNS 服务
[[rules]]
type = "single"
sport = 5353
dport = 53
domain = "8.8.8.8"
protocol = "udp"
ip_version = "ipv4"
comment = "DNS 查询转发"
# 本地重定向:单端口重定向到本机服务
[[rules]]
type = "redirect"
sport = 8080
dport = 3128
protocol = "all"
ip_version = "ipv4"
comment = "代理服务端口重定向"
# 本地重定向:端口段重定向到本机
[[rules]]
type = "redirect"
sport = 30001
sport_end = 30100
dport = 45678
protocol = "tcp"
ip_version = "all"
comment = "批量端口重定向到本机"
# Drop 过滤:阻止特定 IPv4 地址
[[rules]]
type = "drop"
chain = "input"
src_ip = "180.213.132.211"
protocol = "all"
comment = "阻止恶意 IP 访问"
# Drop 过滤:阻止 IPv6 网段
[[rules]]
type = "drop"
chain = "input"
src_ip = "240e:328:1301::/48"
protocol = "all"
comment = "阻止 IPv6 网段访问"
# Drop 过滤:阻止特定端口
[[rules]]
type = "drop"
chain = "input"
dst_port = 22
protocol = "tcp"
comment = "阻止 SSH 端口访问"
# 强制 IPv6 转发
[[rules]]
type = "single"
sport = 9001
dport = 9090
domain = "ipv6.example.com"
protocol = "all"
ip_version = "ipv6"
comment = "IPv6 专用服务"
# 双栈支持:自动选择 IPv4/IPv6
[[rules]]
type = "single"
sport = 10080
dport = 80
domain = "dual-stack.example.com"
protocol = "tcp"
ip_version = "all"
comment = "双栈 Web 服务"
查看 nftables 规则
nft list ruleset
nft list table ip self-nat
nft list table ip6 self-nat
nft list map ip self-nat tcp_dnat
nft list map ip self-nat tcp_dnat_ip
服务管理
systemctl start nat
systemctl stop nat
systemctl restart nat
systemctl status nat
systemctl enable nat
systemctl disable nat
开源地址
- 原项目:https://github.com/arloor/nftables-nat-rust
- 增强版:https://github.com/misaka-cpu/nftables-nat-rust-enhanced
nftables-nat-rust:基于 nftables 的 NAT 转发与过滤工具
https://www.vpsnews.de/archives/nftables-nat-rust-ef-bc-9a-e5-9f-ba-e4-ba-8e-nftables-e7-9a-zhujiwiki-41039
评论