1.优化CPU架构信息获取逻辑。使用单独的脚本进行处理,并且对多种Linux发行版做了适配。2.优化订阅地址下载逻辑,添加了curl/wget多种下载方式,并且提供了重试机制。3.优化对 文件配置的变量值的检测。4.脚本结构优化。

This commit is contained in:
wanhebin 2023-06-09 18:32:52 +08:00
parent e44d96aaaa
commit fb5a21709c
2 changed files with 138 additions and 65 deletions

50
scripts/get_cpu_arch.sh Executable file
View File

@ -0,0 +1,50 @@
#!/bin/bash
# 该脚本的作用是获取Linux操作系统上运行的CPU架构信息并将其输出到标准输出流。
function exitWithError {
local errorMessage="$1"
echo -e "\033[31m[ERROR] $errorMessage\033[0m" >&2
exit 1
}
# Function to get CPU architecture
function get_cpu_arch {
local commands=("$@")
for cmd in "${commands[@]}"; do
local CpuArch
CpuArch=$(command -v $cmd >/dev/null && $cmd 2>/dev/null || type -p $cmd 2>/dev/null)
if [[ -n "$CpuArch" ]]; then
echo "$CpuArch"
return
fi
done
}
# Check if we are running on a supported Linux distribution
if [[ -f "/etc/os-release" ]]; then
. /etc/os-release
case "$ID" in
"ubuntu"|"debian"|"linuxmint")
# Debian-based distributions
CpuArch=$(get_cpu_arch "dpkg-architecture -qDEB_HOST_ARCH_CPU" "dpkg-architecture -qDEB_BUILD_ARCH_CPU" "uname -m")
;;
"centos"|"fedora"|"rhel")
# Red Hat-based distributions
CpuArch=$(get_cpu_arch "uname -m" "arch" "uname")
;;
*)
# Unsupported Linux distribution
CpuArch=$(get_cpu_arch "uname -m" "arch" "uname")
if [[ -z "$CpuArch" ]]; then
exitWithError "Failed to obtain CPU architecture"
fi
;;
esac
elif [[ -f "/etc/redhat-release" ]]; then
# Older Red Hat-based distributions
CpuArch=$(get_cpu_arch "uname -m" "arch" "uname")
else
exitWithError "Unsupported Linux distribution"
fi
echo "CPU architecture: $CpuArch"

View File

