#!/bin/bash

MAP_FILE="/etc/pve/nat-map.conf"
NAT_SCRIPT="/usr/local/bin/update-nat.sh"

# 确保映射配置文件存在
touch "$MAP_FILE"

echo "=============================================="
echo "   NAT 映射添加工具（不会影响 DHCP 或其他配置）"
echo "   映射文件: $MAP_FILE"
echo "=============================================="
echo ""

while true; do
    read -p "请输入内网 IP（例如 10.1.1.10，或 n 结束）: " IP
    [[ "$IP" == "n" ]] && break
    [[ -z "$IP" ]] && continue

    read -p "请输入内网端口（如 22）: " INPORT
    read -p "请输入公网端口（如 1022）: " OUTPORT

    echo "$IP $INPORT $OUTPORT" >> "$MAP_FILE"
    echo "✔ 添加成功：$IP:$INPORT → 公网 $OUTPORT"

    read -p "继续添加？(y/n) [y]: " MORE
    MORE=${MORE:-y}
    [[ "$MORE" == "n" ]] && break
done

echo ""
echo "正在应用 NAT 规则..."
$NAT_SCRIPT

echo ""
echo "=============================================="
echo " NAT 映射添加完成！你可以在 WebUI 查看结果："
echo " Datacenter → Firewall → NAT"
echo "=============================================="
