forked from clangcn/kcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kcptun-server.init
230 lines (225 loc) · 6.55 KB
/
kcptun-server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#! /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.3"
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 ""
}
if [ ! -r ${CONFIGFILE} ]; then
echo "config file ${CONFIGFILE} not found"
exit 1
fi
fun_check_run(){
PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
if [ ! -z $PID ]; then
return 0
else
return 1
fi
}
fun_check_socks5_run(){
SOCKS5PID=`ps -ef | grep -v grep | grep -i "${SOCKS5BIN}" | awk '{print $2}'`
if [ ! -z $SOCKS5PID ]; then
return 0
else
return 1
fi
}
fun_start()
{
log_flag="$1"
if [[ "${arg1}" =~ ^[Ss][Tt][Aa][Rr][Tt]$ ]]; 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}" =~ ^[Ss][Oo][Cc][Kk][Ss]5$ ]]; then
if fun_check_socks5_run; then
echo "${Socks5Name} (pid $SOCKS5PID) already running."
if fun_check_run; then
echo "${ProgramName} (pid $PID) already running."
return 0
fi
return 0
fi
fi
if fun_check_run; then
echo "${ProgramName} (pid $PID) already running."
return 0
fi
if [[ "${log_flag}" =~ ^[Ll][Oo][Gg]$ ]]; then
strLog="${SOCKS5LOGFILE}"
strKcpLog="${LOGFILE}"
else
strLog="/dev/null"
strKcpLog="/dev/null"
fi
if [[ "${runEnv}" =~ ^[Ss][Oo][Cc][Kk][Ss]5$ ]]; then
echo -n "Starting ${Socks5Name}..."
socks5_port=`sed -n '/target/p' ${CONFIGFILE} | sed 's/[[:space:]]*//g;s/,//g;s/\"//g' | cut -d: -f3`
${SOCKS5BIN} 127.0.0.1:${socks5_port} > ${strLog} 2>&1 &
if ! fun_check_socks5_run; then
echo "start failed"
return 1
fi
echo " done"
fi
echo -n "Starting ${ProgramName}..."
${BIN} -c ${CONFIGFILE} > ${strKcpLog} 2>&1 &
sleep 1
if ! fun_check_run; then
echo "start failed"
return 1
fi
echo " done"
${BIN} -v
if [[ "${runEnv}" =~ ^[Ss][Oo][Cc][Kk][Ss]5$ ]]; then
echo "${Socks5Name} (pid $SOCKS5PID) is running."
if [ "${log_flag}" == 'log' ]; then
echo "read ${SOCKS5LOGFILE} for log"
fi
fi
echo "${ProgramName} (pid $PID)is running."
if [[ "${log_flag}" =~ ^[Ll][Oo][Gg]$ ]]; then
echo "read ${LOGFILE} for log"
fi
return 0
}
fun_stop(){
if [[ "${arg1}" =~ ^[Ss][Tt][Oo][Pp]$ ]] || [[ "${arg1}" =~ ^[Rr][Ee][Ss][Tt][Aa][Rr][Tt]$ ]]; then
fun_clangcn
fi
if fun_check_run; then
echo -n "Stoping ${ProgramName} (pid $PID)... "
kill $PID
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
rm -f $PID_FILE
fi
else
echo "${ProgramName} is not running."
fi
if [[ "${runEnv}" =~ ^[Ss][Oo][Cc][Kk][Ss]5$ ]]; then
if fun_check_socks5_run; then
echo -n "Stoping ${Socks5Name} (pid $SOCKS5PID)... "
kill $SOCKS5PID
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(){
local restart_log_flag="$1"
fun_stop
if [[ "${restart_log_flag}" =~ ^[Ll][Oo][Gg]$ ]]; then
fun_start log
else
fun_start
fi
}
fun_status(){
PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
if [ ! -z $PID ]; then
echo "${ProgramName} (pid $PID) is running..."
else
echo "${ProgramName} is stopped"
fi
SOCKS5PID=`ps -ef | grep -v grep | grep -i "${SOCKS5BIN}" | awk '{print $2}'`
if [ ! -z $SOCKS5PID ]; then
echo "${Socks5Name} (pid $SOCKS5PID) is running..."
else
echo "${Socks5Name} is stopped"
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