#!/bin/bash
ARGC=("$#")

if [ $ARGC -lt 1 ]; then
	echo "invalid option."
	exit -1
fi

export Option=$1

validation_func()
{
 if [ $? -ne 0 ]; then
	${1}
	exit -1
 fi
}

ARGC_func()
{
 if [ $ARGC -ne ${1} ]; then
	echo "invalid option : this option must be contain ${2} ARGC"
	exit -1
 fi
}

NGX_CONF="/etc/nginx/conf.d"
VSFFTPdir="/etc/vsftpd/users_config"
SOU_CONF="/home/vdopanel/core/conf-templates"
STUNNEL_CONF="/etc/stunnel/stunnel.conf"
cPanel_USER="/home/vdopanel/users"
RemoteConnLog="/home/vdopanel/core/logs/remoteconnection.log"
ENV_F="/home/vdopanel/public_html/.env"
DB_NAME=`cat ${ENV_F} | grep DB_DATABASE= | cut -d"=" -f2`
OS_CHK=`egrep ID= /etc/os-release | grep -v _ID | cut -d"=" -f2 | tr -d '"'`
Source_L="https://vdopanel.com/dist"

SERV_IP_FUNC()
{
  SERV_IP=`dig +time=1 +tries=1 TXT +short o-o.myaddr.l.google.com @ns1.google.com -4 2>/dev/null | tr -d '"'`
  if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
	IP_CHECK=`ipcalc -c "${SERV_IP}" | grep -i invalid >/dev/null 2>&1 && echo invalid_ip || echo valid_ip`
  else
	IP_CHECK=`ipcalc -cs "${SERV_IP}" && echo valid_ip || echo invalid_ip`
  fi
  if [[ ${IP_CHECK} == "invalid_ip" ]]; then
	SERV_IP=`curl -s ${Source_L}/whatisip.php`
  fi
}

G_COLOR1="--color"
G_COLOR2="=auto"
G_COLOR_="${G_COLOR1}${G_COLOR2}"
cPanel_chk="/usr/local/cpanel/version"

PHP_V_FUNC()
{
  if [[ ! -f ${cPanel_chk} ]]; then
	PHP_RUN="/usr/bin/php"
  else
	PHP_CHK=`/usr/local/cpanel/bin/whmapi1 php_get_installed_versions | grep ea-php`
	if [[ ${PHP_CHK} == *"ea-php83"* ]]; then
		PHP_RUN="/usr/local/bin/ea-php83"
	elif [[ ${PHP_CHK} == *"ea-php82"* ]]; then
		PHP_RUN="/usr/local/bin/ea-php82"
	else
		PHP_RUN="/usr/bin/php"
	fi
  fi
}

if [[ ! -f ${cPanel_chk} ]]; then
	HOME_PATH="/home"
else
	HOME_PATH="/home/vdopanel/users"
fi


if [[ ${Option} == "username" ]]; then
	ARGC_func 3 3
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/usermod -l ${3} -m -d /home/${3} ${2} 1>/dev/null
		validation_func "echo Error : change username"
		mv ${VSFFTPdir}/${2} ${VSFFTPdir}/${3}
		echo -e "local_root=/home/${3}/uploads\nwrite_enable=YES\n" > ${VSFFTPdir}/${3}
	else
#		if [[ -d "${HOME_PATH}/${3}" ]]; then
#			echo "Error : new user already exists."
#			exit -1
#		fi
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp delftp user=${2} destroy=0 >/dev/null 2>&1
		mv ${HOME_PATH}/${2} ${HOME_PATH}/${3}
		FTP_PASS=`mysql ${DB_NAME} -s -N -e "SELECT ftp FROM broadcasters WHERE name='${2}'"`
		FTP_quot=`mysql ${DB_NAME} -s -N -e "SELECT station_storage FROM broadcasters WHERE name='${2}'"`
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp addftp user=${3} pass=${FTP_PASS} quota=${FTP_quot} homedir=users/${3}/uploads >/dev/null 2>&1
	fi

	mv ${NGX_CONF}/${2}-rtmp.conf ${NGX_CONF}/${3}-rtmp.conf
	mv ${NGX_CONF}/${2}-http.http ${NGX_CONF}/${3}-http.http
	sed -i "s/${2}/${3}/g" "${NGX_CONF}/${3}-rtmp.conf"
	sed -i "s/${2}/${3}/g" "${NGX_CONF}/${3}-http.http"
	/bin/systemctl reload nginx.service
#	validation_func "echo Error : change username, nginx reload"
	echo -e "success-done"
fi


