#!/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.
#
. $IPKG_INSTROOT/etc/init.d/shadowsocksr
LOCK_FILE="/var/lock/ssr-monitor.lock"
[ -f "$LOCK_FILE" ] && exit 2
touch "$LOCK_FILE"
server_process_count=$1
redir_tcp_process=$2
redir_udp_process=$3
kcp_process=$4
local_process=$5
pdnsd_process=$6
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)
kcp_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}

while [ "1" == "1" ]; do #死循环
	sleep 000030s
	#redir tcp
	if [ "$redir_tcp_process" -gt 0 ]; then
		icount=$(busybox ps -w | grep ssr-retcp | grep -v grep | wc -l)
		if [ "$icount" == 0 ]; then
			logger -t "$NAME" "ssrplus redir tcp error.restart!"
			echolog "ssrplus redir tcp error.restart!"
			/etc/init.d/shadowsocksr restart
			exit 0
		fi
	fi
	#redir udp
	if [ "$redir_udp_process" -gt 0 ]; then
		icount=$(busybox ps -w | grep ssr-reudp | grep -v grep | wc -l)
		if [ "$icount" == 0 ]; then
			logger -t "$NAME" "ssrplus redir udp error.restart!"
			echolog "ssrplus redir udp error.restart!"
			/etc/init.d/shadowsocksr 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" "ssrplus server error.restart!"
			echolog "ssrplus server error.restart!"
			kill -9 $(busybox ps -w | grep ssr-server | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			/etc/init.d/shadowsocksr restart
			exit 0
		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" "ssrplus kcptun error.restart!"
			echolog "ssrplus kcptun error.restart!"
			killall -q -9 kcptun-client
			(/usr/bin/kcptun-client -r $server:$kcp_port -l :$server_port $password $kcp_param &)
		fi
	fi
	#localsocks
	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" "global socks server error.restart!"
			echolog "global socks server error.restart!"
			kill -9 $(busybox ps -w | grep ssr-local | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			/etc/init.d/shadowsocksr restart
			exit 0
		fi
	fi
	#dns2tcp
	if [ "$pdnsd_process" -eq 1 ]; then
		icount=$(busybox ps -w | grep $TMP_BIN_PATH/dns2tcp | grep -v grep | wc -l)
		if [ "$icount" -lt 1 ]; then #如果进程挂掉就重启它
			logger -t "$NAME" "dns2tcp tunnel error.restart!"
			echolog "dns2tcp tunnel error.restart!"
			dnsserver=$(uci_get_by_type global tunnel_forward 8.8.4.4:53)
			kill -9 $(busybox ps -w | grep $TMP_BIN_PATH/dns2tcp | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			ln_start_bin $(first_type dns2tcp) dns2tcp -L "127.0.0.1#$dns_port" -R "${dnsserver/:/#}"
		fi
	#dns2socks
	elif [ "$pdnsd_process" -eq 2 ]; then
		icount=$(busybox ps -w | grep -e ssrplus-dns -e "dns2socks 127.0.0.1 $tmp_dns_port" | grep -v grep | wc -l)
		if [ "$icount" -lt 2 ]; then #如果进程挂掉就重启它
			logger -t "$NAME" "dns2socks $dnsserver tunnel error.restart!"
			echolog "dns2socks $dnsserver tunnel error.restart!"
			dnsserver=$(uci_get_by_type global tunnel_forward 8.8.4.4:53)
			kill -9 $(busybox ps -w | grep ssrplus-dns | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			kill -9 $(busybox ps -w | grep "dns2socks 127.0.0.1 $tmp_dns_port" | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_dns_port ssrplus-dns
			ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_dns_port $dnsserver 127.0.0.1:$dns_port -q
		fi
	fi
	#chinadns-ng
	if [ "$(uci -q get "dhcp.@dnsmasq[0]._unused_ssrp_changed")" = "1" ]; then
		icount=$(busybox ps -w | grep $TMP_BIN_PATH/chinadns-ng | grep -v grep | wc -l)
		if [ "$icount" -lt 1 ]; then #如果进程挂掉就重启它
			logger -t "$NAME" "chinadns-ng tunnel error.restart!"
			echolog "chinadns-ng tunnel error.restart!"
			chinadns=$(uci_get_by_type global chinadns_forward)
			wandns="$(ifstatus wan | jsonfilter -e '@["dns-server"][0]' || echo "119.29.29.29")"
			case "$chinadns" in
			"wan") chinadns="$wandns" ;;
			""|"wan_114") chinadns="$wandns,114.114.114.114" ;;
			esac
			kill -9 $(busybox ps -w | grep $TMP_BIN_PATH/chinadns-ng | grep -v grep | awk '{print $1}') >/dev/null 2>&1
			ln_start_bin $(first_type chinadns-ng) chinadns-ng -l $china_dns_port -4 china -p 3 -c ${chinadns/:/#} -t 127.0.0.1#$dns_port -N -f -r
		fi
	fi
done
