#!/bin/sh
NAME=bypass

uci_get_by_name(){
	ret=$(uci -q get $NAME.$1.$2)
	echo ${ret:=$3}
}

uci_get_by_type(){
	ret=$(uci -q get $NAME.@$1[0].$2)
	echo ${ret:=$3}
}

redir_tcp=$1
kcp=$2
redir_udp=$3
redir_nf=$4
dns=$5
chinadns=$6
socks5=$7
server_count=$8
server=`cat /tmp/etc/$NAME.include | grep '\-A SS_SPEC_WAN_AC \-d' | grep RETURN | awk '{print$4}' | sed 's/\/.*//'`
GLOBAL_SERVER=$(uci_get_by_type global global_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)
gfw_mode=$(uci_get_by_type global gfw_mode 0)
dns_mode=$(uci_get_by_type global dns_mode 1)
LOG=/tmp/log/$NAME.log
SDNS=/tmp/etc/smartdns
DNS_T=$SDNS/smartdns-by.conf
MDNS=/tmp/etc/mosdns
MDNS_T=$MDNS/mosdns-by.yaml
NAME=bypass
VAR=/tmp/etc/$NAME
[ "$password" != "" ] && password="--key "${password}

log() {
	echo "$(date +'%Y-%m-%d %H:%M:%S') $*" >>$LOG
}

re(){
	/etc/init.d/$NAME stop
	/etc/init.d/$NAME start
}

while :;do
	sleep 30
#redir tcp

	if [ $redir_tcp -gt 0 ] && [ $(busybox ps -w |  grep by-retcp  | grep -v grep | wc -l)  = 0 ];then
			log "Redir TCP :$redir_tcp Error. Restart!"
		re
	fi
#kcptun
	if [ $kcp = 1 ] && [ $(busybox ps -w | grep kcptun-client | grep -v grep | wc -l) = 0 ];then
			log "SSR KCPTUN:$kcp Error. Restart!"
			killall -q -9 kcptun-client >/dev/null 2>&1
			$(which kcptun-client) -r $server:$kcp_port -l :$server_port $password $kcp_param >/dev/null 2>&1 &
	fi
#redir udp
	if [ $redir_udp = 1 ];then
		t=`busybox ps -w | grep by-reudp | grep -v grep | wc -l`
		if [ $t = 0 ];then
			log "Redir UDP :$redir_udp Error. Restart!"
			re
		fi
	fi
#redir netflix
	if [ $redir_nf = 1 ] && [ $(busybox ps -w | grep by-nf | grep -v grep | wc -l) = 0 ];then
		log "Redir Netflix :$redir_nf Error. Restart!"
		re
	fi

#dns
	if [ "$dns" -eq 1 ] && [ $(busybox ps -w | grep smartdns | grep -v grep | wc -l) = 0 ];then
			log "SmartDNS :$dns Error. Restart!"
			kill -9 $(busybox ps -w | grep smartdns | grep -v grep | awk '{print$1}') 2>/dev/null
			service_start $(which smartdns) -c $DNS_T 2>/dev/null
	elif [ "$dns" -eq 2 ]&& [ $(busybox ps -w | grep mosdns | grep -v grep | wc -l) = 0 ];then
			log "MosDNS :$dns Error. Restart!"
			kill -9 $(busybox ps -w | grep mosdns | grep -v grep | grep $MDNS_T | awk '{print $1}') >/dev/null 2>&1
			service_start $(which mosdns) start -c $MDNS_T 2>/dev/null
	fi
#chinadns-ng
	if [ $chinadns = 1 ] && [ $(busybox ps -w | grep chinadns-ng | grep -v grep  | wc -l) = 0 ];then
			log "ChinaDNS-NG :$chinadns Error. Restart!"
			kill -9 $(busybox ps -w | grep chinadns-ng | grep -v grep | awk '{print$1}') 2>/dev/null
		        if [ "$dns" -eq 1 ] ;then
			    # service_start $(which chinadns-ng) -l 5337 -c '127.0.0.1#5336' -t '127.0.0.1#5335' -4 china_v4  -p 3  -6 china_v6 -f -n $([ $gfw_mode = 1 ] && echo -g /tmp/etc/bypass/gfw.list) >/dev/null 2>&1 &
			    service_start $(which chinadns-ng) -l 5337 -c '127.0.0.1#5336' -t '127.0.0.1#5335' -4 china_v4  -p 3 -6 china_v6 -f -n $([ $gfw_mode = 1 ] && echo -g $VAR/gfw.list)  >/dev/null 2>&1 &
		        else
			   bootstrap_dns=$(uci_get_by_type global bootstrap_dns '114.114.114.114')
			   service_start $(which chinadns-ng) -l 5337 -c "${bootstrap_dns}#53" -t '127.0.0.1#5335' -4 china_v4  -p 3 -6 china_v6 -f -n $([ $gfw_mode = 1 ] && echo -g $VAR/gfw.list)  >/dev/null 2>&1 &
		        fi
	fi
#localsocks
	if [ $socks5 = 1 ] && [ $(busybox ps -w | grep by-socks | grep -v grep  | wc -l) = 0 ];then
		log "Global Socks Server :$socks5 Error. Restart!"
		re
	fi
#server
	if [ $server_count -gt 0 ] && [ $(busybox ps -w | grep by-server | grep -v grep  | wc -l) -lt $server_count ];then
		log "SSR Server :$server_count Error. Restart!"
		re
	fi
done
