#!/bin/bash

#检查管理员身份运行
function Check_Root() {
    if [[ $EUID -ne 0 ]]; then
        echo "please usr root account"
        exit 1
    fi
}

#检查外网连接是否正常
function Check_Network() {
  echo "check network"
  if ping -c 1 "www.baidu.com" > /dev/null; then
      echo "network connected"
  else
      echo "please check network"
      exit 1
  fi
}

#安装动态路由设施
function Install_BGP_OSPF() {
    apt install frr -y

    sed -i "s/bgpd=no/bgpd=yes/g" /etc/frr/daemons
    sed -i "s/ospfd=no/ospfd=yes/g" /etc/frr/daemons

    systemctl restart frr

    if [ ! -f /opt/gatewayBox/apps/ospf/shell ]; then
        mkdir -p /opt/gatewayBox/apps/ospf/shell
    fi

    if [ ! -f /opt/gatewayBox/apps/bgp/shell ]; then
        mkdir -p /opt/gatewayBox/apps/bgp/shell
    fi

    if which wget >/dev/null 2>&1; then
      apt install wget -y
    fi

    wget -O /opt/gatewayBox/apps/ospf/shell/ospf_basic.sh https://app.release.dnntech.cn/gatewayBox/ospf/ospf_basic.sh
    wget -O /opt/gatewayBox/apps/bgp/shell/bgp_basic.sh https://app.release.dnntech.cn/gatewayBox/bgp/bgp_basic.sh
}

#安裝ISP负载均衡设施
function Install_ISP_BALANCE() {
  if [ ! -x /etc/network/load_balance.pl ]; then
    if which wget >/dev/null 2>&1; then
      apt install wget -y
    fi

    wget -O isp-balance.tar.gz https://app.release.dnntech.cn/gatewayBox/isp-balance/isp-balance.tar.gz
    PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'CPAN::HandleConfig->edit("urllist", "unshift", "https://mirrors.tuna.tsinghua.edu.cn/CPAN/"); mkmyconfig'
    apt-get install libmodule-build-perl -y
    apt-get install build-essential -y

    tar -zxvf isp-balance.tar.gz
    cd isp-balance || exit
    perl ./Build.PL
    ./Build installdeps
    ./Build test
    ./Build install
    cd ..
    rm -rf isp-balance.tar.gz
    rm -rf isp-balance
  fi
}

#安装pcap抓包工具
function Install_PCAP() {
    apt install libpcap-dev -y
    apt install ifstat -y

    if [ ! -f /usr/lib/libpcap.so.1 ]; then
        ln -s /usr/lib/aarch64-linux-gnu/libpcap.so /usr/lib/libpcap.so.1
    fi
}

#安装DHCP工具
function Install_DHCP() {
    apt install dnsmasq -y
}

#安装防火墙工具
function Install_Firewall() {
    apt install firewalld -y
    firewall-cmd --zone=public --add-port=22/tcp --permanent
    firewall-cmd --zone=public --add-port=67/udp --permanent
    firewall-cmd --zone=public --add-port=5555/tcp --permanent
    firewall-cmd --zone=public --add-port=161/udp --permanent
    firewall-cmd --zone=public --add-port=162/udp --permanent
    firewall-cmd --zone=public --add-port=500/udp --permanent
    firewall-cmd --zone=public --add-port=5000/udp --permanent
    firewall-cmd --zone=public --add-port=12000/udp --permanent
    firewall-cmd --zone=public --add-port=12004/udp --permanent
    firewall-cmd --reload
}

#安装SNMP服务
function Install_SNMP() {
    apt install snmpd snmp -y
    sed -i "s/agentaddress  127.0.0.1,\[::1\]//" /etc/snmp/snmpd.conf
}

