Merge pull request #1103 from sofia-riese/patch-5

refactor: replace recursion with while loop in setcustcore
This commit is contained in:
juewuy
2026-01-10 08:49:14 +08:00
committed by GitHub

View File

@@ -228,15 +228,19 @@ switch_core(){ #clash与singbox内核切换
} }
} }
} }
getcore(){ #下载内核文件
. "$CRASHDIR"/libs/core_tools.sh #调用下载工具 # 下载内核文件
getcore() {
# 调用下载工具
. "$CRASHDIR"/libs/core_tools.sh
[ -z "$crashcore" ] && crashcore=meta [ -z "$crashcore" ] && crashcore=meta
[ -z "$cpucore" ] && check_cpucore [ -z "$cpucore" ] && check_cpucore
[ "$crashcore" = unknow ] && setcoretype [ "$crashcore" = unknow ] && setcoretype
echo "$crashcore" | grep -q 'singbox' && core_new=singbox || core_new=clash echo "$crashcore" | grep -q 'singbox' && core_new=singbox || core_new=clash
#获取在线内核文件 # 获取在线内核文件
echo "-----------------------------------------------" echo "-----------------------------------------------"
echo "正在在线获取$crashcore核心文件……" echo "正在在线获取$crashcore核心文件......"
core_webget core_webget
case "$?" in case "$?" in
0) 0)
@@ -256,21 +260,21 @@ getcore(){ #下载内核文件
;; ;;
esac esac
} }
setcustcore(){ #自定义内核
checkcustcore(){ checkcustcore() {
[ "$api_tag" = "latest" ] && api_url=latest || api_url="tags/$api_tag" [ "$api_tag" = "latest" ] && api_url=latest || api_url="tags/$api_tag"
#通过githubapi获取内核信息 # 通过githubapi获取内核信息
echo -e "\033[32m正在获取内核文件链接\033[0m" echo -e "\033[32m正在获取内核文件链接\033[0m"
webget "$TMPDIR"/github_api https://api.github.com/repos/${project}/releases/${api_url} webget "$TMPDIR"/github_api https://api.github.com/repos/${project}/releases/${api_url}
if [ "$?" = 0 ];then if [ "$?" = 0 ]; then
release_tag=$(cat "$TMPDIR"/github_api | grep '"tag_name":' | awk -F '"' '{print $4}') release_tag=$(cat "$TMPDIR"/github_api | grep '"tag_name":' | awk -F '"' '{print $4}')
release_date=$(cat "$TMPDIR"/github_api | grep '"published_at":' | awk -F '"' '{print $4}') release_date=$(cat "$TMPDIR"/github_api | grep '"published_at":' | awk -F '"' '{print $4}')
update_date=$(cat "$TMPDIR"/github_api | grep '"updated_at":' | head -n 1 | awk -F '"' '{print $4}') update_date=$(cat "$TMPDIR"/github_api | grep '"updated_at":' | head -n 1 | awk -F '"' '{print $4}')
[ -n "$(echo $cpucore | grep mips)" ] && cpu_type=mips || cpu_type=$cpucore [ -n "$(echo $cpucore | grep mips)" ] && cpu_type=mips || cpu_type=$cpucore
cat "$TMPDIR"/github_api | grep "browser_download_url" | grep -oE "https://github.com/${project}/releases/download.*linux.*${cpu_type}.*\.gz\"$" | sed 's/"//' > "$TMPDIR"/core.list cat "$TMPDIR"/github_api | grep "browser_download_url" | grep -oE "https://github.com/${project}/releases/download.*linux.*${cpu_type}.*\.gz\"$" | sed 's/"//' >"$TMPDIR"/core.list
rm -rf "$TMPDIR"/github_api rm -rf "$TMPDIR"/github_api
#
if [ -s "$TMPDIR"/core.list ];then if [ -s "$TMPDIR"/core.list ]; then
echo "-----------------------------------------------" echo "-----------------------------------------------"
echo -e "内核版本:\033[36m$release_tag\033[0m" echo -e "内核版本:\033[36m$release_tag\033[0m"
echo -e "发布时间:\033[33m$release_date\033[0m" echo -e "发布时间:\033[33m$release_date\033[0m"
@@ -283,18 +287,20 @@ setcustcore(){ #自定义内核
read -p "请输入对应数字 > " num read -p "请输入对应数字 > " num
case "$num" in case "$num" in
0) 0)
setcustcore return 0
;; ;;
[1-9]|[1-9][0-9]) [1-9] | [1-9][0-9])
if [ "$num" -le "$(wc -l < "$TMPDIR"/core.list)" ];then if [ "$num" -le "$(wc -l <"$TMPDIR"/core.list)" ]; then
custcorelink=$(sed -n "$num"p "$TMPDIR"/core.list) custcorelink=$(sed -n "$num"p "$TMPDIR"/core.list)
getcore getcore
else else
errornum errornum
sleep 1
fi fi
;; ;;
*) *)
errornum errornum
sleep 1
;; ;;
esac esac
else else
@@ -306,7 +312,11 @@ setcustcore(){ #自定义内核
sleep 1 sleep 1
fi fi
rm -rf "$TMPDIR"/core.list rm -rf "$TMPDIR"/core.list
} }
# 自定义内核
setcustcore() {
while true; do
[ -z "$cpucore" ] && check_cpucore [ -z "$cpucore" ] && check_cpucore
echo "-----------------------------------------------" echo "-----------------------------------------------"
echo -e "\033[36m此处内核通常源自互联网采集此处致谢各位开发者\033[0m" echo -e "\033[36m此处内核通常源自互联网采集此处致谢各位开发者\033[0m"
@@ -328,6 +338,9 @@ setcustcore(){ #自定义内核
echo -e " 0 返回上级菜单" echo -e " 0 返回上级菜单"
read -p "请输入对应数字 > " num read -p "请输入对应数字 > " num
case "$num" in case "$num" in
"" | 0)
break
;;
1) 1)
project=MetaCubeX/mihomo project=MetaCubeX/mihomo
api_tag=latest api_tag=latest
@@ -359,9 +372,14 @@ setcustcore(){ #自定义内核
getcore getcore
;; ;;
*) *)
errornum
sleep 1
break
;; ;;
esac esac
done
} }
setziptype(){ setziptype(){
echo "-----------------------------------------------" echo "-----------------------------------------------"
echo -e "请选择内核内核分支及压缩方式:\033[0m" echo -e "请选择内核内核分支及压缩方式:\033[0m"