forked from affvps/shell_for_ss_ssr_ssrr_kcptun_bbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkcptun.init
executable file
·161 lines (151 loc) · 4.03 KB
/
kcptun.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /bin/bash
# chkconfig: 2345 55 25
# Description: Startup script for kcptun on Debian. Place in /etc/init.d and
# run 'update-rc.d -f kcptun defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add kcptun'
#=======================================================
# System Required: CentOS/Debian/Ubuntu (32bit/64bit)
# Description: Manager for kcptun
#=======================================================
### BEGIN INIT INFO
# Provides: kcptun
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the kcptun
# Description: starts kcptun using start-stop
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
COLOR_RED='\E[1;31m'
COLOR_RED_DEEP='\033[0;31m'
COLOR_GREEN='\E[1;32m'
COLOR_GREEN_DEEP='\033[0;32m'
COLOR_YELLOW='\E[1;33m'
COLOR_BLUE='\E[1;34m'
COLOR_PINK='\E[1;35m'
COLOR_PINKBACK_WHITEFONT='\033[45;37m'
COLOR_GREEN_LIGHTNING='\033[32m \033[05m'
COLOR_END='\E[0m'
ProgramName="kcptun"
BIN=/usr/local/kcptun/kcptun
CONFIGFILE=/usr/local/kcptun/config.json
LOGFILE=/root/kcptun.log
PID_DIR=/var/run
PID_FILE=${PID_DIR}/kcptun.pid
version="2.0"
RETVAL=0
[ -x ${BIN} ] || exit 0
if [ ! -f ${CONFIGFILE} ]; then
echo "${ProgramName}config file ${CONFIGFILE} not found"
exit 1
fi
if [ ! -d $PID_DIR ]; then
mkdir -p $PID_DIR
if [ $? -ne 0 ]; then
echo "Creating PID directory $PID_DIR failed"
exit 1
fi
fi
check_running(){
if [ -r $PID_FILE ]; then
read PID < $PID_FILE
if [ -d "/proc/$PID" ]; then
return 0
else
rm -f $PID_FILE
return 1
fi
else
PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
if [ ! -z $PID ]; then
return 0
else
return 1
fi
fi
}
do_start(){
check_running
if [ $? -eq 0 ]; then
echo "${ProgramName} (pid $PID) is already running."
exit 0
else
echo -e -n "Starting ${ProgramName}:"
if [[ "${log_flag}" =~ ^[Ll][Oo][Gg]$ ]]; then
if [ -f "$LOGFILE" ]; then rm -rf $LOGFILE; fi
${BIN} -c ${CONFIGFILE} --log $LOGFILE > /dev/null 2>&1 &
echo $! > $PID_FILE
else
${BIN} -c ${CONFIGFILE} > /dev/null 2>&1 &
echo $! > $PID_FILE
fi
fi
RETVAL=$?
if check_running; then
echo -e " [${COLOR_GREEN_DEEP} OK ${COLOR_END}]"
else
echo -e " [${COLOR_RED_DEEP}FAILED${COLOR_END}]"
RETVAL=1
fi
}
do_stop(){
echo -e -n "Stopping ${ProgramName}:"
if check_running; then
kill -9 $PID
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo -e " [${COLOR_GREEN_DEEP} OK ${COLOR_END}]"
else
echo -e " [${COLOR_RED_DEEP}FAILED${COLOR_END}]"
RETVAL=1
fi
else
echo -e " [${COLOR_RED_DEEP}FAILED${COLOR_END}]"
echo -e "${COLOR_RED}${ProgramName} not running.${COLOR_END}"
RETVAL=1
fi
RETVAL=$?
[ $RETVAL -eq 0 ]
rm -f $PID_FILE
}
do_status() {
check_running
case $? in
0)
echo "${ProgramName} (pid $PID) is running..."
;;
1)
echo "${ProgramName} is stopped"
RETVAL=1
;;
esac
}
do_restart() {
do_stop
do_start
}
do_config(){
vi ${CONFIGFILE}
}
do_viewconfig() {
cat ${CONFIGFILE}
}
do_version(){
${BIN} -v
}
log_flag=$2
case "$1" in
start|stop|restart|status|config|viewconfig)
check_running
do_$1
;;
[vV][eE][rR][sS][iI][oO][nN]|-[vV][eE][rR][sS][iI][oO][nN]|--[vV][eE][rR][sS][iI][oO][nN]|-[vV]|--[vV])
do_version
;;
*)
echo "Usage: $0 { start | stop | restart | status | config | viewconfig | version }"
RETVAL=1
;;
esac
exit $RETVAL