if [[ ${Option} == "deluser" ]]; then
	ARGC_func 4 4
	for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "utils deluser ${2}" | grep -v "G_COLOR_=" | grep ${2} | awk '{print $2}'`
	do
		kill -9 ${i}
	done
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/userdel -r ${2} >/dev/null 2>&1
#		validation_func "echo Error : delete username"
	else
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp delftp user=${2} destroy=1 >/dev/null 2>&1
		rm -rf ${HOME_PATH}/${2}
	fi
	rm -rf ${NGX_CONF}/${2}-rtmp* ${NGX_CONF}/${2}-http* /etc/nginx/loadb/${2}.balance
	rm -rf /etc/nginx/proxy/${2}.proxy
	rm -rf ${VSFFTPdir}/${2}
	rm -rf /home/vdopanel/public_html/public/uploads/playerpic/*${2}.*
	rm -rf /home/vdopanel/public_html/public/uploads/directorylists/${2}.*
	rm -rf /home/vdopanel/core/logs/*-${2}.log
	BANDW_TMP="/home/vdopanel/core/tmp/bandw"
#	LINE1=`sed -n "\|_${2}_vdo|=" "${STUNNEL_CONF}"`
#	LINE2=$( echo "${LINE1} + 5" | bc )
#	sed -i "${LINE1},${LINE2}d;" "${STUNNEL_CONF}"
#	/bin/systemctl reload stunnel
	KILL_LIST=("${3}" "${4}")
	for Port in "${KILL_LIST[@]}"
	do
		for i1 in `ps -aux | grep bandwidthupdate | grep -v "grep ${G_COLOR_}" | grep -v "utils deluser ${2}" | grep ${2} | grep ${Port} | awk '{print $2}'`
		do
			kill -9 ${i1}
		done
		for i2 in `ps -aux | grep iftop | grep -v "grep ${G_COLOR_}" | grep -v " \-c " | grep ${Port} | awk '{print $2}'`
		do
			kill -9 ${i2}
		done
	done
	rm -rf ${BANDW_TMP}/bandw-${3} ${BANDW_TMP}/bandw-${4}
	rm -rf /home/vdopanel/core/tmp/pids/${3}.pid /home/vdopanel/core/tmp/pids/${4}.pid /home/vdopanel/core/tmp/pids/${2}_*
	Remove_FireW=`/usr/sbin/iptables -L -n -v --line-numbers | grep "${3},${4}" | grep "reject-with\|_LOCK_" | awk '{print $1}' | tac`
	for R_FW in ${Remove_FireW}
	do
		/usr/sbin/iptables -D INPUT ${R_FW} >/dev/null 2>&1
	done

	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		netfilter-persistent save >/dev/null 2>&1
	else
		service iptables save >/dev/null 2>&1
	fi

	/home/vdopanel/core/utils fixstunnel >/dev/null 2>&1
	/bin/systemctl reload nginx.service
#	validation_func "echo Error : delete username, nginx reload"
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/userdel -r ${2} >/dev/null 2>&1
	fi
	echo -e "success-done"
fi


if [[ ${Option} == "ftppass" ]]; then
	ARGC_func 3 3
	if [[ ! -f ${cPanel_chk} ]]; then
		echo -e "${3}\n${3}" | passwd ${2} >/dev/null 2>&1
		validation_func "echo Error : change ftp password"
	else
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp passwd user=${2} pass=${3} >/dev/null 2>&1
	fi
	echo -e "success-done"
fi


if [[ ${Option} == "changeport" ]]; then
	ARGC_func 5 5
	CHK_PORT1=`netstat -an | grep ":${4} "`
	if [[ ${CHK_PORT1} ]]; then
		echo -e "Error : new ${5} port ${4} already in use."
		exit -1
	fi
	if [[ ${5} == "rtmp" ]]; then
		Port_stunn_old1="1${3:1}"
		Port_stunn_old2="4${3:1}"
		Port_stunn_new1="1${4:1}"
		Port_stunn_new2="4${4:1}"
#		sed -i "s/listen ${3}/listen ${4}/g" "${NGX_CONF}/${2}-rtmp.conf"
		sed -i "s/${3}/${4}/g" "${NGX_CONF}/${2}-rtmp.conf"
		sed -i "s/accept = ${Port_stunn_old1}/accept = ${Port_stunn_new1}/g" "${STUNNEL_CONF}"
		sed -i "s/accept = ${Port_stunn_old1}/accept = ${Port_stunn_new2}/g" "${STUNNEL_CONF}"
	elif [[ ${5} == "http" ]]; then
		sed -i "s/listen ${3}/listen ${4}/g" "${NGX_CONF}/${2}-http.http"
		/home/vdopanel/core/utils rebuildproxy ${2} >/dev/null 2>&1
	else
		echo "Port type is wrong."
		exit -1
	fi

	for i1 in `ps -aux | grep bandwidthupdate | grep -v "grep ${G_COLOR_}" | grep -v "utils changeport ${2} " | grep ${2} | grep ${3} | awk '{print $2}'`
	do
		kill -9 ${i1}
	done
	for i2 in `ps -aux | grep iftop | grep -v "grep ${G_COLOR_}" | grep -v " \-c " | grep ${3} | awk '{print $2}'`
	do
		kill -9 ${i2}
	done
	rm -rf /home/vdopanel/core/tmp/pids/${3}.pid

	/bin/systemctl reload nginx.service
	validation_func "echo Error : change port, nginx reload"
	/bin/systemctl restart stunnel >/dev/null 2>&1
	echo -e "success-done"
fi


if [[ ${Option} == "storageupdate" ]]; then
	ARGC_func 3 3
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/setquota -u ${2} ${3}M ${3}M 0 0 / >/dev/null 2>&1
		/home/vdopanel/core/utils fixquota >/dev/null 2>&1
#		validation_func "echo Error : update storage"
	else
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp setquota user=${2} quota=${3} >/dev/null 2>&1
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "storageshow" ]]; then
	ARGC_func 2 2
	U_PATH="${HOME_PATH}/${2}/uploads"
	USED_S=`du -s -m ${U_PATH} 2>/dev/null | awk {'print $1'}`
	if [[ ${USED_S} ]]; then
		echo ${USED_S}
	else
		echo 0
	fi

#	SHOW_R=`/usr/sbin/repquota -a | grep "${2} " | awk '{print $3}' | head -1`
#	validation_func "echo 0"
#	if [[ ! ${SHOW_R} ]]; then
#		echo "0"
#		exit -1
#	fi
#	if [[ $SHOW_R -lt 1024 ]]; then
#		Result=0
#	else
#		Result=`expr ${SHOW_R} / 1024`
#		validation_func "echo Error : show storage in expr"
#	fi
#	echo "${Result}"
fi


if [[ ${Option} == "bitrate" ]]; then
	ARGC_func 4 4
#	sed -i "s/BANDWIDTH=${3}/BANDWIDTH=${4}/g" "${NGX_CONF}/${2}-rtmp.conf"
#	if [[ ${4} == "288" ]]; then
#		Resolution="_low"
#	elif [[ ${4} == "448" ]]; then
#		Resolution="_mid"
#	elif [[ ${4} == "1152" ]]; then
#		Resolution="_high"
#	elif [[ ${4} == "2048" ]]; then
#		Resolution="_hd720"
#	elif [[ ${4} == "4096" ]]; then
#		Resolution="_src"
#	elif [[ ${4} == "90000" ]]; then
#		Resolution="_src2"
#	fi
#	REP_=`cat ${NGX_CONF}/${2}-rtmp.conf | grep hls_variant | awk {'print $2'} | uniq`
#	sed -i "s/${REP_}/${Resolution}/g" "${NGX_CONF}/${2}-rtmp.conf"
#	/bin/systemctl reload nginx.service
#	validation_func "echo Error : change bitrate, nginx reload"
	echo -e "success-done"
fi


if [[ ${Option} == "streamchk" ]]; then
	ARGC_func 2 2
	ST_CHK=`ps -aux | grep ffmpegrun | grep -v "grep ${G_COLOR_}" | grep -v "utils streamchk ${2}" | grep -v "G_COLOR_=" | grep " ${2} " | grep -v "ffmpeg_func" | head -n 1`
	if [[ ${ST_CHK} ]]; then
		echo "stream-running"
	else
		echo "no-process"
	fi
fi

if [[ ${Option} == "stplaylist" ]]; then
	ARGC_func 2 2
	ST_getplaylist=`ps -aux | grep ffmpegrun | grep -v "grep ${G_COLOR_}" | grep -v "G_COLOR_=" | grep -v "utils stplaylist ${2}" | grep " ${2} " | grep -v "ffmpeg_func" | head -n 1 | awk '{print $16}'`
	echo ${ST_getplaylist}
fi


if [[ ${Option} == "clearlogs" ]]; then
	M_SIZE=40000000  #40MB
	U_LOG=`ls ${HOME_PATH}/*/logs/ffmpeg.log 2>/dev/null`
	LARAL=`ls /home/vdopanel/public_html/storage/logs/laravel.log 2>/dev/null`
	VPLOG=`ls /home/vdopanel/core/logs/*.log 2>/dev/null`
	NGX_L=`ls /var/log/nginx/*.log 2>/dev/null`
	LOG_LIST=("${U_LOG}" "${LARAL}" "${VPLOG}" "${NGX_L}")
	for List in "${LOG_LIST[@]}"
	do
		for i in ${List}
		do
			F_SIZE=`stat -c%s ${i}`
			if [[ ${F_SIZE} -ge ${M_SIZE} ]]; then
				echo > ${i}
				echo -e "logs has been cleared for ${i}"
			fi
		done
	done
fi


if [[ ${Option} == "suspend" ]]; then
	ARGC_func 2 2
	for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "utils suspend ${2}" | grep " ${2} " | awk '{print $2}'`
	do
		kill -9 ${i}
	done
	for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "utils suspend ${2}" | grep "/${2}/uploads" | awk '{print $2}'`
	do
		kill -9 ${i}
	done

	rm -rf ${HOME_PATH}/${2}/stream/*/*
	chmod 000 ${HOME_PATH}/${2}/live-streaming/* 2>/dev/null
	rm -rf ${HOME_PATH}/${2}/live-streaming/*/*
	rm -rf ${HOME_PATH}/${2}/stream-hybrid/*/*

	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/usermod -s /bin/false ${2} >/dev/null 2>&1
	else
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp delftp user=${2} destroy=0 >/dev/null 2>&1
	fi

	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		mv ${NGX_CONF}/${2}-rtmp.conf ${NGX_CONF}/${2}-rtmp.conf_suspend
	fi
	mv ${NGX_CONF}/${2}-http.http ${NGX_CONF}/${2}-http_suspend.http
	/bin/systemctl reload nginx.service
	echo -e "success-done"
fi


if [[ ${Option} == "unsuspend" ]]; then
	ARGC_func 2 2
	chmod 777 ${HOME_PATH}/${2}/live-streaming/* 2>/dev/null
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/usermod -s /bin/bash ${2} >/dev/null 2>&1
	else
		FTP_PASS=`mysql ${DB_NAME} -s -N -e "SELECT ftp FROM broadcasters WHERE name='${2}'"`
		FTP_quot=`mysql ${DB_NAME} -s -N -e "SELECT station_storage FROM broadcasters WHERE name='${2}'"`
		/usr/local/cpanel/bin/cpapi2 --user=vdopanel Ftp addftp user=${2} pass=${FTP_PASS} quota=${FTP_quot} homedir=users/${2}/uploads >/dev/null 2>&1
	fi
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf_suspend ]]; then
		mv ${NGX_CONF}/${2}-rtmp.conf_suspend ${NGX_CONF}/${2}-rtmp.conf
	fi
	mv ${NGX_CONF}/${2}-http_suspend.http ${NGX_CONF}/${2}-http.http
	/bin/systemctl reload nginx.service
	echo -e "success-done"
fi


if [[ ${Option} == "resetadmin" ]]; then
	ARGC_func 2 2
	if [[ $EUID -ne 0 ]]; then
		echo "Must be run as root".
		exit -1
	fi
	RAND_P1=`date +%s | sha256sum | base64 | head -c 12`
	RAND_P2=`htpasswd -nbBC 10 USER ${RAND_P1} | cut -d":" -f2`
	USER=`mysql ${DB_NAME} -s -N -e "SELECT name FROM users WHERE id='1'"`
	validation_func "echo Error : can not change password for admin.."
	mysql ${DB_NAME} -s -N -e "UPDATE users SET password = '${RAND_P2}' WHERE id='1'"
	validation_func "echo Error : can not change password for admin.."
	echo -e "\nUser : ${USER}\nPassword : ${RAND_P1}\n"
fi


REBUILD_FUNC()
{
	MAIN_DO=`cat ${ENV_F} | grep APP_URL= | cut -d"/" -f3`
	Domain=`echo "${3%.}" | tr '[:upper:]' '[:lower:]'`
	FILE_REP=`ls ${NGX_CONF}/${2}-http* 2>/dev/null`
	if [[ ! ${FILE_REP} ]]; then
		echo "http config file not exists"
		exit -1
	fi
	DOMAIN_LOCK=`egrep http_referer ${FILE_REP} | awk {'print $4'} | tr -d '")'`
	DOMAIN_LOCK_ST=`cat ${FILE_REP} | grep "#return 500; #Domain_Lock"`
	cat ${SOU_CONF}/${1} > ${FILE_REP}
	sed -i "s/_USER_/${2}/g" "${FILE_REP}"
	sed -i "s/_DOMAIN_/${Domain}/g" "${FILE_REP}"
	sed -i "s/_DOMAIN2_/${MAIN_DO}/g" "${FILE_REP}"
	sed -i "s/_PORT_/${4}/g" "${FILE_REP}"
	GEO_CHK=`mysql ${DB_NAME} -s -N -e "SELECT geoblock FROM broadcasters WHERE name='${2}'"`
	if [[ ${GEO_CHK} == 1 ]]; then
		GEO_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT geoblock_type FROM broadcasters WHERE name='${2}'"`
		GEO_COUNTRIES=`mysql ${DB_NAME} -s -N -e "SELECT geoblock_cntry FROM broadcasters WHERE name='${2}'"`
		if [[ ${GEO_TYPE} == 1 ]]; then
			COUNTRIES=${GEO_COUNTRIES//,/|}
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (${COUNTRIES}))" "${FILE_REP}"
		else
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code \!~ (${COUNTRIES}))" "${FILE_REP}"
		fi
	else
		sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (_NONE_))" "${FILE_REP}"
	fi

	if [[ ${DOMAIN_LOCK} ]]; then
		REP1=`egrep http_referer ${FILE_REP} | awk {'print $4'} | tr -d '")'`
		sed -i "s/${REP1}/${DOMAIN_LOCK}/g" "${FILE_REP}"
		if [[ ! ${DOMAIN_LOCK_ST} ]]; then
			sed -i "/#Domain_Lock/c\           return 500; #Domain_Lock" "${FILE_REP}"
		fi
	fi
	/bin/systemctl reload nginx.service
	validation_func "echo Error : rebuild http conf, nginx reload"
	echo -e "success-done"
	/home/vdopanel/core/utils rebuildproxy ${2} >/dev/null 2>&1
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT loadb FROM broadcasters WHERE name='${2}'"`
	if [[ ${CHK_LOADB_users} == 1 ]]; then
		/home/vdopanel/core/utils loadbrebuild
	fi
}
if [[ ${Option} == "rebuilddomain" ]]; then
	ARGC_func 4 4
	if [[ ! -f ${cPanel_chk} ]]; then
		REBUILD_FUNC "nginx-user-http_ssl.conf" ${2} ${3} ${4}
	else
		REBUILD_FUNC "nginx-user-http_ssl_cpanel.conf" ${2} ${3} ${4}
	fi
fi

if [[ ${Option} == "rebuilddomain2" ]]; then
	ARGC_func 4 4
	if [[ ! -f ${cPanel_chk} ]]; then
		REBUILD_FUNC "nginx-user-http.conf" ${2} ${3} ${4}
	else
		REBUILD_FUNC "nginx-user-http_cpanel.conf" ${2} ${3} ${4}
	fi
fi

if [[ ${Option} == "rebuildhttpforce" ]]; then
	ARGC_func 2 2
	SERV_IP_FUNC
	HTTP_FILE="${NGX_CONF}/${2}-http.http"
	PHP_V_FUNC
	DOMAIN=`${PHP_RUN} /home/vdopanel/public_html/artisan broadcaster:domain ${2}`
	if [[ ! -f ${cPanel_chk} ]]; then
		if [[ ${SERV_IP} == ${DOMAIN} ]]; then
			SOURCE_F="${SOU_CONF}/nginx-user-http.conf"
		else
			SOURCE_F="${SOU_CONF}/nginx-user-http_ssl.conf"
		fi
	else
		if [[ ${SERV_IP} == ${DOMAIN} ]]; then
			SOURCE_F="${SOU_CONF}/nginx-user-http_cpanel.conf"
		else
			SOURCE_F="${SOU_CONF}/nginx-user-http_ssl_cpanel.conf"
		fi
	fi

	cat ${SOURCE_F} > "${HTTP_FILE}"
	MAIN_DO=`cat ${ENV_F} | grep APP_URL= | cut -d"/" -f3`
	HTTP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT http_port FROM broadcasters WHERE name='${2}'"`
	sed -i "s/_USER_/${2}/g" "${HTTP_FILE}"
	sed -i "s/_DOMAIN_/${DOMAIN}/g" "${HTTP_FILE}"
	sed -i "s/_DOMAIN2_/${MAIN_DO}/g" "${HTTP_FILE}"
	sed -i "s/_PORT_/${HTTP_PORT}/g" "${HTTP_FILE}"

	GEO_CHK=`mysql ${DB_NAME} -s -N -e "SELECT geoblock FROM broadcasters WHERE name='${2}'"`
	if [[ ${GEO_CHK} == 1 ]]; then
		GEO_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT geoblock_type FROM broadcasters WHERE name='${2}'"`
		GEO_COUNTRIES=`mysql ${DB_NAME} -s -N -e "SELECT geoblock_cntry FROM broadcasters WHERE name='${2}'"`
		if [[ ${GEO_TYPE} == 1 ]]; then
			COUNTRIES=${GEO_COUNTRIES//,/|}
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (${COUNTRIES}))" "${HTTP_FILE}"
		else
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code \!~ (${COUNTRIES}))" "${HTTP_FILE}"
		fi
	else
		sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (_NONE_))" "${HTTP_FILE}"
	fi

	# DOMAIN LOCK

	/bin/systemctl reload nginx.service
	validation_func "echo Error : rebuild http conf, nginx reload"
	echo -e "success-done"
	/home/vdopanel/core/utils rebuildproxy ${2} >/dev/null 2>&1
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT loadb FROM broadcasters WHERE name='${2}'"`
	if [[ ${CHK_LOADB_users} == 1 ]]; then
		/home/vdopanel/core/utils loadbrebuild
	fi
fi

if [[ ${Option} == "rebuildrtmp" ]]; then
	ARGC_func 2 2
	ls ${NGX_CONF}/${2}-rtmp.conf* >/dev/null 2>&1
	validation_func "echo Error : rtmp config file not exists."
	SERV_IP_FUNC
	RTMP_FILE=`ls ${NGX_CONF}/${2}-rtmp.conf* 2>/dev/null`
	if [[ ! ${RTMP_FILE} ]]; then
		echo "RTMP config not exists"
		exit -1
	fi
	Social_Y_1=`egrep youtube-pclive-stream ${RTMP_FILE}`
	Social_Y_2=`egrep youtube-webtv-stream ${RTMP_FILE}`
	Social_F_1=`egrep facebook-pclive-stream ${RTMP_FILE}`
	Social_F_2=`egrep facebook-webtv-stream ${RTMP_FILE}`
	Social_T_1=`egrep twitch-pclive-stream ${RTMP_FILE}`
	Social_T_2=`egrep twitch-webtv-stream ${RTMP_FILE}`
	Social_P_1=`egrep periscope-pclive-stream ${RTMP_FILE}`
	Social_P_2=`egrep periscope-webtv-stream ${RTMP_FILE}`
	Social_D_1=`egrep dailymotion-pclive-stream ${RTMP_FILE}`
	Social_D_2=`egrep dailymotion-webtv-stream ${RTMP_FILE}`
	Social_C_1=`egrep custom-pclive-stream ${RTMP_FILE}`
	Social_C_1=${Social_C_1////\\/}
	Social_C_2=`egrep custom-webtv-stream ${RTMP_FILE}`
	Social_C_2=${Social_C_2////\\/}
	Social_TEL_1=`egrep telegram-pclive-stream ${RTMP_FILE}`
	Social_TEL_2=`egrep telegram-webtv-stream ${RTMP_FILE}`
	RTMP_PORT=`egrep listen ${RTMP_FILE} | awk {'print $2'} | tr -d ";"`
#	Resolution=`egrep hls_variant ${RTMP_FILE} | head -1 | awk {'print $2'}`
#	Bitrate=`egrep hls_variant ${RTMP_FILE} | head -1 | awk {'print $3'} | cut -d "=" -f2 | tr -d ";"`
	PUB_IP_CHK=`egrep "allow publish" ${RTMP_FILE} | awk {'print $3'} | tr -d ";" | grep -v 127.0.0.1 | grep -v ${SERV_IP}`
	RECORD_CHK=`egrep "#record off; #RecordLive" ${RTMP_FILE}`
	REC_ST="off"
	if [[ ${RECORD_CHK} ]]; then
		REC_ST="on"
	fi

	RTMP_auth_chk=`egrep rtmp-auth ${RTMP_FILE}`
	if [[ ${RTMP_auth_chk} ]]; then
		cat /home/vdopanel/core/conf-templates/nginx-user-rtmp_psk.conf > ${RTMP_FILE}
	else
		cat /home/vdopanel/core/conf-templates/nginx-user-rtmp.conf > ${RTMP_FILE}
		if [[ ${PUB_IP_CHK} ]]; then
			sed -i "s/_publish_IP_/${PUB_IP_CHK}/g" "${RTMP_FILE}"
		fi
		sed -i "s/_publish_IP_/${SERV_IP}/g" "${RTMP_FILE}"
	fi

	if [[ ${REC_ST} == "on" ]]; then
		sed -i "/record off; #RecordLive/c\              #record off; #RecordLive" "${RTMP_FILE}"
	fi

	if [[ -f ${cPanel_chk} ]]; then
		REP_USER="vdopanel\/users\/${2}"
		sed -i "s/home\/_USER_/home\/${REP_USER}/g" "${RTMP_FILE}"
		sed -i "s/-APIPORT-/:1050/g" "${RTMP_FILE}"
	else
		sed -i "s/-APIPORT-//g" "${RTMP_FILE}"
	fi
	sed -i "s/_PORT_/${RTMP_PORT}/g" "${RTMP_FILE}"
	sed -i "s/_USER_/${2}/g" "${RTMP_FILE}"
	sed -i "s/_SERV_IP_/${SERV_IP}/g" "${RTMP_FILE}"
#	sed -i "s/_Resolution_/${Resolution}/g" "${RTMP_FILE}"
#	sed -i "s/_BitRate_/${Bitrate}/g" "${RTMP_FILE}"
	sed -i "/youtube-pclive-stream/c\\${Social_Y_1}" "${RTMP_FILE}"
	sed -i "/youtube-webtv-stream/c\\${Social_Y_2}" "${RTMP_FILE}"
	sed -i "/facebook-pclive-stream/c\\${Social_F_1}" "${RTMP_FILE}"
	sed -i "/facebook-webtv-stream/c\\${Social_F_2}" "${RTMP_FILE}"
	sed -i "/twitch-pclive-stream/c\\${Social_T_1}" "${RTMP_FILE}"
	sed -i "/twitch-webtv-stream/c\\${Social_T_2}" "${RTMP_FILE}"
	sed -i "/periscope-pclive-stream/c\\${Social_P_1}" "${RTMP_FILE}"
	sed -i "/periscope-webtv-stream/c\\${Social_P_2}" "${RTMP_FILE}"
	sed -i "/dailymotion-pclive-stream/c\\${Social_D_1}" "${RTMP_FILE}"
	sed -i "/dailymotion-webtv-stream/c\\${Social_D_2}" "${RTMP_FILE}"
	sed -i "/custom-pclive-stream/c\\${Social_C_1}" "${RTMP_FILE}"
	sed -i "/custom-webtv-stream/c\\${Social_C_2}" "${RTMP_FILE}"
	if [[ ${Social_TEL_1} ]]; then
		sed -i "/telegram-pclive-stream/c\\${Social_TEL_1}" "${RTMP_FILE}"
		sed -i "/telegram-webtv-stream/c\\${Social_TEL_2}" "${RTMP_FILE}"
	fi

	B_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT B_type FROM broadcasters WHERE name='${2}'"`
	if [[ ${B_TYPE} != 3 ]]; then
		sed -i "/push-to-hybrid/c\            # push-to-hybrid" "${RTMP_FILE}"
	fi

	STREAM_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT stream_type FROM broadcasters WHERE name='${2}'"`
	STREAM_MULTI=`mysql ${DB_NAME} -s -N -e "SELECT stream_multi FROM broadcasters WHERE name='${2}'"`
	if [[ ${STREAM_TYPE} == "multi" ]]; then
		Multi_ST_1=""
		Multi_ST_2=""
		Multi_ST_3=""
		Multi_ST_4=""
		Multi_ST_5=""
		PUSH_SRC=""
		if [[ ${STREAM_MULTI} == *"288"* ]]; then
			sed -i "s/#hls_variant _240/hls_variant _240/g" "${RTMP_FILE}"
			Multi_ST_1="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 288k -maxrate 288k -bufsize 288k -vf scale=480:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_240\n"
		fi
		if [[ ${STREAM_MULTI} == *"448"* ]]; then
			sed -i "s/#hls_variant _360/hls_variant _360/g" "${RTMP_FILE}"
			Multi_ST_2="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 448k -maxrate 448k -bufsize 448k -vf scale=720:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_360\n"
		fi
		if [[ ${STREAM_MULTI} == *"1152"* ]]; then
			sed -i "s/#hls_variant _480/hls_variant _480/g" "${RTMP_FILE}"
			Multi_ST_3="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 1152k -maxrate 1152k -bufsize 1152k -vf scale=960:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_480\n"
		fi
		if [[ ${STREAM_MULTI} == *"2048"* ]]; then
			sed -i "s/#hls_variant _720/hls_variant _720/g" "${RTMP_FILE}"
			Multi_ST_4="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 2048k -maxrate 2048k -bufsize 2048k -vf scale=1280:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_720\n"
		fi
		if [[ ${STREAM_MULTI} == *"4096"* ]]; then
			sed -i "s/#hls_variant _1080/hls_variant _1080/g" "${RTMP_FILE}"
			Multi_ST_5="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 4096k -maxrate 4096k -bufsize 4096k -vf scale=1920:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_1080"
		fi
                if [[ ${STREAM_MULTI} == *"90000"* ]]; then
			sed -i "s/#hls_variant _src/hls_variant _src/g" "${RTMP_FILE}"
			PUSH_SRC="            push rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_src;\n"
		fi
		Multi_ST_M="            exec ffmpeg -re -i ${HOME_PATH}/${2}/live-streaming/hls/\$name.m3u8\n"
		Multi_ST_ALL="${Multi_ST_M} ${Multi_ST_1} ${Multi_ST_2} ${Multi_ST_3} ${Multi_ST_4} ${Multi_ST_5}"
		sed -i "/Multi_bitrate/c\\${PUSH_SRC}${Multi_ST_ALL};" "${RTMP_FILE}"

	fi

	/bin/systemctl reload nginx.service
	validation_func "echo Error : rebuild rtmp conf, nginx reload"
	echo "success-done"
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT loadb FROM broadcasters WHERE name='${2}'"`
	if [[ ${CHK_LOADB_users} == 1 ]]; then
		/home/vdopanel/core/utils loadbrebuild
	fi
fi

if [[ ${Option} == "rebuildrtmpforce" ]]; then
	ARGC_func 2 2
	SERV_IP_FUNC
	RTMP_FILE="${NGX_CONF}/${2}-rtmp.conf"
	cat /home/vdopanel/core/conf-templates/nginx-user-rtmp_psk.conf > "${RTMP_FILE}"
	RTMP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT rtmp_port FROM broadcasters WHERE name='${2}'"`
	if [[ -f ${cPanel_chk} ]]; then
		REP_USER="vdopanel\/users\/${2}"
		sed -i "s/home\/_USER_/home\/${REP_USER}/g" "${RTMP_FILE}"
		sed -i "s/-APIPORT-/:1050/g" "${RTMP_FILE}"
	else
		sed -i "s/-APIPORT-//g" "${RTMP_FILE}"
	fi

	# Publish_IP
	# LIVE_record
	sed -i "s/_PORT_/${RTMP_PORT}/g" "${RTMP_FILE}"
	sed -i "s/_USER_/${2}/g" "${RTMP_FILE}"
	sed -i "s/_SERV_IP_/${SERV_IP}/g" "${RTMP_FILE}"

	USER_ID=`mysql ${DB_NAME} -s -N -e "SELECT id FROM broadcasters WHERE name='${2}'"`
	SOCIAL_ST_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT stream_type FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
	if [[ ${SOCIAL_ST_TYPE} ]]; then
		Youtube_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT youtube_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Youtube_STATUS} == "on" ]]; then
			Youtube_KEY=`mysql ${DB_NAME} -s -N -e "SELECT youtube_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/youtube-pclive-stream/c\            push rtmp:\/\/a.rtmp.youtube.com\/live2\/${Youtube_KEY}; # youtube-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/youtube-webtv-stream/c\            push rtmp:\/\/a.rtmp.youtube.com\/live2\/${Youtube_KEY}; # youtube-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Facebook_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT facebook_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Facebook_STATUS} == "on" ]]; then
			Facebook_KEY=`mysql ${DB_NAME} -s -N -e "SELECT facebook_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			STUNNEL_PORT="1${RTMP_PORT:1}"
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/facebook-pclive-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/rtmp\/${Facebook_KEY}; # facebook-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/facebook-webtv-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/rtmp\/${Facebook_KEY}; # facebook-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Twitch_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT twitch_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Twitch_STATUS} == "on" ]]; then
			Twitch_KEY=`mysql ${DB_NAME} -s -N -e "SELECT twitch_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			Twitch_SERV=`mysql ${DB_NAME} -s -N -e "SELECT twitch_server FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/twitch-pclive-stream/c\            push rtmp:\/\/live-${Twitch_SERV}.twitch.tv\/app\/${Twitch_KEY}; # twitch-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/twitch-webtv-stream/c\            push rtmp:\/\/live-${Twitch_SERV}.twitch.tv\/app\/${Twitch_KEY}; # twitch-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Periscope_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT periscope_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Periscope_STATUS} == "on" ]]; then
			Periscope_KEY=`mysql ${DB_NAME} -s -N -e "SELECT periscope_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/periscope-pclive-stream/c\            push rtmp:\/\/de.pscp.tv:80\/x\/${Periscope_KEY}; # periscope-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/periscope-webtv-stream/c\            push rtmp:\/\/de.pscp.tv:80\/x\/${Periscope_KEY}; # periscope-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Dailymotion_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT dailymotion_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Dailymotion_STATUS} == "on" ]]; then
			Dailymotion_KEY=`mysql ${DB_NAME} -s -N -e "SELECT dailymotion_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/dailymotion-pclive-stream/c\            push rtmp:\/\/publish.dailymotion.com\/publish-dm\/${Dailymotion_KEY}; # dailymotion-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/dailymotion-webtv-stream/c\            push rtmp:\/\/publish.dailymotion.com\/publish-dm\/${Dailymotion_KEY}; # dailymotion-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Custom_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT custom_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
		if [[ ${Custom_STATUS} == "on" ]]; then
			Custom_KEY=`mysql ${DB_NAME} -s -N -e "SELECT custom_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			Custom_URL=`mysql ${DB_NAME} -s -N -e "SELECT custom_url FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
			C_URL=${Custom_URL////\\/}
			if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
				sed -i "/custom-pclive-stream/c\            push ${C_URL}\/${Custom_KEY}; # custom-pclive-stream" "${RTMP_FILE}"
			else
				sed -i "/custom-webtv-stream/c\            push ${C_URL}\/${Custom_KEY}; # custom-webtv-stream" "${RTMP_FILE}"
			fi
		fi
		Telegram_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT telegram_status FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
                if [[ ${Telegram_STATUS} == "on" ]]; then
                        Telegram_KEY=`mysql ${DB_NAME} -s -N -e "SELECT telegram_key FROM social_streams WHERE broadcaster_id='${USER_ID}'"`
                        STUNNEL_PORT="4${RTMP_PORT:1}"
                        if [[ ${SOCIAL_ST_TYPE} == 1 ]]; then
                                sed -i "/telegram-pclive-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/s\/${Telegram_KEY}; # telegram-pclive-stream" "${RTMP_FILE}"
                        else
                                sed -i "/telegram-webtv-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/s\/${Telegram_KEY}; # telegram-webtv-stream" "${RTMP_FILE}"
                        fi
                fi
	fi

	STREAM_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT stream_type FROM broadcasters WHERE name='${2}'"`
	STREAM_MULTI=`mysql ${DB_NAME} -s -N -e "SELECT stream_multi FROM broadcasters WHERE name='${2}'"`
	if [[ ${STREAM_TYPE} == "multi" ]]; then
		Multi_ST_1=""
		Multi_ST_2=""
		Multi_ST_3=""
		Multi_ST_4=""
		Multi_ST_5=""
		PUSH_SRC=""
		if [[ ${STREAM_MULTI} == *"288"* ]]; then
			sed -i "s/#hls_variant _240/hls_variant _240/g" "${RTMP_FILE}"
			Multi_ST_1="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 288k -maxrate 288k -bufsize 288k -vf scale=480:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_240\n"
		fi
		if [[ ${STREAM_MULTI} == *"448"* ]]; then
			sed -i "s/#hls_variant _360/hls_variant _360/g" "${RTMP_FILE}"
			Multi_ST_2="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 448k -maxrate 448k -bufsize 448k -vf scale=720:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_360\n"
		fi
		if [[ ${STREAM_MULTI} == *"1152"* ]]; then
			sed -i "s/#hls_variant _480/hls_variant _480/g" "${RTMP_FILE}"
			Multi_ST_3="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 1152k -maxrate 1152k -bufsize 1152k -vf scale=960:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_480\n"
		fi
		if [[ ${STREAM_MULTI} == *"2048"* ]]; then
			sed -i "s/#hls_variant _720/hls_variant _720/g" "${RTMP_FILE}"
			Multi_ST_4="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 2048k -maxrate 2048k -bufsize 2048k -vf scale=1280:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_720\n"
		fi
		if [[ ${STREAM_MULTI} == *"4096"* ]]; then
			sed -i "s/#hls_variant _1080/hls_variant _1080/g" "${RTMP_FILE}"
			Multi_ST_5="-c:v libx264 -c:a aac -g 60 -keyint_min 60 -b 4096k -maxrate 4096k -bufsize 4096k -vf scale=1920:trunc(ow/a/2)*2 -tune zerolatency -preset superfast -crf 23 -f flv rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_1080"
		fi
		if [[ ${STREAM_MULTI} == *"90000"* ]]; then
			sed -i "s/#hls_variant _src/hls_variant _src/g" "${RTMP_FILE}"
			PUSH_SRC="            push rtmp://127.0.0.1:${RTMP_PORT}/multi_live/play_src;\n"
		fi
		Multi_ST_M="            exec ffmpeg -re -i ${HOME_PATH}/${2}/live-streaming/hls/\$name.m3u8\n"
		Multi_ST_ALL="${Multi_ST_M} ${Multi_ST_1} ${Multi_ST_2} ${Multi_ST_3} ${Multi_ST_4} ${Multi_ST_5}"
		sed -i "/Multi_bitrate/c\\${PUSH_SRC}${Multi_ST_ALL};" "${RTMP_FILE}"
	fi

	/bin/systemctl reload nginx.service
	validation_func "echo Error : rebuild rtmp conf, nginx reload"
	echo "success-done"
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT loadb FROM broadcasters WHERE name='${2}'"`
	if [[ ${CHK_LOADB_users} == 1 ]]; then
		/home/vdopanel/core/utils loadbrebuild
	fi
fi


if [[ ${Option} == "bandwidthupdate" ]]; then
	ARGC_func 4 4
	while true
	do
		KILL_OLD_P=`ps -aux | grep iftop | grep -v "grep ${G_COLOR_}" | grep -v "\-c" | grep ${3} | awk '{print $2}'`
		for i in ${KILL_OLD_P}
		do
			kill -9 ${i}
		done
		/usr/sbin/iftop -P -N -n -t -f "port ${3}" -s ${4} 2>/dev/null | grep Cumulative > /home/vdopanel/core/tmp/bandw/bandw-${3}
		RESULT_1=`cat /home/vdopanel/core/tmp/bandw/bandw-${3} | grep Cumulative | sed 's/  */ /g' | awk '{print $NF}'`
		RESULT_2=`cat /home/vdopanel/core/tmp/bandw/bandw-${3} | grep Cumulative | sed 's/  */ /g' | awk '{print $NF}' | tr -d "KMGB"`
		Bandw_F=`cat ${HOME_PATH}/${2}/data/bandwidth | head -n 1`
		Bandw_FILE="${HOME_PATH}/${2}/data/bandwidth"

		if [[ ${RESULT_1} == *"KB"* ]]; then
			Conv2MB=$( echo "scale=3; ${RESULT_2} / 1000" | bc )
			Calc=$( echo "${Conv2MB} + ${Bandw_F}" | bc )
			echo ${Calc} > ${Bandw_FILE}
		elif [[ ${RESULT_1} == *"MB"* ]]; then
			Calc=$( echo "${RESULT_2} + ${Bandw_F}" | bc )
			echo ${Calc} > ${Bandw_FILE}
		elif [[ ${RESULT_1} == *"GB"* ]]; then
			Conv2MB=$( echo "${RESULT_2} * 1000" | bc )
			Calc=$( echo "${Conv2MB} + ${Bandw_F}" | bc )
			echo ${Calc} > ${Bandw_FILE}
		fi
	done
