-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
405 lines (344 loc) · 9.91 KB
/
install.sh
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/sh
exec 2>/tmp/install.log || true
set -x
# Online URL
URL_PREFIX="https://"
UNINSTALL_DOWNLOAD_URL="router.uu.163.com/api/script/uninstall?type="
MONITOR_DOWNLOAD_URL="router.uu.163.com/api/script/monitor?type="
ROUTER="${1:-asuswrt-merlin}"
MODEL="${2}"
BASEDIR=$(dirname "$0")
UNINSTALL_FILE="${BASEDIR}/uninstall.sh"
INSTALL_DIR=""
MONITOR_FILE=""
MONITOR_CONFIG=""
ASUSWRT_MERLIN="asuswrt-merlin"
XIAOMI="xiaomi"
HIWIFI="hiwifi"
OPENWRT="openwrt"
init_param() {
local router="${ROUTER}"
local monitor_filename="uuplugin_monitor.sh"
case "${router}" in
${ASUSWRT_MERLIN})
INSTALL_DIR="/jffs/uu"
MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${ASUSWRT_MERLIN}"
MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${ASUSWRT_MERLIN}"
return 0
;;
${XIAOMI})
URL_PREFIX="http://"
INSTALL_DIR="/data/uu"
MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${XIAOMI}"
MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${XIAOMI}"
return 0
;;
${HIWIFI})
INSTALL_DIR="/plugins/uu"
MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${HIWIFI}"
MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${HIWIFI}"
return 0
;;
${OPENWRT})
URL_PREFIX="http://"
INSTALL_DIR="/usr/sbin/uu"
MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${OPENWRT}"
MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${OPENWRT}"
return 0
;;
*)
return 1
;;
esac
}
# Return: 0 means success.
config_asuswrt() {
# Config jffs file system
nvram set jffs2_enable=1
nvram set jffs2_scripts=1
nvram commit &
return 0
}
# Return: 0 means success.
check_dir() {
if [ ! -d "${INSTALL_DIR}" ];then
mkdir -p "${INSTALL_DIR}"
[ "$?" != "0" ] && return 1
fi
return 0
}
clean_up() {
[ ! -f "${UNINSTALL_FILE}" ] && return 1
chmod u+x "${UNINSTALL_FILE}"
/bin/sh "${UNINSTALL_FILE}" "${ROUTER}" "${MODEL}" 1>/dev/null 2>&1
[ "$?" != "0" ] && return 1
return 0
}
# Return: 0 means success.
download() {
local url="$1"
local file="$2"
local plugin_info=$(curl -L -s -k -H "Accept:text/plain" "${url}" || \
wget -q --no-check-certificate -O - "${url}&output=text" || \
wget -q -O - "${url}&output=text" || \
curl -s -k -H "Accept:text/plain" "${url}"
)
[ "$?" != "0" ] && return 1
[ -z "$plugin_info" ] && return 1
local plugin_url=$(echo "$plugin_info" | cut -d ',' -f 1)
local plugin_md5=$(echo "$plugin_info" | cut -d ',' -f 2)
[ -z "${plugin_url}" ] && return 1
[ -z "${plugin_md5}" ] && return 1
curl -L -s -k "$plugin_url" -o "${file}" >/dev/null 2>&1 || \
wget -q --no-check-certificate "$plugin_url" -O "${file}" >/dev/null 2>&1 || \
wget -q "$plugin_url" -O "${file}" >/dev/null 2>&1 || \
curl -s -k "$plugin_url" -o "${file}" >/dev/null 2>&1
if [ "$?" != "0" ];then
[ -f "${file}" ] && rm "${file}"
return 1
fi
if [ -f "${file}" ];then
local download_md5=$(md5sum "${file}")
local download_md5=$(echo "$download_md5" | sed 's/[ ][ ]*/ /g' | cut -d' ' -f1)
if [ "$download_md5" != "$plugin_md5" ];then
rm "${file}"
return 1
fi
return 0
else
return 1
fi
}
# Return: 0 means success.
start_monitor() {
[ ! -f "${MONITOR_FILE}" ] && return 1
{
echo "router=${ROUTER}";
echo "model=${MODEL}"
} > ${MONITOR_CONFIG}
[ "$?" != "0" ] && return 1
chmod u+x "${MONITOR_FILE}"
/bin/sh "${MONITOR_FILE}" 1>/dev/null 2>&1 &
return 0
}
# Return: 0 means running.
check_running() {
local PID_FILE="/var/run/uuplugin.pid"
local PLUGIN_EXE="uuplugin"
local TIMES="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"
TIMES=${TIMES}" 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45"
TIMES=${TIMES}" 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65"
TIMES=${TIMES}" 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85"
TIMES=${TIMES}" 86 87 88 89 90"
for i in ${TIMES};do
if [ -f "$PID_FILE" ];then
local pid=$(cat $PID_FILE)
local running_pid=$(ps | sed 's/^[ \t]*//g;s/[ \t]*$//g' | \
sed 's/[ ][ ]*/#/g' | grep "${PLUGIN_EXE}" | \
grep -v "grep" | cut -d'#' -f1 | grep -e "^${pid}$")
if [ "$pid" = "${running_pid}" ];then
return 0
else
sleep 1
fi
else
sleep 1
fi
done
return 1
}
# Return: 0 means it is merlin.
check_merlin() {
# Check to see if it is merlin
local br0=$(ip -4 a s br0 | grep inet | grep -v 'grep' | \
sed 's/^[ \t]*//g;s/[ \t]*$//g' | sed 's/[ ][ ]*/#/g' | cut -d'#' -f2 | cut -d/ -f1)
[ -z "${br0}" ] && return 1
local code=$(curl -s -o /dev/null -w "%{http_code}" "http://${br0}/images/merlin-logo.png")
[ "$code" = "200" ] && return 0
code=$(wget -Sq -O - "http://${br0}/images/merlin-logo.png" 2>&1 | grep 'HTTP' | \
sed 's/^[ \t]*//g;s/[ \t]*$//g' | sed 's/[ ][ ]*/#/g' | cut -d'#' -f2)
[ "$code" = "200" ] && return 0
return 1
}
config_asuswrt_bootup() {
check_merlin
if [ "$?" = "0" ];then
config_services_start
return $?
else
config_exec_start
return $?
fi
}
# Return: 0 means success.
config_exec_start() {
local bootup_script="${INSTALL_DIR}/uuplugin_bootup.sh"
{
echo "#!/bin/sh"
echo "nohup /bin/sh ${MONITOR_FILE} &"
} > ${bootup_script}
chmod u+x ${bootup_script}
nvram set jffs2_exec="${bootup_script}"
nvram commit &
return 0
}
# Return: 0 means success.
# Config ${MONITOR_FILE} starts on boot.
config_services_start() {
local SERVICES_START_FILE="/jffs/scripts/services-start"
if [ ! -e "${SERVICES_START_FILE}" ];then
mkdir -p /jffs/scripts
[ "$?" != "0" ] && return 1
touch "${SERVICES_START_FILE}"
[ "$?" != "0" ] && return 1
{ echo "#!/bin/sh"; echo ""; echo ""; } >> "${SERVICES_START_FILE}"
[ "$?" != "0" ] && return 1
fi
chmod u+x "${SERVICES_START_FILE}"
grep "${MONITOR_FILE}" "${SERVICES_START_FILE}" 1>/dev/null 2>&1
if [ "$?" != "0" ];then
echo "/bin/sh ${MONITOR_FILE} &" >> "${SERVICES_START_FILE}"
[ "$?" != "0" ] && return 1
fi
return 0
}
# Return: 0 means success.
config_xiaomi_bootup() {
config_bootup_implemention
return $?
}
# Return: 0 means success.
config_hiwifi_bootup() {
config_bootup_implemention
return $?
}
# Return: 0 means success.
config_openwrt_bootup() {
config_bootup_implemention
return $?
}
config_bootup_implemention() {
local init_script="${INSTALL_DIR}/S99uuplugin"
local link_script="/etc/rc.d/S99uuplugin"
{
echo "#!/bin/sh /etc/rc.common";
echo "";
echo "";
echo "START=99";
echo "start() {"
echo " /bin/sh ${MONITOR_FILE} &";
echo "}"
} > "${init_script}"
[ "$?" != "0" ] && return 1
[ ! -f "${init_script}" ] && return 1
chmod u+x ${init_script}
ln -sf ${init_script} ${link_script}
if [ "$?" != "0" ];then
[ -f "${init_script}" ] && rm ${init_script}
return 1
fi
return 0
}
# Return: 0 means success.
config_bootup() {
local router="${ROUTER}"
case "${router}" in
${ASUSWRT_MERLIN})
config_asuswrt_bootup
return $?
;;
${XIAOMI})
config_xiaomi_bootup
return $?
;;
${HIWIFI})
config_hiwifi_bootup
return $?
;;
${OPENWRT})
config_openwrt_bootup
return $?
;;
*)
return 1
;;
esac
}
# Return: 0 means success.
config_router() {
local router="${ROUTER}"
case "${router}" in
${ASUSWRT_MERLIN})
config_asuswrt
return $?
;;
${XIAOMI} | ${HIWIFI} | ${OPENWRT})
return 0
;;
*)
return 1
;;
esac
}
print_sn() {
local interface=""
case "${ROUTER}" in
${ASUSWRT_MERLIN})
interface="br0"
;;
${XIAOMI} | ${HIWIFI} | ${OPENWRT})
interface="br-lan"
;;
*)
return 1
;;
esac
local info=$(ip addr show ${interface})
local mac=$(echo "${info}" | grep "link/ether" | awk '{print $2}')
echo "sn=${mac}"
return 0
}
install() {
init_param
[ "$?" != "0" ] && return 9
config_router
[ "$?" != "0" ] && return 1
check_dir
[ "$?" != "0" ] && return 2
download "${UNINSTALL_DOWNLOAD_URL}" "${UNINSTALL_FILE}"
[ "$?" != "0" ] && return 3
clean_up
[ "$?" != "0" ] && return 4
download "${MONITOR_DOWNLOAD_URL}" "${MONITOR_FILE}"
if [ "$?" != "0" ];then
[ -f "${MONITOR_FILE}" ] && rm "${MONITOR_FILE}"
return 5
fi
start_monitor
[ "$?" != "0" ] && return 6
check_running
[ "$?" != "0" ] && return 7
config_bootup
[ "$?" != "0" ] && return 8
print_sn
[ "$?" != "0" ] && return 10
return 0
}
# Start to install.
install
status_code=$?
if [ ${status_code} -gt 4 ];then
if [ -f "${UNINSTALL_FILE}" ];then
clean_up
fi
fi
[ -f "${UNINSTALL_FILE}" ] && rm "${UNINSTALL_FILE}"
return $status_code