feat: 支持 IPv6 双栈地址派发

- model: VpnSetting 新增 Subnet6 字段,VpnReservation 新增 IPAddress6
- alloc: 处理 /64 等大子网 cidr.AddressCount 溢出,回退 65536 扫描上限
- protocol: initMessage 新增 ip6/prefix6/server_ip6(omitempty 向后兼容)
- service: 双分配器 alloc4+alloc6,ApplySettings 配置双 IP 与双路由,
  Allocate 返回 v4+v6 一对地址
- switch: SwitchConn 新增 AssignedIP6(),Register/Unregister 注册双 key,
  反欺骗按 IP 版本分别校验 v4/v6 源地址
- tunnel: tunnelConn 新增 assignedIP6,init 消息填充 v6 字段
- handler: 新增 validateSubnet6(/64~/126),settings/预留 API 全面支持 v6
- diag: 新增 IPv6 forwarding 与 NAT66 masquerade 检测
- install_linux.sh: 新增 VPN_SUBNET6、ipv6 forwarding、nft/ip6tables NAT66
- 前端: v6 子网输入框、v6 预留、v6 诊断卡片、客户端列表 v6 列
- 文档: init 消息、IP 分配、子网约束、TUN 配置、NAT、反欺骗全部更新
This commit is contained in:
2026-07-07 11:38:21 +08:00
parent 808b991483
commit a770862c7c
13 changed files with 561 additions and 145 deletions
+30
View File
@@ -6,6 +6,8 @@ set -e
# 若后台修改了子网,需同步修改此处并重新执行脚本,或手动更新 iptables 规则
# ─────────────────────────────────────────────────────────────
VPN_SUBNET="192.168.77.0/24"
# IPv6 子网(留空则不配置 IPv6 NAT;与后台设置保持一致)
VPN_SUBNET6="fd00:dead:beef::/112"
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 用户执行此脚本: sudo bash install_linux.sh"
@@ -42,10 +44,16 @@ chown -R lmvpn:lmvpn /opt/lmvpn
echo ">>> 配置内核 IP 转发..."
# 临时生效
sysctl -w net.ipv4.ip_forward=1 >/dev/null
if [ -n "$VPN_SUBNET6" ]; then
sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
fi
# 持久化
cat > /etc/sysctl.d/99-lmvpn.conf << EOF
net.ipv4.ip_forward = 1
EOF
if [ -n "$VPN_SUBNET6" ]; then
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.d/99-lmvpn.conf
fi
sysctl -p /etc/sysctl.d/99-lmvpn.conf >/dev/null
echo ">>> 配置 NAT 与转发规则..."
@@ -75,9 +83,16 @@ else
nft add table inet lmvpn_nat
nft 'add chain inet lmvpn_nat postrouting { type nat hook postrouting priority 100 ; }'
nft add rule inet lmvpn_nat postrouting oifname "$WAN_IFACE" ip saddr "$VPN_SUBNET" masquerade
if [ -n "$VPN_SUBNET6" ]; then
nft add rule inet lmvpn_nat postrouting oifname "$WAN_IFACE" ip6 saddr "$VPN_SUBNET6" masquerade
fi
nft 'add chain inet lmvpn_nat forward { type filter hook forward priority 0 ; policy accept ; }'
nft add rule inet lmvpn_nat forward ip saddr "$VPN_SUBNET" accept
nft add rule inet lmvpn_nat forward ip daddr "$VPN_SUBNET" accept
if [ -n "$VPN_SUBNET6" ]; then
nft add rule inet lmvpn_nat forward ip6 saddr "$VPN_SUBNET6" accept
nft add rule inet lmvpn_nat forward ip6 daddr "$VPN_SUBNET6" accept
fi
# 持久化 nft 规则
mkdir -p /etc/nftables.d
@@ -99,6 +114,17 @@ else
iptables -A FORWARD -s "$VPN_SUBNET" -j ACCEPT
iptables -A FORWARD -d "$VPN_SUBNET" -j ACCEPT
# IPv6 NAT(需 ip6tables
if [ -n "$VPN_SUBNET6" ] && command -v ip6tables >/dev/null 2>&1; then
echo "配置 IPv6 NAT (ip6tables)..."
ip6tables -t nat -D POSTROUTING -s "$VPN_SUBNET6" -o "$WAN_IFACE" -j MASQUERADE 2>/dev/null || true
ip6tables -t nat -A POSTROUTING -s "$VPN_SUBNET6" -o "$WAN_IFACE" -j MASQUERADE
ip6tables -D FORWARD -s "$VPN_SUBNET6" -j ACCEPT 2>/dev/null || true
ip6tables -D FORWARD -d "$VPN_SUBNET6" -j ACCEPT 2>/dev/null || true
ip6tables -A FORWARD -s "$VPN_SUBNET6" -j ACCEPT
ip6tables -A FORWARD -d "$VPN_SUBNET6" -j ACCEPT
fi
# 持久化 iptables 规则
if command -v netfilter-persistent >/dev/null 2>&1; then
netfilter-persistent save
@@ -106,6 +132,10 @@ else
elif command -v iptables-save >/dev/null 2>&1; then
mkdir -p /etc/iptables
iptables-save > /etc/iptables/rules.v4
if [ -n "$VPN_SUBNET6" ] && command -v ip6tables-save >/dev/null 2>&1; then
ip6tables-save > /etc/iptables/rules.v6
echo "ip6tables 规则已写入 /etc/iptables/rules.v6"
fi
echo "iptables 规则已写入 /etc/iptables/rules.v4"
echo "注意: 重启后规则持久化需配合 iptables-persistent 包,建议安装: apt install iptables-persistent"
else