fi

if [[ ${Option} == "bandwidthreset" ]]; then
	ARGC_func 2 2
	COMM_LS=`ls ${HOME_PATH}/*/data/bandwidth 2>/dev/null`
	for i in ${COMM_LS}
	do
		echo "0" > ${i}
	done

	CONF_F=`ls ${NGX_CONF}/*-http* 2>/dev/null`
	if [[ ${CONF_F} ]]; then
		for i in ${CONF_F}
		do
			GET_USER=`cat ${i} | grep alias | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
			LogDate=`date --date='-1 month' +'%B-%Y'`
			mv -f ${HOME_PATH}/${GET_USER}/logs/access.log ${HOME_PATH}/${GET_USER}/logs/statistics/${LogDate}
			mv -f ${HOME_PATH}/${GET_USER}/logs/statistics.html ${HOME_PATH}/${GET_USER}/logs/statistics/${LogDate}.html
			/bin/systemctl reload nginx.service
			DATE_IN_L=`date '+%b/%Y'`
			echo -e "127.0.0.1 - - [1/${DATE_IN_L}:01:00:00 +0000] \"GET /stream/play.m3u8 HTTP/1.1\" 304 0 \"http://127.0.0.1/broadcaster/${GET_USER}/stream/player\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0\"" > ${HOME_PATH}/${GET_USER}/logs/access.log
			chown -R vdopanel.vdopanel ${HOME_PATH}/${GET_USER}/logs/access.log
		done
	fi

fi

if [[ ${Option} == "bandwidthshow" ]]; then
	ARGC_func 2 2
	if [[ ${2} == "all" ]]; then
		COMM_CAT=`cat ${HOME_PATH}/*/data/bandwidth 2>/dev/null`
		COUNT=0
		for i in ${COMM_CAT}
		do
			COUNT=$( echo "${COUNT} + ${i}" | bc )
		done
		RES=`echo "${COUNT}" | cut -d"." -f1`
		if [[ ${RES} ]]; then
			echo ${RES}
		else
			echo 0
		fi
		exit -1
	fi

	COMM_RES="${HOME_PATH}/${2}/data/bandwidth"
	RESULT=`cat ${COMM_RES} | head -n 1 | cut -d"." -f1`
	if [[ ${RESULT} ]]; then
		echo ${RESULT}
	else
		echo 0
	fi
fi

if [[ ${Option} == "bandwcheck" ]]; then
	ARGC_func 2 2
	CONF_F=`ls ${NGX_CONF}/*-http.http 2>/dev/null`
	if [[ ${CONF_F} ]]; then
		for i in ${CONF_F}
		do
			GET_USER=`cat ${i} | grep alias | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
			H_PORT=`cat ${i} | grep listen | awk '{print $2}'`
#			CHK_H_PORT=`ps aux | grep bandwidthupdate | grep ${H_PORT} | grep ${GET_USER} | grep -v "grep ${G_COLOR_}" | awk '{print $2}'`
			PID_F1="/home/vdopanel/core/tmp/pids/${H_PORT}.pid"
			CHK_PID1=`head -1 ${PID_F1} 2>/dev/null`
			CHK_H_PORT=`ps -o pid= -p ${CHK_PID1} 2>/dev/null`
			if [[ ! ${CHK_H_PORT} ]]; then
				/home/vdopanel/core/utils bandwidthupdate ${GET_USER} ${H_PORT} 60 </dev/null >/dev/null 2>&1 &
				echo $! > ${PID_F1}
			fi
		done
	fi

	CONF_F_S=`ls ${NGX_CONF}/*-http_suspend.http 2>/dev/null`
	if [[ ${CONF_F_S} ]]; then
		for i2 in ${CONF_F_S}
		do
			USER=`cat ${i2} | grep alias | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
			H_PORT_K=`cat ${i2} | grep listen | awk '{print $2}'`
			/home/vdopanel/core/utils killbandw ${USER} ${H_PORT_K}
		done
	fi


	CONF_F=`ls ${NGX_CONF}/*-rtmp.conf 2>/dev/null`
	if [[ ${CONF_F} ]]; then
		for i in ${CONF_F}
		do
			GET_USER=`cat ${i} | grep hls_path | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
			RTMP_PORT=`cat ${i} | grep listen | awk '{print $2}' | tr -d ";"`
#			CHK_RTMP_PORT=`ps aux | grep bandwidthupdate | grep ${RTMP_PORT} | grep ${GET_USER} | grep -v "grep ${G_COLOR_}" | awk '{print $2}'`
			PID_F2="/home/vdopanel/core/tmp/pids/${RTMP_PORT}.pid"
			CHK_PID2=`head -1 ${PID_F2} 2>/dev/null`
			CHK_RTMP_PORT=`ps -o pid= -p ${CHK_PID2} 2>/dev/null`
			if [[ ! ${CHK_RTMP_PORT} ]]; then
				/home/vdopanel/core/utils bandwidthupdate ${GET_USER} ${RTMP_PORT} 80 </dev/null >/dev/null 2>&1 &
				echo $! > ${PID_F2}
			fi
		done
	fi

	CONF_F_S=`ls ${NGX_CONF}/*-rtmp.conf_suspend 2>/dev/null`
	if [[ ${CONF_F_S} ]]; then
		for i2 in ${CONF_F_S}
		do
			USER=`cat ${i2} | grep hls_path | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
			RTMP_PORT_K=`cat ${i2} | grep listen | awk '{print $2}' | tr -d ";"`
			/home/vdopanel/core/utils killbandw ${USER} ${RTMP_PORT_K}
		done
	fi
fi

if [[ ${Option} == "killbandw" ]]; then
	ARGC_func 3 3
	for i in `ps -aux | grep bandwidthupdate | grep -v "grep ${G_COLOR_}" | grep -v "utils killbandw ${2}" | grep ${2} | grep ${3} | awk '{print $2}'`
	do
		kill -9 ${i}
	done
	echo -e "success-done"
fi


if [[ ${Option} == "vlimitupdate" ]]; then
	ARGC_func 6 6
	F_CHK=`/usr/sbin/iptables -L -n -v --line-numbers | grep "${2},${3}" | grep reject-with | awk '{print $1}'`
	if [[ ${F_CHK} ]]; then
		/usr/sbin/iptables -D INPUT ${F_CHK} >/dev/null 2>&1
	fi
	if [[ ${4} != 0 ]]; then
		if [[ ${5} == 1 ]]; then
			IPTA_LIST=`/usr/sbin/iptables -L -n -v --line-numbers | grep "${2},${3}" | grep -v "reject-with" | tac | awk {'print $1'}`
			for i in ${IPTA_LIST}
			do
				/usr/sbin/iptables -D INPUT ${i}
			done
		fi

		/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${2},${3} -m connlimit --connlimit-above ${4} --connlimit-mask 0 -j REJECT
		validation_func "echo Error : when update viewer limit"

		if [[ ${5} == 1 ]]; then
			/home/vdopanel/core/utils ipblock ${6} on >/dev/null 2>&1
		fi
	fi

	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		netfilter-persistent save >/dev/null 2>&1
	else
		service iptables save >/dev/null 2>&1
	fi
	echo -e "success-done"
fi


if [[ ${Option} == "viewercount" ]]; then
#	ARGC_func 2 2
	if [[ ${2} == "all" ]]; then
		COUNT_1=0
		CONF_F1=`ls ${NGX_CONF}/*-http.http 2>/dev/null`
		for i in ${CONF_F1}
		do
			H_PORT=`cat ${i} | grep listen | awk '{print $2}'`
			V_COUNT_1=`netstat -an | grep ${H_PORT} | grep ESTABLISHED | awk '{print $5}' | cut -d":" -f1 | wc -l`
			COUNT_1=$(echo "${COUNT_1} + ${V_COUNT_1}" | bc)
		done

		COUNT_2=0
		CONF_F2=`ls ${NGX_CONF}/*-rtmp.conf 2>/dev/null`
		for i in ${CONF_F2}
		do
			R_PORT=`cat ${i} | grep listen | awk '{print $2}' | tr -d ";"`
			V_COUNT_2=`netstat -an | grep ${R_PORT} | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1 | wc -l`
			COUNT_2=$(echo "${COUNT_2} + ${V_COUNT_2}" | bc)
		done

		TOTAL_CA=$( echo "${COUNT_1} + ${COUNT_2}" | bc )
		echo ${TOTAL_CA}
		exit -1

	elif [[ ${2} == "allforwidget" ]]; then
		for USER in `mysql ${DB_NAME} -s -N -e "SELECT name from broadcasters"`
		do
			HTTP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT http_port from broadcasters WHERE name='${USER}'"`
			RTMP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT rtmp_port from broadcasters WHERE name='${USER}'"`
			V_COUNT=`netstat -an | grep "${HTTP_PORT}\|${RTMP_PORT}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1 | wc -l`
			echo ${USER}-${HTTP_PORT}-${RTMP_PORT}-${V_COUNT}
			mysql ${DB_NAME} -s -N -e "UPDATE broadcasters SET viewers = '${V_COUNT}' WHERE name='${USER}'"
		done
		exit -1
	fi

	V_COUNT=`netstat -an | grep "${2}\|${3}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1 | wc -l`
	echo ${V_COUNT}
fi


if [[ ${Option} == "cpu" ]]; then
	ARGC_func 2 2
	if [[ ${2} == "percentage" ]]; then
		INFO=`/usr/bin/top -b -n1 | grep "Cpu(s)" | awk '{print $8}' | cut -d"." -f1`
		if [[ ${INFO} == "id," ]]; then
			echo "1"
			exit -1
		fi
		RES=$(echo "100 - ${INFO}" | bc)
		echo ${RES}

	elif [[ ${2} == "load" ]]; then
		uptime | awk {'print $(NF-2), $(NF-1), $NF'}
	fi
fi


if [[ ${Option} == "geoblock" ]]; then
	ARGC_func 4 4
#	CONVERT=${3//-/|}
#	REP=`cat ${NGX_CONF}/${2}-http.http | grep geoip | awk '{print $NF}' | tr -d "()"`
#	sed -i "s/${REP}/${CONVERT}/g" "${NGX_CONF}/${2}-http.http"

	if [[ ${3} == "_NONE_" ]]; then
		sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (_NONE_))" "${NGX_CONF}/${2}-http.http"
	else
		COUNTRIES=${3//,/|}
		if [[ ${4} == 1 ]]; then
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code ~ (${COUNTRIES}))" "${NGX_CONF}/${2}-http.http"
		else
			sed -i "/geoip2_data_country_code/c\        if (\$geoip2_data_country_code \!~ (${COUNTRIES}))" "${NGX_CONF}/${2}-http.http"
		fi
	fi
	/bin/systemctl reload nginx.service
	validation_func "echo Error : geoblock, nginx reload"
	echo -e "success-done"
fi

if [[ ${Option} == "geoblockshow" ]]; then
	ARGC_func 2 2
	RESULT=`cat ${NGX_CONF}/${2}-http.http | grep geoip | awk '{print $NF}' | tr -d "()"`
	RESULT=${RESULT//|/,}
	echo ${RESULT}
fi


if [[ ${Option} == "chown" ]]; then
	ARGC_func 2 2
	if [[ ! -f ${cPanel_chk} ]]; then
		for i in `ls ${HOME_PATH}`
		do
			if [[ -d ${HOME_PATH}/${i}/uploads ]]; then
				FOLDERS=`find ${HOME_PATH}/${i}/uploads -type d`
				for chm in ${FOLDERS}
				do
					chmod 777 ${chm}
				done
				chown -R ${i}.vdopanel ${HOME_PATH}/${i}/uploads
				chown -R ${i}.vdopanel ${HOME_PATH}/${i}/logs
				chmod 777 ${HOME_PATH}/${i}/logs/statistics
				chown -R vdopanel.vdopanel ${HOME_PATH}/${i}/playlist/* 2>/dev/null
				chown -R vdopanel.vdopanel ${HOME_PATH}/${i}/imgs/* 2>/dev/null
			fi
		done
	else
		chown -R vdopanel.vdopanel ${HOME_PATH}
	fi
fi


if [[ ${Option} == "license" ]]; then
	ARGC_func 2 2
	if [[ ${2} == "off" ]]; then
		for i in `ps -A | grep ffmpeg | awk {'print $1'}`
		do
			kill -9 ${i}
		done
		rm -rf ${HOME_PATH}/*/stream/hls/*
		rm -rf ${HOME_PATH}/*/live-streaming/hls/*
		CONF_F=`ls ${NGX_CONF}/*-rtmp.conf 2>/dev/null`
		validation_func "echo -e success-done-1"
		if [[ ${CONF_F} ]]; then
			for i in ${CONF_F}
			do
				mv ${i} ${i}_License_off
			done
		fi
		/bin/systemctl reload nginx.service
		for i in `ps -aux | grep "core\/ffmpegrun" | grep -v "grep ${G_COLOR_}" | awk '{print $2}'`
		do
			kill -9 ${i}
		done
		echo -e "success-done"

	elif [[ ${2} == "on" ]]; then
		CONF_F=`ls ${NGX_CONF}/*-rtmp.conf_License_off 2>/dev/null`
		validation_func "echo -e success-done-1"
		if [[ ${CONF_F} ]]; then
			for i in ${CONF_F}
			do
				CONF_BACK=`echo ${i} | cut -d"_" -f1`
				mv ${i} ${CONF_BACK}
			done
		fi
		/bin/systemctl reload nginx.service
		echo -e "success-done"
	fi
fi


if [[ ${Option} == "server" ]]; then
	ARGC_func 2 2
	SERV_IP_FUNC
	echo ${SERV_IP}
fi


if [[ ${Option} == "phpmyadmin" ]]; then
	ARGC_func 2 2
	if [[ ! -f ${cPanel_chk} ]]; then
		cd /usr/local/src
#		API_URL="https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest"
		API_URL="https://api.github.com/repos/phpmyadmin/phpmyadmin/releases"
		phpMyAdmin_Ver=`curl -s "${API_URL}" 2>/dev/null | grep '"name":' | awk {'print $2'} | tr -d '",' | head -1`
		if [[ ! ${phpMyAdmin_Ver} ]]; then
			phpMyAdmin_Ver="5.1.4"
		fi
		wget https://files.phpmyadmin.net/phpMyAdmin/${phpMyAdmin_Ver}/phpMyAdmin-${phpMyAdmin_Ver}-all-languages.zip >/dev/null 2>&1
		validation_func "echo Error : when download phpMyAdmin files"
		unzip -q phpMyAdmin-${phpMyAdmin_Ver}-all-languages.zip
		rm -rf phpMyAdmin-${phpMyAdmin_Ver}-all-languages.zip
		rm -rf /usr/share/phpMyAdmin
		mv phpMyAdmin-${phpMyAdmin_Ver}-all-languages /usr/share/phpMyAdmin
		mv /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php
		mkdir -p /usr/share/phpMyAdmin/tmp
		chown -R vdopanel.vdopanel /usr/share/phpMyAdmin/tmp
		PASSWORD=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32`
		sed -i "s/blowfish_secret'] = ''/blowfish_secret'] = '${PASSWORD}'/g" "/usr/share/phpMyAdmin/config.inc.php"
		/bin/systemctl reload nginx.service
		chown -R vdopanel.vdopanel /var/lib/php/session
		/bin/systemctl restart php-fpm.service
		ln -s /usr/share/phpMyAdmin/ /home/vdopanel/public_html/public/ >/dev/null 2>&1
		SERV_IP_FUNC
		echo -e "\n\n\033[32m phpmyadmin URL : http://${SERV_IP}/phpMyAdmin\033[0m\n\n"
	else
		echo -e "\nphpmyadmin already installed with cpanel server.\n"
	fi
fi


if [[ ${Option} == "update" ]]; then
	TMP_DIR="/home/vdopanel/core/tmp"
	LOG_DIR="/home/vdopanel/core/logs/update.log"
	LOCK_F="/home/vdopanel/core/tmp/update_lock"
	validation_func_update()
	{
	  if [ $? -ne 0 ]; then
		${1}
		echo -e "${2}" > ${LOG_DIR}
		rm -rf ${LOCK_F}
		exit -1
	  fi
	}
	URL_UP="https://vdopanel.com/dist"
	CHK_VER_LOCAL=`cat ${ENV_F} | grep APP_VERSION= | cut -d"=" -f2`
	RAND=`date +%s | sha256sum | base64 | head -c 12`
	CHK_VER_ONLINE="1.5.9"
	update_chk_func()
	{
		echo -e "0,Your Software Version ${CHK_VER_LOCAL} up-to-date."
		exit -1
	}
	if [[ ${2} == "check" ]]; then
		ARGC_func 2 2
		update_chk_func
		echo "1,${CHK_VER_ONLINE},There new version ${CHK_VER_ONLINE} of VDOPanel is available.\nRun this command to start update : vdopanel update"

	elif [[ ${2} == "do" ]]; then
		update_chk_func
		if [[ -f ${LOCK_F} ]]; then
			CUR_TIME=`date +%s`
			TIME_CHK=`head -1 ${LOCK_F}`
			TIME_CHK=$( echo "${CUR_TIME} - ${TIME_CHK}" | bc )
			if [[ ${TIME_CHK} -gt 5400 ]]; then
				rm -rf ${LOCK_F}
			else
				echo "There is already an update process running now"
				exit -1
			fi
		fi
		date +%s > ${LOCK_F}
		echo > ${LOG_DIR}
		ERROR1="Error : When get upgrade source."
		wget ${URL_UP}/upgrade.bin?${RAND} -O ${TMP_DIR}/upgrade.bin  >/dev/null 2>&1
		validation_func_update "echo ${ERROR1}" "${ERROR1}"
		chmod +x ${TMP_DIR}/upgrade.bin >/dev/null 2>&1
		${TMP_DIR}/upgrade.bin ${3}
		rm -rf ${TMP_DIR}/upgrade.bin ${TMP_DIR}/vdo_upgrade_task ${LOCK_F}
	fi
fi


if [[ ${Option} == "bandw" ]]; then
	ARGC_func 2 2
	COMM_LS=`ls ${HOME_PATH}/*/data/bandwidth 2>/dev/null`
	COUNT=0
	for i in ${COMM_LS}
	do
		if [[ ! -f ${cPanel_chk} ]]; then
			USER=`echo ${i} | cut -d"/" -f3`
		else
			USER=`echo ${i} | cut -d"/" -f5`
		fi
		Bandw_V=`cat ${i} | head -n 1 | cut -d"." -f1`
		BANDW_L=`mysql ${DB_NAME} -s -N -e "SELECT traffic_month FROM broadcasters WHERE name='${USER}'"`
		GET_BAND_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT traffic_hold FROM broadcasters WHERE name='${USER}'"`
		if [[ ${BANDW_L} != 0 ]]; then
			if [[ ${Bandw_V} -gt ${BANDW_L} ]]; then