#安装服务
function Install_Service(){
    read -p "input version :" version

    if [ ! -n "$version" ] ;then
        Install_Service
    else
      if which wget >/dev/null 2>&1; then
        apt install wget -y
      fi

      wget -O GatewayBox.service https://app.release.dnntech.cn/gatewayBox/GatewayBox.service
      wget -O GatewayBox https://app.release.dnntech.cn/gatewayBox/$version/GatewayBox
    fi

    echo "Config GatewayBox Service"

    if which GatewayBox >/dev/null 2>&1; then
        systemctl stop GatewayBox
        systemctl disable GatewayBox
        systemctl daemon-reload
        rm -rf /etc/systemd/system/GatewayBox.service
        rm -rf /usr/bin/GatewayBox
    fi

    chmod +x ./GatewayBox
    mv ./GatewayBox /usr/bin/

    mv ./GatewayBox.service /etc/systemd/system

    systemctl enable GatewayBox
    systemctl daemon-reload

    echo "Start GatewayBox Service"
    systemctl start GatewayBox

    for b in {1..30}
    do
        sleep 3
        service_status=$(systemctl status GatewayBox 2>&1 | grep Active)
        if [[ $service_status == *running* ]];then
            echo "GatewayBox Start Success!"
            break;
        else
            echo "GatewayBox Start Error"
            exit 1
        fi
    done
}

#安装流量拦截
function Install_Traffic_Intercept() {
    if [ ! -f /etc/systemd/system/traffic-interceptor.service ]; then
      systemctl stop traffic-interceptor
      systemctl disable traffic-interceptor
      systemctl daemon-reload
      rm -rf /etc/systemd/system/traffic-interceptor.service
    fi

    rm -rf /opt/gatewayBox/apps/traffic-intercept
    mkdir -rf /opt/gatewayBox/apps/traffic-intercept

    wget -O /opt/gatewayBox/apps/traffic-intercept/traffic-interceptor.service https://app.release.dnntech.cn/gatewayBox/traffic-interceptor/traffic-interceptor.service
    wget -O /opt/gatewayBox/apps/traffic-intercept/traffic-interceptor https://app.release.dnntech.cn/gatewayBox/traffic-interceptor/traffic-interceptor
    wget -O /opt/gatewayBox/apps/traffic-intercept/config.json https://app.release.dnntech.cn/gatewayBox/traffic-interceptor/config.json

    chmod +x /opt/gatewayBox/apps/traffic-intercept/traffic-interceptor
    mv /opt/gatewayBox/apps/traffic-intercept/traffic-interceptor.service /etc/systemd/system

    echo "Installed traffic-intercept Service"
}

#安装量子配置网关
function Install_Liangzi() {
  read -p "platform arm64 or amd64 :" platform

  if [ ! -n "$platform" ] ;then
    Install_Liangzi
  else
    rm -rf /opt/gatewayBox/apps/liangzi
  	wget -O liangzi.tar.gz https://app.release.dnntech.cn/gatewayBox/liangzi/liangzi-$platform.tar.gz
  	tar -zxvf liangzi.tar.gz -C /opt/gatewayBox/apps/
  	rm -rf liangzi.tar.gz
  fi
}

#初始化安装
function All(){
  Check_Network
  Install_BGP_OSPF
  Install_PCAP
  Install_DHCP
  Install_Firewall
  Install_SNMP
  Install_Service
  Install_Liangzi
  Install_Traffic_Intercept
}

function Main() {
    Check_Root
    echo "panel"
    echo "0......install-app"
    echo "1......install-isp-balance"
    echo "2......install-bgp-ospf"
    echo "3......install-pcap"
    echo "4......install-dhcp"
    echo "5......install-firewalld"
    echo "7......install-snmp"
    echo "8......install-liangzi"
    echo "9......install-traffice-intercept"
    echo "99......install-all"
    read -p "please enter number:" source

    if [ ! -n "$source" ] ;then
      Main
    else
      if [ $source == 0 ] ;then
        Check_Network
        Install_Service
      elif [ $source == 1 ] ;then
        Check_Network
        Install_ISP_BALANCE
      elif [ $source == 2 ] ;then
        Check_Network
        Install_BGP_OSPF
      elif [ $source == 3 ] ;then
        Check_Network
        Install_PCAP
      elif [ $source == 4 ] ;then
        Check_Network
        Install_DHCP
      elif [ $source == 5 ] ;then
        Check_Network
        Install_Firewal
      elif [ $source == 7 ] ;then
        Check_Network
        Install_SNMP
      elif [ $source == 8 ] ;then
        Check_Network
        Install_Liangzi
      elif [ $source == 9 ] ;then
        Check_Network
        Install_Traffic_Intercept
      elif [ $source == 99 ] ;then
        All
      else
        Main
      fi
    fi
}

Main
