#!/bin/bash

# 检查是否以root身份运行
if [ "$(id -u)" -ne 0; then
  echo "请以root身份运行此脚本。"
  exit 1
fi

# 检查account_mapping文件是否存在
MAPPING_FILE="/etc/ppp/account_mapping"
if [ ! -f "${MAPPING_FILE}" ]; then
  echo "${MAPPING_FILE} 文件不存在。请创建该文件并添加账号映射信息。"
  exit 1
fi

# 安装必要的软件包
install_packages() {
  if command -v yum &> /dev/null; then
    yum install -y rp-pppoe ppp iptables vconfig iptables-services firewalld tcpdump conntrack-tools
  else
    echo "不支持的包管理器。请手动安装必要的软件包。"
    exit 1
  fi
}

# 加载8021q模块
load_vlan_module() {
  if ! lsmod | grep -q 8021q; then
    modprobe 8021q
  fi
}

# 启动并启用服务
enable_services() {
  systemctl start iptables
  systemctl enable iptables
  systemctl start firewalld
  systemctl enable firewalld
}

# 动态检测网卡名称
detect_interfaces() {
  ISP_INTERFACE=$(ip link show | grep -E '^[0-9]+: ens[0-9]+' | awk '{print $2}' | sed 's/://g' | head -n 1)
  CLIENT_INTERFACE=$(ip link show | grep -E '^[0-9]+: ens[0-9]+' | awk '{print $2}' | sed 's/://g' | tail -n 1)

  if [ -z "${ISP_INTERFACE}" ] || [ -z "${CLIENT_INTERFACE}" ]; then
    echo "无法检测到有效的网卡。请手动配置。"
    exit 1
  fi
}

# 解析配置文件并生成PPP配置和VLAN
configure_accounts() {
  mkdir -p /etc/ppp/peers

  while IFS=' ' read -r LOCAL_ACCOUNT ISP_ACCOUNT VLAN_ID; do
    # 创建PPP配置文件
    cat <<EOF > /etc/ppp/peers/${ISP_ACCOUNT}
plugin rp-pppoe.so ${ISP_INTERFACE}.${VLAN_ID}
user "${ISP_ACCOUNT}"
defaultroute
hide-password
noauth
persist
mtu 1492
mru 1492
EOF

    # 添加用户认证信息
    cat <<EOF >> /etc/ppp/chap-secrets
"${ISP_ACCOUNT}" * "password" *
EOF

    cat <<EOF >> /etc/ppp/pap-secrets
"${ISP_ACCOUNT}" * "password" *
EOF

    # 配置VLAN
    if ! ip link show ${ISP_INTERFACE}.${VLAN_ID} &> /dev/null; then
      echo "正在添加VLAN ${VLAN_ID} 到接口 ${ISP_INTERFACE}"
      vconfig add ${ISP_INTERFACE} ${VLAN_ID}
      ifconfig ${ISP_INTERFACE}.${VLAN_ID} up
    fi
  done < "${MAPPING_FILE}"
}

# 配置PPPoE中继
configure_pppoe_relay() {
  if ! ip link show ${CLIENT_INTERFACE} &> /dev/null; then
    echo "客户端网卡 ${CLIENT_INTERFACE} 不存在。"
    exit 1
  fi

  if ! ip link show ${ISP_INTERFACE} &> /dev/null; then
    echo "ISP网卡 ${ISP_INTERFACE} 不存在。"
    exit 1
  fi

  pppoe-relay -C ${CLIENT_INTERFACE} -S ${ISP_INTERFACE}
}

# 设置防火墙和IP转发
configure_firewall() {
  # 启用IP转发
  sysctl -w net.ipv4.ip_forward=1
  echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  echo "net.netfilter.nf_conntrack_helper = 1" >> /etc/sysctl.conf

  # 配置防火墙规则
  iptables -A FORWARD -o ppp+ -j ACCEPT
  iptables -A FORWARD -i ppp+ -j ACCEPT
  sudo iptables -A INPUT -i ens19.348 -j ACCEPT
  sudo iptables -A INPUT -i ens19.2830 -j ACCEPT
  sudo iptables -A INPUT -p pppoe -j ACCEPT
  sudo iptables -A FORWARD -p pppoe -j ACCEPT

  # 保存防火墙规则
  if command -v systemctl &> /dev/null; then
    if systemctl list-units --type=service | grep -q iptables; then
      systemctl restart iptables
    elif systemctl list-units --type=service | grep -q firewalld; then
      systemctl restart firewalld
    else
      echo "未找到iptables或firewalld服务。请手动检查防火墙配置。"
    fi
  else
    service iptables save
  fi
}

# 配置PPP服务端
configure_ppp_server() {
  cat <<EOF > /etc/ppp/options
# PPP options for PPPoE server
require-pap
login
lcp-echo-interval 10
lcp-echo-failure 2
EOF

  cat <<EOF > /etc/ppp/pppoe-server-options
# PPPoE server options
require-pap
login
lcp-echo-interval 10
lcp-echo-failure 2
EOF

  cat <<EOF > /etc/ppp/pppoe-server-up
#!/bin/bash

CLIENT_ACCOUNT="$PEERNAME"
MAPPING_FILE="/etc/ppp/account_mapping"

MAPPING_ENTRY=$(grep "^${CLIENT_ACCOUNT}" ${MAPPING_FILE})
ISP_ACCOUNT=$(echo ${MAPPING_ENTRY} | awk '{print $2}')
VLAN=$(echo ${MAPPING_ENTRY} | awk '{print $3}')

if [ -z "${ISP_ACCOUNT}" ]; then
  echo "No matching ISP account found for client account ${CLIENT_ACCOUNT}"
  exit 1
fi

PEER_FILE="/etc/ppp/peers/${ISP_ACCOUNT}"

if [ ! -f "${PEER_FILE}" ]; then
  echo "Peer file ${PEER_FILE} not found for ISP account ${ISP_ACCOUNT}"
  exit 1
fi

# 确保VLAN接口已配置
if [ -n "${VLAN}" ]; then
  if ! ip link show ${ISP_INTERFACE}.${VLAN} &> /dev/null; then
    vconfig add ${ISP_INTERFACE} ${VLAN}
    ifconfig ${ISP_INTERFACE}.${VLAN} up
  fi
fi

pppd call ${ISP_ACCOUNT}
EOF

  chmod +x /etc/ppp/pppoe-server-up
}

# 运行所有配置步骤
main() {
  install_packages
  load_vlan_module
  enable_services
  detect_interfaces

  # 确认用户的选择
  echo "已选择ISP网卡：${ISP_INTERFACE}"
  echo "已选择客户端网卡：${CLIENT_INTERFACE}"

  configure_accounts
  configure_pppoe_relay
  configure_firewall
  configure_ppp_server

  echo "配置完成。代理服务器已准备好自动处理客户端拨号请求。"
}

main
