Merge pull request #83 from wanhebin/dev

1.优化CPU架构信息获取逻辑。2.优化订阅地址下载优化。3. start.sh 增加对.env中变量的检测。4.脚本结构优化。
This commit is contained in:
wanhebin 2023-06-11 23:52:55 +08:00 committed by GitHub
commit c85777421d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"

153
start.sh
View File

@ -3,83 +3,108 @@
# 加载系统函数库(Only for RHEL Linux)
# [ -f /etc/init.d/functions ] && source /etc/init.d/functions
#################### 脚本初始化任务 ####################
# 获取脚本工作目录绝对路径
export Server_Dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
# 给二进制启动程序添加可执行权限
chmod +x $Server_Dir/bin/*
chmod +x $Server_Dir/tools/subconverter/subconverter
# 加载.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"
Temp_Dir="$Server_Dir/temp"
Log_Dir="$Server_Dir/logs"
URL=${CLASH_URL}
# 获取CLASH SECRET 值
if [ ! $CLASH_SECRET ]; then
# 随机生成并更新 API Secret
Secret=`openssl rand -hex 32`
else
Secret=${CLASH_SECRET}
fi
# 将 CLASH_URL 变量的值赋给 URL 变量,并检查 CLASH_URL 是否为空
URL=${CLASH_URL:?Error: CLASH_URL variable is not set or empty}
# 获取 CLASH_SECRET 值,如果不存在则生成一个随机数
Secret=${CLASH_SECRET:-$(openssl rand -hex 32)}
#################### 函数定义 ####################
# 自定义action函数实现通用action功能
success() {
echo -en "\\033[60G[\\033[1;32m OK \\033[0;39m]\r"
return 0
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
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
local STRING rc
STRING=$1
echo -n "$STRING "
shift
"$@" && success $"$STRING" || failure $"$STRING"
rc=$?
echo
return $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
local ReturnStatus=$3
if [ $ReturnStatus -eq 0 ]; then
action "$1" /bin/true
else
action "$2" /bin/false
exit 1
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 https_proxy
unset no_proxy
## Clash 订阅地址检测及配置文件下载
# 检查url是否有效
echo -e '\n正在检测订阅地址...'
Text1="Clash订阅地址可访问"
Text2="Clash订阅地址不可访问"
for i in {1..10}
do
curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} $URL | grep '[23][0-9][0-9]' &>/dev/null
ReturnStatus=$?
if [ $ReturnStatus -eq 0 ]; then
break
else
continue
fi
curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} $URL | grep '[23][0-9][0-9]' &>/dev/null
ReturnStatus=$?
if [ $ReturnStatus -eq 0 ]; then
break
else
continue
fi
done
if_success $Text1 $Text2 $ReturnStatus
@ -87,27 +112,36 @@ if_success $Text1 $Text2 $ReturnStatus
echo -e '\n正在下载Clash配置文件...'
Text3="配置文件config.yaml下载成功"
Text4="配置文件config.yaml下载失败退出启动"
for i in {1..10}
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
ReturnStatus=$?
if [ $ReturnStatus -eq 0 ]; then
break
else
continue
fi
done
# 尝试使用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配置文件标准尝试转换
## 判断订阅内容是否符合clash配置文件标准尝试转换
echo -e '\n判断订阅内容是否符合clash配置文件标准:'
bash $Server_Dir/scripts/clash_profile_conversion.sh
sleep 3
## 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
@ -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 -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服务...'
Text5="服务启动成功!"
Text6="服务启动失败!"