Continue replace self-invoked with a while true loop

- Use CASE statements instead of if-elif statements.
- Replace self-invoked with a while true loop.
- Wrap the inner function outside the parent function to prevent duplicate declarations caused by the while true loop.
This commit is contained in:
nested-universe
2026-01-01 14:45:37 +08:00
committed by GitHub
parent 4f31373c5f
commit ea2a56f871

View File

@@ -7,13 +7,13 @@ __IS_MODULE_8_TOOLS_LOADED=1
. "$CRASHDIR"/libs/logger.sh
. "$CRASHDIR"/libs/web_get_bin.sh
#工具与优化
tools() {
ssh_tools() {
stop_iptables() {
iptables -w -t nat -D PREROUTING -p tcp -m multiport --dports $ssh_port -j REDIRECT --to-ports 22 >/dev/null 2>&1
ip6tables -w -t nat -A PREROUTING -p tcp -m multiport --dports $ssh_port -j REDIRECT --to-ports 22 >/dev/null 2>&1
}
ssh_tools() {
while true; do
[ -n "$(cat /etc/firewall.user 2>&1 | grep '启用外网访问SSH服务')" ] && ssh_ol=禁止 || ssh_ol=开启
[ -z "$ssh_port" ] && ssh_port=10022
echo "-----------------------------------------------"
@@ -28,7 +28,9 @@ tools() {
echo "-----------------------------------------------"
read -p "请输入对应数字 > " num
case "$num" in
0) ;;
""|0)
break
;;
1)
read -p "请输入端口号(1000-65535) > " num
if [ -z "$num" ]; then
@@ -45,12 +47,10 @@ tools() {
echo -e "\033[32m设置成功请重新开启外网访问SSH功能\033[0m"
fi
sleep 1
ssh_tools
;;
2)
passwd
sleep 1
ssh_tools
;;
3)
if [ "$ssh_ol" = "开启" ]; then
@@ -66,17 +66,25 @@ tools() {
echo "-----------------------------------------------"
echo -e "已禁止外网访问SSH"
fi
break
;;
*)
errornum
sleep 1
break
;;
esac
done
}
#工具与优化
tools() {
while true; do
#获取设置默认显示
grep -qE "^\s*[^#].*otapredownload" /etc/crontabs/root >/dev/null 2>&1 && mi_update=禁用 || mi_update=启用
[ "$mi_mi_autoSSH" = "已配置" ] && mi_mi_autoSSH_type=32m已配置 || mi_mi_autoSSH_type=31m未配置
[ -f "$CRASHDIR"/tools/tun.ko ] && mi_tunfix=32m已启用 || mi_tunfix=31m未启用
#
echo "-----------------------------------------------"
echo -e "\033[30;47m欢迎使用其他工具菜单\033[0m"
echo -e "\033[33m本页工具可能无法兼容全部Linux设备请酌情使用\033[0m"
@@ -94,27 +102,49 @@ tools() {
echo -e " 0 返回上级菜单"
echo "-----------------------------------------------"
read -p "请输入对应数字 > " num
if [ -z "$num" ]; then
errornum
elif [ "$num" = 0 ]; then
i=
elif [ "$num" = 1 ]; then
case "$num" in
""|0)
break
;;
1)
testcommand
elif [ "$num" = 2 ]; then
break
;;
2)
userguide
elif [ "$num" = 3 ]; then
break
;;
3)
log_pusher
tools
elif [ "$num" = 4 ]; then
;;
4)
ssh_tools
sleep 1
tools
elif [ "$num" = 7 ]; then
;;
5)
if [ -x /usr/sbin/otapredownload ]; then
if [ "$mi_update" = "禁用" ]; then
grep -q "otapredownload" /etc/crontabs/root &&
sed -i "/^[^\#]*otapredownload/ s/^/#/" /etc/crontabs/root ||
echo "#15 3,4,5 * * * /usr/sbin/otapredownload >/dev/null 2>&1" >>/etc/crontabs/root
else
grep -q "otapredownload" /etc/crontabs/root &&
sed -i "/^\s*#.*otapredownload/ s/^\s*#//" /etc/crontabs/root ||
echo "15 3,4,5 * * * /usr/sbin/otapredownload >/dev/null 2>&1" >>/etc/crontabs/root
fi
echo "-----------------------------------------------"
echo -e "已\033[33m$mi_update\033[0m小米路由器的自动更新如未生效请在官方APP中同步设置"
sleep 1
fi
;;
6)
if [ "$systype" = "mi_snapshot" ]; then
mi_autoSSH
else
echo "不支持的设备!"
fi
;;
7)
echo "-----------------------------------------------"
if [ ! -f "$CRASHDIR"/tools/ShellDDNS.sh ]; then
echo -e "正在获取在线脚本……"
@@ -129,31 +159,8 @@ tools() {
. "$CRASHDIR"/tools/ShellDDNS.sh
fi
sleep 1
tools
elif [ -x /usr/sbin/otapredownload ] && [ "$num" = 5 ]; then
if [ "$mi_update" = "禁用" ]; then
grep -q "otapredownload" /etc/crontabs/root &&
sed -i "/^[^\#]*otapredownload/ s/^/#/" /etc/crontabs/root ||
echo "#15 3,4,5 * * * /usr/sbin/otapredownload >/dev/null 2>&1" >>/etc/crontabs/root
else
grep -q "otapredownload" /etc/crontabs/root &&
sed -i "/^\s*#.*otapredownload/ s/^\s*#//" /etc/crontabs/root ||
echo "15 3,4,5 * * * /usr/sbin/otapredownload >/dev/null 2>&1" >>/etc/crontabs/root
fi
echo "-----------------------------------------------"
echo -e "已\033[33m$mi_update\033[0m小米路由器的自动更新如未生效请在官方APP中同步设置"
sleep 1
tools
elif [ "$num" = 6 ]; then
if [ "$systype" = "mi_snapshot" ]; then
mi_autoSSH
else
echo "不支持的设备!"
fi
tools
elif [ "$num" = 8 ]; then
;;
8)
if [ -f "$CRASHDIR"/tools/tun.ko ]; then
read -p "是否禁用此功能并移除相关补丁?(1/0) > " res
[ "$res" = 1 ] && {
@@ -181,10 +188,14 @@ tools() {
echo -e "\033[31m当前设备无需设置请勿尝试\033[0m"
sleep 1
fi
tools
else
;;
*)
errornum
fi
sleep 1
break
;;
esac
done
}
mi_autoSSH() {