Skip to content

Commit b4bd9a0

Browse files
committed
Update.
1 parent c2695c0 commit b4bd9a0

1 file changed

Lines changed: 128 additions & 7 deletions

File tree

‎Snell.sh‎

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export PATH
99
# WebSite: https://about.nange.cn
1010
#=================================================
1111

12-
sh_ver="1.7.5"
12+
sh_ver="1.7.6"
1313
snell_v4_version="4.1.1"
1414
snell_v5_version="5.0.0b2"
1515
script_dir=$(cd "$(dirname "$0")"; pwd)
@@ -893,6 +893,110 @@ updateSnellServer(){
893893
startMenu
894894
}
895895

896+
# 自动获取 Snell 最新版本号
897+
getLatestVersionFromWeb(){
898+
local version_type=$1 # "v4" 或 "v5"
899+
local release_page="https://kb.nssurge.com/surge-knowledge-base/zh/release-notes/snell"
900+
901+
echo -e "${Info} 正在从官方页面获取最新版本信息..."
902+
903+
# 下载页面内容
904+
page_content=$(curl -s -L --max-time 10 "$release_page" 2>/dev/null)
905+
906+
if [[ -z "$page_content" ]]; then
907+
echo -e "${Error} 无法获取版本信息页面,使用脚本内置版本号"
908+
return 1
909+
fi
910+
911+
if [[ "$version_type" == "v4" ]]; then
912+
# 提取 v4 最新版本号,查找下载链接中的版本
913+
latest_v4=$(echo "$page_content" | grep -oE "snell-server-v4\.[0-9]+\.[0-9]+-linux" | head -1 | sed 's/snell-server-v//g' | sed 's/-linux//g')
914+
if [[ -n "$latest_v4" ]]; then
915+
echo "$latest_v4"
916+
return 0
917+
fi
918+
elif [[ "$version_type" == "v5" ]]; then
919+
# 提取 v5 最新版本号,查找下载链接中的版本
920+
latest_v5=$(echo "$page_content" | grep -oE "snell-server-v5\.[0-9]+\.[0-9]+[a-z]*[0-9]*-linux" | head -1 | sed 's/snell-server-v//g' | sed 's/-linux//g')
921+
if [[ -n "$latest_v5" ]]; then
922+
echo "$latest_v5"
923+
return 0
924+
fi
925+
fi
926+
927+
echo -e "${Error} 无法解析 $version_type 版本号,使用脚本内置版本号"
928+
return 1
929+
}
930+
931+
# 更新脚本内置版本号(带缓存机制)
932+
updateBuiltinVersions(){
933+
local cache_file="/tmp/snell_version_cache"
934+
local cache_time=3600 # 缓存1小时
935+
local current_time=$(date +%s)
936+
937+
# 检查缓存是否存在且有效
938+
if [[ -f "$cache_file" ]]; then
939+
local cache_timestamp=$(head -1 "$cache_file" 2>/dev/null)
940+
if [[ -n "$cache_timestamp" && $((current_time - cache_timestamp)) -lt $cache_time ]]; then
941+
# 缓存有效,读取缓存的版本信息
942+
local cached_v4=$(sed -n '2p' "$cache_file" 2>/dev/null)
943+
local cached_v5=$(sed -n '3p' "$cache_file" 2>/dev/null)
944+
if [[ -n "$cached_v4" && -n "$cached_v5" ]]; then
945+
snell_v4_version="$cached_v4"
946+
snell_v5_version="$cached_v5"
947+
return 0
948+
fi
949+
fi
950+
fi
951+
952+
echo -e "${Info} 正在检查官方最新版本..."
953+
954+
# 获取最新的 v4 版本
955+
latest_v4_web=$(getLatestVersionFromWeb "v4")
956+
if [[ $? -eq 0 && -n "$latest_v4_web" ]]; then
957+
if [[ "$latest_v4_web" != "$snell_v4_version" ]]; then
958+
echo -e "${Info} 发现 v4 新版本: $latest_v4_web (当前: $snell_v4_version)"
959+
snell_v4_version="$latest_v4_web"
960+
else
961+
echo -e "${Info} v4 版本已是最新: $snell_v4_version"
962+
fi
963+
fi
964+
965+
# 获取最新的 v5 版本
966+
latest_v5_web=$(getLatestVersionFromWeb "v5")
967+
if [[ $? -eq 0 && -n "$latest_v5_web" ]]; then
968+
if [[ "$latest_v5_web" != "$snell_v5_version" ]]; then
969+
echo -e "${Info} 发现 v5 新版本: $latest_v5_web (当前: $snell_v5_version)"
970+
snell_v5_version="$latest_v5_web"
971+
else
972+
echo -e "${Info} v5 版本已是最新: $snell_v5_version"
973+
fi
974+
fi
975+
976+
# 更新缓存
977+
echo "$current_time" > "$cache_file"
978+
echo "$snell_v4_version" >> "$cache_file"
979+
echo "$snell_v5_version" >> "$cache_file"
980+
}
981+
982+
# 强制检查最新版本(清除缓存)
983+
forceCheckVersions(){
984+
echo -e "${Info} 强制检查 Snell 最新版本..."
985+
986+
# 清除缓存
987+
rm -f "/tmp/snell_version_cache"
988+
989+
# 强制获取最新版本
990+
updateBuiltinVersions
991+
992+
echo -e "${Info} 版本检查完成!"
993+
echo -e "${Info} 当前脚本 v4 版本: ${Green_font_prefix}${snell_v4_version}${Font_color_suffix}"
994+
echo -e "${Info} 当前脚本 v5 版本: ${Green_font_prefix}${snell_v5_version}${Font_color_suffix}"
995+
996+
sleep 3s
997+
startMenu
998+
}
999+
8961000
# 卸载 Snell
8971001
uninstallSnell(){
8981002
checkInstalledStatus
@@ -1056,6 +1160,13 @@ startMenu(){
10561160
action=$1
10571161

10581162
# 检查版本更新(在显示菜单前)
1163+
# 首先更新内置版本号(从官方页面获取最新版本,有缓存机制)
1164+
if [[ -e ${snell_bin} && -e ${snell_conf} ]]; then
1165+
# 只有安装了 Snell 才检查版本更新
1166+
updateBuiltinVersions
1167+
fi
1168+
1169+
# 然后检查已安装版本是否需要更新
10591170
checkVersionUpdate
10601171

10611172
# 检查是否安装了 v4 版本,需要显示 v4 到 v5 更新选项
@@ -1076,6 +1187,7 @@ startMenu(){
10761187
Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
10771188
==============================
10781189
${Green_font_prefix} 0.${Font_color_suffix} 更新脚本
1190+
${Green_font_prefix}99.${Font_color_suffix} 强制检查最新版本
10791191
——————————————————————————————
10801192
${Green_font_prefix} 1.${Font_color_suffix} 安装 Snell Server
10811193
${Green_font_prefix} 2.${Font_color_suffix} 卸载 Snell Server"
@@ -1145,11 +1257,11 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
11451257
echo
11461258

11471259
if [[ "$show_v4_to_v5_option" == true ]]; then
1148-
read -e -p " 请输入数字[0-10]:" num
1260+
read -e -p " 请输入数字[0-10,99]:" num
11491261
elif [[ "$show_update_option" == true ]]; then
1150-
read -e -p " 请输入数字[0-9]:" num
1262+
read -e -p " 请输入数字[0-9,99]:" num
11511263
else
1152-
read -e -p " 请输入数字[0-8]:" num
1264+
read -e -p " 请输入数字[0-8,99]:" num
11531265
fi
11541266

11551267
# 根据不同菜单模式处理用户输入
@@ -1158,6 +1270,9 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
11581270
0)
11591271
updateShell
11601272
;;
1273+
99)
1274+
forceCheckVersions
1275+
;;
11611276
1)
11621277
installSnell
11631278
;;
@@ -1192,7 +1307,7 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
11921307
exit 1
11931308
;;
11941309
*)
1195-
echo -e "请输入正确数字${Yellow_font_prefix}[0-10]${Font_color_suffix}"
1310+
echo -e "请输入正确数字${Yellow_font_prefix}[0-10,99]${Font_color_suffix}"
11961311
sleep 2s
11971312
startMenu
11981313
;;
@@ -1202,6 +1317,9 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
12021317
0)
12031318
updateShell
12041319
;;
1320+
99)
1321+
forceCheckVersions
1322+
;;
12051323
1)
12061324
installSnell
12071325
;;
@@ -1233,7 +1351,7 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
12331351
exit 1
12341352
;;
12351353
*)
1236-
echo -e "请输入正确数字${Yellow_font_prefix}[0-9]${Font_color_suffix}"
1354+
echo -e "请输入正确数字${Yellow_font_prefix}[0-9,99]${Font_color_suffix}"
12371355
sleep 2s
12381356
startMenu
12391357
;;
@@ -1243,6 +1361,9 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
12431361
0)
12441362
updateShell
12451363
;;
1364+
99)
1365+
forceCheckVersions
1366+
;;
12461367
1)
12471368
installSnell
12481369
;;
@@ -1271,7 +1392,7 @@ Snell Server 管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
12711392
exit 1
12721393
;;
12731394
*)
1274-
echo -e "请输入正确数字${Yellow_font_prefix}[0-8]${Font_color_suffix}"
1395+
echo -e "请输入正确数字${Yellow_font_prefix}[0-8,99]${Font_color_suffix}"
12751396
sleep 2s
12761397
startMenu
12771398
;;

0 commit comments

Comments
 (0)