核心特性
🔄 动态 NAT 转发:自动监测配置文件和目标域名 IP 变化,实时更新转发规则
🛡️ 防火墙过滤:支持 Drop 功能,实现类似防火墙的黑名单过滤(INPUT/FORWARD链)
🌐 IPv4/IPv6 双栈支持:完整支持 IPv4 和 IPv6 NAT 转发和过滤
📝 灵活配置:支持传统配置文件和 TOML 格式,满足不同使用场景
🎯 精准控制:支持单端口、端口段、TCP/UDP 协议选择、IP地址和网段过滤
🔌 本地重定向:支持端口重定向到本机其他端口
🐋 Docker 兼容:与 Docker 网络完美兼容
⚡ 高性能轻量:基于 Rust 编写,仅依赖标准库和少量核心库
🚀 开机自启:支持 systemd 服务管理,开机自动启动
🔍 域名解析:支持域名和 IP 地址,自动 DNS 解析和缓存
🖥️ Web 管理界面:提供可视化的 WebUI 管理配置和查看规则,并且支持切换后端地址
系统准备
Debian / Ubuntu
# 安装 nftables
apt update && apt install -y nftables
# 禁用 iptables(可选)
systemctl disable --now 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
优势:
✅ 支持配置验证,保证格式正确
✅ 支持注释,便于维护
✅ WebUI 可视化编辑和验证
✅ 结构化配置,可读性更好
# ============ NAT 转发规则 ============
# 1. 单端口转发 - HTTPS 流量转发
[[rules]]
type = "single"
sport = 10443 # 本机监听端口
dport = 443 # 目标服务端口
domain = "example.com" # 目标域名或 IP 地址
protocol = "all" # 协议: all, tcp 或 udp
ip_version = "ipv4" # IP 版本: ipv4, ipv6 或 all
comment = "转发 HTTPS 到 example.com"
# 2. 端口段转发 - 批量游戏端口(目标端口与本机端口相同)
[[rules]]
type = "range"
port_start = 20000 # 起始端口
port_end = 20100 # 结束端口(含)
domain = "game.example.com"
protocol = "tcp" # 仅 TCP 协议
ip_version = "all" # 同时支持 IPv4 和 IPv6
comment = "游戏服务器端口段"
# 2b. 端口段平移 - 本机 20001-20100 转发到目标 10001-10100
[[rules]]
type = "range"
port_start = 20001 # 本机起始端口
port_end = 20100 # 本机结束端口(含)
dport = 10001 # 目标起始端口;省略则与 port_start-port_end 相同
domain = "b.example.com"
protocol = "all"
ip_version = "ipv4"
comment = "等宽平移到另一公网端口段"
# 3. UDP 专用转发 - DNS 服务
[[rules]]
type = "single"
sport = 5353 # 本机 DNS 端口
dport = 53 # 目标 DNS 端口
domain = "8.8.8.8" # 也可以直接使用 IP 地址
protocol = "udp" # 仅 UDP 协议
ip_version = "ipv4"
comment = "DNS 查询转发"
# ============ 本地重定向规则 ============
# 4. 单端口重定向到本机服务
[[rules]]
type = "redirect"
sport = 8080 # 外部访问端口
dport = 3128 # 本机实际服务端口
protocol = "all"
ip_version = "ipv4"
comment = "代理服务端口重定向"
# 5. 端口段重定向到本机
[[rules]]
type = "redirect"
sport = 30001 # 起始端口
sport_end = 30100 # 结束端口
dport = 45678 # 本机目标端口
protocol = "tcp"
ip_version = "all"
comment = "批量端口重定向到本机"
# ============ 防火墙过滤规则 (Drop) ============
# 6. 阻止特定 IPv4 地址访问
[[rules]]
type = "drop"
chain = "input" # 链类型: input 或 forward
src_ip = "180.213.132.211" # 源 IP 地址
protocol = "all" # 协议: all, tcp 或 udp
comment = "阻止恶意 IP 访问"
# 7. 阻止 IPv6 网段访问
[[rules]]
type = "drop"
chain = "input"
src_ip = "240e:328:1301::/48" # IPv6 网段
protocol = "all"
comment = "阻止 IPv6 网段访问"
# 8. 阻止特定端口(如 SSH)
[[rules]]
type = "drop"
chain = "input"
dst_port = 22 # 目标端口
protocol = "tcp"
comment = "阻止 SSH 端口访问"
# 9. 阻止端口范围
[[rules]]
type = "drop"
chain = "forward"
dst_port = 1000 # 起始端口
dst_port_end = 2000 # 结束端口
protocol = "tcp"
comment = "阻止转发到端口范围 1000-2000"
# 10. 组合过滤:特定IP访问特定端口
[[rules]]
type = "drop"
chain = "input"
src_ip = "192.168.1.0/24" # 源 IP 网段
dst_port = 3306 # 目标端口 (MySQL)
protocol = "tcp"
comment = "阻止内网访问 MySQL"
# ============ 高级场景示例 ============
# 11. 强制 IPv6 转发
[[rules]]
type = "single"
sport = 9001
dport = 9090
domain = "ipv6.example.com"
protocol = "all"
ip_version = "ipv6" # 仅使用 IPv6 进行转发
comment = "IPv6 专用服务"
# 12. 双栈支持示例 - 自动选择 IPv4/IPv6
[[rules]]
type = "single"
sport = 10080
dport = 80
domain = "dual-stack.example.com" # 域名同时有 A 和 AAAA 记录
protocol = "tcp"
ip_version = "all" # 根据客户端 IP 版本自动选择
comment = "双栈 Web 服务"
查看 nftables 规则
# 查看所有规则
nft list ruleset
# 仅查看 NAT 表
nft list table ip self-nat
nft list table ip6 self-nat
# 查看 DNAT map(端口 -> 目标)
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
-
所有主机信息仅是为及时分享并收录,不做推荐!


