forked from wbcyclist/ngrok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngrokd.init
364 lines (339 loc) · 9.65 KB
/
ngrokd.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
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
353
354
355
356
357
358
359
360
361
362
363
364
#! /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 <[email protected]>
# 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
# Source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
# Check that networking is up.
[ ${NETWORKING} ="yes" ] || exit 0
fi
ProgramName="Ngrok"
ProGramInstallPath="/usr/local/ngrok"
ProgramPath="/usr/local/ngrok/bin"
NAME=ngrokd
BIN=${ProgramPath}/${NAME}
CONFIGFILE=${ProGramInstallPath}/ngrokd_config.sh
SCRIPTNAME=/etc/init.d/${NAME}
PID_DIR=/var/run
PID_FILE=${PID_DIR}/ngrokd.pid
RET_VAL=0
[ -x $BIN ] || exit 0
if [ ! -d $PID_DIR ]; then
mkdir -p $PID_DIR
if [ $? -ne 0 ]; then
echo "Creating PID directory $PID_DIR failed"
exit 1
fi
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_load_config() {
if [ ! -r ${CONFIGFILE} ]; then
echo "config file ${CONFIGFILE} not found"
return 1
else
. ${CONFIGFILE}
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 fun_check_run; then
echo "${NAME} (pid $PID) already running."
return 0
fi
echo -n "Starting ${ProgramName}: "
fun_load_config
${BIN} -domain="$DOMAIN" -tunnelAddr=":$TUNNEL_PORT" -httpAddr=":$HTTP_PORT" -httpsAddr=":$HTTPS_PORT" -userManageAddr=":$USER_MANAGE_PORT" -userDBPath="$USER_DB_PATH" -tlsCrt=$TLSCRT_PATH -tlsKey=$TLSKEY_PATH -log-level="$LOG_LEVEL" -log=$NGROKD_LOG -pass="$PASSWD" >/dev/null 2>&1 & echo $! > $PID_FILE
RETVAL=$?
PID=$(ps -ef | grep -v grep | grep -i ${BIN} | awk '{print $2}')
if [ -f /etc/rc.d/init.d/functions ]; then
[ $RETVAL -eq 0 ] && success
else
if fun_check_run; then
echo " [ok]"
else
echo " [FAILED]"
RETVAL=1
fi
fi
}
fun_stop() {
if fun_check_run; then
echo -n $"Stopping ${ProgramName}: "
killall ngrokd
RETVAL=$?
[ $RETVAL -eq 0 ]
rm -f $PID_FILE
else
echo "${ProgramName} is not running."
fi
}
fun_restart() {
fun_stop
sleep 1
fun_start
}
fun_status() {
fun_check_run
case $? in
0)
echo "$NAME (pid $PID) is running..."
;;
1|2)
echo "$NAME is stopped"
RET_VAL=1
;;
esac
}
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_set_ngrok_username() {
userName=""
read -p "Please input UserName for Ngrok(e.g.:ZhangSan):" userName
check_ngrok_username
}
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
}
fun_set_ngrok_subdomain() {
# Set ngrok pass
subdomain=""
echo "Please input subdomain for Ngrok(e.g.:dns1 dns2 dns3 dns4 dns5):"
read -p "subdomain:" subdomain
check_ngrok_subdomain
}
str_combine() {
local array="$1"
local delimiter="$2"
local string=""
if [ -n "$array" ]; then
local part
while read -d "$delimiter" part; do
if [ -z "$string" ]; then
string=\"${part}\"
else
string=${string},\"${part}\"
fi
done <<< "$array"
if [ -z "$string" ]; then
string=\"${part}\"
else
string=${string},\"${part}\"
fi
fi
echo $string
}
str_combine_domain() {
local array="$1"
local delimiter="$2"
local domain="$3"
local string=""
if [ -n "$array" ]; then
local part
while read -d "$delimiter" part; do
if [ -z "$string" ]; then
string=\"${part}.${domain}\"
else
string=${string},\"${part}.${domain}\"
fi
done <<< "$array"
if [ -z "$string" ]; then
string=\"${part}.${domain}\"
else
string=${string},\"${part}.${domain}\"
fi
fi
echo $string
}
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
subdns=`str_combine "${subdomain}" " "`
FQDN=`str_combine_domain "${subdomain}" " " "${DOMAIN}"`
echo "Your subdomain: ${subdns}"
fun_set_ngrok_authId
fi
}
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_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
clear
echo curl -H \"Content-Type: application/json\" -H \"Auth:${PASSWD}\" -X POST -d \''{'\"userId\":\"${strPassword}\",\"authId\":\"${userName}\",\"dns\":[${subdns}]'}'\' http://localhost:${USER_MANAGE_PORT}/adduser > ${ProGramInstallPath}/.ngrok_adduser.sh
chmod +x ${ProGramInstallPath}/.ngrok_adduser.sh
. ${ProGramInstallPath}/.ngrok_adduser.sh
rm -f ${ProGramInstallPath}/.ngrok_adduser.sh
echo ""
echo "User list :"
curl -H "Content-Type: application/json" -H "Auth:${PASSWD}" -X GET http://localhost:${USER_MANAGE_PORT}/info
echo "============================================================="
echo "Server: ${DOMAIN}:${TUNNEL_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
fun_load_config
if [ -s "${USER_DB_PATH}/ng/ro/ngrok:${strDelUser}" ]; then
rm -f ${USER_DB_PATH}/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() {
echo "Ngrok user list:"
fun_load_config
ls ${USER_DB_PATH}/ng/ro/ |cut -d ':' -f 2
}
fun_info(){
if fun_check_run; then
fun_load_config
curl -H "Content-Type: application/json" -H "Auth:${PASSWD}" -X GET http://localhost:${USER_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}
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|adduser|deluser|userlist|info}"
RET_VAL=1
;;
esac
exit $RET_VAL