#!/bin/bash

# 添加了端口占用的检测

# todo
# 对某一代理内容的删除

# 当运行时， 可以传入额外的参数
##   ./runParams.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]

#########color code#############
RED="31m"    # Error message
GREEN="32m"  # Success message
YELLOW="33m" # Warning message
BLUE="36m"   # Info message

###############color echo func#################
colorEcho() {
    echo -e "\033[${1}${@:2}\033[0m" 1>&2
}

Help() {
    cat - 1>&2 <<EOF
    本脚本的用法如下：
    [-tcp t] <代理端口> <需要转发的IP> <需要转发的端口>
    [-http] <反向代理域名> <代理端口> <反代的IP> <反代的端口>
    [-h -help]
    -tcp/t <代理端口>代表本机的端口 <需要转发的IP>代表反向代理的ip地址 
           <需要转发的端口>代表反向代理的端口
    -http <反向代理域名>代表转发的url <代理端口>代表本机的端口 
          <反代的IP>代表反向代理的ip地址 <反代的端口>代表反向代理的端口
    -h/help  打印本说明文档

    例子：./addNginxProxy.sh -tcp 9500 192.168.1.248 80
        即为将本地9500端口 转发至 192.168.1.248的80端口！
EOF
}

checkPortInUse(){
    # 如果端口被占用则返回错误
    # 如果端口未占用则返回0
    netstat -ntlp | awk '{print $4}' | cut -d ":" -f 2 | sed -n "3,+99999p"
    if [[ `netstat -ntlp | awk '{print $4}' | cut -d ":" -f 2 | sed -n "3,+99999p" | grep -w ${FRONT_PROXY_PORT} &>/dev/null` -eq 0 ]] 
    then
        colorEcho ${RED} "你想要使用的代理端口 ${FRONT_PROXY_PORT} 已被占用，请重新选用端口！！"
        return 23
    else
        return 0
    fi    
}

addPortForward() {
   
    #判断REVERSE_PROXY_IP是否符合IP的规格
    #判断REVERSE_PROXY_PORT是否被占用

    ## 端口转发模板文件简单，直接写入此处！！
    cat > /etc/nginx/conf.d/stream/${FRONT_PROXY_PORT}_${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf <<EOF
server {
    listen ${FRONT_PROXY_PORT};
    proxy_pass ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT};
}
EOF
    if [[ -a /etc/nginx/conf.d/stream/${FRONT_PROXY_PORT}_${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf ]]; then
        colorEcho ${GREEN} "端口转发配置成功！！"
        nginx -t && nginx -s reload 
        if [[ $? -eq 0 ]];then
            LOCAL_INTERNAL_IP=$(ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/')
            colorEcho ${GREEN} "nignx已成功重启"
            colorEcho ${BLUE} "端口转发为：   ${LOCAL_INTERNAL_IP}:${FRONT_PROXY_PORT} >>>> ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT}"
        else
            colorEcho ${RED} "nginx重启失败，请检查是否端口已被使用！"
        fi
    else
        colorEcho ${RED} "端口转发文件添加失败，请手动添加！！"
        return 34
    fi
    return 0
}

addReverProxy() {
    
    #判断REVERSE_PROXY_IP是否符合IP的规格
    #判断REVERSE_PROXY_PORT是否被占用
    # 读取反向代理的模板然后写入相应的特定反向代理文件
    if [[ -a /etc/nginx/conf.d/reverse_proxy_demo ]] 
    then
        cat > /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf < /etc/nginx/conf.d/reverse_proxy_demo
    else
        colorEcho ${RED} "反向代理的模板文件丢失!!! 无法添加反向代理！"
        return 45
    fi
    
    if [[ -a /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf ]]; then
        sed -i -e "s/\${REVERSE_PROXY_SERVER_NAME}/${REVERSE_PROXY_SERVER_NAME}/g" \
            -e "s/\${FRONT_PROXY_PORT}/${FRONT_PROXY_PORT}/g" \
            -e "s/\${REVERSE_PROXY_IP}/${REVERSE_PROXY_IP}/g" \
            -e "s/\${REVERSE_PROXY_PORT}/${REVERSE_PROXY_PORT}/g" \
        /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf
        colorEcho ${GREEN} "反向代理文件添加成功！！"
        nginx -t && nginx -s reload 
        if [[ $? -eq 0 ]];then
            colorEcho ${GREEN} "nignx已成功重启！！"
            colorEcho ${BLUE} "反向代理为：   ${REVERSE_PROXY_SERVER_NAME}:${FRONT_PROXY_PORT} >>>> ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT}"
        else
            colorEcho ${RED} "nginx重启失败，请检查是否端口已被使用！"
        fi
    else
        colorEcho ${RED} "反向代理配置文件添加失败，请手动添加！！"
        return 34
    fi
    return 0
}

#########################

if [[ $# > 0 ]]
then
case "$1" in
-tcp | -t)
    if [[ $# == 4 ]]; then
        FRONT_PROXY_PORT="${2}"
        REVERSE_PROXY_IP="${3}"
        REVERSE_PROXY_PORT="${4}"
        if [[ `checkPortInUse` -eq 0 ]] 
        then
            addPortForward
        else
            return 23
        fi
    else
        colorEcho ${RED} "输入的参数有误，请重新输入！"
        return 123
    fi
    ;;
-h | --help)
    Help
    ;;
-http)
    if [[ $# == 5 ]]; then
        REVERSE_PROXY_SERVER_NAME="${2}"
        FRONT_PROXY_PORT="${3}"
        REVERSE_PROXY_IP="${4}"
        REVERSE_PROXY_PORT="${5}"
        if [[ `checkPortInUse -ne 0` ]] 
        then
            addReverProxy
        fi
    else
        colorEcho ${RED} "输入的参数有误，请重新输入！"
        return 123
    fi
    ;;
*)
    # unknown option
    colorEcho ${RED} "输入的参数不正确！"
    Help
    return 1
    ;;
esac
else
    colorEcho ${BLUE} "请输入参数！！本脚本的用法如下！！"
    Help
fi

