diff --git a/ngrokd.init b/ngrokd.init new file mode 100644 index 0000000..4fef060 --- /dev/null +++ b/ngrokd.init @@ -0,0 +1,314 @@ +#! /bin/bash +# chkconfig: 2345 55 25 +# Description: Startup script for Ngrok on Debian. Place in /etc/init.d and +# run 'update-rc.d -f ngrokd defaults', or use the appropriate command on your +# distro. For CentOS/Redhat run: 'chkconfig --add ngrokd' +#======================================================= +# System Required: CentOS/Debian/Ubuntu (32bit/64bit) +# Description: Manager for Ngrok, Written by Clang +# Author: Clang +# Intro: http://clangcn.com +#======================================================= +### BEGIN INIT INFO +# Provides: ngrok +# Required-Start: $all +# Required-Stop: $all +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts the ngrok +# Description: starts ngrok using start-stop +### END INIT INFO + +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +ProgramName="Ngrok" +ProGramInstallPath="/usr/local/ngrok" +ProgramPath="/usr/local/ngrok/bin" +NAME=ngrokd +BIN=${ProgramPath}/${NAME} +CONFIGFILE=${ProGramInstallPath}/.ngrok_config.sh +LOGFILE=${ProGramInstallPath}/ngrok.log +SCRIPTNAME=/etc/init.d/${NAME} +PID_DIR=/var/run +PID_FILE=$PID_DIR/ngrok_clang.pid +version="v5.0" +manage_port="4446" +RET_VAL=0 + +[ -x $BIN ] || exit 0 + +fun_clangcn() +{ + echo "" + echo "#############################################################" + echo "# Manager Ngrok ${version} for CentOS/Debian/Ubuntu (32bit/64bit)" + echo "# Intro: http://clang.cn" + echo "#" + echo "# Author: Clang " + echo "#" + echo "#############################################################" + echo "" +} + +fun_check_run() +{ + if netstat -tnpl | grep -q ${NAME};then + return 0 + else + rm -f $PID_FILE + return 1 + fi +} +fun_load_config(){ + if [ ! -r ${CONFIGFILE} ]; then + echo "config file ${CONFIGFILE} not found" + return 1 + else + . ${CONFIGFILE} + log_level="" + [ -n "${loglevel}" ] && log_level=" -log-level=\"${loglevel}\"" + cd ${ProGramInstallPath} + fi +} +fun_randstr(){ + index=0 + strRandomPass="" + for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done + for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done + for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done + for i in {1..16}; do strRandomPass="$strRandomPass${arr[$RANDOM%$index]}"; done + echo $strRandomPass +} +fun_start() +{ + if [ "${arg1}" = "start" ]; then + fun_clangcn + fi + if [ ! -d $PID_DIR ]; then + mkdir -p $PID_DIR || echo "failed creating PID directory ${PID_DIR}"; exit 1 + fi + if fun_check_run; then + echo "${ProgramName} (pid `pidof $NAME`) already running." + return 0 + fi + fun_load_config + echo -n "Starting ${ProgramName}..." + ${BIN} -domain="$dns" -httpAddr=":$http_port" -httpsAddr=":$https_port" -pass="$pass" -tlsCrt="$srtCRT" -tlsKey="$strKey" -tunnelAddr=":$remote_port"${log_level} > ${LOGFILE} 2>&1 & + PID=`pidof ${NAME}` + echo $PID > $PID_FILE + sleep 0.3 + if ! fun_check_run; then + echo "start failed" + return 1 + fi + echo " done" + echo "${ProgramName} (pid `pidof $NAME`)is running." + echo "read ${LOGFILE} for log" + return 0 +} + +fun_stop(){ + if [ "${arg1}" = "stop" ] || [ "${arg1}" = "restart" ]; then + fun_clangcn + fi + if fun_check_run; then + echo -n "Stoping ${ProgramName} (pid `pidof $NAME`)... " + kill `pidof $NAME` + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + else + echo " done" + rm -f $PID_FILE + fi + else + echo "${ProgramName} is not running." + fi +} +fun_restart(){ + fun_stop + fun_start +} +fun_status(){ + if netstat -tnpl | grep -q ${NAME}; then + PID=`pidof ${NAME}` + echo "${ProgramName} (pid $PID) is running..." + else + echo "${ProgramName} is stopped" + exit 0 + fi +} +fun_config(){ + nano ${CONFIGFILE} +} +fun_set_ngrok_username(){ + userName="" + read -p "Please input UserName for Ngrok(e.g.:ZhangSan):" userName + check_ngrok_username +} +fun_set_ngrok_subdomain(){ + # Set ngrok pass + subdomain="" + ddns="" + dns="" + echo "Please input subdomain for Ngrok(e.g.:dns1 dns2 dns3 dns4 dns5):" + read -p "(subdomain number max five:):" subdomain + check_ngrok_subdomain +} +fun_set_ngrok_authId(){ + strPass=`fun_randstr` + echo "Please input the password (more than 8) of Ngrok authId:" + read -p "(Default password: ${strPass}):" strPassword + if [ "$strPassword" = "" ]; then + strPassword=$strPass + fi + check_ngrok_authId +} + +check_ngrok_username(){ + # check ngrok userName + if [ "$userName" = "" ]; then + echo "Your input is empty,please input again..." + fun_set_ngrok_username + else + echo "Your username: ${userName}" + fun_set_ngrok_subdomain + fi +} +check_ngrok_subdomain(){ + # check ngrok subdomain + if [ "$subdomain" = "" ]; then + echo "Your input is empty, please input again." + fun_set_ngrok_subdomain + else + fun_load_config + ddns=(${subdomain}) + [ -n "${ddns[0]}" ] && subdns=\"${ddns[0]}.${userName}\" + [ -n "${ddns[1]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\" + [ -n "${ddns[2]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\" + [ -n "${ddns[3]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\",\"${ddns[3]}.${userName}\" + [ -n "${ddns[4]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\",\"${ddns[3]}.${userName}\",\"${ddns[4]}.${userName}\" + [ -n "${ddns[0]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\" + [ -n "${ddns[1]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\" + [ -n "${ddns[2]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\" + [ -n "${ddns[3]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\",\"${ddns[3]}.${userName}.${dns}\" + [ -n "${ddns[4]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\",\"${ddns[3]}.${userName}.${dns}\",\"${ddns[4]}.${userName}.${dns}\" + echo "Your subdomain: ${subdns}" + fun_set_ngrok_authId + fi +} +check_ngrok_authId(){ + # check ngrok authId + if [ "${strPassword}" = "" ]; then + echo "Your input is empty, please input again..." + fun_set_ngrok_authId + else + echo "Your authId: ${strPassword}" + fun_adduser_command + fi +} +fun_adduser_command(){ + fun_load_config + echo curl -H \"Content-Type: application/json\" -H \"Auth:${pass}\" -X POST -d \''{'\"userId\":\"${strPassword}\",\"authId\":\"${userName}\",\"dns\":[${subdns}]'}'\' http://localhost:${manage_port}/adduser >${ProGramInstallPath}/.ngrok_adduser.sh + chmod +x ${ProGramInstallPath}/.ngrok_adduser.sh + . ${ProGramInstallPath}/.ngrok_adduser.sh + rm -f ${ProGramInstallPath}/.ngrok_adduser.sh + clear + fun_clangcn + echo "User list :" + curl -H "Content-Type: application/json" -H "Auth:${pass}" -X GET http://localhost:${manage_port}/info + echo "=============================================================" + echo "Server: ${dns}" + echo "Server ${remote_port}" + echo "userId: ${userName}" + echo "authId: ${strPassword}" + echo "Subdomain: ${subdns}" + echo "Your FQDN: ${FQDN}" + echo "=============================================================" +} +fun_adduser(){ + if fun_check_run; then + fun_load_config + fun_set_ngrok_username + else + echo "${ProgramName} is not running." + fi +} +fun_deluser(){ + if [ -z "${1}" ]; then + strWantdeluser="" + fun_userlist + echo "" + read -p "Please input del username you want:" strWantdeluser + if [ "${strWantdeluser}" = "" ]; then + echo "Error: You must input username!!" + exit 1 + else + deluser_Confirm_clang "${strWantdeluser}" + fi + else + deluser_Confirm_clang "${1}" + fi +} +deluser_Confirm_clang(){ + if [ -z "${1}" ]; then + echo "Error: You must input username!!" + exit 1 + else + strDelUser="${1}" + echo "You want del ${strDelUser}!" + read -p "(if you want please input: y,Default [no]):" strConfirmDel + case "$strConfirmDel" in + y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) + echo "" + strConfirmDel="y" + ;; + n|N|No|NO|no|nO) + echo "" + strConfirmDel="n" + ;; + *) + echo "" + strConfirmDel="n" + esac + if [ $strConfirmDel = "y" ]; then + if [ -s "/tmp/db-diskv/ng/ro/ngrok:${strDelUser}" ]; then + rm -f /tmp/db-diskv/ng/ro/ngrok:${strDelUser} + echo "Delete user ${strDelUser} ok! Restart ${NAME}..." + fun_restart + else + echo "" + echo "Error: user ${strDelUser} not found!" + fi + else + echo "you cancel!" + fi + fi +} +fun_userlist(){ + fun_clangcn + echo "Ngrok user list:" + ls /tmp/db-diskv/ng/ro/ |cut -d ':' -f 2 +} +fun_info(){ + fun_clangcn + if fun_check_run; then + fun_load_config + curl -H "Content-Type: application/json" -H "Auth:${pass}" -X GET http://localhost:${manage_port}/info + else + echo "${ProgramName} is not running." + fi +} +arg1=$1 +arg2=$2 +[ -z ${arg1} ] +case "${arg1}" in + start|stop|restart|status|config|adduser|deluser|userlist|info) + fun_${arg1} ${arg2} + ;; + *) + fun_clangcn + echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|adduser|deluser|userlist|info}" + RET_VAL=1 + ;; +esac +exit $RET_VAL -- libgit2 0.21.0