Commit 2762816b9abc2bb1ca250e08d925d22e0508f18a
1 parent
aeef4883
Exists in
master
Create ngrokd.init
Showing
1 changed file
with
314 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,314 @@ |
1 | +#! /bin/bash | |
2 | +# chkconfig: 2345 55 25 | |
3 | +# Description: Startup script for Ngrok on Debian. Place in /etc/init.d and | |
4 | +# run 'update-rc.d -f ngrokd defaults', or use the appropriate command on your | |
5 | +# distro. For CentOS/Redhat run: 'chkconfig --add ngrokd' | |
6 | +#======================================================= | |
7 | +# System Required: CentOS/Debian/Ubuntu (32bit/64bit) | |
8 | +# Description: Manager for Ngrok, Written by Clang | |
9 | +# Author: Clang <admin@clangcn.com> | |
10 | +# Intro: http://clangcn.com | |
11 | +#======================================================= | |
12 | +### BEGIN INIT INFO | |
13 | +# Provides: ngrok | |
14 | +# Required-Start: $all | |
15 | +# Required-Stop: $all | |
16 | +# Default-Start: 2 3 4 5 | |
17 | +# Default-Stop: 0 1 6 | |
18 | +# Short-Description: starts the ngrok | |
19 | +# Description: starts ngrok using start-stop | |
20 | +### END INIT INFO | |
21 | + | |
22 | +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
23 | +ProgramName="Ngrok" | |
24 | +ProGramInstallPath="/usr/local/ngrok" | |
25 | +ProgramPath="/usr/local/ngrok/bin" | |
26 | +NAME=ngrokd | |
27 | +BIN=${ProgramPath}/${NAME} | |
28 | +CONFIGFILE=${ProGramInstallPath}/.ngrok_config.sh | |
29 | +LOGFILE=${ProGramInstallPath}/ngrok.log | |
30 | +SCRIPTNAME=/etc/init.d/${NAME} | |
31 | +PID_DIR=/var/run | |
32 | +PID_FILE=$PID_DIR/ngrok_clang.pid | |
33 | +version="v5.0" | |
34 | +manage_port="4446" | |
35 | +RET_VAL=0 | |
36 | + | |
37 | +[ -x $BIN ] || exit 0 | |
38 | + | |
39 | +fun_clangcn() | |
40 | +{ | |
41 | + echo "" | |
42 | + echo "#############################################################" | |
43 | + echo "# Manager Ngrok ${version} for CentOS/Debian/Ubuntu (32bit/64bit)" | |
44 | + echo "# Intro: http://clang.cn" | |
45 | + echo "#" | |
46 | + echo "# Author: Clang <admin@clangcn.com>" | |
47 | + echo "#" | |
48 | + echo "#############################################################" | |
49 | + echo "" | |
50 | +} | |
51 | + | |
52 | +fun_check_run() | |
53 | +{ | |
54 | + if netstat -tnpl | grep -q ${NAME};then | |
55 | + return 0 | |
56 | + else | |
57 | + rm -f $PID_FILE | |
58 | + return 1 | |
59 | + fi | |
60 | +} | |
61 | +fun_load_config(){ | |
62 | + if [ ! -r ${CONFIGFILE} ]; then | |
63 | + echo "config file ${CONFIGFILE} not found" | |
64 | + return 1 | |
65 | + else | |
66 | + . ${CONFIGFILE} | |
67 | + log_level="" | |
68 | + [ -n "${loglevel}" ] && log_level=" -log-level=\"${loglevel}\"" | |
69 | + cd ${ProGramInstallPath} | |
70 | + fi | |
71 | +} | |
72 | +fun_randstr(){ | |
73 | + index=0 | |
74 | + strRandomPass="" | |
75 | + for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
76 | + for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
77 | + for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done | |
78 | + for i in {1..16}; do strRandomPass="$strRandomPass${arr[$RANDOM%$index]}"; done | |
79 | + echo $strRandomPass | |
80 | +} | |
81 | +fun_start() | |
82 | +{ | |
83 | + if [ "${arg1}" = "start" ]; then | |
84 | + fun_clangcn | |
85 | + fi | |
86 | + if [ ! -d $PID_DIR ]; then | |
87 | + mkdir -p $PID_DIR || echo "failed creating PID directory ${PID_DIR}"; exit 1 | |
88 | + fi | |
89 | + if fun_check_run; then | |
90 | + echo "${ProgramName} (pid `pidof $NAME`) already running." | |
91 | + return 0 | |
92 | + fi | |
93 | + fun_load_config | |
94 | + echo -n "Starting ${ProgramName}..." | |
95 | + ${BIN} -domain="$dns" -httpAddr=":$http_port" -httpsAddr=":$https_port" -pass="$pass" -tlsCrt="$srtCRT" -tlsKey="$strKey" -tunnelAddr=":$remote_port"${log_level} > ${LOGFILE} 2>&1 & | |
96 | + PID=`pidof ${NAME}` | |
97 | + echo $PID > $PID_FILE | |
98 | + sleep 0.3 | |
99 | + if ! fun_check_run; then | |
100 | + echo "start failed" | |
101 | + return 1 | |
102 | + fi | |
103 | + echo " done" | |
104 | + echo "${ProgramName} (pid `pidof $NAME`)is running." | |
105 | + echo "read ${LOGFILE} for log" | |
106 | + return 0 | |
107 | +} | |
108 | + | |
109 | +fun_stop(){ | |
110 | + if [ "${arg1}" = "stop" ] || [ "${arg1}" = "restart" ]; then | |
111 | + fun_clangcn | |
112 | + fi | |
113 | + if fun_check_run; then | |
114 | + echo -n "Stoping ${ProgramName} (pid `pidof $NAME`)... " | |
115 | + kill `pidof $NAME` | |
116 | + if [ "$?" != 0 ] ; then | |
117 | + echo " failed" | |
118 | + exit 1 | |
119 | + else | |
120 | + echo " done" | |
121 | + rm -f $PID_FILE | |
122 | + fi | |
123 | + else | |
124 | + echo "${ProgramName} is not running." | |
125 | + fi | |
126 | +} | |
127 | +fun_restart(){ | |
128 | + fun_stop | |
129 | + fun_start | |
130 | +} | |
131 | +fun_status(){ | |
132 | + if netstat -tnpl | grep -q ${NAME}; then | |
133 | + PID=`pidof ${NAME}` | |
134 | + echo "${ProgramName} (pid $PID) is running..." | |
135 | + else | |
136 | + echo "${ProgramName} is stopped" | |
137 | + exit 0 | |
138 | + fi | |
139 | +} | |
140 | +fun_config(){ | |
141 | + nano ${CONFIGFILE} | |
142 | +} | |
143 | +fun_set_ngrok_username(){ | |
144 | + userName="" | |
145 | + read -p "Please input UserName for Ngrok(e.g.:ZhangSan):" userName | |
146 | + check_ngrok_username | |
147 | +} | |
148 | +fun_set_ngrok_subdomain(){ | |
149 | + # Set ngrok pass | |
150 | + subdomain="" | |
151 | + ddns="" | |
152 | + dns="" | |
153 | + echo "Please input subdomain for Ngrok(e.g.:dns1 dns2 dns3 dns4 dns5):" | |
154 | + read -p "(subdomain number max five:):" subdomain | |
155 | + check_ngrok_subdomain | |
156 | +} | |
157 | +fun_set_ngrok_authId(){ | |
158 | + strPass=`fun_randstr` | |
159 | + echo "Please input the password (more than 8) of Ngrok authId:" | |
160 | + read -p "(Default password: ${strPass}):" strPassword | |
161 | + if [ "$strPassword" = "" ]; then | |
162 | + strPassword=$strPass | |
163 | + fi | |
164 | + check_ngrok_authId | |
165 | +} | |
166 | + | |
167 | +check_ngrok_username(){ | |
168 | + # check ngrok userName | |
169 | + if [ "$userName" = "" ]; then | |
170 | + echo "Your input is empty,please input again..." | |
171 | + fun_set_ngrok_username | |
172 | + else | |
173 | + echo "Your username: ${userName}" | |
174 | + fun_set_ngrok_subdomain | |
175 | + fi | |
176 | +} | |
177 | +check_ngrok_subdomain(){ | |
178 | + # check ngrok subdomain | |
179 | + if [ "$subdomain" = "" ]; then | |
180 | + echo "Your input is empty, please input again." | |
181 | + fun_set_ngrok_subdomain | |
182 | + else | |
183 | + fun_load_config | |
184 | + ddns=(${subdomain}) | |
185 | + [ -n "${ddns[0]}" ] && subdns=\"${ddns[0]}.${userName}\" | |
186 | + [ -n "${ddns[1]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\" | |
187 | + [ -n "${ddns[2]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\" | |
188 | + [ -n "${ddns[3]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\",\"${ddns[3]}.${userName}\" | |
189 | + [ -n "${ddns[4]}" ] && subdns=\"${ddns[0]}.${userName}\",\"${ddns[1]}.${userName}\",\"${ddns[2]}.${userName}\",\"${ddns[3]}.${userName}\",\"${ddns[4]}.${userName}\" | |
190 | + [ -n "${ddns[0]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\" | |
191 | + [ -n "${ddns[1]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\" | |
192 | + [ -n "${ddns[2]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\" | |
193 | + [ -n "${ddns[3]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\",\"${ddns[3]}.${userName}.${dns}\" | |
194 | + [ -n "${ddns[4]}" ] && FQDN=\"${ddns[0]}.${userName}.${dns}\",\"${ddns[1]}.${userName}.${dns}\",\"${ddns[2]}.${userName}.${dns}\",\"${ddns[3]}.${userName}.${dns}\",\"${ddns[4]}.${userName}.${dns}\" | |
195 | + echo "Your subdomain: ${subdns}" | |
196 | + fun_set_ngrok_authId | |
197 | + fi | |
198 | +} | |
199 | +check_ngrok_authId(){ | |
200 | + # check ngrok authId | |
201 | + if [ "${strPassword}" = "" ]; then | |
202 | + echo "Your input is empty, please input again..." | |
203 | + fun_set_ngrok_authId | |
204 | + else | |
205 | + echo "Your authId: ${strPassword}" | |
206 | + fun_adduser_command | |
207 | + fi | |
208 | +} | |
209 | +fun_adduser_command(){ | |
210 | + fun_load_config | |
211 | + 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 | |
212 | + chmod +x ${ProGramInstallPath}/.ngrok_adduser.sh | |
213 | + . ${ProGramInstallPath}/.ngrok_adduser.sh | |
214 | + rm -f ${ProGramInstallPath}/.ngrok_adduser.sh | |
215 | + clear | |
216 | + fun_clangcn | |
217 | + echo "User list :" | |
218 | + curl -H "Content-Type: application/json" -H "Auth:${pass}" -X GET http://localhost:${manage_port}/info | |
219 | + echo "=============================================================" | |
220 | + echo "Server: ${dns}" | |
221 | + echo "Server ${remote_port}" | |
222 | + echo "userId: ${userName}" | |
223 | + echo "authId: ${strPassword}" | |
224 | + echo "Subdomain: ${subdns}" | |
225 | + echo "Your FQDN: ${FQDN}" | |
226 | + echo "=============================================================" | |
227 | +} | |
228 | +fun_adduser(){ | |
229 | + if fun_check_run; then | |
230 | + fun_load_config | |
231 | + fun_set_ngrok_username | |
232 | + else | |
233 | + echo "${ProgramName} is not running." | |
234 | + fi | |
235 | +} | |
236 | +fun_deluser(){ | |
237 | + if [ -z "${1}" ]; then | |
238 | + strWantdeluser="" | |
239 | + fun_userlist | |
240 | + echo "" | |
241 | + read -p "Please input del username you want:" strWantdeluser | |
242 | + if [ "${strWantdeluser}" = "" ]; then | |
243 | + echo "Error: You must input username!!" | |
244 | + exit 1 | |
245 | + else | |
246 | + deluser_Confirm_clang "${strWantdeluser}" | |
247 | + fi | |
248 | + else | |
249 | + deluser_Confirm_clang "${1}" | |
250 | + fi | |
251 | +} | |
252 | +deluser_Confirm_clang(){ | |
253 | + if [ -z "${1}" ]; then | |
254 | + echo "Error: You must input username!!" | |
255 | + exit 1 | |
256 | + else | |
257 | + strDelUser="${1}" | |
258 | + echo "You want del ${strDelUser}!" | |
259 | + read -p "(if you want please input: y,Default [no]):" strConfirmDel | |
260 | + case "$strConfirmDel" in | |
261 | + y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) | |
262 | + echo "" | |
263 | + strConfirmDel="y" | |
264 | + ;; | |
265 | + n|N|No|NO|no|nO) | |
266 | + echo "" | |
267 | + strConfirmDel="n" | |
268 | + ;; | |
269 | + *) | |
270 | + echo "" | |
271 | + strConfirmDel="n" | |
272 | + esac | |
273 | + if [ $strConfirmDel = "y" ]; then | |
274 | + if [ -s "/tmp/db-diskv/ng/ro/ngrok:${strDelUser}" ]; then | |
275 | + rm -f /tmp/db-diskv/ng/ro/ngrok:${strDelUser} | |
276 | + echo "Delete user ${strDelUser} ok! Restart ${NAME}..." | |
277 | + fun_restart | |
278 | + else | |
279 | + echo "" | |
280 | + echo "Error: user ${strDelUser} not found!" | |
281 | + fi | |
282 | + else | |
283 | + echo "you cancel!" | |
284 | + fi | |
285 | + fi | |
286 | +} | |
287 | +fun_userlist(){ | |
288 | + fun_clangcn | |
289 | + echo "Ngrok user list:" | |
290 | + ls /tmp/db-diskv/ng/ro/ |cut -d ':' -f 2 | |
291 | +} | |
292 | +fun_info(){ | |
293 | + fun_clangcn | |
294 | + if fun_check_run; then | |
295 | + fun_load_config | |
296 | + curl -H "Content-Type: application/json" -H "Auth:${pass}" -X GET http://localhost:${manage_port}/info | |
297 | + else | |
298 | + echo "${ProgramName} is not running." | |
299 | + fi | |
300 | +} | |
301 | +arg1=$1 | |
302 | +arg2=$2 | |
303 | +[ -z ${arg1} ] | |
304 | +case "${arg1}" in | |
305 | + start|stop|restart|status|config|adduser|deluser|userlist|info) | |
306 | + fun_${arg1} ${arg2} | |
307 | + ;; | |
308 | + *) | |
309 | + fun_clangcn | |
310 | + echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|adduser|deluser|userlist|info}" | |
311 | + RET_VAL=1 | |
312 | + ;; | |
313 | +esac | |
314 | +exit $RET_VAL | ... | ... |