#				if [[ ! -f ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ]]; then
				if [[ ${GET_BAND_STATUS} == 0 ]]; then
					COUNT=$((COUNT +1))
					mv ${NGX_CONF}/${USER}-rtmp.conf ${NGX_CONF}/${USER}-rtmp.conf_bandwhold
					for Kill in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep ${USER} | awk '{print $2}'`
					do
						kill -9 ${Kill}
					done

					rm -rf ${HOME_PATH}/${USER}/stream/hls/*
					rm -rf ${HOME_PATH}/${USER}/live-streaming/hls/*
					mysql ${DB_NAME} -s -N -e "UPDATE broadcasters SET traffic_hold = '1' WHERE name='${USER}'"
					echo -e "User : ${USER} has been hold."
				fi
			else
#				if [[ -f ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ]]; then
				if [[ ${GET_BAND_STATUS} == 1 ]]; then
					COUNT=$((COUNT +1))
					mv ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ${NGX_CONF}/${USER}-rtmp.conf
					mysql ${DB_NAME} -s -N -e "UPDATE broadcasters SET traffic_hold = '0' WHERE name='${USER}'"
					echo -e "User : ${USER} has been unhold."
				fi
			fi
		else
#			if [[ -f ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ]]; then
			if [[ ${GET_BAND_STATUS} == 1 || -f ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ]]; then
				COUNT=$((COUNT +1))
				mv ${NGX_CONF}/${USER}-rtmp.conf_bandwhold ${NGX_CONF}/${USER}-rtmp.conf
				mysql ${DB_NAME} -s -N -e "UPDATE broadcasters SET traffic_hold = '0' WHERE name='${USER}'"
				echo -e "User : ${USER} has been unhold."
			fi
		fi
	done
	if [[ ${COUNT} != 0 ]]; then
		/bin/systemctl reload nginx.service
	fi
fi


if [[ ${Option} == "filemanager" ]]; then
	ARGC_func 3 3
	if [[ ${2} == "spacerep" ]]; then
		UP_PATH="${HOME_PATH}/${3}/uploads"
		FIND_S1_DIR=`find ${UP_PATH} -type d \( -name "* *" -o -name "*&*" -o -name "*,*" -o -name "*#*" -o -name "*@*" -o -name "*\?*" -o -name "*[! -~]*" \)`
		FIND_S1_FIL=`find ${UP_PATH} -type f \( -name "* *" -o -name "*&*" -o -name "*,*" -o -name "*#*" -o -name "*@*" -o -name "*\?*" -o -name "*[! -~]*" \)`

		IFS=$'\n'
		for i in ${FIND_S1_DIR}
		do
			mv "${i}" `echo "${i}" | tr ' &,#?@[\200-\377]' '-'` >/dev/null 2>&1
		done
		for i in ${FIND_S1_FIL}
		do
			mv "${i}" `echo "${i}" | tr ' &,#?@[\200-\377]' '-'` >/dev/null 2>&1
		done
	fi

	if [[ ${2} == "getlist" ]]; then
		find ${HOME_PATH}/${3}/uploads/ -type f | grep -v ".ftpquota" | grep -v "/Live_Recorders/" | grep -v "/YouTube_Downloads/"
		mkdir -p ${HOME_PATH}/${3}/uploads/Live_Recorders
		if [[ ! -f ${cPanel_chk} ]]; then
			chmod 777 ${HOME_PATH}/${3}/uploads/Live_Recorders
			chown -R ${3}.vdopanel ${HOME_PATH}/${3}/uploads/Live_Recorders
		else
			chown -R vdopanel.vdopanel ${HOME_PATH}/${3}/uploads/Live_Recorders
		fi
	fi
fi


if [[ ${Option} == "getstinfo" ]]; then
	ARGC_func 3 3
	GET_STATUS=`ls ${HOME_PATH}/${2}/stream/*/*.m3u8 2>/dev/null;ls ${HOME_PATH}/${2}/live-streaming/*/*.m3u8 2>/dev/null`
	if [[ ${GET_STATUS} ]]; then
		GET_STATUS="Live"
	else
		GET_STATUS="Offline"
	fi

	GET_PLIST=`ps aux | grep ffmpegrun | grep -v "grep ${G_COLOR_}" | grep "sudo \/home" | grep ${2} | grep ${3} | awk '{print $16}' | uniq`
	if [[ ! ${GET_PLIST} ]]; then
		GET_PLIST="Not-Available"
		GET_VFILE="Not-Available"
	else
		STREAM_INFO_F="/home/vdopanel/core/tmp/pids/${2}_STREAM_INFO"
		MODE=`egrep Mode= ${STREAM_INFO_F} | cut -d "=" -f2`
		if [[ ${MODE} != "restream" ]]; then
			GET_VFILE=`egrep Video= ${STREAM_INFO_F} | rev | cut -d "/" -f1 | rev`
		else
			GET_VFILE=`egrep Video= ${STREAM_INFO_F} | cut -d "=" -f2`
		fi
	fi
	echo ${GET_PLIST:0:30},${GET_VFILE:0:35},${GET_STATUS}
fi


if [[ ${Option} == "killp" ]]; then
	ARGC_func 4 4
	for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "utils killp ${2}" | grep -v "multi_live/play" | grep ${2} | grep ${3} | awk '{print $2}'`
	do
		kill -9 ${i}
	done
#	rm -rf ${HOME_PATH}/${2}/stream/*/*
	echo -e "success-done"
fi


if [[ ${Option} == "sslcheck" ]]; then
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/bin/certbot renew
		/home/vdopanel/core/utils loadbrebuild
	else
		/usr/local/cpanel/bin/autossl_check --user=vdopanel
	fi
	/home/vdopanel/core/utils sendpost
fi


