ngrokd.init
12.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#! /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 <admin@clangcn.com>
# 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.3"
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 <admin@clangcn.com>"
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_check_port(){
fun_load_config
strHttpPort=""
strHttpsPort=""
strRemotePort=""
strManPort=""
strHttpPort=`netstat -ntl | grep "\b:${http_port}\b"`
strHttpsPort=`netstat -ntl | grep "\b:${https_port}\b"`
strRemotePort=`netstat -ntl | grep "\b:${remote_port}\b"`
strManagePort=`netstat -ntl | grep "\b:${manage_port}\b"`
if [ -n "${strHttpPort}" ] || [ -n "${strHttpsPort}" ] || [ -n "${strRemotePort}" ] || [ -n "${strManagePort}" ]; then
[ -n "${strHttpPort}" ] && str_http_port="\"${http_port}\""
[ -n "${strHttpsPort}" ] && str_https_port="\"${https_port}\""
[ -n "${strRemotePort}" ] && str_remote_port="\"${remote_port}\""
[ -n "${strManagePort}" ] && str_manage_port="\"${manage_port}\""
echo "Error: Port ${str_http_port} ${str_https_port} ${str_remote_port} ${str_manage_port} is used,view relevant port:"
[ -n "${strHttpPort}" ] && netstat -ntlp | grep "\b:${http_port}\b"
[ -n "${strHttpsPort}" ] && netstat -ntlp | grep "\b:${https_port}\b"
[ -n "${strRemotePort}" ] && netstat -ntlp | grep "\b:${remote_port}\b"
[ -n "${strManagePort}" ] && netstat -ntlp | grep "\b:${manage_port}\b"
echo "start failed"
return 1
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
echo -n "Starting ${ProgramName}..."
fun_check_port
fun_load_config
${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})
if [ "$SingleUser" = "y" ] || [ "$SingleUser" = "1" ]; then
[ -n "${ddns[0]}" ] && subdns=\"${ddns[0]}\"
[ -n "${ddns[1]}" ] && subdns=\"${ddns[0]}\",\"${ddns[1]}\"
[ -n "${ddns[2]}" ] && subdns=\"${ddns[0]}\",\"${ddns[1]}\",\"${ddns[2]}\"
[ -n "${ddns[3]}" ] && subdns=\"${ddns[0]}\",\"${ddns[1]}\",\"${ddns[2]}\",\"${ddns[3]}\"
[ -n "${ddns[4]}" ] && subdns=\"${ddns[0]}\",\"${ddns[1]}\",\"${ddns[2]}\",\"${ddns[3]}\",\"${ddns[4]}\"
[ -n "${ddns[0]}" ] && FQDN=\"${ddns[0]}.${dns}\"
[ -n "${ddns[1]}" ] && FQDN=\"${ddns[0]}.${dns}\",\"${ddns[1]}.${dns}\"
[ -n "${ddns[2]}" ] && FQDN=\"${ddns[0]}.${dns}\",\"${ddns[1]}.${dns}\",\"${ddns[2]}.${dns}\"
[ -n "${ddns[3]}" ] && FQDN=\"${ddns[0]}.${dns}\",\"${ddns[1]}.${dns}\",\"${ddns[2]}.${dns}\",\"${ddns[3]}.${dns}\"
[ -n "${ddns[4]}" ] && FQDN=\"${ddns[0]}.${dns}\",\"${ddns[1]}.${dns}\",\"${ddns[2]}.${dns}\",\"${ddns[3]}.${dns}\",\"${ddns[4]}.${dns}\"
else
[ -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}\"
fi
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