vibe coding 了下我的 NAT 网络地址转换工具,新增了管理页面和端口段 Redirect 支持

1 月 10 日
 arloor

NFTables NAT Rust

基于 nftables 的高性能 NAT 端口转发管理工具,使用 Rust 语言开发。

✨ 核心特性

🖥️ 系统要求

适用于以下 Linux 发行版:

⚙️ 系统准备

CentOS / RHEL / Fedora

# 关闭 firewalld
systemctl disable --now firewalld

# 关闭 SELinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 安装 nftables
yum install -y nftables

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 管理界面

本项目现已支持 Web 管理界面,可以通过浏览器方便地管理 NAT 配置。

安装管理界面 WebUI

bash <(curl -sSLf https://us.arloor.dev/https://github.com/arloor/nftables-nat-rust/releases/download/v2.0.0/setup-console.sh) # -p 5533  -k /root/.acme.sh/arloor.dev/arloor.dev.key -c /root/.acme.sh/arloor.dev/fullchain.cer
  1. 安装过程会交互式提示输入用户名和密码。密码会保存在 systemd 文件中,注意安全。
  2. 通过 -p 参数可以指定 WebUI 监听端口,默认端口为 5533 。
  3. 通过 -c-k 参数可以指定自定义 TLS 证书和私钥文件路径,如果未提供,将自动生成自签名证书。
  4. 安装脚本会自动检测现有 NAT 服务的配置格式,并根据配置格式生成相应的 systemd service 文件。

安装完成后,访问 https://your-server-ip:5533 即可使用管理界面。详细文档请查看 nat-console/README.md

升级 WebUI

bash <(curl -sSLf https://us.arloor.dev/https://github.com/arloor/nftables-nat-rust/releases/download/v2.0.0/setup-console-assets.sh)
systemctl restart nat-console

📝 配置说明

TOML 配置文件(推荐)

配置文件位置:/etc/nat.toml

优势

# ============ 基础转发示例 ============

# 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 = "游戏服务器端口段"

# 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 = "批量端口重定向到本机"

# ============ 高级场景示例 ============

# 6. 强制 IPv6 转发
[[rules]]
type = "single"
sport = 9001
dport = 9090
domain = "ipv6.example.com"
protocol = "all"
ip_version = "ipv6"    # 仅使用 IPv6 进行转发
comment = "IPv6 专用服务"

# 7. 双栈支持示例 - 自动选择 IPv4/IPv6
[[rules]]
type = "single"
sport = 10080
dport = 80
domain = "dual-stack.example.com"  # 域名同时有 A 和 AAAA 记录
protocol = "tcp"
ip_version = "all"     # 根据客户端 IP 版本自动选择
comment = "双栈 Web 服务"

传统配置文件

配置文件位置:/etc/nat.conf

基础格式

参数说明

配置示例

# ============ 基础转发 ============

# 单端口转发 - HTTPS 流量
SINGLE,10443,443,example.com

# 端口段转发 - 游戏服务器端口( 20000-20100 )
RANGE,20000,20100,game.example.com

# ============ 协议指定 ============

# 仅转发 TCP 流量 - Web 服务
SINGLE,10080,80,web.example.com,tcp

# 仅转发 UDP 流量 - DNS 查询
SINGLE,5353,53,8.8.8.8,udp

# ============ 本地重定向 ============

# 单端口重定向到本机服务
REDIRECT,8080,3128

# 端口段重定向到本机( 30001-30100 → 45678 )
REDIRECT,30001-30100,45678

# TCP 专用重定向
REDIRECT,7000-7100,8080,tcp

# ============ IPv6 支持 ============

# 强制使用 IPv6 转发
SINGLE,9001,9090,ipv6.example.com,all,ipv6

# 双栈支持(根据客户端自动选择)
SINGLE,10080,80,dual-stack.example.com,tcp,all

# 禁用的规则(以 # 开头)
# SINGLE,3000,3000,disabled.example.com

🚀 使用方法

启动/停止服务

# 启动服务
systemctl start nat

# 停止服务
systemctl stop nat

# 重启服务
systemctl restart nat

# 查看服务状态
systemctl status nat

# 开机自启
systemctl enable nat

# 取消开机自启
systemctl disable nat

修改配置

修改配置文件后,程序会在 60 秒内自动应用新配置,无需手动重启服务。

# TOML 版本
vim /etc/nat.toml

# 传统版本
vim /etc/nat.conf

查看日志

# 实时查看日志
journalctl -fu nat

# 查看详细日志
journalctl -exfu nat

# 查看最近 100 行日志
journalctl -u nat -n 100

查看 nftables 规则

# 查看所有规则
nft list ruleset

# 仅查看 NAT 表
nft list table ip self-nat
nft list table ip6 self-nat6

🔧 高级配置

自定义源 IP (多网卡场景)

默认使用 masquerade 自动处理 SNAT 。如需指定源 IP:

# 设置自定义源 IP
echo "nat_local_ip=10.10.10.10" > /opt/nat/env

# 重启服务
systemctl restart nat

🐋 Docker 兼容性

本工具已与 Docker 完全兼容。程序会自动调整 nftables 规则以适配 Docker 网络。

说明:Docker v28 将 filter 表 forward 链默认策略改为 DROP ,本工具会自动将其重置为 ACCEPT 以确保 NAT 规则正常工作。

📌 注意事项

REDIRECT 类型限制

REDIRECT 类型工作在 PREROUTING 链,仅对外部流量有效:

原因:本机流量直接进入 OUTPUT 链,不经过 PREROUTING 链。

示例

# 配置:REDIRECT,8000,3128
curl http://remote-server:8000  # ✅ 成功重定向到 3128
curl http://localhost:8000      # ❌ 不会重定向,直接访问 8000

TLS/Trojan 转发

转发 TLS/Trojan 等加密协议时,常见问题是证书配置错误。

解决方案

  1. 简单:客户端禁用证书验证
  2. 推荐:正确配置证书和域名,确保证书域名与中转机匹配

📄 许可证

本项目采用 MIT License 开源协议。

🔗 相关链接


注意:与旧版 iptablesUtils 不兼容,切换时请先卸载旧版或重装系统。

1779 次点击
所在节点    程序员
1 条回复
bigmomo
1 月 10 日
加个星星,就需要这种玩意

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1184503

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX