#!/bin/sh
#
# Copyright (C) 2017 openwrt-ssr
# Copyright (C) 2017 yushi studio <ywb94@qq.com>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

NAME=vssr

uci_get_by_name() {
	local ret=$(uci get $NAME.$1.$2 2>/dev/null)
	echo ${ret:=$3}
}

uci_get_by_type() {
	local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
	echo ${ret:=$3}
}

server_process_count=$1
redir_tcp_process=$2
redir_udp_process=$3
tunnel_process=$4
kcp_process=$5
local_process=$6
pdnsd_process=$7
if [ -z "$pdnsd_process" ]; then
	pdnsd_process=0
fi

i=0

GLOBAL_SERVER=$(uci_get_by_type global global_server)
server=$(uci_get_by_name $GLOBAL_SERVER server)
lkcp_port=$(uci_get_by_name $GLOBAL_SERVER kcp_port)
server_port=$(uci_get_by_name $GLOBAL_SERVER server_port)
password=$(uci_get_by_name $GLOBAL_SERVER kcp_password)
kcp_param=$(uci_get_by_name $GLOBAL_SERVER kcp_param)
[ "$password" != "" ] && password="--key "${password}

sock5_port=$(uci_get_by_type socks5_proxy local_port 1080)

if echo $server | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
	server=${server}
else
	server=$(cat /etc/ssr_ip)
fi

while [ "1" = "1" ]; do #这里是个死循环
	sleep 30
	#redir tcp
	if [ $redir_tcp_process -gt 0 ]; then
		icount=$(busybox ps -w | grep vssr_t | grep -v grep | wc -l)
		if [ $icount = 0 ]; then
			logger -t "$NAME" "ssr redir tcp error.restart!"
			/etc/init.d/vssr restart
			exit 0
		fi
	fi
	#redir udp
	if [ $redir_udp_process -gt 0 ]; then
		icount=$(busybox ps -w | grep vssr_u | grep -v grep | wc -l)
		if [ $icount = 0 ]; then
			logger -t "$NAME" "ssr redir udp error.restart!"
			/etc/init.d/vssr restart
			exit 0
		fi
	fi
	#tunnel
	if [ $tunnel_process -gt 0 ]; then
		icount=$(busybox ps -w | grep ssr-tunnel | grep -v grep | wc -l)
		if [ $icount = 0 ]; then
			logger -t "$NAME" "ssr tunnel error.restart!"
			/etc/init.d/vssr restart
			exit 0
		fi
	fi
	#server
	if [ $server_process_count -gt 0 ]; then
		icount=$(busybox ps -w | grep ssr-server | grep -v grep | wc -l)
		if [ $icount -lt $server_process_count ]; then
			logger -t "$NAME" "ssr server error.restart!"
			killall -q -9 ssr-server
			for i in $(seq $server_process_count); do
				/usr/bin/ssr-server -c /var/etc/vssr_$i.json -u -f /var/run/ssr-server$i.pid
			done
		fi
	fi
	#kcptun
	if [ $kcp_process -gt 0 ]; then
		icount=$(busybox ps -w | grep kcptun-client | grep -v grep | wc -l)
		if [ $icount -lt $kcp_process ]; then
			logger -t "$NAME" "ssr kcptun error.restart!"
			killall -q -9 kcptun-client

			(/usr/bin/kcptun-client -r $server:$kcp_port -l :$server_port $password $kcp_param &)
		fi
	fi
	#local
	#      if [ $local_process -gt 0 ] ;then
	#      icount=`busybox ps -w  | grep ssr-local |grep -v grep| wc -l`
	#      if [ $icount -lt $local_process ]
	#      then
	#      logger -t "$NAME" "ssr local error.restart!"
	#      killall -q -9 ssr-local

	#			( /usr/bin/ssr-local -c /var/etc/vssr_s.json -u -l $sock5_port -f /var/run/ssr-local.pid &)
	#      fi
	#      fi
	#pdnsd
	if [ $pdnsd_process -gt 0 ]; then
		icount=$(busybox ps -w | grep pdnsd | grep -v grep | wc -l)
		if [ $icount -lt $pdnsd_process ]; then #如果进程挂掉就重启它
			logger -t "$NAME" "pdnsd tunnel error.restart!"
			if [ -f /var/run/pdnsd.pid ]; then
				kill $(cat /var/run/pdnsd.pid) >/dev/null 2>&1
			else
				kill -9 $(ps | grep pdnsd | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			fi

			(/usr/sbin/pdnsd -c /var/etc/pdnsd.conf -d &)
		fi
	fi
done
