-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathxhttp.sh
1374 lines (1252 loc) · 59.5 KB
/
xhttp.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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# System Required: CentOS 7+, Debian9+, Ubuntu16+
# Description: Script to Xray manage
#
# Copyright (C) 2024 zxcvos
#
# Xray Official:
# Xray-core: https://github.com/XTLS/Xray-core
# REALITY: https://github.com/XTLS/REALITY
# XHTTP: https://github.com/XTLS/Xray-core/discussions/4113
# Xray-script:
# https://github.com/zxcvos/Xray-script
# Xray-examples:
# https://github.com/chika0801/Xray-examples
# https://github.com/lxhao61/integrated-examples
# https://github.com/XTLS/Xray-core/discussions/4118
# docker-install:
# https://github.com/docker/docker-install
# Cloudflare WARP Proxy:
# https://github.com/haoel/haoel.github.io?tab=readme-ov-file#1043-docker-%E4%BB%A3%E7%90%86
# https://github.com/e7h4n/cloudflare-warp
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/snap/bin
export PATH
# color
readonly RED='\033[1;31;31m'
readonly GREEN='\033[1;31;32m'
readonly YELLOW='\033[1;31;33m'
readonly NC='\033[0m'
# directory
readonly CUR_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
readonly CUR_FILE="$(basename $0)"
# install option
declare INSTALL_OPTION=''
# specified version
declare SPECIFIED_VERSION=''
# status
declare STATUS=''
# warp
declare WARP=''
# automation
declare IS_AUTO=''
# update config
declare UPDATE_CONFIG=''
# xtls config
declare XTLS_CONFIG='xhttp'
# download url
declare DOWNLOAD_URL=''
# xray port
declare XRAY_PORT=443
# xray uuid
declare XRAY_UUID=''
# fallback uuid
declare FALLBACK_UUID=''
# kcp seed
declare KCP_SEED=''
# trojan password
declare TROJAN_PASSWORD=''
# target domain
declare TARGET_DOMAIN=''
# server names
declare SERVER_NAMES=''
# private key
declare PRIVATE_KEY=''
# public key
declare PUBLIC_KEY=''
# short id
declare SHORT_IDS=''
# xhttp path
declare XHTTP_PATH=''
# share link
declare SHARE_LINK=''
# status print
function _input_tips() {
printf "${GREEN}[输入提示] ${NC}"
printf -- "%s" "$@"
}
function _info() {
printf "${GREEN}[信息] ${NC}"
printf -- "%s" "$@"
printf "\n"
}
function _warn() {
printf "${YELLOW}[警告] ${NC}"
printf -- "%s" "$@"
printf "\n"
}
function _error() {
printf "${RED}[错误] ${NC}"
printf -- "%s" "$@"
printf "\n"
exit 1
}
# tools
function _exists() {
local cmd="$1"
if eval type type >/dev/null 2>&1; then
eval type "$cmd" >/dev/null 2>&1
elif command >/dev/null 2>&1; then
command -v "$cmd" >/dev/null 2>&1
else
which "$cmd" >/dev/null 2>&1
fi
local rt=$?
return ${rt}
}
function _os() {
local os=""
[[ -f "/etc/debian_version" ]] && source /etc/os-release && os="${ID}" && printf -- "%s" "${os}" && return
[[ -f "/etc/redhat-release" ]] && os="centos" && printf -- "%s" "${os}" && return
}
function _os_full() {
[[ -f /etc/redhat-release ]] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
[[ -f /etc/os-release ]] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
[[ -f /etc/lsb-release ]] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}
function _os_ver() {
local main_ver="$(echo $(_os_full) | grep -oE "[0-9.]+")"
printf -- "%s" "${main_ver%%.*}"
}
function _error_detect() {
local cmd="$1"
_info "${cmd}"
eval ${cmd}
if [[ $? -ne 0 ]]; then
_error "Execution command (${cmd}) failed, please check it and try again."
fi
}
function _is_digit() {
local input=${1}
if [[ "$input" =~ ^[0-9]+$ ]]; then
return 0
else
return 1
fi
}
function _version_ge() {
test "$(echo "$@" | tr ' ' '\n' | sort -rV | head -n 1)" == "$1"
}
function _is_tls1_3_h2() {
local check_url=$(echo $1 | grep -oE '[^/]+(\.[^/]+)+\b' | head -n 1)
local check_num=$(echo QUIT | stdbuf -oL openssl s_client -connect "${check_url}:443" -tls1_3 -alpn h2 2>&1 | grep -Eoi '(TLSv1.3)|(^ALPN\s+protocol:\s+h2$)|(X25519)' | sort -u | wc -l)
if [[ ${check_num} -eq 3 ]]; then
return 0
else
return 1
fi
}
function _is_network_reachable() {
local url="$1"
curl -s --head --connect-timeout 5 "$url" > /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
function _install() {
local packages_name="$@"
case "$(_os)" in
centos)
if _exists "dnf"; then
dnf update -y
dnf install -y dnf-plugins-core
dnf update -y
for package_name in ${packages_name}; do
dnf install -y ${package_name}
done
else
yum update -y
yum install -y epel-release yum-utils
yum update -y
for package_name in ${packages_name}; do
yum install -y ${package_name}
done
fi
;;
ubuntu | debian)
apt update -y
for package_name in ${packages_name}; do
apt install -y ${package_name}
done
;;
esac
}
function _systemctl() {
local cmd="$1"
local server_name="$2"
case "${cmd}" in
start)
_info "正在启动 ${server_name} 服务"
systemctl -q is-active ${server_name} || systemctl -q start ${server_name}
systemctl -q is-enabled ${server_name} || systemctl -q enable ${server_name}
sleep 2
systemctl -q is-active ${server_name} && _info "已启动 ${server_name} 服务" || _error "${server_name} 启动失败"
;;
stop)
_info "正在暂停 ${server_name} 服务"
systemctl -q is-active ${server_name} && systemctl -q stop ${server_name}
systemctl -q is-enabled ${server_name} && systemctl -q disable ${server_name}
sleep 2
systemctl -q is-active ${server_name} || _info "已暂停 ${server_name} 服务"
;;
restart)
_info "正在重启 ${server_name} 服务"
systemctl -q is-active ${server_name} && systemctl -q restart ${server_name} || systemctl -q start ${server_name}
systemctl -q is-enabled ${server_name} || systemctl -q enable ${server_name}
sleep 2
systemctl -q is-active ${server_name} && _info "已重启 ${server_name} 服务" || _error "${server_name} 启动失败"
;;
reload)
_info "正在重载 ${server_name} 服务"
systemctl -q is-active ${server_name} && systemctl -q reload ${server_name} || systemctl -q start ${server_name}
systemctl -q is-enabled ${server_name} || systemctl -q enable ${server_name}
sleep 2
systemctl -q is-active ${server_name} && _info "已重载 ${server_name} 服务"
;;
dr)
_info "正在重载 systemd 配置文件"
systemctl daemon-reload
;;
esac
}
function check_xray_script_version() {
local url="https://api.github.com/repos/zxcvos/Xray-script/contents"
local local_size=$(stat -c %s "${CUR_DIR}/${CUR_FILE}")
local remote_size=$(curl -fsSL "$url" | jq -r '.[] | select(.name == "xhttp.sh") | .size')
if [[ ${local_size} -ne ${remote_size} ]]; then
_info '发现有新脚本, 是否更新'
_input_tips '退出脚本并显示更新命令 [Y/n] '
read -r is_update_script
case ${is_update_script} in
N | n)
_warn '请及时更新脚本'
sleep 2
;;
*)
echo 'wget --no-check-certificate -O ${HOME}/Xray-script.sh https://raw.githubusercontent.com/zxcvos/Xray-script/main/xhttp.sh && bash ${HOME}/Xray-script.sh'
exit 0
;;
esac
fi
}
function check_os() {
[[ -z "$(_os)" ]] && _error "Not supported OS"
case "$(_os)" in
ubuntu)
[[ -n "$(_os_ver)" && "$(_os_ver)" -lt 16 ]] && _error "Not supported OS, please change to Ubuntu 16+ and try again."
;;
debian)
[[ -n "$(_os_ver)" && "$(_os_ver)" -lt 9 ]] && _error "Not supported OS, please change to Debian 9+ and try again."
;;
centos)
[[ -n "$(_os_ver)" && "$(_os_ver)" -lt 7 ]] && _error "Not supported OS, please change to CentOS 7+ and try again."
;;
*)
_error "Not supported OS"
;;
esac
}
function install_dependencies() {
_info "正在下载相关依赖"
_install "ca-certificates openssl curl wget jq tzdata qrencode"
case "$(_os)" in
centos)
_install "crontabs util-linux iproute procps-ng"
;;
debian | ubuntu)
_install "cron bsdmainutils iproute2 procps"
;;
esac
}
function install_docker() {
if ! _exists "docker"; then
wget --no-check-certificate -O /usr/local/xray-script/install-docker.sh https://get.docker.com
if [[ "$(_os)" == "centos" && "$(_os_ver)" -eq 8 ]]; then
sed -i 's|$sh_c "$pkg_manager install -y -q $pkgs"| $sh_c "$pkg_manager install -y -q $pkgs --allowerasing"|' /usr/local/xray-script/install-docker.sh
fi
sh /usr/local/xray-script/install-docker.sh --dry-run
sh /usr/local/xray-script/install-docker.sh
fi
}
function build_cloudflare_warp() {
if [[ "${WARP}" -ne 1 && ! -d /usr/local/xray-script/warp ]]; then
_info '正在构建 WARP Proxy 镜像'
mkdir -p /usr/local/xray-script/warp
mkdir -p ${HOME}/.warp
_error_detect "wget --no-check-certificate -O /usr/local/xray-script/warp/Dockerfile https://raw.githubusercontent.com/zxcvos/Xray-script/main/cloudflare-warp/Dockerfile"
_error_detect "wget --no-check-certificate -O /usr/local/xray-script/warp/startup.sh https://raw.githubusercontent.com/zxcvos/Xray-script/main/cloudflare-warp/startup.sh"
cd /usr/local/xray-script/warp
docker build -t xray-script-warp .
fi
}
function get_random_number() {
local custom_min=${1}
local custom_max=${2}
if ((custom_min > custom_max)); then
_error "错误:最小值不能大于最大值。"
fi
local random_number=$(od -vAn -N2 -i /dev/urandom | awk '{print int($1 % ('$custom_max' - '$custom_min') + '$custom_min')}')
echo $random_number
}
function get_random_port() {
local random_number=$(get_random_number 1025 65536)
echo $random_number
}
function validate_hex_input() {
local input=$1
if [[ $input =~ ^[0-9a-f]+$ ]] && ((${#input} % 2 == 0)) && ((${#input} <= 16)); then
return 0
else
return 1
fi
}
function check_xray_version_is_exists() {
local xray_version_url="https://github.com/XTLS/Xray-core/releases/tag/v${1##*v}"
local status_code=$(curl -o /dev/null -s -w '%{http_code}\n' "$xray_version_url")
if [[ "$status_code" = "404" ]]; then
_error "无法找到该版本: $1"
fi
}
function enable_warp() {
if [[ "${WARP}" -ne 1 ]]; then
_info '正在开启 WARP Proxy 容器'
docker run -v "${HOME}/.warp":/var/lib/cloudflare-warp:rw --restart=always --name=xray-script-warp xray-script-warp
local outbounds='[{"tag":"warp","protocol":"socks","settings":{"servers":[{"address":"172.17.0.2","port":40001}]}}]'
jq --argjson outbounds $outbounds '.outbounds += $outbounds' /usr/local/etc/xray/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/etc/xray/config.json
jq --argjson warp 1 '.warp = $warp' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
fi
}
function disable_warp() {
if [[ "${WARP}" -eq 1 ]]; then
_info '正在关闭 WARP Proxy 容器'
docker stop xray-script-warp
docker rm xray-script-warp
docker image rm xray-script-warp
rm -rf /usr/local/xray-script/warp
rm -rf ${HOME}/.warp
jq '.outbounds |= map(select(.tag != "warp"))' /usr/local/etc/xray/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/etc/xray/config.json
jq '.routing.rules |= map(select(.outboundTag != "warp"))' /usr/local/etc/xray/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/etc/xray/config.json
jq --argjson warp 0 '.warp = $warp' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
fi
}
function enable_cron() {
if ! [[ -f /usr/local/xray-script/update-dat.sh ]]; then
wget --no-check-certificate -O /usr/local/xray-script/update-dat.sh https://raw.githubusercontent.com/zxcvos/Xray-script/main/tool/update-dat.sh
chmod a+x /usr/local/xray-script/update-dat.sh
(
crontab -l 2>/dev/null
echo "30 6 * * * /usr/local/xray-script/update-dat.sh >/dev/null 2>&1"
) | awk '!x[$0]++' | crontab -
/usr/local/xray-script/update-dat.sh
fi
}
function disable_cron() {
if [[ -f /usr/local/xray-script/update-dat.sh ]]; then
crontab -l | grep -v "/usr/local/xray-script/update-dat.sh >/dev/null 2>&1" | crontab -
rm -rf /usr/local/xray-script/update-dat.sh
fi
}
# 添加规则的函数
function add_rule() {
local CONFIG_FILE='/usr/local/etc/xray/config.json'
local TMP_FILE='/usr/local/xray-script/tmp.json'
local rule_tag=$1
local domain_or_ip=$2
local value=$(echo "$3" | tr ',' '\n' | jq -R . | jq -s .)
local outboundTag=$4
local position=$5 # 插入位置参数,可以是 "before" 或 "after"
local target_tag=$6 # 目标 ruleTag,指定插入位置的 ruleTag
# 读取原始的 rules 数组
local current_rules=$(jq '.routing.rules' "$CONFIG_FILE")
# 检查 ruleTag 是否已经存在
local existing_rule=$(echo "$current_rules" | jq -r --arg ruleTag "$rule_tag" '.[] | select(.ruleTag == $ruleTag)')
if [[ "$existing_rule" ]]; then
# 如果 ruleTag 已存在,添加到 domain 或 ip 数组
if [[ "$domain_or_ip" == "domain" ]]; then
jq --arg ruleTag "$rule_tag" --argjson value "$value" '.routing.rules |= map(if .ruleTag == $ruleTag then .domain += $value | .domain |= unique else . end)' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
elif [[ "$domain_or_ip" == "ip" ]]; then
jq --arg ruleTag "$rule_tag" --argjson value "$value" '.routing.rules |= map(if .ruleTag == $ruleTag then .ip += $value | .ip |= unique else . end)' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
fi
else
# 如果 ruleTag 不存在,创建新的规则
new_rule="[{\"ruleTag\":\"$rule_tag\",\"$domain_or_ip\":$value,\"outboundTag\":\"$outboundTag\"}]"
# 如果提供了 target_tag 和 position
if [[ -n "$target_tag" ]]; then
# 查找目标 ruleTag 是否存在
local target_rule=$(echo "$current_rules" | jq -r --arg ruleTag "$target_tag" '.[] | select(.ruleTag == $ruleTag)')
if [[ "$target_rule" ]]; then
# 获取目标 ruleTag 的位置
local target_index=$(echo "$current_rules" | jq -r --arg ruleTag "$target_tag" 'to_entries | map(select(.value.ruleTag == $ruleTag)) | .[0].key')
if [[ "$position" == "before" ]]; then
# 插入到 target_tag 前
jq --argjson target_index $target_index --argjson new_rule "$new_rule" '.routing.rules |= .[:$target_index] + $new_rule + .[$target_index:]' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
elif [[ "$position" == "after" ]]; then
# 插入到 target_tag 后
jq --argjson target_index $((target_index + 1)) --argjson new_rule "$new_rule" '.routing.rules |= .[:$target_index] + $new_rule + .[$target_index:]' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
else
# 如果 position 不是 "before" 或 "after",则追加到末尾
jq --argjson new_rule "$new_rule" '.routing.rules += $new_rule' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
fi
else
# 如果 target_tag 不存在,则追加到末尾
jq --argjson new_rule "$new_rule" '.routing.rules += $new_rule' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
fi
else
if [[ -n "$position" && "$position" -ge 0 ]]; then
# 如果提供了插入位置并且位置有效(大于等于0),插入到该位置
jq --argjson position $position --argjson new_rule "$new_rule" '.routing.rules |= .[:$position] + $new_rule + .[$position:]' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
else
# 如果没有提供位置或位置无效,则追加到末尾
jq --argjson new_rule "$new_rule" '.routing.rules += $new_rule' "$CONFIG_FILE" >"$TMP_FILE" && mv -f "$TMP_FILE" "$CONFIG_FILE"
fi
fi
fi
}
function add_rule_warp_ip() {
if [[ "${WARP}" -eq 1 ]]; then
_warn '默认使用该功能的用户知道添加 rule 的相关规则'
_info '支持逗号分隔的多个值'
_input_tips '请输入分流到 WARP 的 ip: '
read -r rule_warp_ip
if [[ -n "$rule_warp_ip" ]]; then
add_rule "warp-ip" "ip" "$rule_warp_ip" "warp" "before" "ad-domain"
fi
else
_error '请开启 WARP Proxy 在进行分流操作'
fi
}
function add_rule_warp_domain() {
if [[ "${WARP}" -eq 1 ]]; then
_warn '默认使用该功能的用户知道添加 rule 的相关规则'
_info '支持逗号分隔的多个值'
_input_tips '请输入分流到 WARP 的 domain: '
read -r rule_warp_domain
if [[ -n "$rule_warp_domain" ]]; then
add_rule "warp-domain" "domain" "$rule_warp_domain" "warp" "before" "ad-domain"
fi
else
_error '请开启 WARP Proxy 在进行分流操作'
fi
}
function add_rule_block_ip() {
_warn '默认使用该功能的用户知道添加 rule 的相关规则'
_info '支持逗号分隔的多个值'
_input_tips '请输入需要屏蔽 ip: '
read -r rule_block_ip
if [[ -n "$rule_block_ip" ]]; then
add_rule "block-ip" "ip" "$rule_block_ip" "block" "after" "private-ip"
fi
}
function add_rule_block_domain() {
_warn '默认使用该功能的用户知道添加 rule 的相关规则'
_info '支持逗号分隔的多个值'
_input_tips '请输入需要屏蔽 domain: '
read -r rule_domain_domain
if [[ -n "$rule_domain_domain" ]]; then
add_rule "block-domain" "domain" "$rule_domain_domain" "block" "after" "private-ip"
fi
}
function add_rule_block_bt() {
if [[ ${is_block_bt} =~ ^[Yy]$ ]]; then
add_rule "bt" "protocol" "bittorrent" "block" 1
fi
}
function add_rule_block_cn_ip() {
if [[ ${is_block_cn_ip} =~ ^[Yy]$ ]]; then
add_rule "cn-ip" "ip" "geoip:cn" "block" "after" "private-ip"
fi
}
function add_rule_block_ads() {
if [[ ${is_block_ads} =~ ^[Yy]$ ]]; then
add_rule "ad-domain" "domain" "geosite:category-ads-all" "block"
fi
}
function add_update_geodata() {
if [[ ${is_update_geodata} =~ ^[Yy]$ ]]; then
enable_cron
fi
}
function read_block_bt() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
is_block_bt='Y'
else
_input_tips '是否开启 bittorrent 屏蔽 [y/N] '
read -r is_block_bt
fi
}
function read_block_cn_ip() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
is_block_cn_ip='Y'
else
_input_tips '是否开启国内 ip 屏蔽 [y/N] '
read -r is_block_cn_ip
fi
}
function read_block_ads() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
is_block_ads='Y'
else
_input_tips '是否开启广告屏蔽 [y/N] '
read -r is_block_ads
fi
}
function read_update_geodata() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
is_update_geodata='Y'
else
_input_tips '是否开启 geodata 自动更新功能 [y/N] '
read -r is_update_geodata
fi
}
function read_port() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_info '端口范围是 1-65535 之间的数字, 如果不在范围内, 则使用默认生成'
_input_tips '请输入自定义 port (默认自动生成): '
read -r in_port
}
function read_uuid() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_info '自定义输入的 uuid ,如果不是标准格式,将会使用 xray uuid -i "自定义字符串" 进行 UUIDv5 映射后填入配置'
_input_tips '请输入自定义 UUID (默认自动生成): '
read -r in_uuid
}
function read_seed() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_input_tips '请输入自定义 seed (默认自动生成): '
read -r in_seed
}
function read_password() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_input_tips '请输入自定义 password (默认自动生成): '
read -r in_password
}
function read_domain() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_info "如果输入的自定义域名在 serverNames.json 存在对应的 key , 则代表使用该数据"
until [[ ${is_domain} =~ ^[Yy]$ ]]; do
_input_tips '请输入自定义域名 (默认自动生成): '
read -r in_domain
if [[ -z "${in_domain}" ]]; then
break
fi
check_domain=$(echo ${in_domain} | grep -oE '[^/]+(\.[^/]+)+\b' | head -n 1)
if ! _is_network_reachable "${check_domain}"; then
_warn "\"${check_domain}\" 无法连接"
continue
fi
if ! _is_tls1_3_h2 "${check_domain}"; then
_warn "\"${check_domain}\" 不支持 TLSv1.3 或 h2 ,亦或者 Client Hello 不是 X25519"
_info "如果你明确知道 \"${check_domain}\" 支持 TLSv1.3(h2), X25519, 可能是识别错误, 可选择直接跳过检查"
_input_tips '是否明确确认支持 [y/N] '
read -r is_support
if [[ ${is_support} =~ ^[Yy]$ ]]; then
break
else
continue
fi
fi
is_domain='Y'
done
in_domain=${check_domain}
}
function read_short_ids() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_info 'shortId 内容为 0 到 f, 长度为 2 的倍数,长度上限为 16'
_info '如果输入值为 0 到 8, 则自动生成对 0-16 长度的 shortId'
_info '支持逗号分隔的多个值'
_input_tips '请输入自定义 shortId (默认自动生成): '
read -r in_short_id
}
function read_path() {
if [[ ${IS_AUTO} =~ ^[Yy]$ ]]; then
return
fi
_input_tips '请输入自定义 path (默认自动生成): '
read -r in_path
}
function generate_port() {
local input=${1}
if ! _is_digit "${input}" || [[ ${input} -lt 1 || ${input} -gt 65535 ]]; then
case ${XTLS_CONFIG} in
mkcp) input=$(get_random_port) ;;
*) input=443 ;;
esac
fi
echo ${input}
}
function generate_uuid() {
local input="${1}"
local uuid=""
if [[ -z "${input}" ]]; then
uuid=$(xray uuid)
elif printf "%s" "${input}" | grep -Eq '^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$'; then
uuid="${input}"
else
uuid=$(xray uuid -i "${input}")
fi
echo "${uuid}"
}
function generate_password() {
local seed="${1}"
local length="${2}"
if [[ -z "${length}" ]]; then
length=16
fi
if [[ -z "${seed}" ]]; then
seed=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9!@#$%^&*()_+-=' | head -c $length)
fi
echo "${seed}"
}
function generate_target() {
local target=${1}
if [[ -z "${target}" ]]; then
local length=$(jq -r '. | length' /usr/local/xray-script/serverNames.json)
local random_number=$(get_random_number 0 ${length})
target=$(jq '. | keys | .[]' /usr/local/xray-script/serverNames.json | shuf | jq -s -r --argjson i ${random_number} '.[$i]')
fi
jq --arg target "${target}" '.target = $target' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
echo "${target}:443"
}
function generate_server_names() {
local target=${1%:443}
local local_target=$(jq --arg key "${target}" '. | has($key)' /usr/local/xray-script/serverNames.json)
if [[ "${local_target}" == "false" ]]; then
local all_sns=$(xray tls ping ${target} | sed -n '/with SNI/,$p' | sed -En 's/\[(.*)\]/\1/p' | sed -En 's/Allowed domains:\s*//p' | jq -R -c 'split(" ")' | jq --arg sni "${target}" '. += [$sni]')
local sns=$(echo ${all_sns} | jq 'map(select(test("^[^*]+$"; "g")))' | jq -c 'map(select(test("^((?!cloudflare|akamaized|edgekey|edgesuite|cloudfront|azureedge|msecnd|edgecastcdn|fastly|googleusercontent|kxcdn|maxcdn|stackpathdns|stackpathcdn|policy|privacy).)*$"; "ig")))' | jq 'unique')
fi
jq --arg key "${target}" --argjson serverNames "${sns}" '
if . | has($key) then
.
else
. += { ($key): $serverNames }
end
' /usr/local/xray-script/serverNames.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/serverNames.json
local server_names="$(jq --arg key "${target}" '.[$key]' /usr/local/xray-script/serverNames.json)"
echo "${server_names}"
}
function generate_xray_x25519() {
local xray_x25519=$(xray x25519)
PRIVATE_KEY=$(echo ${xray_x25519} | awk '{print $3}')
PUBLIC_KEY=$(echo ${xray_x25519} | awk '{print $6}')
jq --arg privateKey "${PRIVATE_KEY}" '.privateKey = $privateKey' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
jq --arg publicKey "${PUBLIC_KEY}" '.publicKey = $publicKey' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
}
function generate_short_id() {
local input=$1
local trimmed_input=$(echo "$input" | xargs)
if [[ $trimmed_input =~ ^[0-8]$ ]]; then
echo "$(openssl rand -hex ${trimmed_input})"
elif validate_hex_input "$trimmed_input"; then
echo "$trimmed_input"
else
_error "'$trimmed_input' 不是有效的输入。"
fi
}
function generate_short_ids() {
IFS=',' read -r -a inputs <<<"$1"
result=()
if [[ -z "$inputs" ]]; then
inputs=(4 8)
fi
for input in "${inputs[@]}"; do
short_id=$(generate_short_id "$input")
result+=("$short_id")
done
local short_ids=$(printf '%s\n' "${result[@]}" | jq -R . | jq -s .)
echo "${short_ids}"
}
function generate_path() {
local input="${1}"
if [[ -z "${input}" ]]; then
local package_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
local service_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
local method_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
echo "/${package_name}.${service_name}.${method_name}"
else
echo "/${input#/}"
fi
}
function get_xray_config_data() {
if [[ "${STATUS}" -ne 1 ]]; then
read_block_bt
read_block_cn_ip
read_block_ads
read_update_geodata
fi
read_port
XRAY_PORT=$(generate_port "${in_port}")
_info "port: ${XRAY_PORT}"
jq --argjson port "${XRAY_PORT}" '.port = $port' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
read_uuid
XRAY_UUID="$(generate_uuid "${in_uuid}")"
_info "UUID: ${XRAY_UUID}"
jq --arg uuid "${XRAY_UUID}" '.uuid = $uuid' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
case ${XTLS_CONFIG} in
mkcp)
read_seed
KCP_SEED="$(generate_password "${in_seed}")"
_info "seed: ${KCP_SEED}"
jq --arg seed "${KCP_SEED}" '.kcp = $seed' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
;;
trojan)
read_password
TROJAN_PASSWORD="$(generate_password "${in_password}")"
_info "password: ${TROJAN_PASSWORD}"
jq --arg trojan "${TROJAN_PASSWORD}" '.trojan = $trojan' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
;;
fallback)
_info "设置 fallback UUID"
read_uuid
FALLBACK_UUID="$(generate_uuid "${in_uuid}")"
_info "fallback UUID: ${FALLBACK_UUID}"
jq --arg uuid "${FALLBACK_UUID}" '.fallback = $uuid' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
;;
esac
case ${XTLS_CONFIG} in
xhttp | trojan | fallback)
read_path
XHTTP_PATH="$(generate_path "${in_path}")"
_info "path: ${XHTTP_PATH}"
jq --arg path "${XHTTP_PATH}" '.path = $path' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
;;
esac
case ${XTLS_CONFIG} in
xhttp | vision | trojan | fallback)
read_domain
TARGET_DOMAIN="$(generate_target "${in_domain}")"
_info "target: ${TARGET_DOMAIN}"
SERVER_NAMES="$(generate_server_names "${TARGET_DOMAIN}")"
_info "server names: ${SERVER_NAMES}"
generate_xray_x25519
read_short_ids
SHORT_IDS="$(generate_short_ids "${in_short_id}")"
_info "shortIds: ${SHORT_IDS}"
_info "private key: ${PRIVATE_KEY}"
_info "public key: ${PUBLIC_KEY}"
jq --argjson shortIds "${SHORT_IDS}" '.shortIds = $shortIds' /usr/local/xray-script/config.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/config.json
;;
esac
}
function get_xtls_download_url() {
local url="https://api.github.com/repos/zxcvos/Xray-script/contents/XTLS"
DOWNLOAD_URL=$(curl -fsSL "$url" | jq -r --arg target "${XTLS_CONFIG}" '.[] | select((.name | ascii_downcase | sub("\\.json$"; "")) == $target) | .download_url')
}
function set_mkcp_data() {
jq --argjson port "${XRAY_PORT}" '.inbounds[1].port = $port' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg uuid "${XRAY_UUID}" '.inbounds[1].settings.clients[0].id = $uuid' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg seed "${KCP_SEED}" '.inbounds[1].streamSettings.kcpSettings.seed = $seed' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
}
function get_mkcp_data() {
# -- protocol --
local protocol=$(jq -r '.inbounds[1].protocol' /usr/local/etc/xray/config.json)
# -- uuid --
local uuid=$(jq -r '.inbounds[1].settings.clients[0].id' /usr/local/etc/xray/config.json)
# -- remote_host --
local remote_host=$(curl -fsSL ipv4.icanhazip.com)
# -- port --
local port=$(jq -r '.inbounds[1].port' /usr/local/etc/xray/config.json)
# -- type --
local type=$(jq -r '.inbounds[1].streamSettings.network' /usr/local/etc/xray/config.json)
# -- seed --
local seed=$(jq -r '.inbounds[1].streamSettings.kcpSettings.seed' /usr/local/etc/xray/config.json)
# -- tag --
local tag=$(jq -r '.tag' /usr/local/xray-script/config.json)
# -- SHARE_LINK --
SHARE_LINK="${protocol}://${uuid}@${remote_host}:${port}?type=${type}&seed=${seed}#${tag}"
}
function set_vision_data() {
jq --argjson port "${XRAY_PORT}" '.inbounds[1].port = $port' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg uuid "${XRAY_UUID}" '.inbounds[1].settings.clients[0].id = $uuid' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg target "${TARGET_DOMAIN}" '.inbounds[1].streamSettings.realitySettings.target = $target' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson serverNames "${SERVER_NAMES}" '.inbounds[1].streamSettings.realitySettings.serverNames = $serverNames' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg privateKey "${PRIVATE_KEY}" '.inbounds[1].streamSettings.realitySettings.privateKey = $privateKey' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson shortIds "${SHORT_IDS}" '.inbounds[1].streamSettings.realitySettings.shortIds = $shortIds' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
}
function get_vision_data() {
# -- protocol --
local protocol=$(jq -r '.inbounds[1].protocol' /usr/local/etc/xray/config.json)
# -- uuid --
local uuid=$(jq -r '.inbounds[1].settings.clients[0].id' /usr/local/etc/xray/config.json)
# -- remote_host --
local remote_host=$(curl -fsSL ipv4.icanhazip.com)
# -- port --
local port=$(jq -r '.inbounds[1].port' /usr/local/etc/xray/config.json)
# -- type --
local type=$(jq -r '.inbounds[1].streamSettings.network' /usr/local/etc/xray/config.json)
# -- flow --
local flow=$(jq -r '.inbounds[1].settings.clients[0].flow' /usr/local/etc/xray/config.json)
# -- security --
local security=$(jq -r '.inbounds[1].streamSettings.security' /usr/local/etc/xray/config.json)
# -- serverName --
local server_names_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.serverNames | length' /usr/local/etc/xray/config.json)
local server_names_random=$(get_random_number 0 ${server_names_length})
local server_name=$(jq '.inbounds[1].streamSettings.realitySettings.serverNames | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${server_names_random} '.[$i]')
# -- public_key --
local public_key=$(jq -r '.publicKey' /usr/local/xray-script/config.json)
# -- shortId --
local short_ids_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.shortIds | length' /usr/local/etc/xray/config.json)
local short_ids_random=$(get_random_number 0 ${short_ids_length})
local short_id=$(jq '.inbounds[1].streamSettings.realitySettings.shortIds | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${short_ids_random} '.[$i]')
# -- tag --
local tag=$(jq -r '.tag' /usr/local/xray-script/config.json)
# -- SHARE_LINK --
SHARE_LINK="${protocol}://${uuid}@${remote_host}:${port}?type=${type}&flow=${flow}&security=${security}&sni=${server_name}&pbk=${public_key}&sid=${short_id}&spx=%2F&fp=chrome#${tag}"
}
function set_xhttp_data() {
jq --argjson port "${XRAY_PORT}" '.inbounds[1].port = $port' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg uuid "${XRAY_UUID}" '.inbounds[1].settings.clients[0].id = $uuid' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg target "${TARGET_DOMAIN}" '.inbounds[1].streamSettings.realitySettings.target = $target' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson serverNames "${SERVER_NAMES}" '.inbounds[1].streamSettings.realitySettings.serverNames = $serverNames' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg privateKey "${PRIVATE_KEY}" '.inbounds[1].streamSettings.realitySettings.privateKey = $privateKey' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson shortIds "${SHORT_IDS}" '.inbounds[1].streamSettings.realitySettings.shortIds = $shortIds' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg path "${XHTTP_PATH}" '.inbounds[1].streamSettings.xhttpSettings.path = $path' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
}
function get_xhttp_data() {
# -- protocol --
local protocol=$(jq -r '.inbounds[1].protocol' /usr/local/etc/xray/config.json)
# -- uuid --
local uuid=$(jq -r '.inbounds[1].settings.clients[0].id' /usr/local/etc/xray/config.json)
# -- remote_host --
local remote_host=$(curl -fsSL ipv4.icanhazip.com)
# -- port --
local port=$(jq -r '.inbounds[1].port' /usr/local/etc/xray/config.json)
# -- type --
local type=$(jq -r '.inbounds[1].streamSettings.network' /usr/local/etc/xray/config.json)
# -- security --
local security=$(jq -r '.inbounds[1].streamSettings.security' /usr/local/etc/xray/config.json)
# -- serverName --
local server_names_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.serverNames | length' /usr/local/etc/xray/config.json)
local server_names_random=$(get_random_number 0 ${server_names_length})
local server_name=$(jq '.inbounds[1].streamSettings.realitySettings.serverNames | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${server_names_random} '.[$i]')
# -- public_key --
local public_key=$(jq -r '.publicKey' /usr/local/xray-script/config.json)
# -- shortId --
local short_ids_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.shortIds | length' /usr/local/etc/xray/config.json)
local short_ids_random=$(get_random_number 0 ${short_ids_length})
local short_id=$(jq '.inbounds[1].streamSettings.realitySettings.shortIds | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${short_ids_random} '.[$i]')
# -- path --
local path=$(jq -r '.inbounds[1].streamSettings.xhttpSettings.path' /usr/local/etc/xray/config.json)
# -- tag --
local tag=$(jq -r '.tag' /usr/local/xray-script/config.json)
# -- SHARE_LINK --
SHARE_LINK="${protocol}://${uuid}@${remote_host}:${port}?type=${type}&security=${security}&sni=${server_name}&pbk=${public_key}&sid=${short_id}&path=%2F${path#/}&spx=%2F&fp=chrome#${tag}"
}
function set_trojan_data() {
jq --argjson port "${XRAY_PORT}" '.inbounds[1].port = $port' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg password "${TROJAN_PASSWORD}" '.inbounds[1].settings.clients[0].password = $password' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg target "${TARGET_DOMAIN}" '.inbounds[1].streamSettings.realitySettings.target = $target' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson serverNames "${SERVER_NAMES}" '.inbounds[1].streamSettings.realitySettings.serverNames = $serverNames' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg privateKey "${PRIVATE_KEY}" '.inbounds[1].streamSettings.realitySettings.privateKey = $privateKey' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson shortIds "${SHORT_IDS}" '.inbounds[1].streamSettings.realitySettings.shortIds = $shortIds' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg path "${XHTTP_PATH}" '.inbounds[1].streamSettings.xhttpSettings.path = $path' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
}
function get_trojan_data() {
# -- protocol --
local protocol=$(jq -r '.inbounds[1].protocol' /usr/local/etc/xray/config.json)
# -- password --
local password=$(jq -r '.inbounds[1].settings.clients[0].password' /usr/local/etc/xray/config.json)
# -- remote_host --
local remote_host=$(curl -fsSL ipv4.icanhazip.com)
# -- port --
local port=$(jq -r '.inbounds[1].port' /usr/local/etc/xray/config.json)
# -- type --
local type=$(jq -r '.inbounds[1].streamSettings.network' /usr/local/etc/xray/config.json)
# -- security --
local security=$(jq -r '.inbounds[1].streamSettings.security' /usr/local/etc/xray/config.json)
# -- serverName --
local server_names_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.serverNames | length' /usr/local/etc/xray/config.json)
local server_names_random=$(get_random_number 0 ${server_names_length})
local server_name=$(jq '.inbounds[1].streamSettings.realitySettings.serverNames | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${server_names_random} '.[$i]')
# -- public_key --
local public_key=$(jq -r '.publicKey' /usr/local/xray-script/config.json)
# -- shortId --
local short_ids_length=$(jq -r '.inbounds[1].streamSettings.realitySettings.shortIds | length' /usr/local/etc/xray/config.json)
local short_ids_random=$(get_random_number 0 ${short_ids_length})
local short_id=$(jq '.inbounds[1].streamSettings.realitySettings.shortIds | .[]' /usr/local/etc/xray/config.json | shuf | jq -s -r --argjson i ${short_ids_random} '.[$i]')
# -- path --
local path=$(jq -r '.inbounds[1].streamSettings.xhttpSettings.path' /usr/local/etc/xray/config.json)
# -- tag --
local tag=$(jq -r '.tag' /usr/local/xray-script/config.json)
# -- SHARE_LINK --
SHARE_LINK="${protocol}://${password}@${remote_host}:${port}?type=${type}&security=${security}&sni=${server_name}&pbk=${public_key}&sid=${short_id}&path=%2F${path#/}&spx=%2F&fp=chrome#${tag}"
}
function set_fallback_data() {
jq --argjson port "${XRAY_PORT}" '.inbounds[1].port = $port' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg uuid "${XRAY_UUID}" '.inbounds[1].settings.clients[0].id = $uuid' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg target "${TARGET_DOMAIN}" '.inbounds[1].streamSettings.realitySettings.target = $target' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson serverNames "${SERVER_NAMES}" '.inbounds[1].streamSettings.realitySettings.serverNames = $serverNames' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg privateKey "${PRIVATE_KEY}" '.inbounds[1].streamSettings.realitySettings.privateKey = $privateKey' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --argjson shortIds "${SHORT_IDS}" '.inbounds[1].streamSettings.realitySettings.shortIds = $shortIds' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg uuid "${FALLBACK_UUID}" '.inbounds[2].settings.clients[0].id = $uuid' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json
jq --arg path "${XHTTP_PATH}" '.inbounds[2].streamSettings.xhttpSettings.path = $path' /usr/local/xray-script/xtls.json >/usr/local/xray-script/tmp.json && mv -f /usr/local/xray-script/tmp.json /usr/local/xray-script/xtls.json