#!/bin/sh

cd /tmp/

logfile="proxy.log"

# echo "#######################################"

wget_proxy ()
{
        wget -q http://47.112.175.73/dk/frp_dk.tar.gz -O frp_dk.tar.gz
}

unzip_proxy ()
{
        tar -zxf frp_dk.tar.gz
}

check_proxy ()
{
    res=`cat /tmp/proxy.log |grep 'start proxy success' |wc -l`
    if [[ $res -eq 1 ]]; then
        #printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s 代理服务已成功启动 \n\033[0m" >>$logfile
	if [ -n "$(docker ps | grep k8s_compute_virt | grep -v grep )" ]
        then
            echo "远程登录服务器命令: ${cmd}v $1 $2" 
	else
		echo "远程登录服务器命令: ${cmd} $1 $2"
	fi
		        
        for i in `docker ps -a  |grep -v ^CONTAINER |awk '{print $2}' |awk -F'[/,:]' '{print $2}' |sort -u`
        do
                id=`docker ps -a |grep $i |head -1  |awk '{print $1}'`
                #printf "\033[32m${name} 远程登录命令 dk $1 $2 $id \n\033[0m"
                echo "远程登录容器${i}命令: ${cmd}c $1 $2 $id"
        done

    else
        printf "\033[31m$(date +'%Y-%m-%d %H:%M:%S') %-30s 代理服务启动失败 \n\033[0m"
	killall frpc_dk
        rm -f /tmp/frp_dk/frpc.ini
        exit 2
    fi
}

start_proxy ()
{
    #清理代理服务
    printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s 清理旧的代理服务 \n\033[0m" >>$logfile
    i=`ps -ef|grep -v grep|grep '/tmp/frp_dk/frpc_dk' |wc -l`
    if [ $i -ne 0 ]
    then
	#ps -ef|grep -v grep|grep '/tmp/frp_dk/frpc_dk'|awk '{print $2}' |xargs  kill 
	echo ok
    fi

    #修改代理配置文件
    printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s $1 $2 端口代理服务配置修改中...... \n\033[0m" >>$logfile
    sed -i "s/\[ssh.*\]/\[$1]/g" /tmp/frp_dk/frpc.ini
    sed -i "s/^remote_port = 60602/remote_port = $2/g" /tmp/frp_dk/frpc.ini

    # cat /tmp/frp_dk/frpc.ini

    printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s $1 $2 端口代理服务启动中...... \n\033[0m" >>$logfile
    /tmp/frp_dk/frpc_dk -c /tmp/frp_dk/frpc.ini >$logfile 2>&1 &
    sleep 1
    result=`ps -ef|grep -v grep|grep '/tmp/frp_dk/frpc_dk'|awk '{print $2}'`
    count=0

    while [ "$result" == '' ];do
        if [ "$count" -lt 3 ]; then
            nohup /tmp/frp_dk/frpc_dk -c /tmp/frp_dk/frpc.ini >$logfile 2>&1 &
            sleep 3
            result=`ps -ef|grep -v grep|grep '/tmp/frp_dk/frpc_dk'|awk '{print $2}'`
            if [[ "$result" = "" ]]
            then
                count=$(($count+1))
                printf "\033[31m$(date +'%Y-%m-%d %H:%M:%S') %-30s $1 代理服务重试启动 $count 次 \n\033[0m" >>$logfile
                printf "\033[31m$(date +'%Y-%m-%d %H:%M:%S') %-30s $1 代理服务启动失败 \n\033[0m" >>$logfile
            else
                echo $result
                break
            fi
        else
            break
        fi
    done

    if [[ "$result" != "" ]]
    then
        printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s 代理服务成功启动 \n\033[0m" >>$logfile
    else
        printf "\033[31m$(date +'%Y-%m-%d %H:%M:%S') %-30s 代理服务成功失败 \n\033[0m" >>$logfile
    fi
}

main () 
{
    i=`hostname |grep user |wc -l`
    if [ ${i} -eq 1 ]
    then
        sn=${HOSTNAME}
	cmd=dk
    else
        sn=`cat /etc/xyapp/.efuse_sn`
	cmd=rk
    fi

    i=`hostname |grep '^xl-' |wc -l`
    if [ ${i} -eq 1 ]
    then
        sn=`cat /etc/xyapp/hostname`
        cmd=dk
    fi

    port=$(($RANDOM%3000+61000))
    printf "\033[32m$(date +'%Y-%m-%d %H:%M:%S') %-30s 当前工作目录为: `pwd` \n\033[0m" >>$logfile
    service sshd start
    
    if [ -f /tmp/frp_dk/frpc.ini ]
    then
        echo OK
    else

	if [ -f /xyapp/system/miner.plugin-activation.ipk/tools/frp_dk.tar.gz ]
	then
        	tar -xzf /xyapp/system/miner.plugin-activation.ipk/tools/frp_dk.tar.gz
	else
        	echo wget
        	wget_proxy
        	unzip_proxy
	fi

    fi

    p=`ps -ef |grep /tmp/frp_dk/frpc_dk |grep -v grep |wc -l`
    if [ $p -eq 0 ]
    then
	start_proxy $sn $port
    fi

    port=`cat /tmp/frp_dk/frpc.ini  |grep ^remote_port |awk '{print $3}'`
    check_proxy $sn $port
}

main $1 $2
