clash-for-linux/update.sh
2023-12-12 20:02:01 +08:00

92 lines
2.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 自定义action函数实现通用action功能
success() {
echo -en "\\033[60G[\\033[1;32m OK \\033[0;39m]\r"
return 0
}
failure() {
local rc=$?
echo -en "\\033[60G[\\033[1;31mFAILED\\033[0;39m]\r"
[ -x /bin/plymouth ] && /bin/plymouth --details
return $rc
}
action() {
local STRING rc
STRING=$1
echo -n "$STRING "
shift
"$@" && success $"$STRING" || failure $"$STRING"
rc=$?
echo
return $rc
}
# 函数,判断命令是否正常执行
if_success() {
local ReturnStatus=$3
if [ $ReturnStatus -eq 0 ]; then
action "$1" /bin/true
else
action "$2" /bin/false
exit 1
fi
}
## Clash 订阅地址检测及配置文件下载
# 检查url是否有效
echo -e '\n正在检测订阅地址...'
Text1="Clash 订阅地址可访问!"
Text2="Clash 订阅地址不可访问!"
#curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} $URL | grep '[23][0-9][0-9]' &>/dev/null
curl -o /dev/null -L -k -sS --retry 5 -m 10 --connect-timeout 10 -w "%{http_code}" $URL | grep -E '^[23][0-9]{2}$' &>/dev/null
ReturnStatus=$?
if_success $Text1 $Text2 $ReturnStatus
# 拉取更新config.yml文件
echo -e '\n正在下载 Clash 配置文件...'
Text3="配置文件 config.yaml 下载成功!"
Text4="配置文件 config.yaml 下载失败,退出启动!"
# 尝试使用curl进行下载
curl -L -k -sS --retry 5 -m 10 -o $Temp_Dir/clash.yaml $URL
ReturnStatus=$?
if [ $ReturnStatus -ne 0 ]; then
# 如果使用curl下载失败尝试使用wget进行下载
for i in {1..10}
do
wget -q --no-check-certificate -O $Temp_Dir/clash.yaml $URL
ReturnStatus=$?
if [ $ReturnStatus -eq 0 ]; then
break
else
continue
fi
done
fi
if_success $Text3 $Text4 $ReturnStatus
# 重命名clash配置文件
\cp -a $Temp_Dir/clash.yaml $Temp_Dir/clash_config.yaml
## 判断订阅内容是否符合clash配置文件标准尝试转换当前不支持对 x86_64 以外的CPU架构服务器进行clash配置文件检测和转换此功能将在后续添加
if [[ $CpuArch =~ "x86_64" || $CpuArch =~ "amd64" ]]; then
echo -e '\n判断订阅内容是否符合clash配置文件标准:'
bash $Server_Dir/scripts/clash_profile_conversion.sh
sleep 3
fi
## Clash 配置文件重新格式化及配置
# 取出代理相关配置
#sed -n '/^proxies:/,$p' $Temp_Dir/clash.yaml > $Temp_Dir/proxy.txt
sed -n '/^proxies:/,$p' $Temp_Dir/clash_config.yaml > $Temp_Dir/proxy.txt
# 合并形成新的config.yaml
cat $Temp_Dir/templete_config.yaml > $Temp_Dir/config.yaml
cat $Temp_Dir/proxy.txt >> $Temp_Dir/config.yaml
\cp $Temp_Dir/config.yaml $Conf_Dir/