-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-v2ray-debian.sh
297 lines (222 loc) · 9.76 KB
/
install-v2ray-debian.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
# Copyright 2022 by Kawakaze
# 设置常量,路径尾部均不包含 /
# v2ray 配置文件存放路径
v2ray_config_path=/usr/local/etc/v2ray
# v2ray 日志文件存放路径
v2ray_log_path=/var/log/v2ray
# v2ray 服务文件的存放路径
v2ray_service_path=/etc/systemd/system
# 脚本存放路径
user_script_path=/etc/user_scripts
# 函数-运行前检查
check_before_running() {
# 判断脚本是否由 root 用户运行,如果不是,则报错并退出
[ "$(whoami)" == "root" ] || { echo -e "ERROR: This script must be run by root, please run \"sudo su\" before running this script.\n" ; exit 1 ; }
# 判断 /etc/debian_version 文件是否存在,如果不存在,则报错并退出
[ -f /etc/debian_version ] || { echo -e "ERROR: This system is not supported, please install Debian.\n" ; exit 1 ; }
# 检查依赖
which jq > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"jq\".\n" ; exit 1 ; }
which rm > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"rm\".\n" ; exit 1 ; }
which cat > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"cat\".\n" ; exit 1 ; }
which sed > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"sed\".\n" ; exit 1 ; }
which chmod > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"chmod\".\n" ; exit 1 ; }
which mkdir > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"mkdir\".\n" ; exit 1 ; }
which curl > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"curl\".\n" ; exit 1 ; }
which wget > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"wget\".\n" ; exit 1 ; }
which crontab > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"crontab\".\n" ; exit 1 ; }
which systemctl > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"systemctl\".\n" ; exit 1 ; }
which systemd-sysusers > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"systemd-sysusers\".\n" ; exit 1 ; }
which systemd-tmpfiles > /dev/null 2>&1 || { echo -e "ERROR: Cannot found command \"systemd-tmpfiles\".\n" ; exit 1 ; }
# 检查和 GitHub 的网络连接是否正常,如果不正常则直接退出
wget --spider --quiet --tries=3 --timeout=15 https://github.com || { echo -e "ERROR: Cannot connect to GitHub.\n" ; exit 1 ; }
# 检查是否已安装 v2ray,若是则判断是否已安装最新版
if [ -f '/usr/local/bin/v2ray' ]; then
# 获取已安装的 v2ray 版本(不适用于 v5)
v2ray_current_version="$(/usr/local/bin/v2ray -version 2> /dev/null | awk -F ' ' 'NR==1 {print $2}')"
# 如果没获取到,则重新尝试获取已安装的 v2ray 版本(只适用于 v5)
if [ -z "${v2ray_current_version}" ]; then
v2ray_current_version="$(/usr/local/bin/v2ray version 2> /dev/null | awk -F ' ' 'NR==1 {print $2}')"
fi
# 获取 v2ray 最新 release 版本号
v2ray_release_latest_version="$(curl -sS -H 'Accept: application/vnd.github+json' https://api.github.com/repos/v2fly/v2ray-core/releases/latest | jq -r .tag_name)"
# 检查已安装版本是否是最新版本,若是则提示无新版本可供更新并退出
if [ "${v2ray_current_version}" == "${v2ray_release_latest_version#v}" ]; then
echo "=================================================="
echo "No new version for update."
echo "=================================================="
exit 0
fi
fi
}
# 函数-设置 v2ray
set_v2ray() {
# 安装 v2ray,如果安装失败则直接退出
bash <(curl https://github.com/v2fly/fhs-install-v2ray/master/install-release.sh) || { echo -e "ERROR\: Install v2ray failed.\n" ; exit 1 ; }
echo -e "\n"
# 添加 systemd 专属用户配置
echo 'u v2ray - "V2Ray Service" - -' > /usr/lib/sysusers.d/v2ray.conf
# 检查 systemd 专属用户配置的 SHA256SUM 是否正确,如果错误则直接退出
echo "caca9d88eff50ce7cd695b6fcab4d253344e92c966780f7de6d5c531d48ed80e /usr/lib/sysusers.d/v2ray.conf" | sha256sum -c - || exit 1
echo -e "\n"
# 设置文件权限
chmod 644 /usr/lib/sysusers.d/v2ray.conf
# 添加 systemd 临时文件(夹)配置
echo 'd /var/log/v2ray 0700 v2ray v2ray - -' > /usr/lib/tmpfiles.d/v2ray.conf
# 检查 systemd 临时文件(夹)配置的 SHA256SUM 是否正确,如果错误则直接退出
echo "ae55077bcf7140a7460f192adb03009b4573ec4420af84f4cbe9828cf8ca8e06 /usr/lib/tmpfiles.d/v2ray.conf" | sha256sum -c - || exit 1
echo -e "\n"
# 设置文件权限
chmod 644 /usr/lib/tmpfiles.d/v2ray.conf
# 使 systemd 专属用户配置生效
# 若是首次创建 systemd 专属用户(v2ray),则额外显示空行以改善显示效果
id v2ray &> /dev/null
if [ "$?" != "0" ]; then
systemd-sysusers v2ray.conf
echo -e "\n"
else
systemd-sysusers v2ray.conf
fi
# 使 systemd 临时文件(夹)配置生效
systemd-tmpfiles --create v2ray.conf
# 修改 v2ray 服务
cat << \EOF > ${v2ray_service_path}/v2ray.service
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
LimitCORE=infinity
LimitNOFILE=12800
LimitNPROC=12800
User=v2ray
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray run -config /usr/local/etc/v2ray/config.json
Restart=on-failure
RestartPreventExitStatus=23
[Install]
WantedBy=multi-user.target
EOF
cat << \EOF > ${v2ray_service_path}/v2ray\@.service
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
LimitCORE=infinity
LimitNOFILE=12800
LimitNPROC=12800
User=v2ray
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray run -config /usr/local/etc/v2ray/%i.json
Restart=on-failure
RestartPreventExitStatus=23
[Install]
WantedBy=multi-user.target
EOF
# 设置文件权限
chmod 644 /etc/systemd/system/v2ray.service
chmod 644 /etc/systemd/system/v2ray\@.service
# 检查 v2ray 服务的 SHA256SUM 是否正确,如果错误则直接退出
echo "46241bbae6e318954b374fc08f82b1f655225a23d8123ed3ef0ebf44433b8b5a /etc/systemd/system/v2ray.service" | sha256sum -c - || exit 1
echo -e "\n"
echo '81373e66e3fbded5661f83a9e4971f5653f46f5fd6c39d4d8383fd5e287e9923 /etc/systemd/system/v2ray@.service' | sha256sum -c - || exit 1
echo -e "\n"
# 重新加载 v2ray 服务,使修改生效
systemctl daemon-reload
}
# 函数-安装 v2ray
install_v2ray() {
# 运行“函数-设置 v2ray”
set_v2ray
# 删除额外的 v2ray 服务文件
rm -rf /etc/systemd/system/v2ray.service.d/*
rm -rf /etc/systemd/system/v2ray\@.service.d/*
# 重新加载 v2ray 服务,使修改生效
systemctl daemon-reload
# 删除可能存在的权限错误的日志文件
rm -rf ${v2ray_log_path}/*
# 删除可能存在的多配置文件
rm -rf ${v2ray_config_path}/*
# 创建证书文件存放文件夹
mkdir -p ${v2ray_config_path}/cert
# 修改文件夹所有者和组
chown -R v2ray:v2ray ${v2ray_config_path}/cert
# 创建“清理 v2ray 日志”脚本的存放路径
mkdir -p ${user_script_path}/
# 创建“清理 v2ray 日志”脚本
cat << EOF > ${user_script_path}/clean_v2ray_log.sh
#!/bin/bash
source /etc/profile
# 删除现有的日志备份文件
rm -f ${v2ray_log_path}/access.log.backup
# 生成当前日志的备份
cp ${v2ray_log_path}/access.log ${v2ray_log_path}/access.log.backup
# 清空当前的日志文件
cat /dev/null > ${v2ray_log_path}/access.log
EOF
# 检查“清理 v2ray 日志”脚本的校验和是否正确,如果错误则直接退出
echo "21c30ba0085c4641ecf40928df3953e37572faab924533e9b3ad0dfa7df052b4 ${user_script_path}/clean_v2ray_log.sh" | sha256sum -c - || exit 1
echo -e "\n"
# 设置文件权限
chmod 755 ${user_script_path}/clean_v2ray_log.sh
# 导出现有的定时任务
crontab -l > crontab.temp
# 添加自动清理日志的定时任务,每个月清理一次
echo "0 5 1 * * ${user_script_path}/clean_v2ray_log.sh > /dev/null 2>&1" >> crontab.temp
# 使修改后的定时任务生效
crontab crontab.temp
# 删除临时文件
rm crontab.temp
# 提示安装完毕,并提示 v2ray 配置文件路径,以便修改
echo "=================================================="
echo "Install v2ray successful."
echo ""
echo "Config file path:"
echo "${v2ray_config_path}/config.json"
echo "=================================================="
echo ""
}
# 函数-更新 v2ray
update_v2ray() {
# 停止 v2ray
systemctl stop v2ray
# 等待 1s
sleep 1
# 运行“函数-设置 v2ray”
set_v2ray
# 等待 1s
sleep 1
# 启动 v2ray
systemctl start v2ray
# 等待 1s
sleep 1
# 显示 v2ray 运行状态
systemctl status v2ray
echo -e "\n"
# 提示 v2ray 更新完毕
echo "=================================================="
echo "Update v2ray successful."
echo "=================================================="
echo ""
}
# 函数-主函数
main() {
# 为显示内容做好准备
echo -e "\n"
# 运行“函数-运行前检查”
check_before_running
# 检查是否已安装 v2ray,如果已安装 v2ray,则运行“函数-更新 v2ray”,否则运行“函数-安装 v2ray”
which v2ray > /dev/null 2>&1
if [ "$?" == "0" ]; then
update_v2ray
else
install_v2ray
fi
}
# 运行“函数-主函数”
main