forked from Jrohy/multi-v2ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-v2ray.sh
378 lines (305 loc) · 11.2 KB
/
multi-v2ray.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
# Author: Jrohy
# github: https://github.com/Jrohy/multi-v2ray
#定时任务北京执行时间(0~23)
BEIJING_UPDATE_TIME=3
#记录最开始运行脚本的路径
BEGIN_PATH=$(pwd)
#安装方式0: 全新安装, 1:保留配置更新 , 2:仅更新multi-v2ray源码
INSTARLL_WAY=0
#定义操作变量, 0为否, 1为是
HELP=0
REMOVE=0
FORCE=0
IS_LATEST=0
UPDATE_VERSION=""
APP_PATH="/usr/local/multi-v2ray"
#Centos 临时取消别名
[[ -f /etc/redhat-release && -z $(echo $SHELL|grep zsh) ]] && unalias -a
[[ -z $(echo $SHELL|grep zsh) ]] && ENV_FILE=".bashrc" || ENV_FILE=".zshrc"
#######color code########
RED="31m" # Error message
GREEN="32m" # Success message
YELLOW="33m" # Warning message
BLUE="36m" # Info message
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${@:2}\033[0m"
}
#######get params#########
while [[ $# > 0 ]];do
key="$1"
case $key in
--remove)
REMOVE=1
;;
-h|--help)
HELP=1
;;
-f|--force)
FORCE=1
;;
-v|--version)
UPDATE_VERSION="$2"
echo -e "更新multi-v2ray到 $(colorEcho ${BLUE} $UPDATE_VERSION) 版本\n"
shift
;;
-k|--keep)
INSTARLL_WAY=1
colorEcho ${BLUE} "当前以keep保留配置文件形式更新, 若失败请用全新安装\n"
;;
-c|--code)
INSTARLL_WAY=2
colorEcho ${BLUE} "当前仅更新multi-v2ray源码\n"
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
#############################
checkUpdate(){
LASTEST_VERSION=$(curl -H 'Cache-Control: no-cache' -s "https://api.github.com/repos/Jrohy/multi-v2ray/releases/latest" | grep 'tag_name' | cut -d\" -f4)
if [[ -e /usr/local/bin/v2ray ]];then
VERSION_TEMP_VALUE=$(cat /usr/local/bin/v2ray|grep SHELL_V2RAY|awk 'NR==1'|sed 's/\"//g')
if [[ ! -z $VERSION_TEMP_VALUE ]]; then
CURRENT_VERSION=${VERSION_TEMP_VALUE/*=}
if [[ ! -z $UPDATE_VERSION && $UPDATE_VERSION == $CURRENT_VERSION ]];then
echo -e "multi-v2ray当前版本: $(colorEcho $GREEN $CURRENT_VERSION), 已是指定版本!!!"
IS_LATEST=1
return
fi
if [[ -z $UPDATE_VERSION && $FORCE == 0 && $INSTARLL_WAY != 0 && $LASTEST_VERSION == $CURRENT_VERSION ]]; then
echo -e "multi-v2ray当前版本: $(colorEcho $GREEN $CURRENT_VERSION), 已是最新!!!"
IS_LATEST=1
return
fi
fi
fi
[[ -z $UPDATE_VERSION ]] && UPDATE_VERSION=$LASTEST_VERSION
}
help(){
echo "source multi-v2ray.sh [-h|--help] [-k|--keep] [-c|--code] [-f|--force] [--remove]"
echo " -h, --help Show help"
echo " -k, --keep keep the v2ray config.json to update"
echo " -c, --code only update multi-v2ray code"
echo " -f, --force force to update multi-v2ray lastest code"
echo " -v, --version update multi-v2ray to special version"
echo " --remove remove v2ray && multi-v2ray"
echo " no params to new install"
return 0
}
removeV2Ray() {
#卸载V2ray官方脚本
systemctl stop v2ray >/dev/null 2>&1
systemctl disable v2ray >/dev/null 2>&1
service v2ray stop >/dev/null 2>&1
update-rc.d -f v2ray remove >/dev/null 2>&1
rm -rf /etc/v2ray/ >/dev/null 2>&1
rm -rf /usr/bin/v2ray >/dev/null 2>&1
rm -rf /var/log/v2ray/ >/dev/null 2>&1
rm -rf /lib/systemd/system/v2ray.service >/dev/null 2>&1
rm -rf /etc/init.d/v2ray >/dev/null 2>&1
#卸载multi-v2ray
rm -rf $APP_PATH >/dev/null 2>&1
rm -rf /etc/bash_completion.d/v2ray.bash >/dev/null 2>&1
rm -rf /usr/local/bin/v2ray >/dev/null 2>&1
#删除v2ray定时更新任务
crontab -l|sed '/SHELL=/d;/v2ray/d' > crontab.txt
crontab crontab.txt >/dev/null 2>&1
rm -f crontab.txt >/dev/null 2>&1
if [[ ${OS} == 'CentOS' || ${OS} == 'Fedora' ]];then
service crond restart >/dev/null 2>&1
else
service cron restart >/dev/null 2>&1
fi
#删除multi-v2ray环境变量
sed -i '/v2ray/d' ~/$ENV_FILE
source ~/$ENV_FILE
colorEcho ${GREEN} "卸载完成!"
}
closeSELinux() {
#禁用SELinux
if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
}
checkSys() {
#检查是否为Root
[ $(id -u) != "0" ] && { colorEcho ${RED} "Error: You must be root to run this script"; exit 1; }
#检查系统信息
if [[ -e /etc/redhat-release ]];then
if [[ $(cat /etc/redhat-release | grep Fedora) ]];then
OS='Fedora'
PACKAGE_MANAGER='dnf'
else
OS='CentOS'
PACKAGE_MANAGER='yum'
fi
elif [[ $(cat /etc/issue | grep Debian) ]];then
OS='Debian'
PACKAGE_MANAGER='apt-get'
elif [[ $(cat /etc/issue | grep Ubuntu) ]];then
OS='Ubuntu'
PACKAGE_MANAGER='apt-get'
else
colorEcho ${RED} "Not support OS, Please reinstall OS and retry!"
exit 1
fi
}
#安装依赖
installDependent(){
if [[ ${OS} == 'CentOS' || ${OS} == 'Fedora' ]];then
${PACKAGE_MANAGER} install wget unzip git ntp ntpdate socat crontabs lsof -y
else
${PACKAGE_MANAGER} update
${PACKAGE_MANAGER} install wget unzip git ntp ntpdate socat cron lsof -y
fi
#install python3 & pip3
bash <(curl -sL https://git.io/fhqMz)
pip3 install pyopenssl
}
#设置定时升级任务
planUpdate(){
#计算北京时间早上3点时VPS的实际时间
ORIGIN_TIME_ZONE=$(date -R|awk '{printf"%d",$6}')
LOCAL_TIME_ZONE=${ORIGIN_TIME_ZONE%00}
BEIJING_ZONE=8
DIFF_ZONE=$[$BEIJING_ZONE-$LOCAL_TIME_ZONE]
LOCAL_TIME=$[$BEIJING_UPDATE_TIME-$DIFF_ZONE]
if [ $LOCAL_TIME -lt 0 ];then
LOCAL_TIME=$[24+$LOCAL_TIME]
elif [ $LOCAL_TIME -ge 24 ];then
LOCAL_TIME=$[$LOCAL_TIME-24]
fi
colorEcho ${BLUE} "北京时间${BEIJING_UPDATE_TIME}点,VPS时间为${LOCAL_TIME}点\n"
OLD_CRONTAB=$(crontab -l)
echo "SHELL=/bin/bash" >> crontab.txt
echo "${OLD_CRONTAB}" >> crontab.txt
echo "0 ${LOCAL_TIME} * * * bash <(curl -L -s https://install.direct/go.sh) | tee -a /root/v2rayUpdate.log && service v2ray restart" >> crontab.txt
crontab crontab.txt
sleep 1
if [[ ${OS} == 'CentOS' || ${OS} == 'Fedora' ]];then
service crond restart
else
service cron restart
fi
rm -f crontab.txt
colorEcho ${GREEN} "成功配置每天北京时间${BEIJING_UPDATE_TIME}点自动升级V2ray内核任务\n"
}
updateProject() {
local DOMAIN=""
if [[ -e $APP_PATH/my_domain ]];then
DOMAIN=$(cat $APP_PATH/my_domain|awk 'NR==1')
elif [[ -e $APP_PATH/multi-v2ray.conf ]];then
TEMP_VALUE=$(cat $APP_PATH/multi-v2ray.conf|grep domain|awk 'NR==1')
DOMAIN=${TEMP_VALUE/*=}
fi
cd /usr/local/
if [[ -e multi-v2ray && -e multi-v2ray/.git ]];then
cd multi-v2ray
COMMITTER_EMAIL=$(git log -1 --pretty=format:"%ae")
if [[ $COMMITTER_EMAIL =~ 'euvkzx' ]];then
git reset --hard HEAD && git clean -d -f
if [[ $FORCE == 1 ]]; then
git pull origin master
else
git fetch origin && git checkout $UPDATE_VERSION
fi
else
cd /usr/local/
rm -rf multi-v2ray
git clone https://github.com/Jrohy/multi-v2ray
cd multi-v2ray && git checkout $UPDATE_VERSION
fi
else
git clone https://github.com/Jrohy/multi-v2ray
cd multi-v2ray && git checkout $UPDATE_VERSION
fi
[[ ! -z $DOMAIN ]] && sed -i "s/^domain.*/domain=${DOMAIN}/g" $APP_PATH/multi-v2ray.conf
#更新v2ray bash_completion脚本
cp -f $APP_PATH/v2ray.bash /etc/bash_completion.d/
[[ -z $(echo $SHELL|grep zsh) ]] && source /etc/bash_completion.d/v2ray.bash
#安装/更新V2ray主程序
[[ ${INSTARLL_WAY} != 2 ]] && bash <(curl -L -s https://install.direct/go.sh)
}
#时间同步
timeSync() {
if [[ ${INSTARLL_WAY} == 0 ]];then
systemctl stop ntp &>/dev/null
echo -e "${Info} 正在进行时间同步 ${Font}"
ntpdate time.nist.gov
if [[ $? -eq 0 ]];then
echo -e "${OK} 时间同步成功 ${Font}"
echo -e "${OK} 当前系统时间 `date -R`${Font}"
sleep 1
else
echo -e "${Error} 时间同步失败,可以手动执行命令同步:${Font}${Yellow}ntpdate time.nist.gov${Font}"
fi
fi
}
profileInit() {
rm -f /usr/local/bin/v2ray >/dev/null 2>&1
#配置V2ray初始环境
cp -f $APP_PATH/v2ray /usr/local/bin
chmod +x /usr/local/bin/v2ray
#加入multi-v2ray模块搜索路径
[[ -z $(grep multi-v2ray ~/$ENV_FILE) ]] && echo "export PYTHONPATH=$PYTHONPATH:$APP_PATH" >> ~/$ENV_FILE && source ~/$ENV_FILE
# 加入v2ray tab补全环境变量
[[ -z $(echo $SHELL|grep zsh) && -z $(grep v2ray.bash ~/$ENV_FILE) ]] && echo "source /etc/bash_completion.d/v2ray.bash" >> ~/$ENV_FILE && source ~/$ENV_FILE
#解决Python3中文显示问题
[[ -z $(grep PYTHONIOENCODING=utf-8 ~/$ENV_FILE) ]] && echo "export PYTHONIOENCODING=utf-8" >> ~/$ENV_FILE && source ~/$ENV_FILE
#全新安装的新配置
if [[ ${INSTARLL_WAY} == 0 ]];then
rm -rf /etc/v2ray/config.json
cp $APP_PATH/json_template/server.json /etc/v2ray/config.json
#产生随机uuid
UUID=$(cat /proc/sys/kernel/random/uuid)
sed -i "s/cc4f8d5b-967b-4557-a4b6-bde92965bc27/${UUID}/g" /etc/v2ray/config.json
#产生随机端口
D_PORT=$(shuf -i 1000-65535 -n 1)
sed -i "s/999999999/${D_PORT}/g" /etc/v2ray/config.json
#产生默认配置mkcp+随机3种伪装类型type
python3 -c "from config_modify import stream; stream.StreamModifier().random_kcp();"
python3 $APP_PATH/client.py
else
python3 $APP_PATH/converter.py
fi
bash $APP_PATH/global_setting/clean_iptables.sh
echo ""
echo -e "生成 $(colorEcho $BLUE iptables) 流量统计规则中.."
python3 -c "from utils import open_port; open_port();"
}
installFinish() {
#回到原点
cd ${BEGIN_PATH}
[[ ${INSTARLL_WAY} == 0 ]] && WAY="安装" || WAY="更新"
colorEcho ${GREEN} "multi-v2ray ${WAY}成功!\n"
clear
echo "V2ray配置信息:"
#安装完后显示v2ray的配置信息,用于快速部署
python3 -c "from loader import Loader; print(Loader().profile);"
echo -e "输入 v2ray 回车即可进行服务管理\n"
}
main() {
[[ ${HELP} == 1 ]] && help && return
[[ ${REMOVE} == 1 ]] && removeV2Ray && return
[[ ${FORCE} == 1 ]] && colorEcho ${BLUE} "当前为强制更新模式, 会更新到master最新代码\n"
checkUpdate && [[ $IS_LATEST == 1 ]] && return
[[ ${INSTARLL_WAY} == 0 ]] && colorEcho ${BLUE} "当前为全新安装\n"
if [[ ${INSTARLL_WAY} != 2 ]];then
checkSys
installDependent
closeSELinux
timeSync
#设置定时任务
[[ -z $(crontab -l|grep v2ray) ]] && planUpdate
fi
updateProject
profileInit
service v2ray restart
installFinish
}
main