forked from wulabing/Xray_onekey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
363 lines (326 loc) · 10.6 KB
/
install.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
#!/bin/bash
#====================================================
# System Request:Debian 7+/Ubuntu 14.04+/Centos 6+
# Author: wulabing
# Dscription: V2ray ws+tls onekey
# Version: 3.0
# Blog: https://www.wulabing.com
# Official document: www.v2ray.com
#====================================================
#fonts color
Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
Font="\033[0m"
#notification information
Info="${Green}[信息]${Font}"
OK="${Green}[OK]${Font}"
Error="${Red}[错误]${Font}"
v2ray_conf_dir="/etc/v2ray"
nginx_conf_dir="/etc/nginx/conf.d"
v2ray_conf="${v2ray_conf_dir}/config.json"
nginx_conf="${nginx_conf_dir}/v2ray.conf"
#生成伪装路径
camouflage=`cat /dev/urandom | head -n 10 | md5sum | head -c 8`
source /etc/os-release
check_system(){
if [[ "${ID}" == "centos" && ${VERSION_ID} -ge 7 ]];then
echo -e "${OK} ${GreenBG} 当前系统为 Centos ${VERSION_ID} ${Font} "
INS="yum"
echo -e "${OK} ${GreenBG} SElinux 设置中,请耐心等待,不要进行其他操作${Font} "
setsebool -P httpd_can_network_connect 1
echo -e "${OK} ${GreenBG} SElinux 设置完成 ${Font} "
## Centos 也可以通过添加 epel 仓库来安装,目前不做改动
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
echo -e "${OK} ${GreenBG} Nginx rpm源 安装完成 ${Font}"
elif [[ "${ID}" == "debian" && ${VERSION_ID} -ge 8 ]];then
echo -e "${OK} ${GreenBG} 当前系统为 Debian ${VERSION_ID} ${Font} "
INS="apt"
elif [[ "${ID}" == "ubuntu" && `echo "${VERSION_ID}" | cut -d '.' -f1` -ge 16 ]];then
echo -e "${OK} ${GreenBG} 当前系统为 Ubuntu ${VERSION_ID} ${Font} "
INS="apt"
else
echo -e "${Error} ${RedBG} 当前系统为 ${ID} ${VERSION_ID} 不在支持的系统列表内,安装中断 ${Font} "
exit 1
fi
}
is_root(){
if [ `id -u` == 0 ]
then echo -e "${OK} ${GreenBG} 当前用户是root用户,进入安装流程 ${Font} "
sleep 3
else
echo -e "${Error} ${RedBG} 当前用户不是root用户,请切换到root用户后重新执行脚本 ${Font}"
exit 1
fi
}
judge(){
if [[ $? -eq 0 ]];then
echo -e "${OK} ${GreenBG} $1 完成 ${Font}"
sleep 1
else
echo -e "${Error} ${RedBG} $1 失败${Font}"
exit 1
fi
}
ntpdate_install(){
if [[ "${ID}" == "centos" ]];then
${INS} install ntpdate -y
else
${INS} update
${INS} install ntpdate -y
fi
judge "安装 NTPdate 时间同步服务 "
}
time_modify(){
ntpdate_install
systemctl stop ntp &>/dev/null
echo -e "${Info} ${GreenBG} 正在进行时间同步 ${Font}"
ntpdate time.nist.gov
if [[ $? -eq 0 ]];then
echo -e "${OK} ${GreenBG} 时间同步成功 ${Font}"
echo -e "${OK} ${GreenBG} 当前系统时间 `date -R`(请注意时区间时间换算,换算后时间误差应为三分钟以内)${Font}"
sleep 1
else
echo -e "${Error} ${RedBG} 时间同步失败,请检查ntpdate服务是否正常工作 ${Font}"
fi
}
dependency_install(){
${INS} install wget curl lsof -y
if [[ "${ID}" == "centos" ]];then
${INS} -y install crontabs
else
${INS} install cron
fi
judge "安装 crontab"
${INS} install net-tools -y
judge "安装 net-tools"
${INS} install bc -y
judge "安装 bc"
${INS} install unzip -y
judge "安装 unzip"
}
port_alterid_set(){
stty erase '^H' && read -p "请输入连接端口(default:443):" port
[[ -z ${port} ]] && port="443"
stty erase '^H' && read -p "请输入alterID(default:64):" alterID
[[ -z ${alterID} ]] && alterID="64"
}
modify_port_UUID(){
let PORT=$RANDOM+10000
UUID=$(cat /proc/sys/kernel/random/uuid)
sed -i "/\"port\"/c \ \"port\":${PORT}," ${v2ray_conf}
sed -i "/\"id\"/c \\\t \"id\":\"${UUID}\"," ${v2ray_conf}
sed -i "/\"alterId\"/c \\\t \"alterId\":${alterID}" ${v2ray_conf}
sed -i "/\"path\"/c \\\t \"path\":\"\/${camouflage}\/\"" ${v2ray_conf}
}
modify_nginx(){
## sed 部分地方 适应新配置修正
sed -i "1,/listen/{s/listen 443 ssl;/listen ${port} ssl;/}" ${v2ray_conf}
sed -i "/server_name/c \\\tserver_name ${domain};" ${nginx_conf}
sed -i "/location/c \\\tlocation \/${camouflage}\/" ${nginx_conf}
sed -i "/proxy_pass/c \\\tproxy_pass http://127.0.0.1:${PORT};" ${nginx_conf}
sed -i "/return/c \\\treturn 301 https://${domain}\$request_uri;" ${nginx_conf}
sed -i "/keepalive_timeout/i \\\tproxy_intercept_errors on;" /etc/nginx/nginx.conf
}
web_camouflage(){
##请注意 这里和LNMP脚本的默认路径冲突,千万不要在安装了LNMP的环境下使用本脚本,否则后果自负
rm -rf /home/wwwroot && mkdir -p /home/wwwroot && cd /home/wwwroot
git clone https://github.com/wulabing/sCalc.git
judge "web 站点伪装"
}
v2ray_install(){
if [[ -d /root/v2ray ]];then
rm -rf /root/v2ray
fi
mkdir -p /root/v2ray && cd /root/v2ray
wget --no-check-certificate https://install.direct/go.sh
## wget http://install.direct/go.sh
if [[ -f go.sh ]];then
bash go.sh --force
judge "安装 V2ray"
else
echo -e "${Error} ${RedBG} V2ray 安装文件下载失败,请检查下载地址是否可用 ${Font}"
exit 4
fi
}
nginx_install(){
${INS} install nginx -y
if [[ -d /etc/nginx ]];then
echo -e "${OK} ${GreenBG} nginx 安装完成 ${Font}"
sleep 2
else
echo -e "${Error} ${RedBG} nginx 安装失败 ${Font}"
exit 5
fi
}
ssl_install(){
if [[ "${ID}" == "centos" ]];then
${INS} install socat nc -y
else
${INS} install socat netcat -y
fi
judge "安装 SSL 证书生成脚本依赖"
curl https://get.acme.sh | sh
judge "安装 SSL 证书生成脚本"
}
domain_check(){
stty erase '^H' && read -p "请输入你的域名信息(eg:www.wulabing.com):" domain
domain_ip=`ping ${domain} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
echo -e "${OK} ${GreenBG} 正在获取 公网ip 信息,请耐心等待 ${Font}"
local_ip=`curl -4 ip.sb`
echo -e "域名dns解析IP:${domain_ip}"
echo -e "本机IP: ${local_ip}"
sleep 2
if [[ $(echo ${local_ip}|tr '.' '+'|bc) -eq $(echo ${domain_ip}|tr '.' '+'|bc) ]];then
echo -e "${OK} ${GreenBG} 域名dns解析IP 与 本机IP 匹配 ${Font}"
sleep 2
else
echo -e "${Error} ${RedBG} 域名dns解析IP 与 本机IP 不匹配 是否继续安装?(y/n)${Font}" && read install
case $install in
[yY][eE][sS]|[yY])
echo -e "${GreenBG} 继续安装 ${Font}"
sleep 2
;;
*)
echo -e "${RedBG} 安装终止 ${Font}"
exit 2
;;
esac
fi
}
port_exist_check(){
if [[ 0 -eq `lsof -i:"$1" | wc -l` ]];then
echo -e "${OK} ${GreenBG} $1 端口未被占用 ${Font}"
sleep 1
else
echo -e "${Error} ${RedBG} 检测到 $1 端口被占用,以下为 $1 端口占用信息 ${Font}"
lsof -i:"$1"
echo -e "${OK} ${GreenBG} 5s 后将尝试自动 kill 占用进程 ${Font}"
sleep 5
lsof -i:"$1" | awk '{print $2}'| grep -v "PID" | xargs kill -9
echo -e "${OK} ${GreenBG} kill 完成 ${Font}"
sleep 1
fi
}
acme(){
~/.acme.sh/acme.sh --issue -d ${domain} --standalone -k ec-256 --force
if [[ $? -eq 0 ]];then
echo -e "${OK} ${GreenBG} SSL 证书生成成功 ${Font}"
sleep 2
~/.acme.sh/acme.sh --installcert -d ${domain} --fullchainpath /etc/v2ray/v2ray.crt --keypath /etc/v2ray/v2ray.key --ecc
if [[ $? -eq 0 ]];then
echo -e "${OK} ${GreenBG} 证书配置成功 ${Font}"
sleep 2
fi
else
echo -e "${Error} ${RedBG} SSL 证书生成失败 ${Font}"
exit 1
fi
}
v2ray_conf_add(){
cat>${v2ray_conf_dir}/config.json<<EOF
{
"inbound": {
"port": 10000,
"listen":"127.0.0.1",
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "b831381d-6324-4d53-ad4f-8cda48b30811",
"alterId": 64
}
]
},
"streamSettings":{
"network":"ws",
"wsSettings": {
"path": "/ray/"
}
}
},
"outbound": {
"protocol": "freedom",
"settings": {}
}
}
EOF
modify_port_UUID
judge "V2ray 配置修改"
}
nginx_conf_add(){
touch ${nginx_conf_dir}/v2ray.conf
cat>${nginx_conf_dir}/v2ray.conf<<EOF
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/v2ray/v2ray.crt;
ssl_certificate_key /etc/v2ray/v2ray.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name serveraddr.com;
index index.html index.htm;
root /home/wwwroot/sCalc;
error_page 400 = /400.html;
location /ray/
{
proxy_redirect off;
proxy_pass http://127.0.0.1:10000;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$http_host;
}
}
server {
listen 80;
server_name serveraddr.com;
return 301 https://use.shadowsocksr.win\$request_uri;
}
EOF
modify_nginx
judge "Nginx 配置修改"
}
start_process_systemd(){
### nginx服务在安装完成后会自动启动。需要通过restart或reload重新加载配置
systemctl restart nginx
judge "Nginx 启动"
systemctl start v2ray
judge "V2ray 启动"
}
show_information(){
clear
echo -e "${OK} ${Green} V2ray+ws+tls 安装成功 "
echo -e "${Red} V2ray 配置信息 ${Font}"
echo -e "${Red} 地址(address):${Font} ${domain} "
echo -e "${Red} 端口(port):${Font} ${port} "
echo -e "${Red} 用户id(UUID):${Font} ${UUID}"
echo -e "${Red} 额外id(alterId):${Font} ${alterID}"
echo -e "${Red} 加密方式(security):${Font} 自适应 "
echo -e "${Red} 传输协议(network):${Font} ws "
echo -e "${Red} 伪装类型(type):${Font} none "
echo -e "${Red} 伪装域名(不要落下/):${Font} /${camouflage}/ "
echo -e "${Red} 底层传输安全:${Font} tls "
}
main(){
is_root
check_system
time_modify
dependency_install
domain_check
port_alterid_set
v2ray_install
port_exist_check 80
port_exist_check ${port}
ssl_install
acme
nginx_install
v2ray_conf_add
nginx_conf_add
web_camouflage
show_information
start_process_systemd
}
main