-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdeploy_server
executable file
·612 lines (546 loc) · 22.6 KB
/
deploy_server
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/bin/sh
self="$(\curl -sS https://gitlab.com/zw963/deployment_bash/-/raw/v0.8.13/deploy_start.sh)" && eval "$self"
# self="$(cat /home/common/Project/deployment_bash/deploy_start.sh)" && eval "$self"
export_variable use_xtls="${use_xtls-true}"
if [ "$use_xtls" == "true" ]; then
export_variable service_name=xray
export_variable another_service_name=v2ray
else
export_variable service_name=v2ray
export_variable another_service_name=xray
fi
export_variable target=$1
cat <<HEREDOC
Set \$router_arch env to e.g. \`linux-arm32-v5/' if you want deploy to an asuswrt merlin AC5300 router.
For the correct Arch name, check https://github.com/XTLS/Xray-core/releases assets.
The default arch will assume server and side router use linux-amd64, and router using ${arch}.
HEREDOC
function postinstall () {
set -u
local linux=./downloaded_binary/linux-amd64
mkdir -p $linux/services ./downloaded_binary/${arch}
echo
echo 'Please wait copy generated client config into local $linux ...'
scp $target:/etc/$service_name/client_config.json $linux/client_config.json
scp $target:/etc/$service_name/config.json $linux/server_config.json
scp $target:/usr/local/bin/{geoip-only-cn-private.dat,geoip.dat,geosite.dat,xray,v2ray} $linux/
scp $target:/etc/systemd/system/{v2ray.service,[email protected]} $linux/services/
cp router/opt/etc/{apply_iptables_rule.sh,clean_iptables_rule.sh,update_geodata.sh} $linux/
(
cd $linux/
chmod +x apply_iptables_rule.sh clean_iptables_rule.sh update_geodata.sh xray v2ray
)
cp $linux/{geoip-only-cn-private.dat,geoip.dat,geosite.dat,config.json} ./downloaded_binary/${arch}/
scp $target:/tmp/v2ray-clients/{xray,v2ray} ./downloaded_binary/${arch}/
echo "Run [0m[33m./deploy_router admin@router_ip[0m to deploy to router."
echo "Run [0m[33m./deploy_side_router root@side_router_ip[0m to deploy to side_router."
}
# if grep -qs -e 'change_yours_password_here' ${service_name}_server.json; then
# echo "$service_name served as a shadowsocks server too."
# echo "You must change \`[0m[33mchange_yours_password_here[0m' to a NEW password in \`./${service_name}_server.json'!"
# exit
# fi
# password=$(cat ${service_name}_server.json |grep '"password"' |cut -d':' -f2 |cut -d'"' -f2)
# sed -i "s#change_yours_password_here#${password}#" "${another_service_name}_server.json"
export_variable orig_config=/etc/${service_name}/config.json
ssh $target "[ -e ${orig_config} ] && cp -a ${orig_config} ${orig_config}.$(date '+%Y-%m-%d_%H:%M:%S')"
copy_i ${service_name}_server.json ${orig_config}
if [ "${service_name}" == xray ]; then
copy downloaded_binary/linux-amd64/services/xray.service /etc/systemd/system/xray.service
copy downloaded_binary/linux-amd64/services/[email protected] /etc/systemd/system/[email protected]
fi
export_variable check_ss_port=$(cat <<HEREDOC
cat ${orig_config} 2>/dev/null |grep '"protocol": "shadowsocks"' -A3 |grep port |grep -o '[0-9]*'
HEREDOC
)
export_variable check_ss_method=$(cat <<HEREDOC
cat ${orig_config} 2>/dev/null |grep '"protocol": "shadowsocks"' -A5 |grep method |cut -d: -f2 |sed -e 's#.*"\(.*\)".*#\1#'
HEREDOC
)
export_variable check_port=$(cat <<HEREDOC
cat ${orig_config} 2>/dev/null |grep -A3 -B3 '"protocol":\s*"\(vmess\|vless\)"' |grep '"port"' |grep -o '[0-9]*'
HEREDOC
)
export_variable check_reality_domain_name=$(cat <<HEREDOC
cat ${orig_config} 2>/dev/null |grep '"dest":' |cut -d: -f2- |cut -d'"' -f2
HEREDOC
)
# those config will fetch the default value from server config.
export_variable old_port=$(ssh $target "$check_port")
export_variable old_ss_port=$(ssh $target "$check_ss_port")
export_variable old_ss_method=$(ssh $target "$check_ss_method")
export_variable old_reality_domain_name=$(ssh $target "$check_reality_domain_name")
# those config will generated new config instead if not exists old config.
export_variable old_uuid=$(ssh $target cat ${orig_config} 2>/dev/null |grep '"flow": "xtls-rprx' -A5 -B5 |grep '"id":' |cut -d: -f2 |cut -d'"' -f2)
export_variable old_ss_password=$(ssh $target cat ${orig_config} 2>/dev/null |grep '"protocol": "shadowsocks"' -A5 |grep password |cut -d: -f2 |sed -e 's#.*"\(.*\)".*#\1#')
export_variable old_reality_private_key=$(ssh $target cat ${orig_config} 2>/dev/null |grep '"privateKey":' |cut -d: -f2- |cut -d'"' -f2)
export_variable arch=${router_arch-linux-arm32-v5}
deploy_start
set -eu
# --------------------------------------------------------------------------------
#
# Following script will be run on remote VPS, please change it to meet your's need.
#
# -------------------------------------------------------------------------------
# wget https://install.direct/go.sh
# bash go.sh
if [ -f /etc/systemd/system/$service_name.service ]; then
set +e
systemctl stop $service_name
systemctl stop $another_service_name
systemctl disable $another_service_name
systemctl daemon-reload
set -e
fi
xray_version=$(github_latest_release XTLS/Xray-core)
v2ray_version=$(github_latest_release v2fly/v2ray-core)
set -e
(
download_and_extract https://github.com/XTLS/Xray-core/releases/download/${xray_version}/Xray-linux-64.zip Xray-${xray_version}-linux-64
cd Xray-${xray_version}-linux-64 && chmod +x xray
if ! ./xray version &>/dev/null; then
echo 'Invalid xray version/arch, please download correct version.'
exit
fi
cp xray *.dat /usr/local/bin
)
(
download_and_extract https://github.com/XTLS/Xray-core/releases/download/${xray_version}/Xray-${arch}.zip Xray-${xray_version}-${arch}
mkdir -p /tmp/v2ray-clients
cd Xray-${xray_version}-${arch} && cp xray *.dat /tmp/v2ray-clients
)
(
download_and_extract https://github.com/v2fly/v2ray-core/releases/download/${v2ray_version}/v2ray-linux-64.zip v2ray-${v2ray_version}-linux-64
cd v2ray-${v2ray_version}-linux-64 && chmod +x v2ray
if ! ./v2ray version &>/dev/null; then
echo 'Invalid v2ray version/arch, please download correct version.'
exit
fi
cp v2ray *.dat /usr/local/bin &&
cp systemd/system/*.service /etc/systemd/system
)
(
download_and_extract https://github.com/v2fly/v2ray-core/releases/download/${v2ray_version}/v2ray-${arch}.zip v2ray-${v2ray_version}-${arch}
mkdir -p /tmp/v2ray-clients
cd v2ray-${v2ray_version}-${arch} && cp v2ray *.dat /tmp/v2ray-clients
)
replace_regex 'RestartPreventExitStatus=23' 'RestartPreventExitStatus=23
# Added by user
LimitNPROC=500
LimitNOFILE=1000000' /etc/systemd/system/v2ray.service
replace_regex 'RestartPreventExitStatus=23' 'RestartPreventExitStatus=23
# Added by user
LimitNPROC=500
LimitNOFILE=1000000' /etc/systemd/system/[email protected]
replace_regex '/usr/local/etc/v2ray/config.json' '/etc/v2ray/config.json' /etc/systemd/system/v2ray.service
replace_regex '/usr/local/etc/v2ray/%i.json' '/etc/v2ray/%i.json' /etc/systemd/system/[email protected]
mkdir -p /var/log/xray /var/log/v2ray
# chown nobody:nobody /var/log/xray /var/log/v2ray
if [ "$old_uuid" != NEWUUID ]; then
echo "Old uuid $old_uuid exists, use it."
uuid=$old_uuid
else
uuid=$(cat /proc/sys/kernel/random/uuid)
fi
replace_string1 'NEWUUID' "${uuid}" ${orig_config}
# quic_key=$(uuidgen | sed 's/[-]//g' | head -c 20)
if [ -n "$old_port" ]; then
replace_multiline1 '("protocal":\s*"vless",.*?)"port":\s*[0-9]*,' '$1"port":'" ${old_port}" ${orig_config}
v2ray_port=$old_port
else
v2ray_port=$($check_port)
fi
if [ -n "$old_ss_port" ]; then
replace_multiline1 '("protocol": "shadowsocks",.*"port": )\d+' '${1}'"${old_ss_port}" ${orig_config}
ss_port=$old_ss_port
else
ss_port=$($check_ss_port)
fi
if [ -n "$old_ss_method" ]; then
replace_regex1 '"method": ".*"' "\"method\": \"${old_ss_method}\"" ${orig_config}
ss_method=$old_ss_method
else
ss_method=$($check_ss_method)
fi
if [[ -n "$old_ss_password" ]] && [[ "$old_ss_password" != "SSPASS" ]]; then
ss_password=$old_ss_password
# replace_regex1 '"password": ".*"' "\"password\": \"${ss_password}\"" ${orig_config}
else
# 生成的密钥长度与 2022-blake3-chacha20-poly1305 2022-blake3-aes-256-gcm 兼容
ss_password=$(openssl rand -base64 32)
fi
replace_string1 'SSPASS' "$ss_password" ${orig_config}
config_sysctl_for_proxy
systemctl enable $service_name
if [ "$use_xtls" == "true" ]; then
if [ -z "$old_reality_domain_name" ]; then
# 使用 xray_server.json 中的默认配置,即: www.hko.gov.hk:443
# 还可以选择以下候选者:
# www.td.gov.hk
# www.cmchk.org.hk
# 韩国推荐 freegame.gg
# 要求: 1. HTTP/2+ 2. TLS 1.3+
reality_domain_name=$($check_reality_domain_name)
client_server_name=$(echo ${reality_domain_name} |cut -d: -f1)
echo "reality domain name use ${reality_domain_name}"
else
# 使用上次部署的配置
reality_domain_name=$old_reality_domain_name
client_server_name=$(echo ${reality_domain_name} |cut -d: -f1)
echo "Using exists reality domain name ${old_reality_domain_name}..."
replace_regex1 '"dest": ".*"' "\"dest\": \"${reality_domain_name}\"" ${orig_config}
replace_regex '"serverNames": \[".*"\]' "\"serverNames\": [\"${client_server_name}\"]" ${orig_config}
fi
if [ -n "$old_reality_private_key" ] && [[ "$old_reality_private_key" != "REALITY_PRIVATE_KEY_HERE" ]]; then
echo "Use exists x25519 key pairs ..."
reality_private_key=$old_reality_private_key
reality_public_key=$(/usr/local/bin/xray x25519 -i $reality_private_key |tail -n1 |cut -d: -f2 |cut -d' ' -f2)
else
# 重新生成
echo "Geneate new x25519 key pairs ..."
key_pairs=$(/usr/local/bin/xray x25519)
reality_private_key=$(echo "$key_pairs" |head -n1 |cut -d: -f2 |cut -d' ' -f2)
reality_public_key=$(echo "$key_pairs" |tail -n1 |cut -d: -f2 |cut -d' ' -f2)
fi
replace_string 'REALITY_PRIVATE_KEY_HERE' "$reality_private_key" ${orig_config}
fi
# geoip_version=$(github_latest_release v2fly/geoip)
# wget https://github.com/v2fly/geoip/releases/download/${geoip_version}/geoip-only-cn-private.dat -o geoip-only-cn-private.dat
systemctl restart $service_name
systemctl status $service_name
systemctl enable $service_name
expose_port $v2ray_port
expose_port $ss_port
set -u
cat <<HEREDOC > /etc/$service_name/client_config.json
// 本脚本当运行 deploy_server 时自动生成。
// 本脚本可作为 普通 v2ray 客户端脚本,也可以作为 ASUS 梅林路由器/旁路由 透明代理脚本,
// 运行 ./deploy_router [email protected] 部署这个配置文件到路由器。
// 运行 ./deploy_side_router root@one_ip 部署这个配置文件到旁路由。
{
"log": {
"loglevel": "warning"
},
"dns": {
"hosts": {
"geosite:category-ads-all": "127.0.0.1"
},
"servers": [
// V2Ray 会使用 DNS over HTTPS (RFC8484, 简称DOH) 进行查询。
// DoH 默认走第一个 outbound 来查询 (DoH 域名需要在 hosts 中指定 IP 地址)
"8.8.4.4",
"1.1.1.1",
{
// 180.76.76.76 是百度的 DNS,你可以替换为当地运营商的
// 这里之前是 localhost, 造成了死循环,严重影响访问国内网站的性能。
"address": "180.76.76.76",
"port": 53,
"domains": [
"geosite:geolocation-cn"
],
"expectIPs": [
"ext:geoip-only-cn-private.dat:cn"
]
},
{
"address": "8.8.4.4",
"port": 53,
"domains": [
"services.googleapis.cn",
"googleapis.com",
"gstatic.com",
"xn--ngstr-lra8j.com",
"googleusercontent.com"
]
},
{
// fakeDNS 必须,否则会被解析为 fake IP.
// 注意,某些视频网站视频无法播放,貌似就是因为访问网站时,
// 解析到错误的 IP 地址。
"address": "180.76.76.76",
"port": 53,
"domains": [
"router.asus.com",
"arch.pool.ntp.org",
"dl.google.com",
"sourceforge.net",
"380tv.com",
"vidhub.tv",
"staticfile.org",
"www.naifei.im",
"crystal-lang.org",
"yzzy-online.com",
"cjkypo.com",
"gitlab.com",
"fonts.gstatic.com",
"fonts.googleapis.com",
"scootersoftware.com",
"archlinux.org",
"woaimoon.net",
"bing.com",
"cloudflare.com",
"fonts.gstatic.com"
]
}
],
// 默认为 false, 开启后,如果通过默认的 DNS 访问返回结果不匹配 cn site/ip,
// 将使用 servers 里面的第一个,8.8.4.4 访问。
"disableFallback": true
},
"routing": {
"rules": [
{
// tproxy 透明代理必需。
"type": "field",
"inboundTag": ["transparent"],
"port": 53,
"network": "udp",
"outboundTag": "dns-outbound"
},
{
"type": "field",
"domain": [
// 这里添加域名白名单.
"geosite:geolocation-cn",
"router.asus.com",
"arch.pool.ntp.org",
"dl.google.com",
"sourceforge.net",
"380tv.com",
"vidhub.tv",
"staticfile.org",
"www.naifei.im",
"crystal-lang.org",
"yzzy-online.com",
"cjkypo.com",
"gitlab.com",
"fonts.gstatic.com",
"fonts.googleapis.com",
"scootersoftware.com",
"archlinux.org",
"woaimoon.net",
"bing.com",
"cloudflare.com",
"fonts.gstatic.com"
],
"outboundTag": "direct"
},
{
"type": "field",
"ip": ["180.76.76.76"],
"outboundTag": "direct"
},
{
"type": "field",
"ip": [
"ext:geoip-only-cn-private.dat:cn",
"ext:geoip-only-cn-private.dat:private"
],
"outboundTag": "direct"
},
{
"type": "field",
"inboundTag": ["transparent"],
"port": "123,323",
"network": "udp",
"outboundTag": "direct"
},
{ // BT 流量直连
"type": "field",
"protocol":["bittorrent"],
"outboundTag": "direct"
},
{
"type": "field",
"ip": ["8.8.4.4","1.1.1.1"],
"outboundTag": "proxy"
},
{
"type": "field",
"domain": [
// 这里添加必须走 proxy 的域名例外.
"services.googleapis.cn",
"googleapis.com",
"gstatic.com",
"xn--ngstr-lra8j.com",
"googleusercontent.com",
"raw.githubusercontent.com",
"github.githubassets.com"
],
"outboundTag": "proxy"
},
{
"type": "field",
"domain": [
"geosite:category-ads-all"
],
"outboundTag": "block"
}
]
},
"inbounds": [
{
"tag": "transparent",
"protocol": "dokodemo-door",
"port": 1081, // 透明代理 1081 端口
"settings": {
"network": "tcp,udp",
// 当值为 true 时,dokodemo-door 会识别出由 iptables 转发而来的数据,并转发到相应的目标地址。详见 传输配置 中的 tproxy 设置。
"followRedirect": true
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
},
"streamSettings": {
"sockopt": {
// 当 Dokodemo-door 中指定了 followRedirect,且 sockopt.tproxy 为空时,
// sockopt.tproxy 的值会被设为 "redirect",因此该设定 redirect 模式非必须的。
// 下面的选项会在 patch_router 的时候,自动选择 tproxy/redirect, 并被替换。
"tproxy": "tproxy",
"mark": 255
}
}
},
{
"protocol": "socks", // 入口协议为 SOCKS 5
"port": 1080, // 监听端口
"settings": {
"auth": "noauth" //socks的认证设置,noauth 代表不认证,由于 socks 通常在客户端使用,所以这里不认证
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
},
{
"protocol": "http",
"port": 3128,
"settings": {
"timeout": 0
}
}
],
"outbounds": [
// 下面两个顺序不可以颠倒, 因为列表中的第一个元素作为主出站协议, 当路由匹配不存在或没有匹配成功时,
// 流量由主出站协议发出, 我们要确保主出站协议必须是 proxy.
{
"tag": "proxy",
"protocol": "vless", // 出口协议
"settings": {
"vnext": [
{
"address": "$targetip", // 服务器地址,请修改为你自己的服务器 IP 或域名
"port": $v2ray_port, // 服务器端口
"users": [
{
// 注意, vmess 加密默认开启, 也必须开启, 否则上不了网.
// alterId 默认为 0, 表示开启 VMessAEAD.
// vless 则默认关闭加密 none,但必须有这个设定
"encryption": "none",
// Splice 模式的的使用限制:
// Linux 环境
// 入站协议为 Dokodemo door、Socks、HTTP 等纯净的 TCP 连接, 或其它使用了 XTLS 的入站协议
// 出站协议为 VLESS + XTLS
// 需要注意的是,使用 mKCP 协议时不会使用 Splice(是的,虽然没有报错,但实际上根本没用到
// 此外,使用 Splice 时网速显示会滞后,这是特性,不是 bug。
// 使用 Vision 模式 如果满足上述条件 会自动启用 Splice
"flow": "xtls-rprx-vision",
"id": "$uuid" // 用户 ID,必须与服务器端配置相同
}
]
}
]
},
"streamSettings": {
"network": "tcp",
// reality 方式,用户无需拥有自己的域名,可以使用任何合法的网站,速度也更快。
"security": "reality",
"realitySettings": {
"fingerprint": "chrome",
"serverName": "${client_server_name}",
"publicKey": "${reality_public_key}"
},
"sockopt": {
"tcpcongestion": "bbr", // 如果不设定,会自动使用系统设置
"tcpMptcp": true,
"tcpNoDelay": true,
"mark": 255
}
}
},
{
"protocol": "shadowsocks",
"settings": {
"servers": [
{
"address": "$targetip", // Shadowsocks 的服务器地址
"method": "$ss_method", // Shadowsocks 的加密方式
"password": "$ss_password", // Shadowsocks 的密码
"port": $ss_port
}
]
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
},
{
// 识别这是一个 DNS 请求, 并发送到内部 DNS 进行查询.
"tag": "dns-outbound",
"protocol": "dns",
"proxySettings": {
"tag": "proxy"
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
},
{
"tag": "direct",
"protocol": "freedom",
"settings": {
// 这里我选择加上试试, 搞不好我的方案, freedom 真的又去访问 dnsmasq
// 的 53 端口, 又被转到 V2ray 的 DNS 呢.
"domainStrategy": "UseIPv4"
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
},
{
"tag": "block",
"protocol": "blackhole",
"settings": {
"response": {
"type": "http"
}
}
}
],
"policy": {
"levels": {
"0": {
"bufferSize": 4
}
}
}
}
HEREDOC
if [ "$use_xtls" != true ]; then
replace_string '"protocol": "vless",' '"protocol": "vmess",' /etc/$service_name/client_config.json
replace_multiline1 '"encryption": "none",\s*"flow": "xtls-rprx-.*?",' '' /etc/$service_name/client_config.json
replace_multiline1 '"network": "tcp",\s*"security":.*?},' '"network": "quic",
"quicSettings": {
"header": {
"type": "wechat-video"
}
},' /etc/$service_name/client_config.json
fi
echo 'Congratulations, Deploy succssful!'