forked from LopSdir/clash-for-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·146 lines (123 loc) · 3.85 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# 获取脚本工作目录绝对路径
export CLASH_TOP_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
# 加载env变量文件
source ${CLASH_TOP_DIR}/env.conf
# 添加可执行权限
chmod +x ${CLASH_TOP_DIR}/bin/*
chmod +x ${CLASH_TOP_DIR}/scripts/*
chmod +x ${CLASH_TOP_DIR}/tools/subconverter/subconverter
CONF_DIR="${CLASH_TOP_DIR}/conf"
Temp_Dir="${CLASH_TOP_DIR}/temp"
LOG_DIR="${CLASH_TOP_DIR}/logs"
# 将 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 2)}
# 自定义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 exe_result=$3
if [ $exe_result -eq 0 ]; then
action "$1" /bin/true
else
action "$2" /bin/false
exit 1
fi
}
## 获取CPU架构信息
# Source the script to get CPU architecture
source ${CLASH_TOP_DIR}/scripts/get_cpu_arch.sh
# Check if we obtained CPU architecture
if [[ -z "${CPU_ARCH}" ]]; then
echo "Failed to obtain CPU architecture"
exit 1
fi
# 临时取消环境变量
unset http_proxy
unset https_proxy
unset no_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset NO_PROXY
# 拉取更新config.yml文件
echo -e '\n正在下载Clash配置文件...'
Text1="yaml配置文件下载成功!"
Text2="yaml配置文件下载失败,退出启动!"
CONF_XML=config.yaml
CONF_XML_TMP=config_temp.yaml
CONF_XML_SUBCONVERT_TMP=config_subconvert_temp.yaml
curl -L -k -sS --retry 5 -m 10 -o ${CONF_DIR}/${CONF_XML_TMP} $URL
exe_result=$?
if [ $exe_result -ne 0 ]; then
# 如果使用curl下载失败,尝试使用wget进行下载
for i in {1..3}
do
wget -q --no-check-certificate -O ${CONF_DIR}/${CONF_XML_TMP} $URL
exe_result=$?
if [ $exe_result -eq 0 ]; then
break
else
continue
fi
done
fi
if_success $Text1 $Text2 $exe_result
## 判断config xml是否符合clash配置文件标准
if [[ ${CPU_ARCH} =~ "x86_64" || ${CPU_ARCH} =~ "amd64" ]]; then
echo -e '\n判断订阅内容是否符合clash配置文件标准:'
source ${CLASH_TOP_DIR}/scripts/clash_profile_conversion.sh
sleep 3
fi
# Configure Clash Dashboard
dashboard="${CLASH_TOP_DIR}/dashboard/public"
sed -ri "s@^# external-ui:.*@external-ui: ${dashboard}@g" ${CONF_DIR}/${CONF_XML}
sed -r -i '/^secret: /s@(secret: ).*@\1'${Secret}'@g' ${CONF_DIR}/${CONF_XML}
sed -ri 's/port:.*$/port: 7898/g' ${CONF_DIR}/${CONF_XML}
sed -ri 's/socks-port:.*$/socks-port: 7899/g' ${CONF_DIR}/${CONF_XML}
sed -ri 's/redir-port:.*$/redir-port: 7900/g' ${CONF_DIR}/${CONF_XML}
## 启动Clash服务
echo -e '\n正在启动Clash服务...'
Text5="服务启动成功!"
Text6="服务启动失败!"
if [[ ${CPU_ARCH} =~ "x86_64" || ${CPU_ARCH} =~ "amd64" ]]; then
${CLASH_TOP_DIR}/bin/clash-linux-amd64-v1.8.0 -d ${CONF_DIR}
exe_result=$?
if_success $Text5 $Text6 $exe_result
elif [[ ${CPU_ARCH} =~ "aarch64" || ${CPU_ARCH} =~ "arm64" ]]; then
nohup ${CLASH_TOP_DIR}/bin/clash-linux-arm64 -d ${CONF_DIR} &> ${LOG_DIR}/clash.log &
exe_result=$?
if_success $Text5 $Text6 $exe_result
elif [[ ${CPU_ARCH} =~ "armv7" ]]; then
nohup ${CLASH_TOP_DIR}/bin/clash-linux-armv7 -d ${CONF_DIR} &> ${LOG_DIR}/clash.log &
exe_result=$?
if_success $Text5 $Text6 $exe_result
else
echo -e "\033[31m\n[ERROR] Unsupported CPU Architecture!\033[0m"
exit 1
fi
# Output Dashboard access address and Secret
echo ''
echo -e "Clash Dashboard url: http://127.0.0.1:9090/ui"
echo -e "Secret: ${Secret}"
echo ''