-
Notifications
You must be signed in to change notification settings - Fork 40
/
daji.sh
500 lines (412 loc) · 14.7 KB
/
daji.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/bin/bash
# Copyright (C) gdy666
# http://192.168.31.70:9999/install_v2.sh
# wget -q -O /tmp/install.sh http://192.168.31.70:9999/install_v2.sh && sh /tmp/install.sh
#wget -q -O /tmp/install.sh https://fastly.jsdelivr.net/gh/gdy666/lucky-files@main/golucky.sh && sh /tmp/install.sh https://fastly.jsdelivr.net/gh/gdy666/lucky-files@main 1.1.3
echo='echo -e' && [ -n "$(echo -e|grep e)" ] && echo=echo
#[ -z "$1" ] && test=0 || test=$1
luckPathSuff='lucky.daji'
luckyTgzFile=''
luckyshMenu(){
echo "*************************************************"
echo "** 欢迎使用 **"
echo "** Lucky 管理脚本 **"
echo "** by 古大羊 **"
echo "** 2023.07.01 23:51 **"
echo "*************************************************"
echo -e " 1 \033[32m安装\033[0mLucky"
echo -e " 2 \033[32m卸载\033[0mLucky"
echo -----------------------------------------------
echo -e " 0 \033[0m退出脚本\033[0m"
read -p "请输入对应数字 > " num
if [ -z "$num" ];then
errornum
exit 1;
elif [ "$num" = 0 ]; then
echo "退出脚本"
exit 0;
elif [ "$num" = 1 ]; then
echo "安装Lucky..."
install
elif [ "$num" = 2 ]; then
echo "卸载Lucky..."
uninstall
else
errornum
exit 1;
fi
}
errornum(){
echo -----------------------------------------------
echo -e "\033[31m请输入正确的数字!\033[0m"
}
# dir_avail 查询路径可用空间
dir_avail(){
df -h $1 |awk '{ for(i=1;i<=NF;i++){ if(NR==1){ arr[i]=$i; }else{ arr[i]=arr[i]" "$i; } } } END{ for(i=1;i<=NF;i++){ print arr[i]; } }' |grep Ava |awk '{print $2}'
}
getcpucore(){
cputype=$(uname -ms | tr ' ' '_' | tr '[A-Z]' '[a-z]')
[ -n "$(echo $cputype | grep -E "linux.*armv.*")" ] && cpucore="armv5"
[ -n "$(echo $cputype | grep -E "linux.*armv7.*")" ] && [ -n "$(cat /proc/cpuinfo | grep vfp)" ] && [ ! -d /jffs/clash ] && cpucore="armv7"
[ -n "$(echo $cputype | grep -E "linux.*aarch64.*|linux.*armv8.*")" ] && cpucore="arm64"
[ -n "$(echo $cputype | grep -E "linux.*86.*")" ] && cpucore="i386"
[ -n "$(echo $cputype | grep -E "linux.*86_64.*")" ] && cpucore="x86_64"
if [ -n "$(echo $cputype | grep -E "linux.*mips.*")" ];then
mipstype=$(echo -n I | hexdump -o 2>/dev/null | awk '{ print substr($2,6,1); exit}') #通过判断大小端判断mips或mipsle
[ "$mipstype" = "0" ] && cpucore="mips_softfloat" || cpucore="mipsle_softfloat"
fi
}
setcpucore(){
cpucore_list="armv5 armv7 arm64 i386 x86_64 mipsle_softfloat mipsle-hardfloat mips_softfloat"
echo -----------------------------------------------
echo -e "当前可供在线下载的处理器架构为:"
echo $cpucore_list | awk -F " " '{for(i=1;i<=NF;i++) {print i" "$i }}'
echo -e "如果您的CPU架构未在以上列表中,请运行【uname -a】命令,并复制好返回信息"
echo -e "之后联系作者"
echo -----------------------------------------------
read -p "请输入对应数字 > " num
setcpucore=$(echo $cpucore_list | awk '{print $"'"$num"'"}' )
if [ -z "$setcpucore" ];then
echo -e "\033[31m请输入正确的处理器架构!\033[0m"
sleep 1
cpucore=""
else
cpucore=$setcpucore
fi
}
getTargetFileURL(){
#download_url=$cdnurl'/dist/lucky_'$version'_Linux_'$cpucore'.tar.gz'
#https://fastly.jsdelivr.net/gh/gdy666/lucky-files@main/1.1.3/lucky_1.1.3_Linux_i386.tar.gz
download_url=$cdnurl'/'$version'/lucky_'$version'_Linux_'$cpucore'.tar.gz'
echo "目标文件下载链接:"$download_url
}
getLinuxSysType(){
[ -f "/etc/storage/started_script.sh" ] && systype=Padavan && initdir='/etc/storage/started_script.sh'
[ -d "/jffs" ] && systype="asusrouter" && {
[ -f "/jffs/.asusrouter" ] && initdir='/jffs/.asusrouter'
[ -d "/jffs/scripts" ] && initdir='/jffs/scripts/init-start'
[ -z "$initdir" ] && initdir='/jffs/scripts/init-start' && mkdir -p '/jffs/scripts'
}
[ -f "/data/etc/crontabs/root" -a "$(dir_avail /etc)" = 0 ] && systype=mi_snapshot
}
installStartDaemon(){
echo "设为保守模式启动"
type nohup >/dev/null 2>&1 && nohup=nohup
$nohup $luckydir/lucky -c "$luckydir/lucky.conf" >/dev/null 2>&1 &
cronset '#lucky保守模式守护进程' "*/1 * * * * test -z \"\$(pidof lucky)\" && $luckydir/lucky -c $luckydir/lucky.conf & #lucky保守模式守护进程"
}
installSetInit(){
#判断系统类型写入不同的启动文件
if [ -f /etc/rc.common ];then
#设为init.d方式启动
echo "设为init.d方式启动"
cp -f $luckydir/scripts/luckyservice /etc/init.d/$luckPathSuff
chmod 755 /etc/init.d/$luckPathSuff
/etc/init.d/$luckPathSuff enable
/etc/init.d/$luckPathSuff start
else
[ -w /etc/systemd/system ] && sysdir=/etc/systemd/system
[ -w /usr/lib/systemd/system ] && sysdir=/usr/lib/systemd/system
if [ -n "$sysdir" ];then
#设为systemd方式启动
echo "设为systemd方式启动"
echo "sysdir:"$sysdir
mv $luckydir/scripts/lucky.service $sysdir/$luckPathSuff.service
sed -i "s%/etc/lucky%$luckydir%g" $sysdir/$luckPathSuff.service
chmod 000 $sysdir/$luckPathSuff.service
systemctl daemon-reload
if [ ! $? = 0 ];then
echo "systemctl daemon-reload 出错, 转为保守模式..."
installStartDaemon
else
systemctl enable $luckPathSuff.service
systemctl start $luckPathSuff.service
fi
else
#设为保守模式启动
installStartDaemon
fi
fi
#华硕/Padavan额外设置,开机启动
[ -n "$initdir" ] && {
echo "华硕/Padavan开机启动额外设置"$initdir
sed -i '/Lucky启动/'d $initdir >/dev/null 2>&1
if [ ! -e $initdir ];then
touch $initdir
echo "#!/bin/sh" >> $initdir
fi
sed -i '/Lucky启动/'d $initdir 2>/dev/null
echo "$luckydir/lucky -c $luckydir/lucky.conf >/dev/null& #Lucky启动脚本" >> $initdir
chmod +x $initdir
}
#小米镜像化OpenWrt额外设置
if [ "$systype" = "mi_snapshot" ]; then
if [ "$dir" = "/data" ]; then
echo '执行小米镜像化OpenWrt额外设置'
chmod 755 $luckydir/scripts/misnap_init.sh
uci set firewall.Lucky=include
uci set firewall.Lucky.type='script'
uci set firewall.Lucky.path='/data/lucky.daji/scripts/misnap_init.sh'
uci set firewall.Lucky.enabled='1'
uci commit firewall
else
echo '执行小米10K镜像化OpenWrt额外设置'
chmod 755 $luckydir/scripts/misnap_10k_init.sh
uci set firewall.Lucky=include
uci set firewall.Lucky.type='script'
uci set firewall.Lucky.path='/data/userdisk/lucky.daji/scripts/misnap_10k_init.sh'
uci set firewall.Lucky.enabled='1'
uci commit firewall
fi
fi
}
installSetProfile(){
[ -w /opt/etc/profile ] && profile=/opt/etc/profile
[ -w /jffs/configs/profile.add ] && profile=/jffs/configs/profile.add
[ -w ~/.bashrc ] && profile=~/.bashrc
[ -w /etc/profile ] && profile=/etc/profile
if [ -n "$profile" ];then
sed -i '/alias lucky=*/'d $profile
echo "alias lucky=\"$luckydir/lucky\"" >> $profile #设置快捷命令环境变量
sed -i '/export luckydir=*/'d $profile
echo "export luckydir=\"$luckydir\"" >> $profile #设置快捷命令环境变量
else
echo 无法写入环境变量!请检查安装权限!
exit 1
fi
echo 'Profile:'$profile
}
checkRoot(){
#检查root权限
if [ "$USER" != "root" -a -z "$systype" ];then
echo 当前用户:$USER
$echo "\033[31m请尽量使用root用户(不要直接使用sudo命令!)执行安装!\033[0m"
echo -----------------------------------------------
read -r -p "仍要安装?可能会产生未知错误!(1/0) > " res
[ "$res" != "1" ] && echo "Lukcy 安装脚本中止运行" && exit 1
fi
}
croncmd(){
if [ -n "$(crontab -h 2>&1 | grep '\-l')" ];then
crontab $1
else
crondir="$(crond -h 2>&1 | grep -oE 'Default:.*' | awk -F ":" '{print $2}')"
[ ! -w "$crondir" ] && crondir="/etc/storage/cron/crontabs"
[ ! -w "$crondir" ] && crondir="/var/spool/cron/crontabs"
[ ! -w "$crondir" ] && crondir="/var/spool/cron"
[ ! -w "$crondir" ] && echo "你的设备不支持定时任务配置"
[ "$1" = "-l" ] && cat $crondir/$USER 2>/dev/null
[ -f "$1" ] && cat $1 > $crondir/$USER
fi
}
cronset(){
# 参数1代表要移除的关键字,参数2代表要添加的任务语句
tmpcron=/tmp/cron_$USER
croncmd -l > $tmpcron
sed -i "/$1/d" $tmpcron
sed -i '/^$/d' $tmpcron
echo "$2" >> $tmpcron
croncmd $tmpcron
rm -f $tmpcron
}
install(){
#getTargetFileURL
checkdir #检测路由环境
selectLuckyFile #选择Lucky文件
setdir #设置安装路径
installLuckyFileFromTgz #解压安装lucky
#getFilesFromNetwork #下载解压设置权限
installSetProfile #设置环境变量
installSetInit #设置开机启动
i=0
while [ "$i" -lt 10 ];do
sleep 1
PID=$(pidof lucky) && [ -n "$PID" ] && echo "lucky已成功运行" && exit 0 && i=10 || i=$((i+1))
done
echo "检测超时,请自行检查lucky运行情况"
read -p "是否使用保守模式(1/0)?" res
if [ "$res" = "1" ] ;then
installStartDaemon
fi
i=0
while [ "$i" -lt 10 ];do
sleep 1
PID=$(pidof lucky) && [ -n "$PID" ] && echo "lucky已成功运行" && exit 0 && i=10 || i=$((i+1))
done
}
uninstall(){
uninstallgetdir #获取lucky安装路径
systemctl disable $luckPathSuff.service >/dev/null 2>&1 &
systemctl stop $luckPathSuff.service >/dev/null 2>&1 &
#结束进程
PID=$(pidof lucky) && [ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1
if [ -z $luckydir ];then
echo "找不到lucky文件夹,卸载中止"
exit 0
fi
echo "删除 "$luckydir" 所有文件"
read -p "确认删除?"$luckydir"所有文件(1/0) > " res
if [ -z $res ];then
echo 卸载已取消
exit 1;
elif [ "$res" = "1" ];then
rm -rf $luckydir
else
echo 卸载已取消
exit 1;
fi
[ -n "$initdir" ] && { #删除开机启动
sed -i '/Lucky启动/'d $initdir 2>/dev/null
}
if [ "$systype" = "mi_snapshot" ];then ##删除开机启动
uci del firewall.Lucky
uci commit firewall
fi
/etc/init.d/$luckPathSuff stop >/dev/null 2>&1
rm -rf /etc/init.d/$luckPathSuff
sed -i '/alias lucky=*/'d $profile
sed -i '/export luckydir=*/'d $profile
cronset '#lucky保守模式守护进程' #删除保守模式定时
}
checkdir(){
if [ -n "$systype" ];then
[ "$systype" = "Padavan" ] && dir=/etc/storage
[ "$systype" = "asusrouter" ] && dir=/jffs
[ "$systype" = "mi_snapshot" ] && dir=/data
echo "当前处于路由器系统环境:"+$systype
fi
}
installLuckyFileFromTgz(){
echo "准备从$luckyTgzFile 解压安装"
mkdir -p $luckydir > /dev/null
tar -zxvf $luckyTgzFile -C $luckydir/
[ $? -ne 0 ] && echo "文件解压失败!" && rm -rf $luckyTgzFile && exit 1
echo 已解压到 $luckydir
chmod +x $luckydir/lucky
chmod +x $luckydir/scripts/*
rm -rf /tmp/lucky.tar.gz
}
selectLuckyFile(){
echo '选择Lucky压缩包文件(如果没有显示,说明你下载的tar.gz文件不适用当前CPU架构)'
echo '当前CPU结构:'$cpucore
ls *.tar.gz | grep "$cpucore" | awk '{print NR " "$0}'
echo '0 退出安装'
read -p "请输入相应数字 > " num
if [ -z $num ];then
echo 安装已取消
exit 1;
elif [ "$num" = "0" ];then
echo 安装已取消
exit 1;
else
echo "用户选择了$num"
luckyTgzFile=$(ls *.tar.gz | grep "$cpucore" | awk '{if(NR=='$num') print $0}')
fi
if [ ! -f $luckyTgzFile ];then
echo "找不到文件 $luckyTgzFile !"
exit 1;
fi
echo "选择的文件为: $luckyTgzFile"
}
setdir(){
echo -----------------------------------------------
$echo "\033[33m安装lucky至少需要预留约6MB的磁盘空间\033[0m"
$echo " 1 在\033[32m/etc\033[0m 目录下安装(适合root用户)"
$echo " 2 在\033[32m/usr/share\033[0m目录下安装(适合Linux设备)"
$echo " 3 在\033[32m当前用户目录\033[0m下安装(适合非root用户)"
$echo " 4 在\033[32m/data\033[0m目录(小米官方路由系统)"
$echo " 5 在\033[32m/data/userdisk \033[0m目录(小米官方路由系统.万兆)"
$echo " 6 在\033[32m/jffs\033[0m目录(华硕/梅林)"
$echo " 7 在\033[32m/etc/storage目录\033[0m(Padavan)"
$echo " 8 手动设置安装目录"
if [ -n "$dir" ];then
$echo " 5 在\033[32m""$dir""\033[0m(当前路由器环境推荐目录下安装)"
fi
$echo " 0 退出安装"
echo -----------------------------------------------
read -p "请输入相应数字 > " num
#设置目录
if [ -z $num ];then
echo 安装已取消
exit 1;
elif [ "$num" = "1" ];then
dir=/etc
elif [ "$num" = "2" ];then
dir=/usr/share
elif [ "$num" = "3" ];then
dir=~/.local/share
elif [ "$num" = "4" ];then
dir=/data
elif [ "$num" = "5" ];then
dir=/data/userdisk
elif [ "$num" = "6" ];then
dir=/jffs
elif [ "$num" = "7" ];then
dir=/jffs
elif [ "$num" = "8" ];then
echo -----------------------------------------------
echo '可用路径 剩余空间:'
df -h | awk '{print $6,$4}'| sed 1d
echo '路径是必须带 / 的格式,注意写入虚拟内存(/tmp,/opt,/sys...)的文件会在重启后消失!!!'
read -p "请输入自定义路径 > " dir
if [ -z "$dir" ];then
$echo "\033[31m路径错误!请重新设置!\033[0m"
setdir
fi
elif [ "$num" = "5" ];then
echo ""
else
echo 安装已取消!!!
exit 1;
fi
if [ ! -w $dir ];then
$echo "\033[31m没有$dir目录写入权限!请重新设置!\033[0m" && sleep 1 && setdir
else
$echo "目标目录\033[32m$dir\033[0m空间剩余:$(dir_avail $dir)"
read -p "确认安装?(1/0) > " res
if [ -z $res ];then
echo 安装已取消
exit 1;
elif [ "$res" = "1" ];then
luckydir=$dir/$luckPathSuff
echo "luckdir:"$luckydir
else
echo 安装已取消
exit 1;
fi
fi
}
uninstallgetdir(){
[ -w /opt/etc/profile ] && profile=/opt/etc/profile
[ -w /jffs/configs/profile.add ] && profile=/jffs/configs/profile.add
[ -w ~/.bashrc ] && profile=~/.bashrc
[ -w /etc/profile ] && profile=/etc/profile
luckydir=$(cat $profile | grep luckydir | awk -F "\"" '{print $2}')
if [ -n "$luckydir" ];then
return
fi
if [ -n "$systype" ];then
[ "$systype" = "Padavan" ] && dir=/etc/storage
[ "$systype" = "asusrouter" ] && dir=/jffs
[ "$systype" = "mi_snapshot" ] && dir=/data
luckydir=$dir/$luckPathSuff
fi
}
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
#获取CPU结构
getcpucore
if [ -z "$cpucore" ];then
echo '未能正确识别当前系统CPU架构,请与作者联系'
exit 1
fi
echo "当前CPU架构:"$cpucore
#判断linux环境, $systype ,$initdir
getLinuxSysType
[ -n "$initdir" ] && echo 'initdir:'$initdir
[ -n "$systype" ] && echo '当前处于路由器运行环境[' $systype']!'
luckyshMenu #显示菜单
#wget -q -O /tmp/install.sh http://192.168.31.70:9999/install_v2.sh && sh /tmp/install.sh http://192.168.31.70:9999 1.1.3
# apt-get install --reinstall systemd
#根据cdn,版本,cpu架构 拼接生成文件下载链接 $download_url
#getTargetFileURL