2021-01-25 19:39:21 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2021-10-21 00:34:27 +08:00
|
|
|
|
# 加载系统函数库(Only for RHEL Linux)
|
|
|
|
|
# [ -f /etc/init.d/functions ] && source /etc/init.d/functions
|
|
|
|
|
|
2023-03-03 12:59:32 +08:00
|
|
|
|
# 获取脚本工作目录绝对路径
|
|
|
|
|
Server_Dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
|
|
|
|
|
|
|
|
|
|
# 加载.env变量文件
|
|
|
|
|
source $Server_Dir/.env
|
|
|
|
|
|
|
|
|
|
Conf_Dir="$Server_Dir/conf"
|
|
|
|
|
Temp_Dir="$Server_Dir/temp"
|
|
|
|
|
Log_Dir="$Server_Dir/logs"
|
|
|
|
|
URL=${CLASH_URL}
|
|
|
|
|
|
2021-10-21 00:34:27 +08:00
|
|
|
|
# 自定义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
|
|
|
|
|
}
|
2021-01-25 19:39:21 +08:00
|
|
|
|
|
|
|
|
|
# 判断命令是否正常执行 函数
|
|
|
|
|
if_success() {
|
2023-02-16 21:28:57 +08:00
|
|
|
|
local ReturnStatus=$3
|
|
|
|
|
if [ $ReturnStatus -eq 0 ]; then
|
|
|
|
|
action "$1" /bin/true
|
|
|
|
|
else
|
|
|
|
|
action "$2" /bin/false
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2021-01-25 19:39:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 10:36:27 +08:00
|
|
|
|
# 临时取消环境变量
|
|
|
|
|
unset http_proxy
|
|
|
|
|
unset https_proxy
|
|
|
|
|
unset no_proxy
|
|
|
|
|
|
2021-10-21 00:34:27 +08:00
|
|
|
|
# 检查url是否有效
|
2023-02-16 21:28:57 +08:00
|
|
|
|
echo -e '\n正在检测订阅地址...'
|
2021-10-21 00:34:27 +08:00
|
|
|
|
Text1="Clash订阅地址可访问!"
|
|
|
|
|
Text2="Clash订阅地址不可访问!"
|
2023-02-16 18:21:55 +08:00
|
|
|
|
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
|
|
|
|
|
done
|
|
|
|
|
if_success $Text1 $Text2 $ReturnStatus
|
2021-01-25 19:39:21 +08:00
|
|
|
|
|
|
|
|
|
# 拉取更新config.yml文件
|
2023-02-16 21:28:57 +08:00
|
|
|
|
echo -e '\n正在下载Clash配置文件...'
|
2021-10-21 00:34:27 +08:00
|
|
|
|
Text3="配置文件config.yaml下载成功!"
|
|
|
|
|
Text4="配置文件config.yaml下载失败,退出启动!"
|
2023-02-16 18:21:55 +08:00
|
|
|
|
for i in {1..10}
|
|
|
|
|
do
|
2023-03-08 15:11:34 +08:00
|
|
|
|
#curl -s -o $Temp_Dir/clash.yaml $URL
|
2023-03-27 15:09:14 +08:00
|
|
|
|
wget -q --no-check-certificate -O $Temp_Dir/clash.yaml $URL
|
2023-02-16 18:21:55 +08:00
|
|
|
|
ReturnStatus=$?
|
|
|
|
|
if [ $ReturnStatus -eq 0 ]; then
|
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if_success $Text3 $Text4 $ReturnStatus
|
2021-01-25 19:39:21 +08:00
|
|
|
|
|
2021-01-25 21:28:47 +08:00
|
|
|
|
# 取出代理相关配置
|
2021-01-25 19:39:21 +08:00
|
|
|
|
sed -n '/^proxies:/,$p' $Temp_Dir/clash.yaml > $Temp_Dir/proxy.txt
|
2021-01-25 20:51:31 +08:00
|
|
|
|
|
2021-01-25 19:39:21 +08:00
|
|
|
|
# 合并形成新的config.yaml
|
2021-01-25 20:51:31 +08:00
|
|
|
|
cat $Temp_Dir/templete_config.yaml > $Temp_Dir/config.yaml
|
2021-01-25 19:39:21 +08:00
|
|
|
|
cat $Temp_Dir/proxy.txt >> $Temp_Dir/config.yaml
|
|
|
|
|
\cp $Temp_Dir/config.yaml $Conf_Dir/
|
|
|
|
|
|
2022-02-17 14:30:03 +08:00
|
|
|
|
# Configure Clash Dashboard
|
|
|
|
|
Work_Dir=$(cd $(dirname $0); pwd)
|
|
|
|
|
Dashboard_Dir="${Work_Dir}/dashboard/public"
|
|
|
|
|
sed -ri "s@^# external-ui:.*@external-ui: ${Dashboard_Dir}@g" $Conf_Dir/config.yaml
|
2023-04-06 22:45:32 +08:00
|
|
|
|
# 随机生成并更新 API Secret
|
|
|
|
|
Secret=`openssl rand -hex 32`
|
|
|
|
|
sed -r -i '/^secret: /s@(secret: ).*@\1'${Secret}'@g' $Conf_Dir/config.yaml
|
2022-02-17 14:30:03 +08:00
|
|
|
|
|
2023-02-17 21:10:39 +08:00
|
|
|
|
# 获取CPU架构
|
2023-02-18 03:02:04 +08:00
|
|
|
|
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`
|
2023-02-17 21:10:39 +08:00
|
|
|
|
else
|
|
|
|
|
echo -e "\033[31m\n[ERROR] Failed to obtain CPU architecture!\033[0m"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2021-01-25 19:39:21 +08:00
|
|
|
|
# 启动Clash服务
|
2023-02-16 21:28:57 +08:00
|
|
|
|
echo -e '\n正在启动Clash服务...'
|
2021-10-21 00:34:27 +08:00
|
|
|
|
Text5="服务启动成功!"
|
|
|
|
|
Text6="服务启动失败!"
|
2023-02-17 21:10:39 +08:00
|
|
|
|
if [[ $CpuArch =~ "x86_64" ]]; then
|
2022-01-14 23:14:50 +08:00
|
|
|
|
nohup $Server_Dir/bin/clash-linux-amd64 -d $Conf_Dir &> $Log_Dir/clash.log &
|
2023-02-16 18:21:55 +08:00
|
|
|
|
ReturnStatus=$?
|
|
|
|
|
if_success $Text5 $Text6 $ReturnStatus
|
2023-03-28 12:57:32 +08:00
|
|
|
|
elif [[ $CpuArch =~ "aarch64" || $CpuArch =~ "arm64" ]]; then
|
2023-03-28 00:13:01 +08:00
|
|
|
|
nohup $Server_Dir/bin/clash-linux-arm64 -d $Conf_Dir &> $Log_Dir/clash.log &
|
|
|
|
|
ReturnStatus=$?
|
|
|
|
|
if_success $Text5 $Text6 $ReturnStatus
|
|
|
|
|
elif [[ $CpuArch =~ "armv7" ]]; then
|
2022-01-14 23:14:50 +08:00
|
|
|
|
nohup $Server_Dir/bin/clash-linux-armv7 -d $Conf_Dir &> $Log_Dir/clash.log &
|
2023-02-16 18:21:55 +08:00
|
|
|
|
ReturnStatus=$?
|
|
|
|
|
if_success $Text5 $Text6 $ReturnStatus
|
2022-01-14 23:14:50 +08:00
|
|
|
|
else
|
2023-02-17 21:10:39 +08:00
|
|
|
|
echo -e "\033[31m\n[ERROR] Unsupported CPU Architecture!\033[0m"
|
2022-01-14 23:14:50 +08:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2021-01-25 19:39:21 +08:00
|
|
|
|
|
2022-02-17 14:30:03 +08:00
|
|
|
|
# Output Dashboard access address and Secret
|
|
|
|
|
echo ''
|
2023-04-09 15:50:25 +08:00
|
|
|
|
echo -e "Clash Dashboard 访问地址:http://<ip>:9090/ui"
|
2022-02-17 14:30:03 +08:00
|
|
|
|
echo -e "Secret:${Secret}"
|
|
|
|
|
echo ''
|
|
|
|
|
|
2021-10-21 00:34:27 +08:00
|
|
|
|
# 添加环境变量(root权限)
|
2022-12-09 16:26:48 +08:00
|
|
|
|
cat>/etc/profile.d/clash.sh<<EOF
|
|
|
|
|
# 开启系统代理
|
|
|
|
|
function proxy_on() {
|
|
|
|
|
export http_proxy=http://127.0.0.1:7890
|
|
|
|
|
export https_proxy=http://127.0.0.1:7890
|
|
|
|
|
export no_proxy=127.0.0.1,localhost
|
|
|
|
|
echo -e "\033[32m[√] 已开启代理\033[0m"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 关闭系统代理
|
|
|
|
|
function proxy_off(){
|
|
|
|
|
unset http_proxy
|
|
|
|
|
unset https_proxy
|
|
|
|
|
unset no_proxy
|
|
|
|
|
echo -e "\033[31m[×] 已关闭代理\033[0m"
|
|
|
|
|
}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
echo -e "请执行以下命令加载环境变量: source /etc/profile.d/clash.sh\n"
|
|
|
|
|
echo -e "请执行以下命令开启系统代理: proxy_on\n"
|
2023-01-31 17:07:50 +08:00
|
|
|
|
echo -e "若要临时关闭系统代理,请执行: proxy_off\n"
|