-
Notifications
You must be signed in to change notification settings - Fork 4
/
iSH-Tools.sh
821 lines (806 loc) · 31.2 KB
/
iSH-Tools.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#!/bin/bash
# 镜像测速部分代码来自:https://github.com/lework/script/blob/master/shell/test/os_repo_speed_test.sh
# Os repo mirror site speed test. lework copyright
# coremark https://github.com/eembc/coremark
# cpuid2cpuflags https://github.com/projg2/cpuid2cpuflags
# coremark参考成绩来自:https://www.bilibili.com/read/cv21181867
# Moded by lurenJBD 2023.05.14
# iSH-Tools by lurenJBD 2020-10-17
########### Variable ###########
tools_version="3.1"
inite_repo="wget ncurses openrc"
HOST="baidu.com"
NAMESERVER="223.5.5.5"
speed_test_log="/tmp/speed_test.log"
file_path="/alpine/v3.14/releases/x86/alpine-minirootfs-3.14.0-x86.tar.gz"
file_name="alpine-minirootfs-3.14.0-x86.tar.gz"
error_times=0
# 终端颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
PLAIN='\033[0m'
echo_INFO="echo -e "${CYAN}INFO${PLAIN}""
echo_WARNING="echo -e "${YELLOW}WARNING${PLAIN}""
echo_ERROR="echo -e "${RED}ERROR${PLAIN}""
########### Function ###########
init_run() {
# 获取运行环境信息
if grep -q "SUPER AWESOME" /proc/version; then
ish_ver=$(cat /proc/ish/version | awk '{print $2 " " $3}')
else
ish_ver=$(sed 's/.* iSH \([0-9]\.[0-9]\.[0-9]\) (\([0-9]\{1,\}\)) \(.*\)/\1 (\2)/' /proc/version)
fi
if ! [[ $ish_ver =~ ^[0-9]+\.[0-9]+\.[0-9]+\ \([0-9]+\)$ ]]; then
$echo_ERROR 未知的iSH版本,脚本尚未支持 && exit 1
fi
alpine_version=$(awk -F. '{if ($1 == 3) print "v3."$2}' /etc/alpine-release)
local init_check=1 && check_connection
# 第一次初始化脚本
if [ ! -e /etc/iSH-Tools/tools_inited ];then
mkdir -p /etc/iSH-Tools
if [ -e /opt/iSH-VNC ];then
if [ -e /opt/iSH-VNC/VNC_installed ]; then
sed -i 's/^installed_DE=\(.*\)/installed_DE="\1"/' /opt/iSH-VNC/VNC_installed
source /opt/iSH-VNC/VNC_installed && echo installed_apk_repo=\"$installed_DE\" > /etc/iSH-Tools/VNC_installed
source /opt/iSH-VNC/VNC_installed_name && echo installed_apk_name=$installed_DE_name >> /etc/iSH-Tools/VNC_installed
fi
rm -rf /opt/iSH-VNC
else
$echo_INFO 检测到第一次运行脚本,正在初始化
[[ $No_Network -eq 1 ]] && $echo_ERROR 无网络连接,初始化失败,脚本自动退出 && exit 1
timeout 30s apk add -q ${inite_repo}
have_been_timeout=$?
if [ "$have_been_timeout" = 143 ]; then
$echo_WARNING 超过30s未完成安装,可能是源下载太慢,进行镜像源替换 && init_run_WARNING=1
mirrors_manager 3
rm -f /etc/apk/repositories
echo "http://mirrors.aliyun.com/alpine/$alpine_version/main" >>/etc/apk/repositories
echo "http://mirrors.aliyun.com/alpine/$alpine_version/community" >>/etc/apk/repositories
$echo_INFO 再次尝试安装所需的软件包
apk update &>/dev/null
apk add -q ${inite_repo}
have_been_timeout=$?
fi
if [ "$have_been_timeout" = 0 ]; then
sed -i "s#::sysinit:/sbin/openrc sysinit#::sysinit:/sbin/openrc#g" /etc/inittab
echo inited_version=$tools_version >>/etc/iSH-Tools/tools_inited
echo inited_repo=\"$inite_repo\" >>/etc/iSH-Tools/tools_inited
fi
fi
else
source /etc/iSH-Tools/tools_inited
if [ "$tools_version" != "$inited_version" ]; then
rm -f /etc/iSH-Tools/tools_inited
echo inited_version=$tools_version >>/etc/iSH-Tools/tools_inited
echo inited_repo=\"$inite_repo\" >>/etc/iSH-Tools/tools_inited
fi
fi
} # 初始化脚本
check_connection() {
ping_host() {
ping -4 -c 1 -w 1 -A $HOST &>/dev/null
}
$echo_INFO 正在检查网络状况...
if ! ping_host; then
if [ "$init_check" = 1 ];then
$echo_WARNING 网络连接异常,尝试更改DNS重新测试
cp /etc/resolv.conf /etc/resolv.conf.bak
echo "nameserver ${NAMESERVER}" > /etc/resolv.conf
if ! ping_host; then
mv /etc/resolv.conf.bak /etc/resolv.conf
$echo_WARNING 网络连接异常,只能执行部分脚本功能
fi
init_run_WARNING=1
fi
No_Network=1
fi
} # 检查网络状况
mirrors_manager() {
local repos_c="${YELLOW}repositories.bk${PLAIN}"
# 镜像源列表
declare -A mirrors=(
[1]="官方源:http://dl-cdn.alpinelinux.org"
[2]="交大源:https://mirrors.sjtug.sjtu.edu.cn"
[3]="中科源:http://mirrors.ustc.edu.cn"
[4]="兰大源:http://mirror.lzu.edu.cn"
[5]="南大源:http://mirrors.nju.edu.cn"
[6]="北外源:https://mirrors.bfsu.edu.cn"
[7]="东软源:http://mirrors.neusoft.edu.cn"
[8]="清华源:http://mirrors.tuna.tsinghua.edu.cn"
[9]="华为源:http://repo.huaweicloud.com"
[10]="腾讯源:http://mirrors.cloud.tencent.com"
[11]="阿里源:http://mirrors.aliyun.com"
)
mirrors_speedtest() {
mirrors_speedtest_spin() {
local LC_CTYPE=C spin='-\|/' i=0
mirrors_speedtest_wget "$@" &
tput civis
while kill -0 $! 2>/dev/null; do
i=$(((i + 1) % ${#spin}))
printf "\r%s" "${spin:$i:1}"
echo -en "\033[1D"
sleep .1
done
tput cnorm
wait $!
} # 旋转动画
mirrors_speedtest_wget() {
local output=$(LANG=C wget -4O /dev/null -T30 "$1" 2>&1)
local speed=$(printf '%s' "$output" | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}')
local ipaddress=$(printf '%s' "$output" | awk -F'|' '/Connecting to .*\|([^\|]+)\|/ {print $2}' | tail -1)
local time=$(printf '%s' "$output" | awk -F= '/100% / {print $2}')
local size=$(printf '%s' "$output" | awk '/Length:/ {s=$3} END {gsub(/\(|\)/,"",s); print s}')
[ -z "$speed" ] && speed=0KB/s
[ -z "$ipaddress" ] && ipaddress=null time=null size=null
printf "${YELLOW}%-12s${GREEN}%-19s${CYAN}%-12s${PLAIN}%-11s${RED}%-10s${PLAIN}\n" "$2" "${ipaddress}" "${size}" "${time}" "${speed}"
speed=$(echo "$speed" | awk '{if ($0 ~ /MB\/s/) printf "%.0fKB/s", $1*1024; else print}')
echo "$2 ${mirrors[$key]#*:} $speed" >>$speed_test_log
} # 测速功能
clear
$echo_INFO "正在进行镜像源优选,会需要一些时间"
echo -e "\n[测试信息]"
echo -e "系统信息: ${YELLOW}Alpine ${alpine_version}${PLAIN}"
echo -e "下载文件: ${YELLOW}${file_name}${PLAIN}"
echo
rm -f $speed_test_log
printf "%-13s%-21s%-16s%-15s%-10s\n" "镜像站点" "IPv4地址" "文件大小" "下载用时" "下载速度"
for key in $(seq 1 11); do
mirrors_speedtest_spin "${mirrors[$key]#*:}${file_path}" "${mirrors[$key]%:h*}"
done
sort -k 3 -n -r -o $speed_test_log $speed_test_log
mirror_url=$(head -n 1 $speed_test_log | cut -d ' ' -f2)
mirror_name=$(head -n 1 $speed_test_log | cut -d ' ' -f1)
} # 镜像源测速
backup_sources() {
if [ ! -e /etc/apk/repositories.bk ]; then
$echo_INFO "创建 ${repos_c} 备份"
cp /etc/apk/repositories /etc/apk/repositories.bk
else
$echo_INFO "检测到 ${repos_c} 备份,是否要覆盖? [y/n]"
read -n 1 user_choice
case $user_choice in
[yY])
rm -f /etc/apk/repositories.bk
cp /etc/apk/repositories /etc/apk/repositories.bk;;
[nN]|*)
$echo_INFO "不覆盖 ${repos_c} 备份";;
esac
fi
} # 备份源文件
restore_sources(){
if [ ! -e /etc/apk/repositories.bk ]; then
$echo_INFO "没找到 ${repos_c} 备份文件,需要先备份才能恢复"
else
mv /etc/apk/repositories.bk /etc/apk/repositories
$echo_INFO 已恢复源信息
fi
} # 恢复源文件
change_sources() {
$echo_INFO "是否将 ${YELLOW}$2${PLAIN} 作为镜像源使用? [y/n]"
read -n 1 user_choice
case $user_choice in
[yY])
backup_sources
rm -rf /etc/apk/repositories /ish
echo "$1/alpine/$alpine_version/main" >>/etc/apk/repositories
echo "$1/alpine/$alpine_version/community" >>/etc/apk/repositories
$echo_INFO "正在更新源缓存"
apk update -q
$echo_INFO "源信息修改完成";;
[nN]|*)
$echo_INFO "源信息未做更改";;
esac
} # 更换源文件
select_sources() {
while :; do
sleep 0.1
echo -e "\n[镜像站点]"
for key in $(seq 1 11); do
printf "%0s.${PLAIN}%-2s: ${GREEN}%-3s${PLAIN}\n" ${key} "${mirrors[$key]%:h*}" "${mirrors[$key]#*:}"
done
read -p "请输入编号[0-11]:(输入 0 进行优选, q 返回上层)" mirror
if [ "$mirror" = "q" ]; then
return
elif [[ ! $mirror =~ ^[0-9]+$ ]]; then
clear && $echo_ERROR "请输入正确的数字!"
elif [ "$mirror" = 0 ]; then
check_connection
[[ $No_Network -eq 1 ]] && $echo_ERROR 无网络连接,无法进行优选 && return
mirrors_speedtest
break
elif [[ ! -v mirrors[$mirror] ]]; then
clear && $echo_ERROR "输入的数字不在选项中,请重新输入!"
else
mirror_name="${mirrors[$mirror]%:h*}"
mirror_url="${mirrors[$mirror]#*:}"
break
fi
done
change_sources $mirror_url $mirror_name
} # 选择镜像源
case $1 in
1) select_sources;;
2) restore_sources;;
3) backup_sources;;
esac
} # 镜像源管理
error_tips() {
case $1 in
1) $echo_ERROR "只能输入 [Y/N]";;
2) $echo_ERROR "输入内容有误?";;
3) clear && $echo_ERROR 无效的选项,请重新选择;;
esac
error_times=$((error_times + 1))
[ $error_times -ge 10 ] && $echo_ERROR "已累计出现${error_times}次错误,脚本已退出" && exit 1
} # 错误提醒与终止
shutdown() {
tput cnorm
echo
$echo_INFO "感谢使用本脚本 by 路人去甲剩丙丁"
} # 脚本退出前执行
trap shutdown EXIT
xinit_vnc() {
[ ! -e /etc/X11/xorg.conf.d ] && mkdir -p /etc/X11/xorg.conf.d
if [ ! -e /etc/X11/xorg.conf.d/10-headless.conf ]; then
cat >/etc/X11/xorg.conf.d/10-headless.conf <<-EOF
Section "Monitor"
Identifier "dummy_monitor"
HorizSync 28.0-80.0
VertRefresh 48.0-75.0
DisplaySize 250 174
EndSection
Section "Device"
Identifier "dummy_card"
VideoRam 256000
Driver "dummy"
EndSection
Section "Screen"
Identifier "dummy_screen"
Device "dummy_card"
Monitor "dummy_monitor"
SubSection "Display"
depth 24
Modes $VL
EndSubSection
EndSection
EOF
fi
if [ ! -e /root/.xinitrc ]; then
cat > /root/.xinitrc <<-'EOF'
xrdb -merge ~/.Xresources
EOF
fi
if [ "$CMD" = "exec i3" ]; then
echo "xterm -geometry 80x50+494+51 &" >>/root/.xinitrc
echo "xterm -geometry 80x20+494-0 &" >>/root/.xinitrc
fi
echo "${CMD}" >>/root/.xinitrc
if [ ! -e /root/.Xresources ]; then
cat >/root/.Xresources <<-'EOF'
Xft.dpi: 264
xterm*VT100.Translations: #override \
Ctrl <Key> minus: smaller-vt-font() \n\
Ctrl <Key> plus: larger-vt-font() \n\
Ctrl <Key> 0: set-vt-font(d)
EOF
fi
} # X-org配置初始化
create_service() {
if [ ! -e /etc/init.d/x11vnc ]; then
cat >/etc/init.d/x11vnc <<-'EOF'
#!/sbin/openrc-run
name="x11vnc"
description="x11vnc is a Virtual Network Computing server program to access X Windows desktop session"
start_pre() {
if ! pidof xinit >/dev/null; then
rc-service xinit start
sleep 3
fi
}
start() {
ebegin "Starting x11vnc"
start-stop-daemon -Sbmp /run/x11vnc.pid --exec x11vnc -- -xkb -noxrecord -noxfixes -noxdamage -display :0 -noshm -nopw -forever
eend $?
}
stop() {
ebegin "Stopping x11vnc"
start-stop-daemon -Kqp /run/x11vnc.pid
eend $?
rc-service xinit stop
}
EOF
chmod +x /etc/init.d/x11vnc
fi
if [ ! -e /etc/init.d/xinit ]; then
cat >/etc/init.d/xinit <<-'EOF'
#!/sbin/openrc-run
name="xinit"
description="xinit is a tool to starts the X Window System server"
start() {
ebegin "Starting xinit"
start-stop-daemon -Sbmp /run/xinit.pid --exec xinit -- X :0
eend $?
}
stop() {
ebegin "Stopping xinit"
start-stop-daemon -Kqp /run/xinit.pid
eend $?
}
EOF
chmod +x /etc/init.d/xinit
fi
if [ ! -e /etc/init.d/get_location ]; then
cat >/etc/init.d/get_location <<-'EOF'
#!/sbin/openrc-run
name="get_location"
description="get location to keep iSH running in the background"
start() {
ebegin "Starting get_location"
start-stop-daemon -Sb -m -p /run/get_location.pid --exec cat -- /dev/location >/dev/null
eend $?
}
stop() {
ebegin "Stopping get_location"
start-stop-daemon -Kqp /run/get_location.pid
eend $?
}
EOF
chmod +x /etc/init.d/get_location
fi
} # 创建服务启动文件
background_running() {
if pgrep -f "cat /dev/location" >/dev/null; then
$echo_INFO "iSH已经可以保持后台运行了"
read -p "* 是否要取消保持后台运行?[Y/N]" user_choice
case $user_choice in
[yY])
killall -TERM cat
rc-service get_location stop 2>/dev/null
rc-update del get_location 2>/dev/null ;;
*) $echo_INFO "iSH会继续保持后台运行" ;;
esac
else
local i=0
cat /dev/location >/tmp/location.log &
$echo_INFO "申请位置权限仅用于保持iSH后台运行"
$echo_INFO "请在iOS授权界面上点击 '使用App时允许'"
while ((i < 15)); do
if [ -s /tmp/location.log ]; then
$echo_INFO "已赋予位置权限"
killall -TERM cat && rm /tmp/location.log
create_service
rc-update add get_location 2>/dev/null
rc-service get_location start 2>/dev/null
if [ $? != 0 ]; then
cat /dev/location >/dev/null &
fi
break
else
sleep 1
((i++))
fi
done
if [ $i -gt 16 ]; then
$echo_ERROR "超时15秒,无法获取位置权限,iSH无法保持后台运行"
killall -TERM cat && rm /tmp/location.log
fi
fi
} # 获取位置权限,用于保持后台运行
install_script() {
clear
$echo_INFO "正在将脚本复制到'/usr/local/bin'"
cp iSH-Tools.sh /usr/local/bin/iSH-Tools || wget https://github.com/lurenJBD/iSH-Tools/raw/main/iSH-Tools.sh -qO /usr/local/bin/iSH-Tools
chmod +x /usr/local/bin/iSH-Tools
$echo_INFO "安装完成,输入 iSH-Tools 即可运行本脚本"
} # 安装脚本
get_services_status() {
for service in SSH VNC; do
if [ -e /etc/iSH-Tools/${service}_installed ]; then
eval ${service}_install="已安装"
eval ${service}_color="\$GREEN"
else
eval ${service}_install="未安装"
eval ${service}_color="\$RED"
fi
done
for services_name in sshd x11vnc; do
if rc-service ${services_name} status 2>/dev/null | grep -qi "started"; then
eval ${services_name}_status="已启动"
eval ${services_name}_color="\$GREEN"
else
eval ${services_name}_status="未启动"
eval ${services_name}_color="\$RED"
fi
done
} # 获取服务的安装和运行状态
run_tools() {
local tools_dir="/etc/iSH-Tools/other_tools" tool=$1
mkdir -p "$tools_dir"
if [ ! -e "$tools_dir/$tool" ]; then
$echo_WARNING "缺少"$tool"文件,从Github下载"
check_connection
[[ $No_Network -eq 1 ]] && $echo_ERROR "无网络连接,无法下载"$tool"" && return
urls=("https://github.com" "https://download.fastgit.org" "https://kgithub.com" "https://ghproxy.com/https://github.com" "https://hub.gitmirror.com/https://github.com")
for url in "${urls[@]}"; do
wget -T15 -qO ${tools_dir}/${tool} ${url}/lurenJBD/iSH-Tools/releases/download/Tools/${tool} && break
done
chmod +x ${tools_dir}/${tool}
ln -s ${tools_dir}/${tool} /usr/local/bin/${tool}
fi
case $tool in
cpuid2cpuflags)
cpuid2cpuflags | sed -n 's/^CPU_FLAGS_X86: //p' | awk '{printf "支持的指令集:"; for(i=1;i<=NF;i++) printf "%s ", $i; printf "\n"}'
;;
coremark)
$echo_INFO "正在进行 coremark 性能测试,请稍等..."
coremark | grep "CoreMark 1.0" | awk '{print strftime("%Y-%m-%d %H:%M:%S"), $4}' >> "$tools_dir/coreark_results.log"
echo "本次成绩为: $(tail -n 1 "$tools_dir/coreark_results.log" | awk '{print $NF}')"
echo -e "参考成绩\nJ1900(x86) 4核 34060\nMT7621(MIPS) 2核 4547\nN1(ARM) 4核 18404"
$echo_INFO "历史成绩保存在$tools_dir/coreark_results.log"
;;
esac
sleep 0.5 && echo
} # 运行各种工具
change_root_password() {
$echo_INFO "正在修改root账户密码,Ctrl + C 取消修改"
$echo_INFO "输入的密码是看不见的,需要输入两次"
passwd root
if [ $? = 0 ]; then
$echo_INFO "修改root账户密码成功"
else
$echo_INFO "修改root账户密码失败"
fi
} # 修改Root账户密码
config_services() {
local do_what=$1
local ask_info apk_repo CMD
case $do_what in
1) ask_info="安装";;
2) ask_info="删除";;
3) ask_info="更改";;
4) do_something_command;;
esac
if [[ "$do_type" == "vnc" && "$do_what" != "2" ]]; then
clear
declare -A options=(
[1]="awesome桌面"
[2]="i3wm桌面"
)
print_menu 1 2 返回上层 选择桌面环境
while :; do
read -p "* 请选择想${ask_info}的桌面环境[1-2]:" apk_name
if [[ $apk_name == 1 || $apk_name == 2 ]]; then
break
elif [[ $apk_name == "q" ]]; then
break
else
error_tips 2
fi
done
fi
case "$apk_name" in
1)
apk_repo='awesome feh lua adwaita-gtk2-theme adwaita-icon-theme'
apk_name='awesome'
CMD='exec awesome'
;;
2)
apk_repo='i3wm i3wm-doc i3status i3status-doc i3lock i3lock-doc ttf-dejavu'
apk_name='i3wm'
CMD='exec i3'
;;
openssh)
apk_repo='openssh'
;;
ohmyzsh)
apk_repo='zsh git'
;;
q)
clear
config_vnc_menu
;;
esac
clear
do_something_command $do_what
} # 配置服务(安装、删除或更改)
do_something_command() {
[ -e /etc/iSH-Tools/${services}_installed ] && source /etc/iSH-Tools/${services}_installed
do_chance() {
if [ "$apk_name" = "$installed_apk_name" ]; then
$echo_WARNING "${apk_name}桌面环境已经安装,无需更换"
elif [ ! -e /etc/iSH-Tools/${services}_installed ] ; then
$echo_WARNING "未安装过${services}服务,请先安装"
else
do_del
do_install
fi
}
chance_vnc_resolution() {
[ ! -e /etc/X11/xorg.conf.d/10-headless.conf ] && $echo_ERROR "没找到VNC服务配置文件,无法修改" && config_vnc_menu
config_vnc_resolution
sed -i "s#^ Modes.*# Modes "$VL"#g" /etc/X11/xorg.conf.d/10-headless.conf
$echo_INFO "VNC分辨率已修改为$VL" && config_vnc_menu
}
do_del() {
if [ ! -e "/etc/iSH-Tools/${services}_installed" ] || [ "$apk_name" != "$installed_apk_name" ]; then
$echo_WARNING "未安装过${services}服务"
else
apk del -q ${installed_apk_repo}
rm -rf ${rm_file} /etc/iSH-Tools/${services}_installed
if [ "$do_type" = zsh ]; then
sh ~/.oh-my-zsh/tools/uninstall.sh
sed -i 's/\/bin\/zsh/\/bin\/ash/g' /etc/passwd
$echo_INFO "已修改默认终端为ash,重启iSH App以查看效果"
else
rc-update del ${services_name}
local notes="INFO ${services}服务已经启动,请用${services}客户端访问 <设备IP>:${services_port}"
sed -i "/${notes}/d" /etc/motd
fi
$echo_INFO "已删除${apk_name}及配置文件"
fi
}
do_install() {
if [ -e "/etc/iSH-Tools/${services}_installed" ] || [ "$apk_name" = "$installed_apk_name" ]; then
$echo_INFO "${services}服务已安装,无需重复安装"
else
check_connection
[[ $No_Network -eq 1 ]] && $echo_ERROR "无网络连接,无法安装${services}服务" && return
if [ "$do_type" = vnc ]; then
[ ! -e /etc/X11/xorg.conf.d/10-headless.conf ] && config_vnc_resolution && clear
xinit_vnc
create_service
vnc_de='xvfb x11vnc x11vnc-doc xorg-server xdpyinfo xdpyinfo-doc xf86-video-dummy xterm' rm_file='/root/.xinitrc'
fi
$echo_INFO "正在安装${services}服务和${apk_name}"
apk update &>/dev/null
timeout 120s apk add -q ${apk_repo} ${vnc_de}
have_been_timeout=$?
if [ "$have_been_timeout" = 143 ]; then
$echo_WARNING 超过120s未完成安装,可能是源下载太慢,进行镜像源替换
mirrors_manager 1
$echo_INFO "再次尝试安装${services}服务和${apk_name}"
apk add -q ${apk_repo} ${vnc_de}
have_been_timeout=$?
fi
if [ "$do_type" = ssh ]; then
[ ! -e /etc/ssh/ssh_host_ed25519_key ] && $echo_INFO "正在生成SSH安全密匙" && ssh-keygen -A
rm_file='/etc/ssh/sshd_config'
echo 'root:alpine' | chpasswd
sed -i "s/^#Port.*/Port 8022/g" /etc/ssh/sshd_config
sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
$echo_INFO "SSH登入信息:\n用户名: root\n密码: alpine"
fi
if [ "$do_type" = zsh ]; then
REMOTE=https://ghproxy.com/https://github.com/ohmyzsh/ohmyzsh.git BRANCH=master rm_file='/etc/iSH-Tools/ohmyzsh/tools/install.sh'
if [ ! -e /etc/iSH-Tools/ohmyzsh/install.sh ]; then
git config --global http.postBuffer 524288000
git config --global pack.threads 1
mkdir -p /etc/iSH-Tools/ohmyzsh/
wget -qO /etc/iSH-Tools/ohmyzsh/install.sh https://raw.gitmirror.com/ohmyzsh/ohmyzsh/master/tools/install.sh
fi
sh /etc/iSH-Tools/ohmyzsh/install.sh --unattended
sed -i 's/\/bin\/ash/\/bin\/zsh/g' /etc/passwd
$echo_INFO "已修改默认终端为zsh,重启iSH App以查看效果"
fi
if [ "$have_been_timeout" = 0 ]; then
echo installed_apk_name=$apk_name > "/etc/iSH-Tools/${services}_installed"
echo installed_apk_repo=\"$apk_repo $vnc_de\" >> "/etc/iSH-Tools/${services}_installed"
echo rm_file=\'$rm_file\' >> "/etc/iSH-Tools/${services}_installed"
$echo_INFO "${services}服务安装成功"
else
$echo_ERROR "${services}服务安装失败"
fi
fi
}
case "$do_what" in
1) do_install ;;
2) do_del ;;
3) do_chance ;;
4) chance_vnc_resolution ;;
esac
} # 执行动作(安装、删除或更改)
config_vnc_resolution() {
local resolution vnc_resolution
custom_vnc_resolution() {
echo "请输入分辨率长度和高度,用'x'分隔(只取前4位数字):"
read resolution
if [[ $resolution =~ ^[0-9]{3,4}x[0-9]{3,4}$ ]]; then
$echo_INFO "输入的分辨率为 $resolution,确认修改 [Y/N]"
read var
case $var in
[yY]) VL="\"$resolution\"";;
[nN]|*)
$echo_INFO "重新选择VNC分辨率"
sleep 1 && config_vnc_resolution;;
esac
else
$echo_WARNING "无效值,请重新输入!" && custom_vnc_resolution
fi
}
declare -A options=(
[1]="1280x720 推荐iPhone使用"
[2]="1024x768 推荐iPad使用"
[3]="1280x1024 推荐iPad Pro使用"
[4]="自定义分辨率"
)
clear
print_menu 1 4 返回上层 配置VNC分辨率
read -p "* 请选择分辨率 [1/2/3]:" var
case $var in
q) clear && config_vnc_menu;;
1) VL='"1280x720"';;
2) VL='"1024x768"';;
3) VL='"1280x1024"';;
4) custom_vnc_resolution;;
*) error_tips 3 && config_vnc_resolution;;
esac
} # 配置VNC分辨率
config_services_boot() {
local notes="INFO ${services}服务已经启动,请用${services}客户端访问 <设备IP>:${services_port}"
declare -A options=(
[1]="立刻运行${services}服务"
[2]="停止运行${services}服务"
[3]="将${services}服务设为自启动"
[4]="取消${services}服务的自启动"
)
print_menu 1 4 返回上层 ${services}服务管理菜单
case $chosen_option in
1)
clear
rc-service ${services_name} start 2>/dev/null
if [ $? = 0 ]; then
$echo_INFO "${services}服务已启动\n请用${services}客户端连接 <设备IP>:${services_port} 以访问"
else
$echo_WARNING "${services}服务启动失败"
$echo_INFO "因iSH存在BUG,首次安装${services}服务后需要重启iSH App才能正常启动"
fi ;;
2) clear
rc-service ${services_name} stop
if [ $? = 0 ]; then
$echo_INFO "${services}服务已停止"
else
$echo_WARNING "${services}服务停止失败"
$echo_INFO "因iSH存在BUG,首次安装${services}服务后需要重启iSH App才能正常停止"
fi ;;
3)
clear
rc-update add ${services_name} 2>/dev/null
if [ $? = 0 ]; then
if ! grep -q "$notes" /etc/motd; then
echo "$notes" >> /etc/motd
fi
$echo_INFO "已经将${services}服务设置为自动启动"
fi ;;
4)
clear
rc-update del ${services_name}
if [ $? = 0 ]; then
sed -i "/${notes}/d" /etc/motd
$echo_INFO "已取消${services}服务自动启动"
else
$echo_INFO "${services}服务尚未设置自动启动"
fi ;;
q) clear && config_${do_type}_menu ;;
esac
config_services_boot
} # 配置服务启动
########### Menu ###########
other_tools_menu() {
local do_type=zsh services=ZSH apk_name=ohmyzsh
declare -A options=(
[1]="CoreMark跑分:run_tools coremark"
[2]="查询CPU指令集:run_tools cpuid2cpuflags"
[3]="让iSH保持后台运行:background_running"
[4]="安装ohmyzsh:config_services 1"
[5]="删除ohmyzsh:config_services 2"
)
print_menu 1 5 返回主菜单 其他工具菜单
case $chosen_option in
q)
clear && main_menu;;
[1-5])
clear && ${options[$chosen_option]#*:};;
*) error_tips 3;;
esac
other_tools_menu
} # 其他工具菜单
manage_mirror_menu() {
declare -A options=(
[1]="更改镜像源:mirrors_manager 1"
[2]="备份镜像源信息:mirrors_manager 3"
[3]="还原镜像源信息:mirrors_manager 2"
)
print_menu 1 3 返回主菜单 管理镜像源菜单
case $chosen_option in
q)
clear && main_menu;;
[1-3])
clear && ${options[$chosen_option]#*:};;
*) error_tips 3 ;;
esac
manage_mirror_menu
} # 管理镜像源菜单
config_ssh_menu() {
local do_type=ssh services=SSH services_port=8022 services_name=sshd apk_name=openssh
declare -A options=(
[1]="安装SSH服务:config_services 1"
[2]="删除SSH服务:config_services 2"
[3]="更改root账户密码:change_root_password"
[4]="管理SSH服务启动状态:config_services_boot"
)
print_menu 1 4 返回主菜单 SSH配置菜单
case $chosen_option in
q) clear && main_menu;;
[1-4])
clear && ${options[$chosen_option]#*:};;
*) error_tips 3 ;;
esac
config_ssh_menu
} # SSH配置菜单
config_vnc_menu() {
local do_type=vnc services=VNC services_port=5900 services_name=x11vnc
declare -A options=(
[1]="安装VNC服务和桌面环境:config_services 1"
[2]="删除VNC服务和桌面环境:config_services 2"
[3]="更改使用的DE桌面环境:config_services 3"
[4]="更改VNC分辨率:config_services 4"
[5]="管理VNC服务启动状态:config_services_boot"
)
print_menu 1 5 返回主菜单 VNC配置菜单
case $chosen_option in
q) clear && main_menu;;
[1-5])
clear
${options[$chosen_option]#*:}
;;
*) error_tips 3 ;;
esac
config_vnc_menu
} # VNC配置菜单
main_menu() {
declare -A options=(
[1]="配置VNC服务:config_vnc_menu"
[2]="配置SSH服务:config_ssh_menu"
[3]="管理镜像源:manage_mirror_menu"
[4]="其他工具:other_tools_menu"
[5]="安装脚本:install_script"
)
get_services_status
print_menu 3 5 退出脚本 主菜单
case $chosen_option in
q)
exit 0;;
[1-5])
clear && ${options[$chosen_option]#*:};;
*)
error_tips 3;;
esac
} # 主菜单
print_menu() {
sleep 0.1
local a1=$1 b1=$2
printf " %-15s ${YELLOW}%1s\t${PLAIN}\n" "iSH-Tools $tools_version" "$4"
echo " ================================"
if [[ $4 == "主菜单" ]]; then
printf "| %-1d:${CYAN}%-16s ${VNC_color}%-3s${PLAIN}/${x11vnc_color}%-3s${PLAIN} |\n" "1" "${options[1]%:*}" "${VNC_install}" "${x11vnc_status}"
printf "| %-1d:${CYAN}%-16s ${SSH_color}%-3s${PLAIN}/${sshd_color}%-3s${PLAIN} |\n" "2" "${options[2]%:*}" "${SSH_install}" "${sshd_status}"
a1=1 b1=5
fi
for i in $(seq $1 $2); do
printf "| %-1d:${CYAN}%-28s\t${PLAIN} |\n" $i "${options[$i]%:*}"
done
printf "| q:${PURPLE}%-28s\t${PLAIN} |\n" "$3"
echo " ================================"
printf '%-30s\n' "iSH版本:$ish_ver"
[[ $4 == *"菜单"* ]] && read -p "* 请选择需要的功能 [$a1-$b1]:" chosen_option
} # 菜单循环打印显示函数
########### Main ###########
init_run && [[ $init_run_WARNING -ne 1 ]] && clear
while :; do
main_menu
done