@ -3,28 +3,36 @@
# 加载系统函数库(Only for RHEL Linux) # 加载系统函数库(Only for RHEL Linux)
# [ -f /etc/init.d/functions ] && source /etc/init.d/functions # [ -f /etc/init.d/functions ] && source /etc/init.d/functions
#################### 脚本初始化任务 ####################
# 获取脚本工作目录绝对路径 # 获取脚本工作目录绝对路径
export Server_Dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) export Server_Dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
# 给二进制启动程序添加可执行权限
chmod +x $Server_Dir/bin/*
chmod +x $Server_Dir/tools/subconverter/subconverter
# 加载.env变量文件 # 加载.env变量文件
source $Server_Dir/.env source $Server_Dir/.env
# 给二进制启动程序、脚本等添加可执行权限
chmod +x $Server_Dir/bin/*
chmod +x $Server_Dir/scripts/*
chmod +x $Server_Dir/tools/subconverter/subconverter
#################### 变量设置 ####################
Conf_Dir="$Server_Dir/conf" Conf_Dir="$Server_Dir/conf"
Temp_Dir="$Server_Dir/temp" Temp_Dir="$Server_Dir/temp"
Log_Dir="$Server_Dir/logs" Log_Dir="$Server_Dir/logs"
URL=${CLASH_URL}
# 获取CLASH SECRET 值 # 将 CLASH_URL 变量的值赋给 URL 变量,并检查 CLASH_URL 是否为空
if [ ! $CLASH_SECRET ]; then URL=${CLASH_URL:?Error: CLASH_URL variable is not set or empty}
# 随机生成并更新 API Secret
Secret=`openssl rand -hex 32` # 获取 CLASH_SECRET 值,如果不存在则生成一个随机数
else Secret=${CLASH_SECRET:-$(openssl rand -hex 32)}
Secret=${CLASH_SECRET}
fi
#################### 函数定义 ####################
# 自定义action函数实现通用action功能 # 自定义action函数实现通用action功能
success() { success() {
@ -62,11 +70,28 @@ if_success() {
fi fi
} }
# 临时取消环境变量
#################### 任务执行 ####################
## 获取CPU架构信息
# Source the script to get CPU architecture
source $Server_Dir/scripts/get_cpu_arch.sh
# Check if we obtained CPU architecture
if [[ -z "$CpuArch" ]]; then
echo "Failed to obtain CPU architecture"
exit 1
fi
## 临时取消环境变量
unset http_proxy unset http_proxy
unset https_proxy unset https_proxy
unset no_proxy unset no_proxy
## Clash 订阅地址检测及配置文件下载
# 检查url是否有效 # 检查url是否有效
echo -e '\n正在检测订阅地址...' echo -e '\n正在检测订阅地址...'
Text1="Clash订阅地址可访问" Text1="Clash订阅地址可访问"
@ -87,9 +112,14 @@ if_success $Text1 $Text2 $ReturnStatus
echo -e '\n正在下载Clash配置文件...' echo -e '\n正在下载Clash配置文件...'
Text3="配置文件config.yaml下载成功" Text3="配置文件config.yaml下载成功"
Text4="配置文件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} for i in {1..10}
do do
#curl -L -k -sS --retry 3 -m 10 -o $Temp_Dir/clash.yaml $URL
wget -q --no-check-certificate -O $Temp_Dir/clash.yaml $URL wget -q --no-check-certificate -O $Temp_Dir/clash.yaml $URL
ReturnStatus=$? ReturnStatus=$?
if [ $ReturnStatus -eq 0 ]; then if [ $ReturnStatus -eq 0 ]; then
@ -98,16 +128,20 @@ do
continue continue
fi fi
done done
fi
if_success $Text3 $Text4 $ReturnStatus if_success $Text3 $Text4 $ReturnStatus
# 重命名clash配置文件 # 重命名clash配置文件
\cp -a $Temp_Dir/clash.yaml $Temp_Dir/clash_config.yaml \cp -a $Temp_Dir/clash.yaml $Temp_Dir/clash_config.yaml
# 判断订阅内容是否符合clash配置文件标准尝试转换
## 判断订阅内容是否符合clash配置文件标准尝试转换
echo -e '\n判断订阅内容是否符合clash配置文件标准:' echo -e '\n判断订阅内容是否符合clash配置文件标准:'
bash $Server_Dir/scripts/clash_profile_conversion.sh bash $Server_Dir/scripts/clash_profile_conversion.sh
sleep 3 sleep 3
## Clash 配置文件重新格式化及配置
# 取出代理相关配置 # 取出代理相关配置
#sed -n '/^proxies:/,$p' $Temp_Dir/clash.yaml > $Temp_Dir/proxy.txt #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 sed -n '/^proxies:/,$p' $Temp_Dir/clash_config.yaml > $Temp_Dir/proxy.txt
@ -123,19 +157,8 @@ Dashboard_Dir="${Work_Dir}/dashboard/public"
sed -ri "s@^# external-ui:.*@external-ui: ${Dashboard_Dir}@g" $Conf_Dir/config.yaml sed -ri "s@^# external-ui:.*@external-ui: ${Dashboard_Dir}@g" $Conf_Dir/config.yaml
sed -r -i '/^secret: /s@(secret: ).*@\1'${Secret}'@g' $Conf_Dir/config.yaml sed -r -i '/^secret: /s@(secret: ).*@\1'${Secret}'@g' $Conf_Dir/config.yaml
# 获取CPU架构
if /bin/arch &>/dev/null; then
CpuArch=`/bin/arch`
elif /usr/bin/arch &>/dev/null; then
CpuArch=`/usr/bin/arch`
elif /bin/uname -m &>/dev/null; then
CpuArch=`/bin/uname -m`
else
echo -e "\033[31m\n[ERROR] Failed to obtain CPU architecture\033[0m"
exit 1
fi
# 启动Clash服务 ## 启动Clash服务
echo -e '\n正在启动Clash服务...' echo -e '\n正在启动Clash服务...'
Text5="服务启动成功!" Text5="服务启动成功!"
Text6="服务启动失败!" Text6="服务启动失败!"