-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
#! /bin/bash | ||
# chkconfig: 2345 55 25 | ||
# Description: Startup script for kcp-server on Debian. Place in /etc/init.d and | ||
# run 'update-rc.d -f kcp-server defaults', or use the appropriate command on your | ||
# distro. For CentOS/Redhat run: 'chkconfig --add kcp-server' | ||
#======================================================= | ||
# System Required: CentOS/Debian/Ubuntu (32bit/64bit) | ||
# Description: Manager for kcp-server, Written by Clang | ||
# Author: Clang | ||
# Intro: http://koolshare.cn/forum-72-1.html | ||
#======================================================= | ||
### BEGIN INIT INFO | ||
# Provides: kcp-server | ||
# Required-Start: $all | ||
# Required-Stop: $all | ||
# Default-Start: 2 3 4 5 | ||
# Default-Stop: 0 1 6 | ||
# Short-Description: starts the kcp-server | ||
# Description: starts kcp-server using start-stop | ||
### END INIT INFO | ||
|
||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | ||
runEnv="socks5" # set "ss" or "socks5",default socks5 | ||
ProgramName="kcp-server" | ||
ProgramPath="/usr/local/kcp-server" | ||
Socks5Name="socks5" | ||
NAME=kcp-server | ||
BIN=${ProgramPath}/${NAME} | ||
SOCKS5BIN=${ProgramPath}/${Socks5Name} | ||
CONFIGFILE=${ProgramPath}/server-kcptun.json | ||
LOGFILE=${ProgramPath}/${ProgramName}.log | ||
SOCKS5LOGFILE=${ProgramPath}/${Socks5Name}.log | ||
SCRIPTNAME=/etc/init.d/${NAME} | ||
PID_DIR=/var/run | ||
PID_FILE=${PID_DIR}/${ProgramName}.pid | ||
PID_FILE_SOCKS5=${PID_DIR}/${Socks5Name}.pid | ||
version="2.0" | ||
RET_VAL=0 | ||
|
||
[ -x ${BIN} ] || exit 0 | ||
[ -x ${SOCKS5BIN} ] || exit 0 | ||
|
||
fun_clangcn() | ||
{ | ||
echo "" | ||
echo "+--------------------------------------------------+" | ||
echo "| Manager for ${NAME}, Written by Clang |" | ||
echo "+--------------------------------------------------+" | ||
echo "| Intro: http://koolshare.cn/forum-72-1.html |" | ||
echo "+--------------------------------------------------+" | ||
echo "" | ||
} | ||
|
||
fun_check_run(){ | ||
if netstat -ntupl | grep -q ${NAME};then | ||
return 0 | ||
else | ||
rm -f ${PID_FILE} | ||
return 1 | ||
fi | ||
} | ||
fun_check_socks5_run(){ | ||
if netstat -ntupl | grep -q ${Socks5Name};then | ||
return 0 | ||
else | ||
rm -f ${PID_FILE_SOCKS5} | ||
return 1 | ||
fi | ||
} | ||
fun_load_config(){ | ||
if [ ! -r ${CONFIGFILE} ]; then | ||
echo "config file ${CONFIGFILE} not found" | ||
return 1 | ||
fi | ||
} | ||
fun_start() | ||
{ | ||
log_flag="$1" | ||
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 [ "${runEnv}" == 'socks5' ]; then | ||
if fun_check_socks5_run; then | ||
echo "${Socks5Name} (pid `pidof $Socks5Name`) already running." | ||
if fun_check_run; then | ||
echo "${ProgramName} (pid `pidof $NAME`) already running." | ||
return 0 | ||
fi | ||
return 0 | ||
fi | ||
fi | ||
if fun_check_run; then | ||
echo "${ProgramName} (pid `pidof $NAME`) already running." | ||
return 0 | ||
fi | ||
fun_load_config | ||
if [ "${runEnv}" == 'socks5' ]; then | ||
echo -n "Starting ${Socks5Name}..." | ||
socks5_port=`sed -n '/target/p' ${CONFIGFILE} | sed 's/[[:space:]]*//g;s/,//g;s/\"//g' | cut -d: -f3` | ||
if [ "${log_flag}" == 'log' ]; then | ||
${SOCKS5BIN} 0.0.0.0:${socks5_port} > ${SOCKS5LOGFILE} 2>&1 & | ||
else | ||
${SOCKS5BIN} 0.0.0.0:${socks5_port} >/dev/null 2>&1 & | ||
fi | ||
socks5PID=`pidof ${Socks5Name}` | ||
echo ${socks5PID} > ${PID_FILE_SOCKS5} | ||
sleep 0.3 | ||
if ! fun_check_socks5_run; then | ||
echo "start failed" | ||
return 1 | ||
fi | ||
echo " done" | ||
fi | ||
echo -n "Starting ${ProgramName}..." | ||
if [ "${log_flag}" == 'log' ]; then | ||
${BIN} -c ${CONFIGFILE} > ${LOGFILE} 2>&1 & | ||
else | ||
${BIN} -c ${CONFIGFILE} >/dev/null 2>&1 & | ||
fi | ||
PID=`pidof ${NAME}` | ||
echo $PID > $PID_FILE | ||
sleep 0.3 | ||
if ! fun_check_run; then | ||
echo "start failed" | ||
return 1 | ||
fi | ||
echo " done" | ||
${BIN} -v | ||
if [ "${runEnv}" == 'socks5' ]; then | ||
echo "${Socks5Name} (pid `pidof $Socks5Name`)is running." | ||
if [ "${log_flag}" == 'log' ]; then | ||
echo "read ${SOCKS5LOGFILE} for log" | ||
fi | ||
fi | ||
echo "${ProgramName} (pid `pidof $NAME`)is running." | ||
if [ "${log_flag}" == 'log' ]; then | ||
echo "read ${LOGFILE} for log" | ||
fi | ||
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 | ||
if [ "${runEnv}" == 'socks5' ]; then | ||
if fun_check_socks5_run; then | ||
echo -n "Stoping ${Socks5Name} (pid `pidof $Socks5Name`)... " | ||
kill `pidof $Socks5Name` | ||
if [ "$?" != 0 ] ; then | ||
echo " failed" | ||
exit 1 | ||
else | ||
echo " done" | ||
rm -f $PID_FILE_SOCKS5 | ||
fi | ||
else | ||
echo "${Socks5Name} is not running." | ||
fi | ||
fi | ||
} | ||
fun_restart(){ | ||
fun_stop | ||
fun_start | ||
} | ||
fun_status(){ | ||
if netstat -ntulp | grep -q ${NAME}; then | ||
PID=`pidof ${NAME}` | ||
echo "${ProgramName} (pid $PID) is running..." | ||
netstat -ntulp | grep "${NAME}" | ||
else | ||
echo "${ProgramName} is stopped" | ||
exit 0 | ||
fi | ||
echo "" | ||
if [ "${runEnv}" == 'socks5' ]; then | ||
if netstat -ntulp | grep -q ${Socks5Name}; then | ||
socks5PID=`pidof ${Socks5Name}` | ||
echo "${Socks5Name} (pid $socks5PID) is running..." | ||
netstat -ntulp | grep "${Socks5Name}" | ||
else | ||
echo "${Socks5Name} is stopped" | ||
exit 0 | ||
fi | ||
fi | ||
} | ||
checkos(){ | ||
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then | ||
OS=CentOS | ||
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then | ||
OS=Debian | ||
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then | ||
OS=Ubuntu | ||
else | ||
echo "Not support OS, Please reinstall OS and retry!" | ||
exit 1 | ||
fi | ||
} | ||
fun_config(){ | ||
if [ -s ${CONFIGFILE} ]; then | ||
vi ${CONFIGFILE} | ||
else | ||
echo "${ProgramName} configuration file not found!" | ||
fi | ||
} | ||
fun_version(){ | ||
${BIN} -v | ||
} | ||
|
||
arg1=$1 | ||
arg2=$2 | ||
[ -z ${arg1} ] | ||
case "${arg1}" in | ||
start|stop|restart|status|config) | ||
fun_${arg1} ${arg2} | ||
;; | ||
[vV][eE][rR][sS][iI][oO][nN]|-[vV][eE][rR][sS][iI][oO][nN]|--[vV][eE][rR][sS][iI][oO][nN]|-[vV]|--[vV]) | ||
fun_version | ||
;; | ||
*) | ||
fun_clangcn | ||
echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|version}" | ||
RET_VAL=1 | ||
;; | ||
esac | ||
exit $RET_VAL |