if [[ ${Option} == "social-YT" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/youtube-pclive-stream/c\            # youtube-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/youtube-webtv-stream/c\            # youtube-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 4 4
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		if [[ ${4} == "1" ]]; then
			sed -i "/youtube-pclive-stream/c\            push rtmp:\/\/a.rtmp.youtube.com\/live2\/${3}; # youtube-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/youtube-webtv-stream/c\            # youtube-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/youtube-webtv-stream/c\            push rtmp:\/\/a.rtmp.youtube.com\/live2\/${3}; # youtube-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/youtube-pclive-stream/c\            # youtube-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-FB" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/facebook-pclive-stream/c\            # facebook-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/facebook-webtv-stream/c\            # facebook-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 5 5
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		STUNNEL_PORT="1${5:1}"
		if [[ ${4} == "1" ]]; then
			sed -i "/facebook-pclive-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/rtmp\/${3}; # facebook-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/facebook-webtv-stream/c\            # facebook-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/facebook-webtv-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/rtmp\/${3}; # facebook-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/facebook-pclive-stream/c\            # facebook-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"

			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-TW" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/twitch-pclive-stream/c\            # twitch-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/twitch-webtv-stream/c\            # twitch-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 5 5
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		if [[ ${4} == "1" ]]; then
			sed -i "/twitch-pclive-stream/c\            push rtmp:\/\/live-${5}.twitch.tv\/app\/${3}; # twitch-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/twitch-webtv-stream/c\            # twitch-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/twitch-webtv-stream/c\            push rtmp:\/\/live-${5}.twitch.tv\/app\/${3}; # twitch-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/twitch-pclive-stream/c\            # twitch-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-PS" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/periscope-pclive-stream/c\            # periscope-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/periscope-webtv-stream/c\            # periscope-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 4 4
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		if [[ ${4} == "1" ]]; then
			sed -i "/periscope-pclive-stream/c\            push rtmp:\/\/de.pscp.tv:80\/x\/${3}; # periscope-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/periscope-webtv-stream/c\            # periscope-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/periscope-webtv-stream/c\            push rtmp:\/\/de.pscp.tv:80\/x\/${3}; # periscope-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/periscope-pclive-stream/c\            # periscope-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-DM" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/dailymotion-pclive-stream/c\            # dailymotion-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/dailymotion-webtv-stream/c\            # dailymotion-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 4 4
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		if [[ ${4} == "1" ]]; then
			sed -i "/dailymotion-pclive-stream/c\            push rtmp:\/\/publish.dailymotion.com\/publish-dm\/${3}; # dailymotion-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/dailymotion-webtv-stream/c\            # dailymotion-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/dailymotion-webtv-stream/c\            push rtmp:\/\/publish.dailymotion.com\/publish-dm\/${3}; # dailymotion-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/dailymotion-pclive-stream/c\            # dailymotion-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-CS" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/custom-pclive-stream/c\            # custom-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/custom-webtv-stream/c\            # custom-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi
	ARGC_func 5 5
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		if [[ ${4} == "1" ]]; then
			C_URL=${5////\\/}
			sed -i "/custom-pclive-stream/c\            push ${C_URL}\/${3}; # custom-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/custom-webtv-stream/c\            # custom-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		else
			C_URL=${5////\\/}
			sed -i "/custom-webtv-stream/c\            push ${C_URL}\/${3}; # custom-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/custom-pclive-stream/c\            # custom-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi

if [[ ${Option} == "social-TEL" ]]; then
	if [[ ${3} == "off" ]]; then
		if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
			sed -i "/telegram-pclive-stream/c\            # telegram-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/telegram-webtv-stream/c\            # telegram-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
			echo -e "success-done"
		fi
		exit -1
	fi

	ARGC_func 5 5
	if [[ -f ${NGX_CONF}/${2}-rtmp.conf ]]; then
		STUNNEL_PORT="4${5:1}"
		if [[ ${4} == "1" ]]; then
			sed -i "/telegram-pclive-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/s\/${3}; # telegram-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/telegram-webtv-stream/c\            # telegram-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			/bin/systemctl reload nginx.service
		elif [[ ${4} == "2" ]]; then
			sed -i "/telegram-webtv-stream/c\            push rtmp:\/\/127.0.0.1:${STUNNEL_PORT}\/s\/${3}; # telegram-webtv-stream" "${NGX_CONF}/${2}-rtmp.conf"
			sed -i "/telegram-pclive-stream/c\            # telegram-pclive-stream" "${NGX_CONF}/${2}-rtmp.conf"

			/bin/systemctl reload nginx.service
		fi
	fi
	echo -e "success-done"
fi


if [[ ${Option} == "newfeatures" ]]; then
	CUR_VER=`cat ${ENV_F} | grep APP_VERSION= | cut -d"=" -f2 | tr -d "."`
	for i in `curl -s https://vdopanel.com/dist/releaselog/all-release.txt`
	do
		VER_DOT2=`echo ${i} | tr -d "."`
		if [[ "${VER_DOT2}" -gt "${CUR_VER}" ]]; then
			echo "* v${i}"
			curl -s https://vdopanel.com/dist/releaselog/${i}.txt
		fi
	done
fi


if [[ ${Option} == "restart" ]]; then
	ARGC_func 2 2
	if [[ ${2} == "nginx" ]]; then
		/bin/systemctl reload nginx.service
		validation_func "echo Error : when restart nginx service"
		echo -e "nginx service restarted successfully"
	elif [[ ${2} == "stunnel" ]]; then
		/bin/systemctl daemon-reload
		/bin/systemctl restart stunnel.service
		validation_func "echo Error : when restart stunnel service"
		echo -e "stunnel service restarted successfully"
	elif [[ ${2} == "ftp" ]]; then
		if [[ ! -f ${cPanel_chk} ]]; then
			/bin/systemctl restart vsftpd
			validation_func "echo Error : when restart FTP service"
			echo -e "FTP service restarted successfully"
		else
			echo -e "This is cPanel server please restart ftp from WHM"
		fi
	elif [[ ${2} == "php-fpm" ]]; then
		if [[ ! -f ${cPanel_chk} ]]; then
			if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
				/bin/systemctl restart php8.3-fpm.service
			else
				/bin/systemctl restart php-fpm.service
			fi
			validation_func "echo Error : when restart php-fpm service"
			/bin/systemctl reload nginx.service
			validation_func "echo Error : when restart php-fpm service"
			echo -e "php-fpm service restarted successfully"
		else
			echo -e "This is cPanel server please restart from WHM"
		fi
	fi
fi


if [[ ${Option} == "testremote" ]]; then
	ARGC_func 5 5
	REMOTE_HOST=${2}
	REMOTE_PORT=${3}
	REMOTE_USER=${4}
	REMOTE_PASS=${5}
	ssh-keygen -R ${REMOTE_HOST} >> ${RemoteConnLog} 2>&1
	sshpass -p "${REMOTE_PASS}" ssh -p ${REMOTE_PORT} -o ConnectTimeout=7 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "exit" >> ${RemoteConnLog} 2>&1
	validation_func "echo Connection failed, remote host not connected."
	echo "Remote host connect success."
fi

if [[ ${Option} == "listremote_b" ]]; then
	ARGC_func 7 7
	REMOTE_HOST=${2}
	REMOTE_PORT=${3}
	REMOTE_USER=${4}
	REMOTE_PASS=${5}
	REMOTE_DEST=${6}
	REMOTE_TYPE=${7}
	R_CONN=`sshpass -p "${REMOTE_PASS}" ssh -p ${REMOTE_PORT} -o ConnectTimeout=7 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "ls -la ${REMOTE_DEST}/${REMOTE_TYPE}/broadcasters"`
	validation_func "echo Error happened."
	COUNT=0
	IFS=$'\n'
	for i in ${R_CONN}
	do
		COUNT=$((COUNT +1))
		if [[ ${COUNT} == 1 ]] || [[ ${COUNT} == 2 ]] || [[ ${COUNT} == 3 ]]; then
			continue
		fi
		echo ${i} | awk {'print $6,$7,$8,$9'}
	done
fi

if [[ ${Option} == "backupstatus" ]]; then
	ARGC_func 2 2
	Backup_Enable_L=`mysql ${DB_NAME} -s -N -e "SELECT local_status FROM backups"`
	Backup_Enable_R=`mysql ${DB_NAME} -s -N -e "SELECT remote_status FROM backups"`
	Backup_Logs="/home/vdopanel/core/logs"
	CHECK_B_S_FUNC()
	{
		if [[ ${2} == 0 ]]; then
			echo -e "${3} backup disabled."
		elif [[ ${2} == 1 ]]; then
			if [[ -f ${Backup_Logs}/${1} ]]; then
				B_STATUS=`tail -1 ${Backup_Logs}/${1} | grep backup-status | cut -d "=" -f2 | tr -d "'"`
				if [[ ! ${B_STATUS} ]]; then
					echo -e "Status\t : Not available now, May be still backup process working on the server, check it later."
					exit -1
				fi
				END_T=`tail -2 ${Backup_Logs}/${1} | grep "End date :" | awk '{for (i=1; i<=NF-2; i++) $i = $(i+3); NF-=3; print}'`
				T_mod=$(date -r ${Backup_Logs}/${1} +%s)
				T_now=$(date +%s)
				T_days=$(expr \( $T_now - $T_mod \) / 86400)
				T_hours=$(expr \( $T_now - $T_mod \) / 3600)
				T_minutes=$(expr \( $T_now - $T_mod \) / 60)
				if [[ ${T_days} == "0" ]]; then
					Since_T="${T_hours} Hours"
					if [[ ${T_hours} == 0 ]]; then
						Since_T="${T_minutes} Minutes"
					fi
				else
					Since_T="${T_days} Days"
				fi
				if [[ ${B_STATUS} == "success" ]]; then
					echo -e "Status\t : ${B_STATUS}\nEnded at : [ ${END_T} ]\nSince\t : [ ${Since_T} ]"
				elif [[ ${B_STATUS} == "fail" ]]; then
					echo -e "Status\t : ${B_STATUS}\nEnded at : [ ${END_T} ]\nSince\t : [ ${Since_T} ]"
				else
					echo -e "Status\t : Unknown, please check local backup logs for more info \n[ ${END_T} ]"
				fi
			else
				echo -e "Status\t: Not available yet, check it later."
			fi
		fi
	}
	if [[ ${2} == "local_b" ]]; then
		CHECK_B_S_FUNC backup-log ${Backup_Enable_L} Local
	elif [[ ${2} == "remote_b" ]]; then
		CHECK_B_S_FUNC backup-log-remote ${Backup_Enable_R} Remote
	fi
fi

if [[ ${Option} == "dobackup" ]]; then
	ARGC_func 2 2
	Backuprun_FUNC()
	{
	   AD="admin"
	   B_CHK_P=`ps -aux | grep "\/core\/backup ${1}${AD}" | grep -v "grep ${G_COLOR_}" | grep -v "G_COLOR_="`
	   if [[ ! ${B_CHK_P} ]]; then
		if [[ ${1} == "fullbackup-all-" ]]; then
			/home/vdopanel/core/backup ${1}${AD} >/dev/null 2>&1 &
			echo "backup process working now in background."
		elif [[ ${1} == "manually-" ]]; then
			echo "run"
		elif [[ ${1} == "restore-" ]]; then
			echo "run"
		elif [[ ${1} == "transfer-do-" ]]; then
			echo "run"
		fi
	   else
		echo "there other process working right now, task aborted."
	   fi
	}

	if [[ ${2} == "scheduling" ]]; then
		Backuprun_FUNC fullbackup-all-
	elif [[ ${2} == "manually" ]]; then
		Backuprun_FUNC manually-
	elif [[ ${2} == "restore" ]]; then
		Backuprun_FUNC restore-
	elif [[ ${2} == "transfer" ]]; then
		Backuprun_FUNC transfer-do-
	fi
fi


if [[ ${Option} == "rtmpauth" ]]; then
	ARGC_func 2 2
	CHK_AUTH=`cat ${NGX_CONF}/${2}-rtmp.conf* 2>/dev/null | grep rtmp-auth`
	if [[ ${CHK_AUTH} ]]; then
		echo "rtmp-auth"
	else
		echo "locked-ip"
	fi
fi


if [[ ${Option} == "serv-control" ]]; then
	ARGC_func 4 4
	KILL_P_FUNC()
	{
	  for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "G_COLOR_=" | grep ${1} | grep ${2} | awk '{print $2}'`
	  do
		kill -9 ${i}
	  done
	}
	if [[ ${2} == "restart" ]]; then
		KILL_P_FUNC ${3} ${4}
		rm -rf ${HOME_PATH}/${3}/live-streaming/*/* ${HOME_PATH}/${3}/stream/*/* ${HOME_PATH}/${3}/stream-hybrid/*/*
		/bin/systemctl reload nginx.service
		echo -e "Broadcasting service restarted."
	fi

	if [[ ${2} == "stop" ]]; then
		if [[ -f ${NGX_CONF}/${3}-rtmp.conf ]]; then
			mv ${NGX_CONF}/${3}-rtmp.conf ${NGX_CONF}/${3}-rtmp.conf_stop
		fi
		KILL_P_FUNC ${3} ${4}
		rm -rf ${HOME_PATH}/${3}/live-streaming/*/* ${HOME_PATH}/${3}/stream/*/* ${HOME_PATH}/${3}/stream-hybrid/*/*
		/bin/systemctl reload nginx.service
		echo -e "Broadcasting service stopped."
	fi

	if [[ ${2} == "start" ]]; then
		if [[ -f ${NGX_CONF}/${3}-rtmp.conf_stop ]]; then
			mv ${NGX_CONF}/${3}-rtmp.conf_stop ${NGX_CONF}/${3}-rtmp.conf
		fi
		/bin/systemctl reload nginx.service
		echo -e "Broadcasting service started."
	fi
fi


if [[ ${Option} == "sendpost" ]]; then
	DOMAIN=`mysql ${DB_NAME} -s -N -e "SELECT domain FROM users WHERE name='admin'"`
	Broad_COUNT=`mysql ${DB_NAME} -s -N -e "SELECT COUNT(*) FROM broadcasters"`
	LK_1="li";LK_2="cen";LK_3="ce"
	LK=`mysql ${DB_NAME} -s -N -e "SELECT ${LK_1}${LK_2}${LK_3} FROM users WHERE name='admin'"`
	APP_VER=`cat ${ENV_F} | grep APP_VERSION= | cut -d"=" -f2`
	OS_T=`egrep PRETTY_NAME= /etc/os-release 2>/dev/null | cut -d "=" -f2 | tr -d '"'`
	if [[ -f ${cPanel_chk} ]]; then
		OS_T="cPanel with ${OS_T}"
	fi
	if [[ ! ${OS_T} ]]; then
		OS_T="Unknown"
	fi
	RAND_TIME=`echo $(( (RANDOM % 20) + 1 ))`
	sleep "${RAND_TIME}"
	SERV_IP_FUNC
	curl -d "action=total&ip=${SERV_IP}&domain=${DOMAIN}&total=${Broad_COUNT}&appver=${APP_VER}&ostype=${OS_T}&lk=${LK}" \
	     -X POST https://directory.vdopanel.com/add.php >/dev/null 2>&1
fi


if [[ ${Option} == "cpanel" ]]; then
	if [[ ! -f ${cPanel_chk} ]]; then
		echo "nocpanel"
	else
		echo "cpanel"
	fi
fi


if [[ ${Option} == "recorder" ]]; then
	ARGC_func 3 3
	RTMP_FILE=`ls ${NGX_CONF}/${3}-rtmp.conf* 2>/dev/null`
	if [[ ${2} == "status" ]]; then
		ST_CHK=`egrep "#record off" ${RTMP_FILE}`
		if [[ ${ST_CHK} ]]; then
			echo "yes"
		else
			echo "no"
		fi
		exit -1
	fi
	if [[ ${2} == "enable" ]]; then
		sed -i "/record off; #RecordLive/c\              #record off; #RecordLive" "${RTMP_FILE}"
	elif [[ ${2} == "disable" ]]; then
		sed -i "/record off; #RecordLive/c\              record off; #RecordLive" "${RTMP_FILE}"
	fi
	/bin/systemctl reload nginx.service
	validation_func "echo 'nginx configuration error.'"
	echo "success-done"
fi

if [[ ${Option} == "recordrestream" ]]; then
	ARGC_func 4 4
	RTMP_FILE=`ls ${NGX_CONF}/${3}-rtmp.conf* 2>/dev/null`
	sed -i "s/#record off; #RecordRestream_/record off; #RecordRestream_/g" "${RTMP_FILE}"
	if [[ ${4} == "multi" ]]; then
		RECORD_TYPE="#RecordRestream_WebtvMulti"
	else
		RECORD_TYPE="#RecordRestream_WebtvSingle"
	fi
	if [[ ${2} == "enable" ]]; then
		sed -i "/record off; ${RECORD_TYPE}/c\              #record off; ${RECORD_TYPE}" "${RTMP_FILE}"
	fi
	/bin/systemctl reload nginx.service
fi


if [[ ${Option} == "dellivecach" ]]; then
	rm -rf ${HOME_PATH}/${2}/live-streaming/*/*
	GET_STORAGE=`mysql ${DB_NAME} -s -N -e "SELECT station_storage FROM broadcasters WHERE name='${2}'"`
	GET_USAGE=`/home/vdopanel/core/utils storageshow ${2}`
	if [[ ${GET_USAGE} -ge ${GET_STORAGE} && ${GET_STORAGE} != 0 ]]; then
		/home/vdopanel/core/utils recorder disable ${2}
	fi
fi


if [[ ${Option} == "statistics" ]]; then
	ARGC_func 3 3
	USER_PATH="${HOME_PATH}/${2}"
	if [[ ${3} == "now-cron" ]]; then
		/usr/local/bin/goaccess -f ${USER_PATH}/logs/access.log -a -o ${USER_PATH}/logs/statistics.html
	elif [[ ${3} == "now" ]]; then
		if [[ ! -f ${USER_PATH}/logs/statistics.html ]]; then
			/usr/local/bin/goaccess -f ${USER_PATH}/logs/access.log -a -o ${USER_PATH}/logs/statistics.html
		fi
	else
		if [[ ! -f ${USER_PATH}/logs/statistics/${3}.html ]]; then
			/usr/local/bin/goaccess -f ${USER_PATH}/logs/statistics/${3} -a -o ${USER_PATH}/logs/statistics/${3}.html
		fi
	fi
fi


if [[ ${Option} == "videost" ]]; then
	ARGC_func 4 4
	H_PORT=`egrep listen ${NGX_CONF}/${2}-http.http | awk '{print $2}'`
	GET_VIP=`netstat -an | grep "${3}\|${H_PORT}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1 | uniq`
	if [[ ${GET_VIP} ]]; then
		USER_PATH="${HOME_PATH}/${2}"
		COUNT=0
		GEO_SOURCE="/etc/nginx/geoip/GeoLite2-City.mmdb"
		DATE_M=`date '+%B-%Y'`
		ST_FILE="${USER_PATH}/logs/videostatistics/${DATE_M}"
		declare -a arr=()
		IFS=$'\n'
		for i in ${GET_VIP}
		do
			COUNT=$((COUNT +1))
			INFO1=`mmdblookup --file ${GEO_SOURCE} --ip ${i} country names en | cut -d '"' -f2 | tr -d '\n'`
			INFO2=`mmdblookup --file ${GEO_SOURCE} --ip ${i} country iso_code | cut -d '"' -f2 | tr -d '\n'`
			arr+=("${INFO1}_${INFO2}")
		done
		SORT_U1=`printf "%s\n" "${arr[@]}" | sort -u | tr '\n' ','`
		SORT_U2=`printf "%s\n" "${arr[@]}" | sort -u`
		CHK_VIDEO_F=`egrep ${4} ${ST_FILE}`
		if [[ ! ${CHK_VIDEO_F} ]]; then
			echo -e "${4}:${COUNT}:Countries:${SORT_U1}"
			echo -e "${4}:${COUNT}:Countries:${SORT_U1}" >> ${ST_FILE}
		else
			old_count=`egrep ${4} ${ST_FILE} | cut -d ":" -f2`
			new_count=$( echo "${old_count} + ${COUNT}" | bc )
			sed -i "s/${4}:${old_count}/${4}:${new_count}/g" "${ST_FILE}"
			for i2 in ${SORT_U2}
			do
				if [[ ${CHK_VIDEO_F} != *"${i2}"* ]]; then
					sed -i "/${4}:${new_count}/s/$/${i2},/" "${ST_FILE}"
				fi
			done
			echo -e "Video ${4} exists and updated."
		fi
	fi
fi

if [[ ${Option} == "showvideosST" ]]; then
	ARGC_func 3 3
	USER_PATH="${HOME_PATH}/${2}"
	Show_RES=`cat ${USER_PATH}/logs/videostatistics/${3}`
        IFS=$'\n'
	for i in ${Show_RES}
	do
		VID_NAME=`echo $i | cut -d ":" -f1`
		COUNT=`echo $i | cut -d ":" -f2`
		Countries=`echo $i | cut -d ":" -f4`
		echo ${VID_NAME},${COUNT},${Countries}
	done
fi



if [[ ${Option} == "geoip" ]]; then
	GEO_SOURCE="/etc/nginx/geoip/GeoLite2-City.mmdb"
	INFO_FUNC()
	  {
		INFO1=`mmdblookup --file ${GEO_SOURCE} --ip ${i2} country iso_code 2>/dev/null | cut -d '"' -f2 | tr -d '\n'`
		INFO2=`mmdblookup --file ${GEO_SOURCE} --ip ${i2} country names en 2>/dev/null | cut -d '"' -f2 | tr -d '\n'`
		INFO3=`mmdblookup --file ${GEO_SOURCE} --ip ${i2} location latitude 2>/dev/null | awk {'print $1'} | tr -d '\n'`
		INFO4=`mmdblookup --file ${GEO_SOURCE} --ip ${i2} location longitude 2>/dev/null | awk {'print $1'} | tr -d '\n'`
		if [[ ! ${INFO1} ]]; then
			echo "unknown,unknown,unknown,unknown"
		else
			echo "${INFO1},${INFO2},${INFO3},${INFO4}"
		fi
	  }
	if [[ ${2} == "all-admin" ]]; then
		ARGC_func 2 2
		CONF_F1=`ls ${NGX_CONF}/*-http.http 2>/dev/null`
		for i in ${CONF_F1}
		do
			H_PORT=`cat ${i} | grep listen | awk '{print $2}'`
			GET_VIP=`netstat -an | grep "${H_PORT}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1`
			for i2 in ${GET_VIP}
			do
				INFO_FUNC
			done
		done

		CONF_F2=`ls ${NGX_CONF}/*-rtmp.conf 2>/dev/null`
		for i in ${CONF_F2}
		do
			R_PORT=`cat ${i} | grep listen | awk '{print $2}' | tr -d ";"`
			GET_VIP=`netstat -an | grep "${R_PORT}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1`
			for i2 in ${GET_VIP}
			do
				INFO_FUNC
			done
		done
		exit -1
	fi

	ARGC_func 3 3
	GET_VIP=`netstat -an | grep "${2}\|${3}" | grep ESTABLISHED | awk '{print $5}' | grep -v "127.0.0.1" | cut -d":" -f1`
	for i2 in ${GET_VIP}
	do
		INFO_FUNC
	done
fi

if [[ ${Option} == "iplocation" ]]; then
	ARGC_func 2 2
	GEO_SOURCE="/etc/nginx/geoip/GeoLite2-City.mmdb"
	COUNTRY=`mmdblookup --file ${GEO_SOURCE} --ip ${2} country names en | cut -d '"' -f2 | tr -d '\n'`
	CONTINENT=`mmdblookup --file ${GEO_SOURCE} --ip ${2} continent names en | cut -d '"' -f2 | tr -d '\n'`
	COUNTRY_ISO=`mmdblookup --file ${GEO_SOURCE} --ip ${2} country iso_code | cut -d '"' -f2 | tr -d '\n' | tr '[:upper:]' '[:lower:]'`
	echo ${COUNTRY} - ${CONTINENT}
	echo ${COUNTRY_ISO}
fi


if [[ ${Option} = "domainlock" ]]; then
        HTTP_FILE=`ls ${NGX_CONF}/${3}-http.http* 2>/dev/null`
        if [[ ${2} == "status" ]]; then
		ARGC_func 3 3
                ST_CHK=`cat ${HTTP_FILE} | grep "#return 500; #Domain_Lock"`
                if [[ ${ST_CHK} ]]; then
                        echo "no"
                else
                        echo "yes"
                fi
		GET_VALUE=`egrep http_referer ${HTTP_FILE} | awk {'print $4'} | tr -d '")' | cut -d "|" -f1`
		if [[ ${GET_VALUE} == "_REF_DO_" ]]; then
			echo 
		else
			GET_DO=`egrep http_referer ${HTTP_FILE} | awk {'print $4'} | tr -d '")' | rev | cut -c24- | rev`
			echo ${GET_DO}
		fi
                exit -1
        fi
	ARGC_func 4 4
        if [[ ${2} == "enable" ]]; then
		REP1=`egrep http_referer ${HTTP_FILE} | awk {'print $4'} | tr -d '")'`
		sed -i "s/${REP1}/${4}|directory.vdopanel.com/g" "${HTTP_FILE}"
		sed -i "/#Domain_Lock/c\           return 500; #Domain_Lock" "${HTTP_FILE}"
        elif [[ ${2} == "disable" ]]; then
		sed -i "/#Domain_Lock/c\           #return 500; #Domain_Lock" "${HTTP_FILE}"
        fi
        /bin/systemctl reload nginx.service
        validation_func "echo 'nginx configuration error.'"
        echo "success-done"
fi


if [[ ${Option} == "storagedu" ]]; then
	ARGC_func 2 2
	du -sh ${2} | awk {'print $1'}
fi


if [[ ${Option} == "vdocron" ]]; then
	PHP_V_FUNC
	if [[ ! -f ${cPanel_chk} ]]; then
		chown -R vdopanel.vdopanel /var/lib/php/session
		rm -rf ${NGX_CONF}/php-fpm.conf
	fi

	CRON_PID_FUNC()
	{
	  schedule_F="/home/vdopanel/core/tmp/cron_${1}_PID"
	  sch_PID=`head -1 ${schedule_F} 2>/dev/null`
	  CHK_sch_PID=`ps -o pid= -p ${sch_PID} 2>/dev/null`
	  if [[ ! ${CHK_sch_PID} ]]; then
		${2} &
		echo $! > ${schedule_F}
		echo "run ${2}"
	  else
		echo "old process exists for ${2} so aborted."
	  fi
	}

	CRON_PID_FUNC "1" "${PHP_RUN} /home/vdopanel/public_html/artisan schedule:run"
	CRON_PID_FUNC "2" "/home/vdopanel/core/utils bandwcheck all"
	CRON_PID_FUNC "3" "/home/vdopanel/core/utils bandw action"
	CRON_PID_FUNC "4" "/home/vdopanel/core/utils loadbsyncvod"
	CRON_PID_FUNC "5" "/home/vdopanel/core/utils chown all"

	CHK_P=`ps aux | grep "artisan queue:work\|reverb:start" | grep -v "grep" | grep -v "utils"`
	if [[ ${CHK_P} != *"queue:work"* ]]; then
		${PHP_RUN} /home/vdopanel/public_html/artisan queue:work </dev/null >/dev/null 2>&1 &
	fi
	if [[ ${CHK_P} != *"reverb:start"* ]]; then
		/home/vdopanel/core/utils restart-reverb
	fi
	echo ${CHK_P}

	for i in {1..3}
	do
		/home/vdopanel/core/utils viewercount allforwidget & sleep 20
	done </dev/null >/dev/null 2>&1 &

	Date_=`date '+%I%M'`
	if [[ ${Date_} == "1000" ]]; then
		CONF_F=`ls ${NGX_CONF}/*-http.* 2>/dev/null`
		if [[ ${CONF_F} ]]; then
			for i in ${CONF_F}
			do
				GET_USER=`cat ${i} | grep alias | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
				/home/vdopanel/core/utils statistics ${GET_USER} now-cron
			done
		fi
	fi
fi


if [[ ${Option} == "lst-manual-b" ]]; then
	ls /home/vdo-*.tar.gz >/dev/null 2>&1
	validation_func
	for i in `ls /home/vdo-*.tar.gz 2>/dev/null`
	do
		FILE_NAME=`echo ${i} | cut -d "/" -f3`
		USER=`echo ${FILE_NAME} | cut -d "." -f1 | cut -d "-" -f2`
		FILE_USE=`du -sh ${i} | awk {'print $1'}`
		DATE_=`date -r ${i} '+%a %d-%b-%Y %I:%M:%S %p %Z'`
		echo ${USER},${FILE_NAME},${FILE_USE},${DATE_},
	done
fi


if [[ ${Option} == "killwebtv" ]]; then
	ARGC_func 3 3
	for i in `ps -aux | grep ffmpeg | grep -v "grep ${G_COLOR_}" | grep -v "utils killwebtv ${2}" | grep -v "multi_live/play" | grep ${2} | grep ${3} | awk '{print $2}'`
	do
		kill -9 ${i}
	done
fi


if [[ ${Option} == "ipblock" ]]; then
	ARGC_func 3 3
	Count=0
	USER=${2}
	USER_ID=`mysql ${DB_NAME} -s -N -e "SELECT id FROM broadcasters WHERE name='${USER}'"`
	IP_LOCK_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT ip_lock FROM broadcasters WHERE id='${USER_ID}'"`
	IP_LOCK_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT iplock_type FROM broadcasters WHERE id='${USER_ID}'"`
	HTTP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT http_port FROM broadcasters WHERE id='${USER_ID}'"`
	RTMP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT rtmp_port FROM broadcasters WHERE id='${USER_ID}'"`
	IPT_RUL=`/usr/sbin/iptables -L -n -v --line-numbers | grep "_${USER}_${USER_ID}_LOCK_" | grep -v source-country | awk '{print $1}' | tac`
	for Del in ${IPT_RUL}
	do
		/usr/sbin/iptables -D INPUT ${Del} >/dev/null 2>&1
	done

	if [[ ${3} == "on" ]]; then
		IP_L_FILE="/home/${USER}/data/iplock"
		if [[ -f ${IP_L_FILE} ]]; then
			sed -i '/^\s*$/d' ${IP_L_FILE}
			tr -d '\r' < ${IP_L_FILE} > /home/${USER}/data/iplock_tmp
			cat /home/${USER}/data/iplock_tmp > ${IP_L_FILE}
			rm -rf /home/${USER}/data/iplock_tmp
			for chk_error in `cat ${IP_L_FILE}`
			do
				if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
					IP_CHECK=`ipcalc -c "${chk_error}" | grep -i invalid >/dev/null 2>&1 && echo invalid_ip || echo valid_ip`
				else
					IP_CHECK=`ipcalc -cs "${chk_error}" && echo valid_ip || echo invalid_ip`
				fi

				if [[ ${IP_CHECK} == "invalid_ip" ]]; then
					echo "${chk_error} invalid ip, please fix it."
					exit -1
				fi
			done
		else
			echo "iplock source not exists."
			exit -1
		fi

		SERV_IP_FUNC
		if [[ ${IP_LOCK_TYPE} == 2 ]]; then
			Count=$((Count +1))
			/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${HTTP_PORT},${RTMP_PORT} -s ${SERV_IP} -j ACCEPT -m comment --comment "_${USER}_${USER_ID}_LOCK_"
			/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${HTTP_PORT},${RTMP_PORT} -s 127.0.0.1 -j ACCEPT -m comment --comment "_${USER}_${USER_ID}_LOCK_"
		fi

		for i in `cat ${IP_L_FILE}`
		do
			if [[ ${IP_LOCK_TYPE} == 1 ]]; then
				/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${HTTP_PORT},${RTMP_PORT} -s ${i} -j DROP -m comment --comment "_${USER}_${USER_ID}_LOCK_"
			elif [[ ${IP_LOCK_TYPE} == 2 ]]; then
				Count=$((Count +1))
				/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${HTTP_PORT},${RTMP_PORT} -s ${i} -j ACCEPT -m comment --comment "_${USER}_${USER_ID}_LOCK_"
			fi
		done
		if [[ ${Count} != 0 ]]; then
			/usr/sbin/iptables -A INPUT -p tcp --match multiport --dports ${HTTP_PORT},${RTMP_PORT} -j DROP -m comment --comment "_${USER}_${USER_ID}_LOCK_"
		fi
	fi

	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		netfilter-persistent save >/dev/null 2>&1
	else
		service iptables save >/dev/null 2>&1
	fi

	echo "success-done"
fi


if [[ ${Option} == "loadbgetlog" ]]; then
	REMOTE_HOST=${2}
	REMOTE_PORT=${3}
	REMOTE_USER=${4}
	SERV_ID=${5}
	KEY_PATH="/root/.ssh"
	ssh -i ${KEY_PATH}/LB_key -p ${REMOTE_PORT} -o ConnectTimeout=4 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cat /tmp/lb_log" > /home/vdopanel/core/logs/loadbalancer-${SERV_ID}.log
fi

if [[ ${Option} == "loadbaddserv" ]]; then
	ARGC_func 5 5
	REMOTE_HOST=${2}
	REMOTE_PORT=${3}
	REMOTE_USER=${4}
	REMOTE_PASS=${5}
	KEY_PATH="/root/.ssh"

	if [[ ! -f ${KEY_PATH}/LB_key ]]; then
		ssh-keygen -t rsa -N "" -f ${KEY_PATH}/LB_key >> ${RemoteConnLog} 2>&1
	fi
	if [[ ${5} == "test-with-key" ]]; then
		ssh -i ${KEY_PATH}/LB_key -p ${REMOTE_PORT} -o ConnectTimeout=4 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "exit" >> ${RemoteConnLog} 2>&1
		validation_func "echo Error_: Connection failed, remote host not connected."
		echo -e "success-done"
		exit -1
        fi

	APP_URL="https://vdopanel.com/dist/lb-setup.sh"
	SERV_IP_FUNC
	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		IP_CHECK=`ipcalc -c "${SERV_IP}" | grep -i invalid >/dev/null 2>&1 && echo invalid_ip || echo valid_ip`
	else
		IP_CHECK=`ipcalc -cs "${SERV_IP}" && echo valid_ip || echo invalid_ip`
	fi
	if [[ ${IP_CHECK} == "invalid_ip" ]]; then
		echo "Error_: Server IP invalid."
		exit -1
	fi
	ssh-keygen -R ${REMOTE_HOST} >> ${RemoteConnLog} 2>&1
	sshpass -p "${REMOTE_PASS}" ssh -p ${REMOTE_PORT} -o ConnectTimeout=7 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "exit" >> ${RemoteConnLog} 2>&1
	validation_func "echo Error_: Connection failed, remote host not connected."
	sshpass -p "${REMOTE_PASS}" ssh-copy-id -i ${KEY_PATH}/LB_key.pub -p ${REMOTE_PORT} -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} >> ${RemoteConnLog} 2>&1
	ssh -i ${KEY_PATH}/LB_key -p ${REMOTE_PORT} -o ConnectTimeout=7 -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "curl -s ${APP_URL} | bash -s ${REMOTE_HOST} ${SERV_IP}" < /dev/null >> ${RemoteConnLog} 2>&1
	echo -e "success-done"
fi

if [[ ${Option} == "loadbrebuild" ]]; then
	TMP_DIR="/home/vdopanel/core/tmp"
	NGINX_LOADB_CONFIG="/etc/nginx/loadb"
	validation_(){
	  if [ $? -ne 0 ]; then
		echo -e "`date` : ${1}" >> /home/vdopanel/core/logs/loadb_error.log
		${2}
	  fi
	}
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT name FROM broadcasters WHERE loadb='1'"`
	CHK_LOADB_servers=`mysql ${DB_NAME} -s -N -e "SELECT * FROM loadbalancers WHERE setup_status='1'"`
	SERV_IP_FUNC
	if [[ ${CHK_LOADB_users} && ${CHK_LOADB_servers} ]]; then
		CHK_LOADB_TYPE=`mysql ${DB_NAME} -s -N -e "SELECT loadb_type FROM users WHERE id='1'"`

		LOADB_SERVERS_FUNC(){
		   LB_SERVER_IP=`mysql ${DB_NAME} -s -N -e "SELECT serv_ip FROM loadbalancers WHERE id='${i}'"`
		   LB_NAME=`mysql ${DB_NAME} -s -N -e "SELECT serv_name FROM loadbalancers WHERE id='${i}'"`
		   LB_USER=`mysql ${DB_NAME} -s -N -e "SELECT serv_user FROM loadbalancers WHERE id='${i}'"`
		   LB_PORT=`mysql ${DB_NAME} -s -N -e "SELECT serv_port FROM loadbalancers WHERE id='${i}'"`
		   LB_WEIGHT=`mysql ${DB_NAME} -s -N -e "SELECT weight FROM loadbalancers WHERE id='${i}'"`
		}

		for L_USER in `mysql ${DB_NAME} -s -N -e "SELECT name FROM broadcasters WHERE loadb='1'"`
		do
			HTTP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT http_port FROM broadcasters WHERE name='${L_USER}'"`
			RTMP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT rtmp_port FROM broadcasters WHERE name='${L_USER}'"`
			BITRATE=`mysql ${DB_NAME} -s -N -e "SELECT bitrate FROM broadcasters WHERE name='${L_USER}'"`
			DOMAIN_CHK=`grep server_name ${NGX_CONF}/${L_USER}-http* | awk {'print $2'} | tr -d ";"`
			if [[ ${DOMAIN_CHK} ]]; then
				DOMAIN=${DOMAIN_CHK}
				HTTP_S_="https://"
				if [[ -f ${cPanel_chk} ]]; then
					loadb_http_file="loadb_http_ssl_cpanel.conf"
				else
					loadb_http_file="loadb_http_ssl.conf"
				fi
			else
				DOMAIN=${SERV_IP}
				HTTP_S_="http://"
				loadb_http_file="loadb_http.conf"
			fi

			if [[ ${CHK_LOADB_TYPE} == 1 ]]; then
				echo -e "upstream ${L_USER} {\n   ip_hash;" > ${NGINX_LOADB_CONFIG}/${L_USER}.balance
			elif [[ ${CHK_LOADB_TYPE} == 2 ]]; then
				echo -e "map \$geoip2_data_continent_code \$nearest_server_${L_USER} {\n   default all_${L_USER};" > ${NGINX_LOADB_CONFIG}/${L_USER}.balance
			fi
			loadbalancers_table=`mysql ${DB_NAME} -s -N -e "SELECT id FROM loadbalancers WHERE setup_status='1'"`
			for i in ${loadbalancers_table}
			do
				LOADB_SERVERS_FUNC
				if [[ ${CHK_LOADB_TYPE} == 1 ]]; then
					echo -e "      server ${LB_SERVER_IP}:${HTTP_PORT} weight=${LB_WEIGHT} max_fails=1 fail_timeout=10s ;" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
				fi
				if [[ -d /etc/letsencrypt/archive ]]; then
					rsync -avzR --recursive -e "ssh -p ${LB_PORT} -i /root/.ssh/LB_key -o ConnectTimeout=5 -o StrictHostKeyChecking=no" \
					/etc/letsencrypt/archive /etc/letsencrypt/live ${LB_USER}@${LB_SERVER_IP}:/ >/dev/null 2>&1
					validation_ "error when sync letsencrypt ssl files for ${LB_SERVER_IP}" continue
				fi
				if [[ -f ${cPanel_chk} ]]; then
					rsync -avzR --recursive -e "ssh -p ${LB_PORT} -i /root/.ssh/LB_key -o ConnectTimeout=5 -o StrictHostKeyChecking=no" \
					/var/cpanel/ssl/apache_tls ${LB_USER}@${LB_SERVER_IP}:/ >/dev/null 2>&1
					validation_ "error when sync cPanel ssl files for ${LB_SERVER_IP}" continue
				fi
				ssh -i /root/.ssh/LB_key -p ${LB_PORT} -o ConnectTimeout=5 -o StrictHostKeyChecking=no ${LB_USER}@${LB_SERVER_IP} \
				"mkdir -p /home/${L_USER}/live-streaming/hls /home/${L_USER}/stream/hls /home/${L_USER}/stream-hybrid/hls; chmod -R 777 /home/${L_USER}" >/dev/null 2>&1
				validation_ "error when sync create users directory at home for ${LB_SERVER_IP}" continue

				cat ${SOU_CONF}/${loadb_http_file} > ${TMP_DIR}/${L_USER}-http.http
				validation_ "error when create ${L_USER}-http.http for ${LB_SERVER_IP}" continue
				sed -i "s/_PORT_/${HTTP_PORT}/g" ${TMP_DIR}/${L_USER}-http.http
				sed -i "s/_USER_/${L_USER}/g" ${TMP_DIR}/${L_USER}-http.http
				sed -i "s/_DOMAIN_/${DOMAIN}/g" ${TMP_DIR}/${L_USER}-http.http
				sed -i "s/_MAIN_SERV_IP_/${SERV_IP}/g" ${TMP_DIR}/${L_USER}-http.http

				cat ${SOU_CONF}/loadb_rtmp.conf > ${TMP_DIR}/${L_USER}-rtmp.conf
				validation_ "error when create ${L_USER}-rtmp.conf for ${LB_SERVER_IP}" continue
				sed -i "s/_PUBLIC_SERV_IP_/${LB_SERVER_IP}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				sed -i "s/_PORT_/${RTMP_PORT}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				sed -i "s/_USER_/${L_USER}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				sed -i "s/_MAIN_SERV_IP_/${SERV_IP}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
#				sed -i "s/_BitRate_/${BITRATE}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
#				Resolution=`grep hls_variant ${NGX_CONF}/${L_USER}-rtmp* | head -1 | awk {'print $2'}`
#				sed -i "s/_Resolution_/${Resolution}/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				STREAM_MULTI=`mysql ${DB_NAME} -s -N -e "SELECT stream_multi FROM broadcasters WHERE name='${L_USER}'"`
				if [[ ${STREAM_MULTI} == *"288"* ]]; then
					sed -i "s/#hls_variant _240/hls_variant _240/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				if [[ ${STREAM_MULTI} == *"448"* ]]; then
					sed -i "s/#hls_variant _360/hls_variant _360/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				if [[ ${STREAM_MULTI} == *"1152"* ]]; then
					sed -i "s/#hls_variant _480/hls_variant _480/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				if [[ ${STREAM_MULTI} == *"2048"* ]]; then
					sed -i "s/#hls_variant _720/hls_variant _720/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				if [[ ${STREAM_MULTI} == *"4096"* ]]; then
					sed -i "s/#hls_variant _1080/hls_variant _1080/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				if [[ ${STREAM_MULTI} == *"90000"* ]]; then
					sed -i "s/#hls_variant _src/hls_variant _src/g" ${TMP_DIR}/${L_USER}-rtmp.conf
				fi
				rsync -avz --recursive -e "ssh -p ${LB_PORT} -i /root/.ssh/LB_key -o ConnectTimeout=5 -o StrictHostKeyChecking=no" \
				${TMP_DIR}/${L_USER}-http.http ${TMP_DIR}/${L_USER}-rtmp.conf ${LB_USER}@${LB_SERVER_IP}:${NGX_CONF} >/dev/null 2>&1
				validation_ "error when sync nginx vhost config files for ${LB_SERVER_IP}" continue
				ssh -i /root/.ssh/LB_key -p ${LB_PORT} -o ConnectTimeout=5 -o StrictHostKeyChecking=no ${LB_USER}@${LB_SERVER_IP} \
				"/bin/systemctl reload nginx.service" >/dev/null 2>&1
				validation_ "error when reload nginx service for ${LB_SERVER_IP}" continue				
			done

			if [[ ${CHK_LOADB_TYPE} == 1 ]]; then
				PROXY_PASS_URL=${L_USER}
				echo -e "}" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
			elif [[ ${CHK_LOADB_TYPE} == 2 ]]; then
				PROXY_PASS_URL="\$nearest_server_${L_USER}"
				GEO_SOURCE="/etc/nginx/geoip/GeoLite2-Country.mmdb"
				declare -a arr=()
				for i in ${loadbalancers_table}
				do
					LOADB_SERVERS_FUNC
					continent_code=`mmdblookup --file ${GEO_SOURCE} --ip ${LB_SERVER_IP} continent code | cut -d '"' -f2 | tr -d '\n'`
					arr+=("${continent_code}")
				done
				SORT=`printf "%s\n" "${arr[@]}" | sort -u`
				LAST=`printf "%s\n" "${arr[@]}" | sort -u | tr '\n' ' ' | awk '{print $NF}'`
				for i2 in ${SORT}
				do
					lower_continent=`echo ${i2} | tr '[:upper:]' '[:lower:]'`
					echo -e "   ${i2} ${lower_continent}_${L_USER};" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
					if [[ ${LAST} == ${i2} ]]; then
						echo -e "}\n" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
					fi
				done

				DEFAULT_SERV=`mysql ${DB_NAME} -s -N -e "SELECT serv_ip FROM loadbalancers WHERE default_all='1'"`
				DEFAULT_SERV_WEIGHT=`mysql ${DB_NAME} -s -N -e "SELECT weight FROM loadbalancers WHERE default_all='1'"`
				echo -e "upstream all_${L_USER} {\n   ip_hash;\n   server ${DEFAULT_SERV}:${HTTP_PORT} weight=${DEFAULT_SERV_WEIGHT} max_fails=1 fail_timeout=10s ;\n}\n" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance

				for i3 in ${SORT}
				do
					lower_continent=`echo ${i3} | tr '[:upper:]' '[:lower:]'`
					echo -e "upstream ${lower_continent}_${L_USER} {\n   ip_hash;" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
					for i in ${loadbalancers_table}
					do
						LOADB_SERVERS_FUNC
						continent_code=`mmdblookup --file ${GEO_SOURCE} --ip ${LB_SERVER_IP} continent code | cut -d '"' -f2 | tr -d '\n'`
						if [[ ${i3} == ${continent_code} ]]; then
							echo -e "   server ${LB_SERVER_IP}:${HTTP_PORT} weight=${LB_WEIGHT} max_fails=1 fail_timeout=10s ;" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
						fi
					done
					echo -e "}\n" >> ${NGINX_LOADB_CONFIG}/${L_USER}.balance
				done
			fi

			sed -i "/LOADB_SET/c\           proxy_pass ${HTTP_S_}${PROXY_PASS_URL}; #LOADB_SET" ${NGX_CONF}/${L_USER}-http*

			sed -i "/_LoadB_PUSH_SET_/d" ${NGX_CONF}/${L_USER}-rtmp*
			LINE_NO1=`sed -n "\|_LoadB_PUSH_pclive_|=" ${NGX_CONF}/${L_USER}-rtmp*`
			for i in ${loadbalancers_table}
			do
				LOADB_SERVERS_FUNC
				LINE_NO1=$((LINE_NO1 +1))
				sed -i "${LINE_NO1}s/.*/            push rtmp:\/\/${LB_SERVER_IP}:${RTMP_PORT}\/${L_USER}live\/${L_USER}live; # _LoadB_PUSH_SET_pclive_\n/" ${NGX_CONF}/${L_USER}-rtmp*
			done
			LINE_NO2=`sed -n "\|_LoadB_PUSH_webtv_|=" ${NGX_CONF}/${L_USER}-rtmp*`
			for i in ${loadbalancers_table}
			do
				LOADB_SERVERS_FUNC
				LINE_NO2=$((LINE_NO2 +1))
				sed -i "${LINE_NO2}s/.*/            push rtmp:\/\/${LB_SERVER_IP}:${RTMP_PORT}\/${L_USER}stream\/play; # _LoadB_PUSH_SET_webtv_\n/" ${NGX_CONF}/${L_USER}-rtmp*
			done
			LINE_NO3=`sed -n "\|_LoadB_PUSH_multipclive_|=" ${NGX_CONF}/${L_USER}-rtmp*`
			for i in ${loadbalancers_table}
			do
				LOADB_SERVERS_FUNC
				LINE_NO3=$((LINE_NO3 +1))
				sed -i "${LINE_NO3}s/.*/            push rtmp:\/\/${LB_SERVER_IP}:${RTMP_PORT}\/multi_live; # _LoadB_PUSH_SET_multipclive_\n/" ${NGX_CONF}/${L_USER}-rtmp*
			done
			LINE_NO4=`sed -n "\|_LoadB_PUSH_multiwebtv_|=" ${NGX_CONF}/${L_USER}-rtmp*`
			for i in ${loadbalancers_table}
			do
				LOADB_SERVERS_FUNC
				LINE_NO4=$((LINE_NO4 +1))
				sed -i "${LINE_NO4}s/.*/            push rtmp:\/\/${LB_SERVER_IP}:${RTMP_PORT}\/multi_web; # _LoadB_PUSH_SET_multiwebtv_\n/" ${NGX_CONF}/${L_USER}-rtmp*
			done
		done
		rm -rf ${TMP_DIR}/*-http.http ${TMP_DIR}/*-rtmp.conf
		/bin/systemctl reload nginx.service >/dev/null 2>&1
		validation_func "echo Error_: nginx error."
		echo -e "success-done"
	else
		echo -e "success-done"
	fi
fi

if [[ ${Option} == "loadb" ]]; then
	ARGC_func 3 3
	if [[ ${2} == "disable" ]]; then
		/home/vdopanel/core/utils rebuildrtmp ${3} >/dev/null 2>&1
		DOMAIN_CHK=`egrep server_name ${NGX_CONF}/${3}-http* 2>/dev/null | awk {'print $2'} | tr -d ";"`
		HTTP_PORT=`egrep listen ${NGX_CONF}/${3}-http* 2>/dev/null | awk {'print $2'}`
		if [[ ${DOMAIN_CHK} ]]; then
			PARS_="rebuilddomain"
			DOMAIN=${DOMAIN_CHK}
		else
			PARS_="rebuilddomain2"
			SERV_IP_FUNC
			DOMAIN=${SERV_IP}
		fi
		/home/vdopanel/core/utils ${PARS_} ${3} ${DOMAIN} ${HTTP_PORT} >/dev/null 2>&1
		rm -rf /etc/nginx/loadb/${3}.balance
		/bin/systemctl reload nginx.service >/dev/null 2>&1
		validation_func "echo Error_: nginx error."
		echo -e "success-done"
	fi

elif [[ ${Option} == "loadbcheckstatus" ]]; then
	bash <(curl -s ${Source_L}/loadbstatus)
fi

if [[ ${Option} == "loadbsyncvod" ]]; then
	CHK_LOADB_users=`mysql ${DB_NAME} -s -N -e "SELECT name FROM broadcasters WHERE loadb='1'"`
	CHK_LOADB_servers=`mysql ${DB_NAME} -s -N -e "SELECT * FROM loadbalancers WHERE setup_status='1'"`
	if [[ ${CHK_LOADB_users} && ${CHK_LOADB_servers} ]]; then
		tmp_process="/home/vdopanel/core/tmp/loadbsyncprocess"
		if [[ ! -f ${tmp_process} ]]; then
			echo > ${tmp_process}
			LOADB_SERVERS_FUNC(){
			   LB_SERVER_IP=`mysql ${DB_NAME} -s -N -e "SELECT serv_ip FROM loadbalancers WHERE id='${i}'"`
			   LB_USER=`mysql ${DB_NAME} -s -N -e "SELECT serv_user FROM loadbalancers WHERE id='${i}'"`
			   LB_PORT=`mysql ${DB_NAME} -s -N -e "SELECT serv_port FROM loadbalancers WHERE id='${i}'"`
			}
			for L_USER in `mysql ${DB_NAME} -s -N -e "SELECT name FROM broadcasters WHERE loadb='1'"`
			do
				loadbalancers_table=`mysql ${DB_NAME} -s -N -e "SELECT id FROM loadbalancers WHERE setup_status='1'"`
				for i in ${loadbalancers_table}
				do
					LOADB_SERVERS_FUNC
					rsync -avz --recursive -og --chown=root:root --delete-after \
					-e "ssh -p ${LB_PORT} -i /root/.ssh/LB_key -o ConnectTimeout=5 -o StrictHostKeyChecking=no" \
					${HOME_PATH}/${L_USER}/uploads ${LB_USER}@${LB_SERVER_IP}:/home/${L_USER}/
				done
			done
			rm -rf ${tmp_process}
		else
			echo "There is already an old process running now."
		fi
	fi
fi


if [[ ${Option} == "youtubedownload" ]]; then
        ARGC_func 3 3
	if [[ ${3} == "chk" ]]; then
		CHK_P=`ps aux | grep "yt-dlp" | grep "/${2}/uploads" | grep -v "grep ${G_COLOR_}" | grep -v "core/utils"`
		if [[ ${CHK_P} ]]; then
			echo "There is already an old process running now."
		else
			echo "pass"
		fi
		exit -1
	fi

	if [[ ${3} == "chkp" ]]; then
		CHK_P=`ps aux | grep "yt-dlp" | grep "/${2}/" | grep "youtube.com\|youtu.be" | grep -v "grep ${G_COLOR_}" | grep -v "core/utils"`
		if [[ ${CHK_P} ]]; then
			PROCESS_ID=`echo ${CHK_P} | awk {'print $2'}`
			Y_URL=`echo ${CHK_P} | awk {'print $17'}`
			echo -e "${PROCESS_ID}\n${Y_URL}"
		else
			echo "no_process"
		fi
		exit -1
	fi

	if [[ ${3} == "killp" ]]; then
		kill -9 ${2}
		echo "success-done"
		exit -1
	fi

	YT_CO=`mysql ${DB_NAME} -s -N -e "SELECT ytcookies FROM  broadcasters WHERE name='${2}'"`
	Y_LOG_F="/home/vdopanel/core/logs/youtube-${2}.log"
	echo > ${Y_LOG_F}
	mkdir -p ${HOME_PATH}/${2}/uploads/YouTube_Downloads
	chmod 777 ${HOME_PATH}/${2}/uploads/YouTube_Downloads
	COOKIES_=""
	if [[ ${YT_CO} == 1 ]]; then
		COOKIES_="--cookies ${HOME_PATH}/${2}/data/ytco"
	fi
	/usr/local/bin/yt-dlp -f "best[height<=720]" -o "${HOME_PATH}/${2}/uploads/YouTube_Downloads/%(title)s.%(ext)s" "${3}" ${COOKIES_} > ${Y_LOG_F}
	if [ $? -ne 0 ]; then
		echo -e "Warning: some of requests may be appear error, If the video already downloaded so dont worry." >> ${Y_LOG_F}
#		echo -e "\nError happened, invalid youtube URL or process has been killed." >> ${Y_LOG_F}
#		exit -1
	fi
	echo -e "\nProcess has been finished." >> ${Y_LOG_F}
fi


if [[ ${Option} == "delsess" ]]; then
	ARGC_func 3 3
	UserID=${3}
	CLEAR_FUNC()
	{
		GET_ID=`cat ${i} | rev | cut -d ";" -f2 | rev | cut -d":" -f2`
		if [[ ${UserID} == ${GET_ID} ]]; then
			rm -rf ${i}
			Session_Name=`echo ${i} | rev | cut -d "/" -f1 | rev`
			echo "session [ ${Session_Name} ] has been deleted."
		fi
	}
	for i in `ls /home/vdopanel/public_html/storage/framework/sessions/* 2>/dev/null`
	do
		if [[ ${2} == "admin" ]]; then
			rm -rf ${i}
			Session_Name=`echo ${i} | rev | cut -d "/" -f1 | rev`
			echo "session [ ${Session_Name} ] has been deleted."
		elif [[ ${2} == "broadcaster" ]]; then
			FOR_BROADC=`cat ${i} | grep login_broadcaster_`
			if [[ ${FOR_BROADC} ]]; then
				CLEAR_FUNC
			fi
		elif [[ ${2} == "reseller" ]]; then
			FOR_RESEL=`cat ${i} | grep login_reseller_`
			if [[ ${FOR_RESEL} ]]; then
				CLEAR_FUNC
			fi
		elif [[ ${2} == "administrator" ]]; then
			FOR_SUPERV=`cat ${i} | grep login_supervisor_`
			if [[ ${FOR_SUPERV} ]]; then
				CLEAR_FUNC
			fi
		fi
	done
fi


if [[ ${Option} == "videolist" ]]; then
	ARGC_func 3 3
	if [[ ${2} == "vodlist" ]]; then
		FIND_COM=`find ${HOME_PATH}/${3}/uploads -type f \( -name "*.mp3*" -o -name "*.mp4*" -o -name "*.webm*" -o -name "*.flv*" -o -name "*.avi*" \)`
	else
		FIND_COM=`find ${HOME_PATH}/${3}/uploads -type f \( -name "*.mp3*" -o -name "*.mp4*" -o -name "*.webm*" -o -name "*.flv*" -o -name "*.avi*" -o -name "*.mkv*" -o -name "*.mpeg*" -o -name "*.wmv*" -o -name "*.mov*" \)`
	fi
	for i in ${FIND_COM}
	do
		if [[ -f ${cPanel_chk} ]]; then
			REP_L=${i//\/home\/vdopanel\/users\/${3}\/uploads\//}
		else
			REP_L=${i//\/home\/${3}\/uploads\//}
		fi
		echo ${REP_L}
	done
fi


if [[ ${Option} == "gocom" ]]; then
	bash <(curl -s ${Source_L}/update1.sh)
fi


if [[ ${Option} == "convertmp4" ]]; then
	ARGC_func 3 3
	LOG_F="/home/vdopanel/core/logs/${2}_convert.log"
	if [[ ${3} == "clear_log_file" ]]; then
		echo > ${LOG_F}
		exit -1
	fi
	if [[ ${3} == "get_log" ]]; then
		cat ${LOG_F}
		exit -1
	fi
	if [[ ${3} == *"../"* ]]; then
		echo -e "Wrong Inputs !!.." >> ${LOG_F}
		cat ${LOG_F}
		exit -1
	fi
	if [[ ! -f "${HOME_PATH}/${2}/uploads/${3}" ]]; then
		echo -e "Video file not exists !!.." >> ${LOG_F}
		cat ${LOG_F}
		exit -1
	fi
	File_NAME=`echo ${3%.*}`
	rm -rf "${HOME_PATH}/${2}/uploads/${File_NAME}_convert.mp4"
	echo -e "Start convert media file for ${3} ...\n" >> ${LOG_F}
	ffmpeg -i "${HOME_PATH}/${2}/uploads/${3}" -codec copy "${HOME_PATH}/${2}/uploads/${File_NAME}_convert.mp4" &>> ${LOG_F}
	echo -e "\nEnd convert media file.\n" >> ${LOG_F}
	echo -e "Start replace converted file to the source ...\n" >> ${LOG_F}
	rm -rf "${HOME_PATH}/${2}/uploads/${3}"
	mv -f "${HOME_PATH}/${2}/uploads/${File_NAME}_convert.mp4" "${HOME_PATH}/${2}/uploads/${File_NAME}.mp4"
	echo -e "End replace converted file to the source.\n" >> ${LOG_F}
	echo -e "Process has been Finished for ${3}\n" >> ${LOG_F}
	echo -e "done"
fi


if [[ ${Option} == "systeminfo" ]]; then
	ARGC_func 2 2
	if [[ ${2} == "osname" ]]; then
		OS_T=`egrep PRETTY_NAME= /etc/os-release 2>/dev/null | cut -d "=" -f2 | tr -d '"'`
		if [[ -f ${cPanel_chk} ]]; then
			OS_T="cPanel with ${OS_T}"
		fi
		if [[ ! ${OS_T} ]]; then
			OS_T="Unknown"
		fi
		echo ${OS_T}
	elif [[ ${2} == "appv" ]]; then
		APPV=`egrep APP_VERSION= ${ENV_F} 2>/dev/null | cut -d"=" -f2`
		if [[ ! ${APPV} ]]; then
			APPV="Unknown"
		fi
		echo ${APPV}
	elif [[ ${2} == "appt" ]]; then
		APPT=`egrep APP_TIER= ${ENV_F} 2>/dev/null | cut -d "=" -f2`
		if [[ ! ${APPT} ]]; then
			APPT="Unknown"
		fi
		echo ${APPT}
	elif [[ ${2} == "appurl" ]]; then
		APPURL=`egrep APP_URL= ${ENV_F} 2>/dev/null | cut -d "/" -f3`
		if [[ ! ${APPURL} ]]; then
			APPURL="Unknown"
		fi
		echo ${APPURL}
	elif [[ ${2} == "rootstorage" ]]; then
		df -h --total
	elif [[ ${2} == "ptrdns" ]]; then
		SERV_IP_FUNC
		host ${SERV_IP} 2>/dev/null | awk {'print $NF'}
	elif [[ ${2} == "iphostinfo" ]]; then
		SERV_IP_FUNC
		GEO_SOURCE="/etc/nginx/geoip/GeoLite2-City.mmdb"
		LO1=`mmdblookup --file ${GEO_SOURCE} --ip ${SERV_IP} location latitude 2>/dev/null | awk {'print $1'} | tr -d '\n'`
		LO2=`mmdblookup --file ${GEO_SOURCE} --ip ${SERV_IP} location longitude 2>/dev/null | awk {'print $1'} | tr -d '\n'`
		TZONE=`mmdblookup --file ${GEO_SOURCE} --ip ${SERV_IP} location time_zone 2>/dev/null | cut -d '"' -f2 | tr -d '\n'`
		COUNTRY=`mmdblookup --file ${GEO_SOURCE} --ip ${SERV_IP} country names en 2>/dev/null | cut -d '"' -f2 | tr -d '\n'`
		COUNTRY_ISO=`mmdblookup --file ${GEO_SOURCE} --ip ${SERV_IP} country iso_code 2>/dev/null | cut -d '"' -f2 | tr -d '\n'`
		if [[ ${LO1} ]]; then
			echo ${LO1},${LO2}
			echo ${TZONE}
			echo ${COUNTRY} ,${COUNTRY_ISO}
		else
			echo -e "Unknown\nUnknown\nUnknown"
		fi
		CHK_ORG=`curl -s -o /dev/null -w "%{http_code}" https://ipinfo.io`
		if [[ ${CHK_ORG} == 200 ]]; then
			ORG=`curl -s https://ipinfo.io/org 2>/dev/null`
			echo ${ORG}
		else
			echo "Unknown"
		fi
	fi
fi


if [[ ${Option} == "fixquota" ]]; then
	if [[ ! -f ${cPanel_chk} ]]; then
		/usr/sbin/quotacheck -cugm /
		for User in `mysql ${DB_NAME} -s -N -e "SELECT name FROM broadcasters"`
		do
			echo "Fix quota for ${User}"
			Storage=`mysql ${DB_NAME} -s -N -e "SELECT station_storage FROM broadcasters WHERE name='${User}'"`
			/usr/sbin/setquota -u ${User} ${Storage}M ${Storage}M 0 0 /
		done
	else
		echo "VDOPanel fix quota not supported on cPanel servers"
	fi
fi


if [[ ${Option} == "fixstunnel" ]]; then
	mkdir -p /var/run/stunnel
	chmod 777 /var/run/stunnel
	curl -s ${Source_L}/config/stunnel.service.txt > /lib/systemd/system/stunnel.service
	curl -s ${Source_L}/config/stunnel.conf.txt > ${STUNNEL_CONF}
	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		sed -i "s/nobody/root/g" ${STUNNEL_CONF}
	fi
	for i in `ls /etc/nginx/conf.d/*-rtmp* 2>/dev/null`
	do
		User=`cat ${i} | grep hls_path | grep live-streaming | rev | cut -d"/" -f3 | rev | head -1`
		Port_rtmp=`cat ${i} | grep listen | awk '{print $2}' | tr -d ";"`
		Port_stunn1="1${Port_rtmp:1}"
		Port_stunn2="4${Port_rtmp:1}"
		echo -e "## _${User}_vdo\n[${User}-fb-live]\nclient = yes\naccept = ${Port_stunn1}" >> ${STUNNEL_CONF}
		echo -e "connect = live-api-s.facebook.com:443" >> ${STUNNEL_CONF}
		echo -e "[${User}-tel-live]\nclient = yes\naccept = ${Port_stunn2}" >> ${STUNNEL_CONF}
		TEL_URL_CHK=`mysql ${DB_NAME} -s -N -e "SELECT telegram_url from social_streams WHERE broadcaster_id='5'" 2>/dev/null | cut -d "/" -f3`
		if [[ ${TEL_URL_CHK} ]]; then
			TEL_URL=${TEL_URL_CHK}
		else
			TEL_URL="dc4-1.rtmp.t.me"
		fi
		echo -e "connect = ${TEL_URL}:443\n" >> ${STUNNEL_CONF}
	done
	/bin/systemctl daemon-reload
	/bin/systemctl restart stunnel
	echo -e "\nStunnel Fixed\n"
fi


if [[ ${Option} == "rebuildproxy" ]]; then
	ARGC_func 2 2
	CHK_PROXY_STATUS=`mysql ${DB_NAME} -s -N -e "SELECT proxy_status FROM broadcasters WHERE name='${2}'"`
	if [[ ${CHK_PROXY_STATUS} == 1 ]]; then
		PHP_V_FUNC
		PROXY_F="/etc/nginx/proxy/${2}.proxy"
		if [[ ! -f ${cPanel_chk} ]]; then
			PROXY_T="${SOU_CONF}/proxy.conf"
		else
			PROXY_T="${SOU_CONF}/proxy_cpanel.conf"
		fi

		cat ${PROXY_T} > ${PROXY_F}
		HTTP_PORT=`mysql ${DB_NAME} -s -N -e "SELECT http_port FROM broadcasters WHERE name='${2}'"`
		DOMAIN=`${PHP_RUN} /home/vdopanel/public_html/artisan broadcaster:domain ${2}`
		sed -i "s/_PORT_/${HTTP_PORT}/g" ${PROXY_F}
		sed -i "s/_DOMAIN_/${DOMAIN}/g" ${PROXY_F}
		SERV_IP_FUNC
		if [[ ${SERV_IP} == ${DOMAIN} ]]; then
			sed -i "s/_SSL_TYPE_/http/g" ${PROXY_F}
		else
			sed -i "s/_SSL_TYPE_/https/g" ${PROXY_F}
		fi
	else
		rm -rf /etc/nginx/proxy/${2}.proxy
	fi

	if [[ ! -f ${cPanel_chk} ]]; then
		/bin/systemctl reload nginx.service
	else
		/bin/systemctl reload httpd
	fi
	echo -e "success-done"
fi


if [[ ${Option} == "downloadurlchk" ]]; then
	ARGC_func 3 3
	PID_F="/home/vdopanel/core/tmp/pids/${2}_URL_DL"
	PID_P=`head -1 ${PID_F} 2>/dev/null`
	PID_CHK=`ps -o pid= -p ${PID_P} 2>/dev/null`
	if [[ ! ${PID_CHK} ]]; then
		echo "ready"
		F_SIZE=`wget --spider "${3}" 2>&1 | grep Length: | awk {'print $2'}`
		SIZE=$( echo "${F_SIZE} / 1024 / 1024" | bc 2>/dev/null )
		if [[ ${SIZE} ]]; then
			echo ${SIZE}
		else
			echo 0
		fi
	fi
fi

if [[ ${Option} == "downloadurl" ]]; then
	ARGC_func 4 4
	DL_LOG_F="/home/vdopanel/core/logs/URL_DL-${2}.log"
	PID_F="/home/vdopanel/core/tmp/pids/${2}_URL_DL"
	PID_P=`head -1 ${PID_F} 2>/dev/null`
	PID_CHK=`ps -o pid= -p ${PID_P} 2>/dev/null`
	if [[ ${PID_CHK} ]]; then
		echo "There is already a current process running now."
		exit -1
	fi

	OUTPUT_FUNC()
	{
	   echo -e "${1}"
	   echo -e "${1}" >> ${DL_LOG_F}
	}

	> ${DL_LOG_F}
	OUTPUT_FUNC "URL : ${4}"
#	F_SIZE=`curl -s -L -I "${4}" | grep Content-Length: | awk {'print $2'}`
	F_SIZE=`wget --spider "${4}" 2>&1 | grep Length: | awk {'print $2'}`
	re='^[0-9]+$'
	if [[ ${F_SIZE} && ${F_SIZE} =~ $re ]]; then
		SIZE=$( echo "${F_SIZE} / 1024 / 1024" | bc )
		OUTPUT_FUNC "File size : ${SIZE} MB"
		F_NAME=`echo "${4}" | rev | cut -d "/" -f1 | rev`
		OUTPUT_FUNC "File name : ${F_NAME}"
		F_EXT=`echo "${F_NAME}" | rev | cut -d "." -f1 | rev | tr '[:upper:]' '[:lower:]'`
		if ! [[ ${F_EXT} =~ ^(mp4|webm|mp3|flv|avi|mkv|mpeg|wmv|mov)$ ]]; then
			OUTPUT_FUNC "Error happened, This file extension type not supported."
			exit -1
		fi

		OUTPUT_FUNC "Download started, Please wait..."
		cd "${3}"
		if [ $? -ne 0 ]; then
			OUTPUT_FUNC "Error happened, invalid location."
			exit -1
		fi
		bash -c "echo $$ > ${PID_F}; exec curl -L -O -J \"${4}\" -A \"Mozilla/4.0\""
		if [ $? -ne 0 ]; then
			OUTPUT_FUNC "Download failed."
		else
			PHP_V_FUNC
			${PHP_RUN} /home/vdopanel/public_html/artisan handel-downloaded-file:run ${2} "${F_NAME}" "${3}" >/dev/null 2>&1
			OUTPUT_FUNC "Download successed."
		fi
		/home/vdopanel/core/utils chown all
	else
		OUTPUT_FUNC "Error happened, invalid or unsupported URL."
	fi
fi


if [[ ${Option} == "restart-reverb" ]]; then
	PHP_V_FUNC
	${PHP_RUN} /home/vdopanel/public_html/artisan cache:clear
	${PHP_RUN} /home/vdopanel/public_html/artisan config:clear
	${PHP_RUN} /home/vdopanel/public_html/artisan reverb:restart
	${PHP_RUN} /home/vdopanel/public_html/artisan reverb:start </dev/null >/dev/null 2>&1 &
fi

if [[ ${Option} == "reverb-conf-rebuild" ]]; then
	SERV_IP_FUNC
	HOSTNAME=`mysql ${DB_NAME} -s -N -e "SELECT domain FROM users WHERE id='1'"`
	REVERB_CHK=`egrep REVERB_APP_ID= ${ENV_F}`
	if [[ ! ${REVERB_CHK} ]]; then
		sed -i "/REVERB_/d" ${ENV_F}
		echo "REVERB_APP_ID=248791" >> ${ENV_F}
		echo "REVERB_APP_KEY=req1a8kresadpjaxulpv" >> ${ENV_F}
		echo "REVERB_APP_SECRET=0c8l7azos6aqendy5n1k" >> ${ENV_F}
		echo "REVERB_HOST=_SERV_IP_" >> ${ENV_F}
		echo "REVERB_HOSTNAME=_SERV_DOMAIN_" >> ${ENV_F}
		echo "REVERB_PORT=5080" >> ${ENV_F}
		echo "REVERB_SCHEME=http" >> ${ENV_F}
		echo "REVERB_LOCAL_CERT=" >> ${ENV_F}
		echo "REVERB_LOCAL_PK=" >> ${ENV_F}
	fi
	sed -i "s/REVERB_HOST=.*/REVERB_HOST=${SERV_IP}/g" ${ENV_F}
	sed -i "s/REVERB_HOSTNAME=.*/REVERB_HOSTNAME=${HOSTNAME}/g" ${ENV_F}
	sed -i "s/BROADCAST_DRIVER=.*/BROADCAST_DRIVER=reverb/g" ${ENV_F}
	if [[ ${OS_CHK} == "ubuntu" || ${OS_CHK} == "debian" ]]; then
		IP_CHECK=`ipcalc -c "${HOSTNAME}" | grep -i invalid >/dev/null 2>&1 && echo invalid_ip || echo valid_ip`
	else
		IP_CHECK=`ipcalc -cs "${HOSTNAME}" && echo valid_ip || echo invalid_ip`
	fi
	if [[ ${IP_CHECK} == "valid_ip" ]]; then
		sed -i "s/REVERB_SCHEME=.*/REVERB_SCHEME=http/g" ${ENV_F}
		sed -i "s/REVERB_LOCAL_CERT=.*/REVERB_LOCAL_CERT=/g" ${ENV_F}
		sed -i "s/REVERB_LOCAL_PK=.*/REVERB_LOCAL_PK=/g" ${ENV_F}
	else
		REVERB_CERT="\/etc\/letsencrypt\/live\/${HOSTNAME}\/fullchain.pem"
		REVERB_PK="\/etc\/letsencrypt\/live\/${HOSTNAME}\/privkey.pem"
		sed -i "s/REVERB_SCHEME=.*/REVERB_SCHEME=https/g" ${ENV_F}
		sed -i "s/REVERB_LOCAL_CERT=.*/REVERB_LOCAL_CERT=${REVERB_CERT}/g" ${ENV_F}
		sed -i "s/REVERB_LOCAL_PK=.*/REVERB_LOCAL_PK=${REVERB_PK}/g" ${ENV_F}
	fi
	/home/vdopanel/core/utils restart-reverb
fi
