From 0fcd696677d02faf1fdf5bf9606a388ca3e444ef Mon Sep 17 00:00:00 2001 From: libm Date: Sun, 23 Feb 2025 20:32:16 +0800 Subject: [PATCH] fix: extract public download steps as function --- install.sh | 13 ++---------- script/clashctl.sh | 23 ++++++++++---------- script/common.sh | 52 +++++++++++++++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/install.sh b/install.sh index 550d1d9..fe7de53 100755 --- a/install.sh +++ b/install.sh @@ -5,7 +5,6 @@ _valid_env _get_kernel -_get_os [ -d "$CLASH_BASE_DIR" ] && _error_quit "已安装,如需重新安装请先执行卸载脚本" @@ -14,16 +13,8 @@ install -D -m +x <(gzip -dc $ZIP_KERNEL) $BIN_KERNEL # shellcheck disable=SC2086 tar -xf $ZIP_CONVERT -C "$BIN_BASE_DIR" _valid_config "$RESOURCES_CONFIG" || { - _okcat '输入订阅链接:' | tr -d '\n' - read -r url - _download_config "$url" "$RESOURCES_CONFIG" || _error_quit "下载失败: 请自行粘贴配置内容到 ${RESOURCES_CONFIG} 后再执行安装脚本" - _okcat "下载成功:内核验证配置..." - _valid_config "$RESOURCES_CONFIG" || { - _failcat "验证失败:本地订阅转换..." - _download_convert_config "$url" "$RESOURCES_CONFIG" - _okcat "转换成功:内核验证配置..." - _valid_config "$RESOURCES_CONFIG" || _error_quit "配置无效:请检查配置内容:$RESOURCES_CONFIG" - } + _okcat '输入订阅链接:' | tr -d '\n' && read -r url + _download_config "$url" "$RESOURCES_CONFIG" || _error_quit "配置无效:请检查配置内容:$RESOURCES_CONFIG" } _okcat '✅' '配置可用' echo "$url" >"$CLASH_CONFIG_URL" diff --git a/script/clashctl.sh b/script/clashctl.sh index 86e1469..f3a9c86 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -142,8 +142,8 @@ function clashupdate() { [ -n "$2" ] && url=$2 ;; log) - tail "${CLASH_UPDATE_LOG}" - return $? + sudo tail "${CLASH_UPDATE_LOG}" 2>/dev/null || _failcat "暂无更新日志" + return 0 ;; *) [ -n "$1" ] && url=$1 @@ -152,7 +152,7 @@ function clashupdate() { # 如果没有提供有效的订阅链接(url为空或者不是http开头),则使用默认配置文件 [ "${url:0:4}" != "http" ] && { - _failcat "没有提供有效的订阅链接,使用${CLASH_CONFIG_RAW}进行更新..." + _failcat "没有提供有效的订阅链接:使用 ${CLASH_CONFIG_RAW} 进行更新..." url="file://$CLASH_CONFIG_RAW" } @@ -161,19 +161,18 @@ function clashupdate() { sudo grep -qs 'clashupdate' "$CLASH_CRON_TAB" || echo "0 0 */2 * * . $BASHRC;clashupdate $url" | sudo tee -a "$CLASH_CRON_TAB" >&/dev/null _okcat "定时任务设置成功" && return 0 } - sudo cat "$CLASH_CONFIG_RAW" | sudo tee "$CLASH_CONFIG_RAW_BAK" >&/dev/null - _download_config "$url" "$CLASH_CONFIG_RAW" - # 校验并更新配置 - _valid_config "$CLASH_CONFIG_RAW" || _download_convert_config "$CLASH_CONFIG_RAW" "$url" - _valid_config "$CLASH_CONFIG_RAW" || { - echo "$(date +"%Y-%m-%d %H:%M:%S") 配置更新失败 ❌ $url" | sudo tee -a "${CLASH_UPDATE_LOG}" >&/dev/null + _okcat "备份配置:$CLASH_CONFIG_RAW_BAK" + sudo cat "$CLASH_CONFIG_RAW" | sudo tee "$CLASH_CONFIG_RAW_BAK" >&/dev/null + _download_config "$url" "$CLASH_CONFIG_RAW" || { + _failcat "验证失败:回滚配置..." sudo cat "$CLASH_CONFIG_RAW_BAK" | sudo tee "$CLASH_CONFIG_RAW" >&/dev/null - _error_quit '下载失败或配置无效:已回滚配置' + _failcat '❌' "[$(date +"%Y-%m-%d %H:%M:%S")] 订阅更新失败:$url" 2>&1 | sudo tee -a "${CLASH_UPDATE_LOG}" + _error_quit } - _merge_config_restart && _okcat '配置更新成功,已重启生效' + _merge_config_restart echo "$url" | sudo tee "$CLASH_CONFIG_URL" >&/dev/null - echo "$(date +"%Y-%m-%d %H:%M:%S") 配置更新成功 ✅ $url" | sudo tee -a "${CLASH_UPDATE_LOG}" >&/dev/null + _okcat '✅' "[$(date +"%Y-%m-%d %H:%M:%S")] 订阅更新成功:$url" | sudo tee -a "${CLASH_UPDATE_LOG}" } function clashmixin() { diff --git a/script/common.sh b/script/common.sh index 9c0eb15..34d5f42 100644 --- a/script/common.sh +++ b/script/common.sh @@ -44,17 +44,9 @@ function _get_kernel() { /bin/rm -rf $ZIP_KERNEL _download_clash "$cpu_arch" } - - [ -e $ZIP_MIHOMO ] && { - ZIP_KERNEL=$ZIP_MIHOMO - BIN_KERNEL=$BIN_MIHOMO - } || { - ZIP_KERNEL=$ZIP_CLASH - BIN_KERNEL=$BIN_CLASH - } } -function _get_os() { +_adapt() { local os_info=$(cat /etc/os-release) echo "$os_info" | grep -iqsE "rhel|centos" && { CLASH_CRON_TAB='/var/spool/cron/root' @@ -64,7 +56,18 @@ function _get_os() { CLASH_CRON_TAB='/var/spool/cron/crontabs/root' BASHRC='/etc/bash.bashrc' } + + # shellcheck disable=SC2086 + # shellcheck disable=SC2015 + [ -e $ZIP_MIHOMO ] && { + ZIP_KERNEL=$ZIP_MIHOMO + BIN_KERNEL=$BIN_MIHOMO + } || { + ZIP_KERNEL=$ZIP_CLASH + BIN_KERNEL=$BIN_CLASH + } } +_adapt function _get_port() { local mixed_port=$(sudo $BIN_YQ '.mixed-port // ""' $CLASH_CONFIG_RUNTIME) @@ -98,7 +101,7 @@ function _get_port() { } [ "$arg" = "$UI_PORT" ] && { newPort=$(_random_port) - msg="端口占用:$UI_PORT,随机分配:$newPort" + msg="端口占用:${UI_PORT},随机分配:$newPort" sudo "$BIN_YQ" -i ".external-controller = \"0.0.0.0:$newPort\"" $CLASH_CONFIG_RUNTIME UI_PORT=$newPort _failcat "$msg" @@ -135,15 +138,17 @@ function _failcat() { local emoji=😾 [ $# -gt 1 ] && emoji=$1 && shift local msg="${emoji} $1" - _color_msg "$color" "$msg" && return 1 + _color_msg "$color" "$msg" >&2 && return 1 } # bash执行 $0为脚本执行路径 # source执行 $0为bash function _error_quit() { - local color=#f92f60 - local msg="❌ $1" - _color_msg "$color" "$msg" + [ $# -gt 0 ] && { + local color=#f92f60 + local msg="❌ $1" + _color_msg "$color" "$msg" + } echo "$0" | grep -qs 'bash' && exec bash || exit 1 } @@ -198,7 +203,7 @@ function _valid_config() { } } -function _download_config() { +_download_raw_config() { local url=$1 local dest=$2 local agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0' @@ -257,10 +262,23 @@ _stop_convert() { pkill -9 -f subconverter >&/dev/null } -function _download_convert_config() { +_download_convert_config() { local url=$1 local dest=$2 _start_convert - _download_config "$(_convert_url "$url")" "$dest" + _download_raw_config "$(_convert_url "$url")" "$dest" _stop_convert } + +function _download_config() { + local url=$1 + local dest=$2 + _download_raw_config "$url" "$dest" || _error_quit "下载失败: 请自行粘贴配置内容到 ${dest}" + _okcat "下载成功:内核验证配置..." + _valid_config "$dest" || { + _failcat "验证失败:本地订阅转换..." + _download_convert_config "$url" "$dest" + _okcat "转换成功:内核验证配置..." + _valid_config "$dest" + } +}