forked from testiknn/opt-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2ray.sh
579 lines (517 loc) · 15.5 KB
/
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
version=v4.4
[ ! -z "$( alias | grep 'alias cp=')" ] && unalias cp
[ ! -z "$( alias | grep 'alias mv=')" ] && unalias mv
[ ! -z "$( alias | grep 'alias rm=')" ] && unalias rm
SYSTEMCTL_CMD=$(command -v systemctl)
SERVICE_CMD=$(command -v service)
#Check Root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
#Check OS
if [ -f /etc/redhat-release ];then
OS='CentOS'
elif [ ! -z "`cat /etc/issue | grep bian`" ];then
OS='Debian'
elif [ ! -z "`cat /etc/issue | grep Ubuntu`" ];then
OS='Ubuntu'
elif [ ! -z "`cat /etc/issue | grep CentOS`" ];then
OS='CentOS'
else
echo "Not support OS, Please reinstall OS and retry!"
exit 1
fi
# Get Public IP address
ipc=$(ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1)
if [[ "$IP" = "" ]]; then
ipc=$(wget -qO- -t1 -T2 ipv4.icanhazip.com)
fi
uuid=$(cat /proc/sys/kernel/random/uuid)
function Install(){
#Install Basic Packages
echo '安装基本软件包,请稍候!'
if [[ ${OS} == 'CentOS' ]];then
yum install curl wget unzip ntp ntpdate -y
else
apt-get update
apt-get install curl unzip ntp wget ntpdate -y
fi
#Set DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
#Update NTP settings
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate us.pool.ntp.org
#Disable 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
#Run Install
cd /root
curl -L -k https://opt.cn2qq.com/opt-script/go.sh > /root/go.sh
if [ ! -s /root/go.sh ]; then
rm -f /root/go.sh
wget --no-check-certificate https://opt.cn2qq.com/opt-script/go.sh
fi
chmod +x "/root/go.sh"
echo "V2Ray 安装 $version"
/root/go.sh --version $version
rm -f /root/v2ray_server_json
ln -sf /etc/v2ray /root/v2ray_server_json
echo "安装完成"
}
function remove_v2ray(){
echo '删除 V2ray 请稍候!'
cd /root
curl -L -k https://opt.cn2qq.com/opt-script/go.sh > /root/go.sh
if [ ! -s /root/go.sh ]; then
rm -f /root/go.sh
wget --no-check-certificate https://opt.cn2qq.com/opt-script/go.sh
fi
chmod +x "/root/go.sh"
/root/go.sh --remove
echo '删除 V2ray 完成!'
}
function up_v2ray(){
if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ] || [ -f "/etc/systemd/system/v2ray.service" ]; then
killall keey.sh
${SYSTEMCTL_CMD} stop v2ray
systemctl disable v2ray.service
fi
if [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
killall keey.sh
${SERVICE_CMD} v2ray stop
fi
curl -L -s -k https://opt.cn2qq.com/opt-script/go.sh > /root/go.sh
if [ ! -s /root/go.sh ]; then
rm -f /root/go.sh
wget --no-check-certificate https://opt.cn2qq.com/opt-script/go.sh
fi
chmod +x "/root/go.sh"
echo "V2Ray 安装 $version"
/root/go.sh --version $version
echo "安装完成"
ntpdate us.pool.ntp.org &
if [ -f "/etc/v2ray/config.back0" ]; then
cp -f /etc/v2ray/config.back0 /etc/v2ray/config.json
if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ] || [ -f "/etc/systemd/system/v2ray.service" ]; then
echo "Restarting V2Ray service."
systemctl enable v2ray.service
${SYSTEMCTL_CMD} start v2ray
elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
echo "Restarting V2Ray service."
${SERVICE_CMD} v2ray start
fi
echo '##########################################################################'
rm -f /root/v2ray_config.pb
/usr/bin/v2ray/v2ctl config < /root/config.json > /root/v2ray_config.pb
[ -f /root/v2ray_config.pb ] && echo "已经生成 pb 配置文件 /root/v2ray_config.pb"
echo '##########################################################################'
exit
else
echo "未完成配置生成,请继续配置"
fi
}
echo 'V2Ray 输入数字继续一键安装'
while :; do echo
read -p "输入数字继续(【新安装或重新生成配置】请输入1,【更新V2Ray】请输入0,【删除V2Ray】请输入3):" up_vv
if [[ ! $up_vv =~ ^[0-1]$ ]]; then
if [[ $up_vv == 3 ]]; then
remove_v2ray
exit
fi
echo "${CWARNING}输入错误! 请输入正确的数字!${CEND}"
else
break
fi
done
echo ''
if [[ $up_vv == '0' ]];then
up_v2ray
fi
#clear
echo 'V2Ray 一键安装|配置脚本 Author:Kirito && 雨落无声'
echo ''
echo '此脚本会关闭iptables防火墙(可选项),切勿用于生产环境!'
while :; do echo
read -p "输入用户等级(自用请输入1,共享请输入0):" level
if [[ ! $level =~ ^[0-1]$ ]]; then
echo "${CWARNING}输入错误! 请输入正确的数字!${CEND}"
else
break
fi
done
echo ''
SEED=`tr -cd 0-9 </dev/urandom | head -c 8`
mainport_x=`echo $SEED 31000 33000|awk '{srand($1);printf "%d",rand()*10000%($3-$2)+$2}'`
#32000
read -p "输入主要端口(默认:$mainport_x ):" mainport
[ -z "$mainport" ] && mainport=$mainport_x
echo ''
read -p "是否启用HTTP伪装?(默认开启y) [y/n]:" ifhttpheader
[ -z "$ifhttpheader" ] && ifhttpheader='y'
if [[ $ifhttpheader == 'y' ]];then
httpheader=',
"streamSettings": {
"network": "tcp",
"tcpSettings": {
"connectionReuse": true,
"header": {
"type": "http",
"request": {
"version": "1.1",
"method": "GET",
"path": ["/"],
"headers": {
"Host": ["www.163.com", "www.sogou.com"],
"User-Agent": [
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46"
],
"Accept-Encoding": ["gzip, deflate"],
"Connection": ["keep-alive"],
"Pragma": "no-cache"
}
},
"response": {
"version": "1.1",
"status": "200",
"reason": "OK",
"headers": {
"Content-Type": ["application/octet-stream", "application/x-msdownload", "text/html", "application/x-shockwave-flash"],
"Transfer-Encoding": ["chunked"],
"Connection": ["keep-alive"],
"Pragma": "no-cache"
}
}
}
}
}'
else
httpheader=''
read -p "是否启用mKCP协议?(默认开启y) [y/n]:" ifmkcp
[ -z "$ifmkcp" ] && ifmkcp='y'
if [[ $ifmkcp == 'y' ]];then
mkcp=',
"streamSettings": {
"network": "kcp"
}'
else
mkcp=''
fi
fi
echo ''
read -p "是否启用动态端口?(默认开启y) [y/n]:" ifdynamicport
[ -z "$ifdynamicport" ] && ifdynamicport='y'
if [[ $ifdynamicport == 'y' ]];then
subport1_x=$(($mainport_x + 1))
#32001
read -p "输入数据端口起点(默认:$subport1_x ):" subport1
[ -z "$subport1" ] && subport1=$subport1_x
subport2_x=$(($mainport_x + 1500))
#32500
read -p "输入数据端口终点(默认:$subport2_x ):" subport2
[ -z "$subport2" ] && subport2=$subport2_x
SEED=`tr -cd 0-9 </dev/urandom | head -c 8`
portnum_x=`echo $SEED 8 15|awk '{srand($1);printf "%d",rand()*10000%($3-$2)+$2}'`
#10
read -p "输入每次开放端口数(默认:$portnum_x ):" portnum
[ -z "$portnum" ] && portnum=$portnum_x
SEED=`tr -cd 0-9 </dev/urandom | head -c 8`
porttime_x=`echo $SEED 4 7|awk '{srand($1);printf "%d",rand()*10000%($3-$2)+$2}'`
#5
read -p "输入端口变更时间(单位:分钟)(默认:$porttime_x ):" porttime
[ -z "$porttime" ] && porttime=$porttime_x
dynamicport="
\"inboundDetour\": [
{
\"protocol\": \"vmess\",
\"port\": \"$subport1-$subport2\",
\"tag\": \"detour\",
\"settings\": {},
\"allocate\": {
\"strategy\": \"random\",
\"concurrency\": $portnum,
\"refresh\": $porttime
}${mkcp}${httpheader}
}
],
"
else
dynamicport=''
fi
echo ''
read -p "是否启用 Mux.Cool?(默认开启y) [y/n]:" ifmux
[ -z "$ifmux" ] && ifmux='y'
if [[ $ifmux == 'y' ]];then
mux=',
"mux": {
"enabled": true
}
'
else
mux=""
fi
while :; do echo
echo '1. HTTP代理(默认1)'
echo '2. Socks代理'
read -p "请选择客户端代理类型: " chooseproxytype
[ -z "$chooseproxytype" ] && chooseproxytype=1
if [[ ! $chooseproxytype =~ ^[1-2]$ ]]; then
echo '输入错误,请输入正确的数字!'
else
break
fi
done
if [[ $chooseproxytype == 1 ]];then
proxytype='http'
else
proxytype='socks'
fi
#CheckIfInstalled
if [ ! -f "/usr/bin/v2ray/v2ray" ]; then
Install
fi
read -p "是否关闭iptables防火墙?(默认关闭y) [y/n]:" ifoffiptables
[ -z "$ifoffiptables" ] && ifoffiptables='y'
if [[ $ifoffiptables == 'y' ]];then
#Disable iptables
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
else
if [[ $ifdynamicport == 'y' ]];then
#$subport1:$subport2
iptables -I INPUT -p tcp --dport $subport1:$subport2 -j ACCEPT
iptables -I FORWARD -p tcp --dport $subport1:$subport2 -j ACCEPT
iptables -I OUTPUT -p tcp --dport $subport1:$subport2 -j ACCEPT
iptables -I INPUT -p tcp --sport $subport1:$subport2 -j ACCEPT
iptables -I FORWARD -p tcp --sport $subport1:$subport2 -j ACCEPT
iptables -I OUTPUT -p tcp --sport $subport1:$subport2 -j ACCEPT
fi
if [ ! -z "$mainport" ];then
#$mainport
iptables -I INPUT -p tcp --dport $mainport -j ACCEPT
iptables -I FORWARD -p tcp --dport $mainport -j ACCEPT
iptables -I OUTPUT -p tcp --dport $mainport -j ACCEPT
iptables -I INPUT -p tcp --sport $mainport -j ACCEPT
iptables -I FORWARD -p tcp --sport $mainport -j ACCEPT
iptables -I OUTPUT -p tcp --sport $mainport -j ACCEPT
fi
fi
if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ] || [ -f "/etc/systemd/system/v2ray.service" ]; then
killall keey.sh
${SYSTEMCTL_CMD} stop v2ray
systemctl disable v2ray.service
fi
if [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
killall keey.sh
${SERVICE_CMD} v2ray stop
fi
#Configure Server
#service v2ray stop
rm -rf config
cat << EOF > config
{"log" : {
"error": "/var/log/v2ray/error.log",
"loglevel": "warning"
},
"inbound": {
"port": $mainport,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "$uuid",
"level": $level,
"alterId": 32
}
]
}${mkcp}${httpheader}
},
"outbound": {
"protocol": "freedom",
"settings": {}
},
${dynamicport}
"outboundDetour": [
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}
],
"routing": {
"strategy": "rules",
"settings": {
"rules": [
{
"type": "field",
"ip": [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"::1/128",
"fc00::/7",
"fe80::/10"
],
"outboundTag": "blocked"
}
]
}
}
}
EOF
rm -rf /etc/v2ray/config.back
mv -f /etc/v2ray/config.json /etc/v2ray/config.back
mv -f config /etc/v2ray/config.json
cp -f /etc/v2ray/config.json /etc/v2ray/config.back0
read -p "输入客户端(路由)的 IP 地址(默认:192.168.123.1 ):" ipip
[ -z "$ipip" ] && ipip='192.168.123.1'
read -p "输入客户端(路由)的 本地代理 端口(默认:1088 ):" ip_port
[ -z "$ip_port" ] && ip_port='1088'
read -p "输入客户端(路由)的 透明代理 端口(默认:1099 ):" ip_door
[ -z "$ip_door" ] && ip_door='1099'
# 客户端配置
rm /root/config.json
cat << EOF > /root/config.json
{
"log": {
"error": "/tmp/syslog.log",
"loglevel": "warning"
},
"inbound": {
"port": $ip_port,
"listen": "$ipip",
"protocol": "$proxytype",
"settings": {
"auth": "noauth",
"udp": true,
"ip": "$ipip"
}
},
"inboundDetour": [
{
"port": "$ip_door",
"listen": "0.0.0.0",
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"timeout": 30,
"followRedirect": true
}
}
],
"outbound": {
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "$ipc",
"port": $mainport,
"users": [
{
"id": "$uuid",
"alterId": 32
}
]
}
]
}${mkcp}${httpheader}${mux}
},
"outboundDetour": [
{
"protocol": "freedom",
"settings": {},
"tag": "direct"
}
],
"dns": {
"servers": [
"8.8.8.8",
"8.8.4.4",
"localhost"
]
},
"routing": {
"strategy": "rules",
"settings": {
"rules": [
{
"type": "chinasites",
"outboundTag": "direct"
},
{
"type": "field",
"ip": [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"100.100.100.100/32",
"188.188.188.188/32",
"110.110.110.110/32",
"::1/128",
"fc00::/7",
"fe80::/10"
],
"outboundTag": "direct"
},
{
"type": "chinaip",
"outboundTag": "direct"
}
]
}
}
}
EOF
ntpdate us.pool.ntp.org &
if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ] || [ -f "/etc/systemd/system/v2ray.service" ]; then
echo "Restarting V2Ray service."
systemctl enable v2ray.service
${SYSTEMCTL_CMD} start v2ray
elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
echo "Restarting V2Ray service."
${SERVICE_CMD} v2ray start
fi
#service v2ray start
#clear
#INstall Success
echo 'Telegram Group: https://t.me/functionclub'
echo 'Github: https://github.com/FunctionClub'
echo '教程地址:https://github.com/FunctionClub/V2ray-Bash/blob/master/README.md'
echo '##########################################################################'
echo '配置完成,客户端配置文件在 /root/config.json'
echo '配置完成,客户端配置文件在 /root/config.json'
echo '配置完成,客户端配置文件在 /root/config.json'
echo ''
echo "程序主端口:$mainport"
echo "UUID: $uuid"
echo 'cat /root/config.json'
echo "打开 /root/config.json 复制里面的内容到路由的 v2ray 配置文件"
echo '##########################################################################'
rm -f /root/v2ray_config.pb
/usr/bin/v2ray/v2ctl config < /root/config.json > /root/v2ray_config.pb
[ -f /root/v2ray_config.pb ] && echo "已经生成 pb 配置文件 /root/v2ray_config.pb"
echo '##########################################################################'