forked from aleskxyz/reality-ezpz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reality-ezpz.sh
2149 lines (2076 loc) · 66.8 KB
/
reality-ezpz.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
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -e
declare -A defaults
declare -A config_file
declare -A args
declare -A config
declare -A users
declare -A path
declare -A service
declare -A md5
declare -A regex
declare -A image
config_path="/opt/reality-ezpz"
compose_project='reality-ezpz'
tgbot_project='tgbot'
BACKTITLE=RealityEZPZ
MENU="Select an option:"
HEIGHT=30
WIDTH=60
CHOICE_HEIGHT=20
image[xray]="teddysun/xray:1.8.3"
image[sing-box]="gzxhwq/sing-box:v1.3.0"
image[nginx]="nginx:1.24.0"
image[certbot]="certbot/certbot:v2.6.0"
image[haproxy]="haproxy:2.8.0"
image[python]="python:3.11-alpine"
defaults[transport]=tcp
defaults[domain]=www.google.com
defaults[port]=443
defaults[safenet]=OFF
defaults[warp]=OFF
defaults[warp_license]=""
defaults[warp_private_key]=""
defaults[warp_token]=""
defaults[warp_id]=""
defaults[warp_client_id]=""
defaults[warp_interface_ipv4]=""
defaults[warp_interface_ipv6]=""
defaults[core]=sing-box
defaults[security]=reality
defaults[server]=$(curl -fsSL --ipv4 https://ifconfig.io)
defaults[tgbot]=OFF
defaults[tgbot_token]=""
defaults[tgbot_admins]=""
config_items=(
"core"
"security"
"service_path"
"public_key"
"private_key"
"short_id"
"transport"
"domain"
"server"
"port"
"safenet"
"warp"
"warp_license"
"warp_private_key"
"warp_token"
"warp_id"
"warp_client_id"
"warp_interface_ipv4"
"warp_interface_ipv6"
"tgbot"
"tgbot_token"
"tgbot_admins"
)
regex[domain]="^[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*\.[a-zA-Z]{2,}$"
regex[port]="^[1-9][0-9]*$"
regex[warp_license]="^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{8}-[a-zA-Z0-9]{8}$"
regex[username]="^[a-zA-Z0-9]+$"
regex[ip]="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
regex[tgbot_token]="^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$"
regex[tgbot_admins]="^[a-zA-Z][a-zA-Z0-9_]{4,31}(,[a-zA-Z][a-zA-Z0-9_]{4,31})*$"
function show_help {
echo ""
echo "Usage: reality-ezpz.sh [-t|--transport=tcp|http|grpc|ws] [-d|--domain=<domain>] [--server=<server>] [--regenerate] [--default]
[-r|--restart] [--enable-safenet=true|false] [--port=<port>] [-c|--core=xray|sing-box]
[--enable-warp=true|false] [--warp-license=<license>] [--security=reality|letsencrypt|selfsigned] [-m|--menu] [--show-server-config]
[--add-user=<username>] [--lists-users] [--show-user=<username>] [--delete-user=<username>] [-u|--uninstall]"
echo ""
echo " -t, --transport <tcp|http|grpc|ws> Transport protocol (ws, http, grpc, ws, default: ${defaults[transport]})"
echo " -d, --domain <domain> Domain to use as SNI (default: ${defaults[domain]})"
echo " --server <server> IP address or domain name of server (Must be a valid domain if using letsencrypt security)"
echo " --regenerate Regenerate public and private keys"
echo " --default Restore default configuration"
echo " -r --restart Restart services"
echo " -u, --uninstall Uninstall reality"
echo " --enable-safenet <true|false> Enable or disable safenet (blocking malware and adult content)"
echo " --port <port> Server port (default: ${defaults[port]})"
echo " --enable-warp <true|false> Enable or disable Cloudflare warp"
echo " --warp-license <warp-license> Add Cloudflare warp+ license"
echo " -c --core <sing-box|xray> Select core (xray, sing-box, default: ${defaults[core]})"
echo " --security <reality|letsencrypt|selfsigned> Select type of TLS encryption (reality, letsencrypt, selfsigned, default: ${defaults[security]})"
echo " -m --menu Show menu"
echo " --enable-tgbot <true|false> Enable Telegram bot for user management"
echo " --tgbot-token <token> Token of Telegram bot"
echo " --tgbot-admins <telegram-username> Usernames of telegram bot admins (Comma separated list of usernames without leading '@')"
echo " --show-server-config Print server configuration"
echo " --add-user <username> Add new user"
echo " --list-users List all users"
echo " --show-user <username> Shows the config and QR code of the user"
echo " --delete-user <username> Delete the user"
echo " -h, --help Display this help message"
return 1
}
function parse_args {
local opts
opts=$(getopt -o t:d:ruc:mh --long transport:,domain:,server:,regenerate,default,restart,uninstall,enable-safenet:,port:,warp-license:,enable-warp:,core:,security:,menu,show-server-config,add-user:,list-users,show-user:,delete-user:,enable-tgbot:,tgbot-token:,tgbot-admins:,help -- "$@")
if [[ $? -ne 0 ]]; then
return 1
fi
eval set -- "$opts"
while true; do
case $1 in
-t|--transport)
args[transport]="$2"
case ${args[transport]} in
tcp|http|grpc|ws)
shift 2
;;
*)
echo "Invalid transport protocol: ${args[transport]}"
return 1
;;
esac
;;
-d|--domain)
args[domain]="$2"
if ! [[ ${args[domain]} =~ ${regex[domain]} ]]; then
echo "Invalid domain: ${args[domain]}"
return 1
fi
shift 2
;;
--server)
args[server]="$2"
if ! [[ ${args[server]} =~ ${regex[domain]} || ${args[server]} =~ ${regex[ip]} ]]; then
echo "Invalid server: ${args[domain]}"
return 1
fi
shift 2
;;
--regenerate)
args[regenerate]=true
shift
;;
--default)
args[default]=true
shift
;;
-r|--restart)
args[restart]=true
shift
;;
-u|--uninstall)
args[uninstall]=true
shift
;;
--enable-safenet)
case "$2" in
true|false)
$2 && args[safenet]=ON || args[safenet]=OFF
shift 2
;;
*)
echo "Invalid safenet option: $2"
return 1
;;
esac
;;
--enable-warp)
case "$2" in
true|false)
$2 && args[warp]=ON || args[warp]=OFF
shift 2
;;
*)
echo "Invalid warp option: $2"
return 1
;;
esac
;;
--port)
args[port]="$2"
if ! [[ ${args[port]} =~ ${regex[port]} ]]; then
echo "Invalid port number: ${args[port]}"
return 1
elif ((args[port] < 1 || args[port] > 65535)); then
echo "Port number out of range: ${args[port]}"
return 1
fi
shift 2
;;
--warp-license)
args[warp_license]="$2"
if ! [[ ${args[warp_license]} =~ ${regex[warp_license]} ]]; then
echo "Invalid warp license: ${args[warp_license]}"
return 1
fi
shift 2
;;
-c|--core)
args[core]="$2"
case ${args[core]} in
xray|sing-box)
shift 2
;;
*)
echo "Invalid core: ${args[core]}"
return 1
;;
esac
;;
--security)
args[security]="$2"
case ${args[security]} in
reality|letsencrypt|selfsigned)
shift 2
;;
*)
echo "Invalid TLS security option: ${args[security]}"
return 1
;;
esac
;;
-m|--menu)
args[menu]=true
shift
;;
--enable-tgbot)
case "$2" in
true|false)
$2 && args[tgbot]=ON || args[tgbot]=OFF
shift 2
;;
*)
echo "Invalid enable-tgbot option: $2"
return 1
;;
esac
;;
--tgbot-token)
args[tgbot_token]="$2"
if [[ ! ${args[tgbot_token]} =~ ${regex[tgbot_token]} ]]; then
echo "Invalid Telegram Bot Token: ${args[tgbot_token]}"
return 1
fi
if ! curl -sSfL "https://api.telegram.org/bot${args[tgbot_token]}/getMe" >/dev/null 2>&1; then
echo "Invalid Telegram Bot Token: Telegram Bot Token is incorrect. Check it again."
return 1
fi
shift 2
;;
--tgbot-admins)
args[tgbot_admins]="$2"
if [[ ! ${args[tgbot_admins]} =~ ${regex[tgbot_admins]} || $tgbot_admins =~ .+_$ || $tgbot_admins =~ .+_,.+ ]]; then
echo "Invalid Telegram Bot Admins Username: ${args[tgbot_admins]}\nThe usernames must separated by ',' without leading '@' character or any extra space."
return 1
fi
shift 2
;;
--show-server-config)
args[server-config]=true
shift
;;
--add-user)
args[add_user]="$2"
if ! [[ ${args[add_user]} =~ ${regex[username]} ]]; then
echo "Invalid username: ${args[add_user]}\nUsername can only contains A-Z, a-z and 0-9"
return 1
fi
shift 2
;;
--list-users)
args[list_users]=true
shift
;;
--show-user)
args[show_config]="$2"
shift 2
;;
--delete-user)
args[delete_user]="$2"
shift 2
;;
-h|--help)
return 1
;;
--)
shift
break
;;
*)
echo "Unknown option: $1"
return 1
;;
esac
done
if [[ ${args[uninstall]} == true ]]; then
uninstall
fi
if [[ -n ${args[warp_license]} ]]; then
args[warp]=ON
fi
}
function dict_expander {
local -n dict=$1
for key in "${!dict[@]}"; do
echo "${key} ${dict[$key]}"
done
}
function parse_config_file {
if [[ ! -r "${path[config]}" ]]; then
generate_keys
return 0
fi
while IFS= read -r line; do
if [[ "${line}" =~ ^\s*# ]] || [[ "${line}" =~ ^\s*$ ]]; then
continue
fi
key=$(echo "$line" | cut -d "=" -f 1)
value=$(echo "$line" | cut -d "=" -f 2-)
config_file["${key}"]="${value}"
done < "${path[config]}"
if [[ -z "${config_file[public_key]}" || \
-z "${config_file[private_key]}" || \
-z "${config_file[short_id]}" || \
-z "${config_file[service_path]}" ]]; then
generate_keys
fi
return 0
}
function parse_users_file {
mkdir -p "$config_path"
touch "${path[users]}"
while read -r line; do
if [[ "${line}" =~ ^\s*# ]] || [[ "${line}" =~ ^\s*$ ]]; then
continue
fi
IFS="=" read -r key value <<< "${line}"
users["${key}"]="${value}"
done < "${path[users]}"
if [[ -n ${args[add_user]} ]]; then
if [[ -z "${users["${args[add_user]}"]}" ]]; then
users["${args[add_user]}"]=$(cat /proc/sys/kernel/random/uuid)
else
echo 'User "'"${args[add_user]}"'" already exists.'
fi
fi
if [[ -n ${args[delete_user]} ]]; then
if [[ -n "${users["${args[delete_user]}"]}" ]]; then
if [[ ${#users[@]} -eq 1 ]]; then
echo -e "You cannot delete the only user.\nAt least one user is needed.\nCreate a new user, then delete this one."
exit 1
fi
unset users["${args[delete_user]}"]
else
echo "User "${args[delete_user]}" does not exists."
exit 1
fi
fi
if [[ ${#users[@]} -eq 0 ]]; then
users[RealityEZPZ]=$(cat /proc/sys/kernel/random/uuid)
echo "RealityEZPZ=${users[RealityEZPZ]}" >> "${path[users]}"
return 0
fi
return 0
}
function restore_defaults {
local defaults_items=("${!defaults[@]}")
local keep=false
local exclude_list=(
"warp_license"
"tgbot_token"
)
if [[ -n ${config[warp_id]} && -n ${config[warp_token]} ]]; then
warp_delete_account "${config[warp_id]}" "${config[warp_token]}"
fi
for item in "${defaults_items[@]}"; do
keep=false
for i in "${exclude_list[@]}"; do
if [[ "${i}" == "${item}" ]]; then
keep=true
break
fi
done
if [[ ${keep} == true ]]; then
continue
fi
config["${item}"]="${defaults[${item}]}"
done
}
function build_config {
local free_80=true
if [[ ${args[regenerate]} == true ]]; then
generate_keys
fi
for item in "${config_items[@]}"; do
if [[ -n ${args["${item}"]} ]]; then
config["${item}"]="${args[${item}]}"
elif [[ -n ${config_file["${item}"]} ]]; then
config["${item}"]="${config_file[${item}]}"
else
config["${item}"]="${defaults[${item}]}"
fi
done
if [[ ${args[default]} == true ]]; then
restore_defaults
return 0
fi
if [[ ${config[tgbot]} == 'ON' && -z ${config[tgbot_token]} ]]; then
echo 'To enable Telegram bot, you have to give the token of bot with --tgbot-token option.'
exit 1
fi
if [[ ${config[tgbot]} == 'ON' && -z ${config[tgbot_admins]} ]]; then
echo 'To enable Telegram bot, you have to give the list of authorized Telegram admins username with --tgbot-admins option.'
exit 1
fi
if [[ ${config[warp]} == 'ON' && -z ${config[warp_license]} ]]; then
echo 'To enable WARP+, you have to give WARP+ license with --warp-license option.'
exit 1
fi
if [[ ! ${config[server]} =~ ${regex[domain]} && ${config[security]} == 'letsencrypt' ]]; then
echo 'You have to assign a domain to server with "--server <domain>" option if you want to use "letsencrypt" as TLS certifcate.'
exit 1
fi
if [[ ${config[transport]} == 'ws' && ${config[security]} == 'reality' ]]; then
echo 'You cannot use "ws" transport with "reality" TLS certificate. Use other transports or change TLS certifcate to letsencrypt or selfsigned'
exit 1
fi
if [[ ${config[security]} == 'letsencrypt' && ${config[port]} -ne 443 ]]; then
if lsof -i :80 >/dev/null 2>&1; then
free_80=false
for container in $(${docker_cmd} -p ${compose_project} ps -q); do
if docker port "${container}"| grep '0.0.0.0:80' >/dev/null 2>&1; then
free_80=true
break
fi
done
fi
if [[ ${free_80} != 'true' ]]; then
echo 'Port 80 must be free if you want to use "letsencrypt" as the security option.'
exit 1
fi
fi
if [[ -n "${args[security]}" && "${args[security]}" == 'reality' && "${config_file[security]}" != 'reality' ]]; then
config[domain]="${defaults[domain]}"
fi
if [[ -n "${args[security]}" && "${args[security]}" != 'reality' && "${config_file[security]}" == 'reality' ]]; then
config[domain]="${config[server]}"
fi
if [[ -n "${args[server]}" && "${config[security]}" != 'reality' ]]; then
config[domain]="${config[server]}"
fi
if [[ -n "${args[warp]}" && "${args[warp]}" == 'OFF' && "${config_file[warp]}" == 'ON' ]]; then
if [[ -n ${config[warp_id]} && -n ${config[warp_token]} ]]; then
warp_delete_account "${config[warp_id]}" "${config[warp_token]}"
fi
fi
if { [[ -n "${args[warp]}" && "${args[warp]}" == 'ON' && "${config_file[warp]}" == 'OFF' ]] || \
[[ "${config[warp]}" == 'ON' && ( -z ${config[warp_private_key]} || \
-z ${config[warp_token]} || \
-z ${config[warp_id]} || \
-z ${config[warp_client_id]} || \
-z ${config[warp_interface_ipv4]} || \
-z ${config[warp_interface_ipv6]} ) ]]; }; then
config[warp]='OFF'
warp_create_account || exit 1
warp_add_license "${config[warp_id]}" "${config[warp_token]}" "${config[warp_license]}" || exit 1
config[warp]='ON'
fi
if [[ -n ${args[warp_license]} && -n ${config_file[warp_license]} && "${args[warp_license]}" != "${config_file[warp_license]}" ]]; then
if ! warp_add_license "${config[warp_id]}" "${config[warp_token]}" "${args[warp_license]}"; then
config[warp]='OFF'
config[warp_license]=""
warp_delete_account "${config[warp_id]}" "${config[warp_token]}"
echo "WARP has been disabled due to the license error."
fi
fi
}
function update_config_file {
mkdir -p "${config_path}"
touch "${path[config]}"
for item in "${config_items[@]}"; do
if grep -q "^${item}=" "${path[config]}"; then
sed -i "s|^${item}=.*|${item}=${config[${item}]}|" "${path[config]}"
else
echo "${item}=${config[${item}]}" >> "${path[config]}"
fi
done
check_reload
}
function update_users_file {
rm -f "${path[users]}"
for user in "${!users[@]}"; do
echo "${user}=${users[${user}]}" >> "${path[users]}"
done
check_reload
}
function generate_keys {
local key_pair
key_pair=$(docker run --rm ${image[xray]} xray x25519)
config_file[public_key]=$(echo "${key_pair}" | grep 'Public key:' | awk '{print $3}')
config_file[private_key]=$(echo "${key_pair}" | grep 'Private key:' | awk '{print $3}')
config_file[short_id]=$(openssl rand -hex 8)
config_file[service_path]=$(openssl rand -hex 4)
}
function uninstall {
if docker compose >/dev/null 2>&1; then
docker compose --project-directory "${config_path}" down --timeout 2 || true
docker compose --project-directory "${config_path}" -p ${compose_project} down --timeout 2 || true
docker compose --project-directory "${config_path}/tgbot" -p ${tgbot_project} down --timeout 2 || true
elif which docker-compose >/dev/null 2>&1; then
docker-compose --project-directory "${config_path}" down --timeout 2 || true
docker-compose --project-directory "${config_path}" -p ${compose_project} down --timeout 2 || true
docker-compose --project-directory "${config_path}/tgbot" -p ${tgbot_project} down --timeout 2 || true
fi
rm -rf "${config_path}"
exit 0
}
function install_packages {
if ! which qrencode whiptail jq >/dev/null 2>&1; then
if which apt >/dev/null 2>&1; then
apt update
apt install qrencode whiptail jq -y
return 0
fi
if which yum >/dev/null 2>&1; then
yum makecache
yum install epel-release -y || true
yum install qrencode newt jq -y
return 0
fi
echo "OS is not supported!"
return 1
fi
}
function install_docker {
if ! which docker >/dev/null 2>&1; then
curl -fsSL https://get.docker.com | bash
systemctl enable --now docker
docker_cmd="docker compose"
return 0
fi
if docker compose >/dev/null 2>&1; then
docker_cmd="docker compose"
return 0
fi
if which docker-compose >/dev/null 2>&1; then
docker_cmd="docker-compose"
return 0
fi
curl -fsSL https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker_cmd="docker-compose"
return 0
}
function generate_docker_compose {
cat >"${path[compose]}" <<EOF
version: "3"
networks:
reality:
driver: bridge
ipam:
config:
- subnet: 10.255.255.0/24
gateway: 10.255.255.1
services:
engine:
image: ${image[${config[core]}]}
$([[ ${config[security]} == 'reality' ]] && echo "ports:" || true)
$([[ ${config[security]} == 'reality' && ${config[port]} -eq 443 ]] && echo '- 80:8080' || true)
$([[ ${config[security]} == 'reality' ]] && echo "- ${config[port]}:8443" || true)
$([[ ${config[security]} != 'reality' ]] && echo "expose:" || true)
$([[ ${config[security]} != 'reality' ]] && echo "- 8443" || true)
restart: always
environment:
TZ: Etc/UTC
volumes:
- ./${path[engine]#${config_path}/}:/etc/${config[core]}/config.json
$([[ ${config[security]} != 'reality' ]] && { [[ ${config[transport]} == 'http' ]] || [[ ${config[transport]} == 'tcp' ]]; } && echo "- ./${path[server_crt]#${config_path}/}:/etc/${config[core]}/server.crt" || true)
$([[ ${config[security]} != 'reality' ]] && { [[ ${config[transport]} == 'http' ]] || [[ ${config[transport]} == 'tcp' ]]; } && echo "- ./${path[server_key]#${config_path}/}:/etc/${config[core]}/server.key" || true)
networks:
- reality
$(if [[ ${config[security]} != 'reality' ]]; then
echo "
nginx:
image: ${image[nginx]}
expose:
- 80
restart: always
networks:
- reality
haproxy:
image: ${image[haproxy]}
ports:
$([[ ${config[security]} == 'letsencrypt' || ${config[port]} -eq 443 ]] && echo '- 80:80' || true)
- ${config[port]}:443
restart: always
volumes:
- ./${path[haproxy]#${config_path}/}:/usr/local/etc/haproxy/haproxy.cfg
- ./${path[server_pem]#${config_path}/}:/usr/local/etc/haproxy/server.pem
networks:
- reality"
fi)
$(if [[ ${config[security]} == 'letsencrypt' ]]; then
echo "
certbot:
build:
context: ./certbot
expose:
- 80
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./certbot/data:/etc/letsencrypt
- ./$(dirname "${path[server_pem]#${config_path}/}"):/certificate
- ./${path[certbot_deployhook]#${config_path}/}:/deployhook.sh
- ./${path[certbot_startup]#${config_path}/}:/startup.sh
networks:
- reality
entrypoint: /bin/sh
command: /startup.sh"
fi)
EOF
}
function generate_tgbot_compose {
cat >"${path[tgbot_compose]}" <<EOF
version: "3"
services:
tgbot:
build: ./
restart: always
environment:
BOT_TOKEN: ${config[tgbot_token]}
BOT_ADMIN: ${config[tgbot_admins]}
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ../:/opt/reality-ezpz
EOF
}
function generate_haproxy_config {
cat >"${path[haproxy]}" << EOF
global
ssl-default-bind-options ssl-min-ver TLSv1.2
defaults
retries 3
option http-server-close
timeout connect 5s
timeout client 5s
timeout client-fin 1s
timeout server-fin 1s
timeout server 5s
timeout tunnel 300s
timeout http-keep-alive 1s
timeout http-request 5s
timeout queue 15s
timeout tarpit 5s
frontend http
mode http
bind :80
$([[ ${config[security]} == 'letsencrypt' ]] && echo 'use_backend certbot if { path_beg /.well-known/acme-challenge }' || true)
$([[ ${config[security]} == 'letsencrypt' ]] && echo 'acl letsencrypt-acl path_beg /.well-known/acme-challenge' || true)
$([[ ${config[security]} == 'letsencrypt' ]] && echo 'redirect scheme https if !letsencrypt-acl' || true)
use_backend default
frontend tls
bind :443 $([[ ${config[transport]} != 'tcp' ]] && echo 'ssl crt /usr/local/etc/haproxy/server.pem alpn h2,http/1.1' || true)
mode $([[ ${config[transport]} != 'tcp' ]] && echo 'http' || echo 'tcp')
$([[ ${config[transport]} != 'tcp' ]] && echo "http-request set-header Host ${config[server]}" || true)
$([[ ${config[security]} == 'letsencrypt' && ${config[transport]} != 'tcp' ]] && echo 'use_backend certbot if { path_beg /.well-known/acme-challenge }' || true)
use_backend engine $([[ ${config[transport]} != 'tcp' ]] && echo "if { path_beg /${config[service_path]} }" || true)
$([[ ${config[transport]} != 'tcp' ]] && echo 'use_backend default' || true)
backend engine
retry-on conn-failure empty-response response-timeout
mode $([[ ${config[transport]} != 'tcp' ]] && echo 'http' || echo 'tcp')
server engine engine:8443 check tfo $([[ ${config[transport]} == 'grpc' ]] && echo 'proto h2' || true) $([[ ${config[transport]} == 'http' ]] && echo 'ssl verify none' "$([[ ${config[core]} == sing-box ]] && echo 'proto h2' || true)" || true)
$([[ ${config[security]} == 'letsencrypt' ]] && echo 'backend certbot' || true)
$([[ ${config[security]} == 'letsencrypt' ]] && echo ' mode http' || true)
$([[ ${config[security]} == 'letsencrypt' ]] && echo ' server certbot certbot:80' || true)
backend default
mode http
server nginx nginx:80
EOF
}
function generate_certbot_script {
cat >"${path[certbot_startup]}" << EOF
#!/bin/sh
trap exit TERM
fullchain_path=/etc/letsencrypt/live/${config[server]}/fullchain.pem
if [[ -r "\${fullchain_path}" ]]; then
fullchain_fingerprint=\$(openssl x509 -noout -fingerprint -sha256 -in "\${fullchain_path}" 2>/dev/null |\
awk -F= '{print \$2}' | tr -d : | tr '[:upper:]' '[:lower:]')
installed_fingerprint=\$(openssl x509 -noout -fingerprint -sha256 -in /certificate/server.pem 2>/dev/null |\
awk -F= '{print \$2}' | tr -d : | tr '[:upper:]' '[:lower:]')
if [[ \$fullchain_fingerprint != \$installed_fingerprint ]]; then
/deployhook.sh /certificate ${compose_project} ${config[server]} ${service[server_crt]} $([[ ${config[transport]} != 'tcp' ]] && echo "${service[server_pem]}" || true)
fi
fi
while true; do
response=\$(curl -skL --max-time 3 http://${config[server]})
if echo "\$response" | grep 'Welcome to nginx!' >/dev/null; then
break
fi
echo "Domain ${config[server]} is not pointing to the server"
sleep 5
done
while true; do
certbot certonly -n \\
--standalone \\
--key-type ecdsa \\
--elliptic-curve secp256r1 \\
--agree-tos \\
--register-unsafely-without-email \\
-d ${config[server]} \\
--deploy-hook "/deployhook.sh /certificate ${compose_project} ${config[server]} ${service[server_crt]} $([[ ${config[transport]} != 'tcp' ]] && echo "${service[server_pem]}" || true)"
sleep 1h &
wait \${!}
done
EOF
}
function generate_certbot_deployhook {
cat >"${path[certbot_deployhook]}" << EOF
#!/bin/sh
cert_path=\$1
compose_project=\$2
domain=\$3
renewed_path=/etc/letsencrypt/live/\$domain
cat "\$renewed_path/fullchain.pem" > "\$cert_path/server.crt"
cat "\$renewed_path/privkey.pem" > "\$cert_path/server.key"
cat "\$renewed_path/fullchain.pem" "\$renewed_path/privkey.pem" > "\$cert_path/server.pem"
i=4
while [ \$i -le \$# ]; do
eval service=\\\${\$i}
docker compose -p "${compose_project}" restart --timeout 2 "\$service"
i=\$((i+1))
done
EOF
chmod +x "${path[certbot_deployhook]}"
}
function generate_certbot_dockerfile {
cat >"${path[certbot_dockerfile]}" << EOF
FROM ${image[certbot]}
RUN apk add --no-cache docker-cli-compose curl
EOF
}
function generate_tgbot_dockerfile {
cat >"${path[tgbot_dockerfile]}" << EOF
FROM ${image[python]}
WORKDIR /opt/reality-ezpz/tgbot
RUN apk add --no-cache docker-cli-compose curl bash newt libqrencode sudo openssl jq
RUN pip install --no-cache-dir python-telegram-bot==13.5
CMD [ "python", "./tgbot.py" ]
EOF
}
function download_tgbot_script {
curl -fsSL https://raw.githubusercontent.com/aleskxyz/reality-ezpz/master/tgbot.py -o "${path[tgbot_script]}"
}
function generate_selfsigned_certificate {
openssl ecparam -name prime256v1 -genkey -out "${path[server_key]}"
openssl req -new -key "${path[server_key]}" -out /tmp/server.csr -subj "/CN=${config[server]}"
openssl x509 -req -days 365 -in /tmp/server.csr -signkey "${path[server_key]}" -out "${path[server_crt]}"
cat "${path[server_key]}" "${path[server_crt]}" > "${path[server_pem]}"
rm -f /tmp/server.csr
}
function generate_engine_config {
local users_object=""
local reality_object=""
local tls_object=""
local warp_object=""
if [[ ${config[core]} == 'sing-box' ]]; then
reality_object='"tls": {
"enabled": true,
"server_name": "'"${config[domain]}"'",
"alpn": [],
"reality": {
"enabled": true,
"handshake": {
"server": "'"${config[domain]}"'",
"server_port": 443
},
"private_key": "'"${config[private_key]}"'",
"short_id": ["'"${config[short_id]}"'"],
"max_time_difference": "1m"
}
}'
tls_object='"tls": {
"enabled": true,
"certificate_path": "/etc/sing-box/server.crt",
"key_path": "/etc/sing-box/server.key"
}'
if [[ ${config[warp]} == 'ON' ]]; then
warp_object='{
"type": "wireguard",
"server": "engage.cloudflareclient.com",
"server_port": 2408,
"system_interface": false,
"local_address": [
"'"${config[warp_interface_ipv4]}"'/32",
"'"${config[warp_interface_ipv6]}"'/128"
],
"private_key": "'"${config[warp_private_key]}"'",
"peer_public_key": "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
"reserved": '"$(warp_decode_reserved "${config[warp_client_id]}")"',
"mtu": 1280
},'
fi
for user in "${!users[@]}"; do
if [ -n "$users_object" ]; then
users_object="${users_object},"$'\n'
fi
users_object=${users_object}'{"uuid": "'"${users[${user}]}"'", "flow": "'"$([[ ${config[transport]} == 'tcp' ]] && echo 'xtls-rprx-vision' || true)"'", "name": "'"${user}"'"}'
done
cat >"${path[engine]}" <<EOF
{
"log": {
"level": "error",
"timestamp": true
},
"dns": {
"servers": [
$([[ ${config[safenet]} == ON ]] && echo '{"address": "tcp://1.1.1.3", "detour": "dns"},{"address": "tcp://1.0.0.3", "detour": "dns"}' || echo '{"address": "tcp://1.1.1.1", "detour": "dns"},{"address": "tcp://1.0.0.1", "detour": "dns"}')
],
"strategy": "ipv4_only"
},
"inbounds": [
{
"type": "direct",
"listen": "::",
"listen_port": 8080,
"network": "tcp",
"override_address": "${config[domain]}",
"override_port": 80
},
{
"type": "vless",
"listen": "::",
"listen_port": 8443,
"sniff": true,
"sniff_override_destination": true,
"domain_strategy": "ipv4_only",
"users": [${users_object}],
$(if [[ ${config[security]} == 'reality' ]]; then
echo "${reality_object}"
elif [[ ${config[transport]} == 'http' || ${config[transport]} == 'tcp' ]]; then
echo "${tls_object}"
else
echo '"tls":{"enabled": false}'
fi)
$( if [[ ${config[transport]} == http ]]; then
echo ',"transport": {"type": "http", "host": ["'"${config[server]}"'"], "path": "/'"${config[service_path]}"'"}'
fi
if [[ ${config[transport]} == grpc ]]; then
echo ',"transport": {"type": "grpc","service_name": "'"${config[service_path]}"'"}'
fi
if [[ ${config[transport]} == ws ]]; then
echo ',"transport": {"type": "ws", "headers": {"Host": "'"${config[server]}"'"}, "path": "/'"${config[service_path]}"'"}'
fi
)
}
],
"outbounds": [
$([[ ${config[warp]} == ON ]] && echo "${warp_object}" || echo '{"type": "direct"},')
{
"type": "direct",
"tag": "dns"
},
{
"type": "block",
"tag": "block"
}
],
"route": {
"rules": [
{
"geoip": [
$([[ ${config[warp]} == OFF ]] && echo '"cn", "ir",')
"private"
],
"outbound": "block"
},
{
"geosite": [
$([[ ${config[safenet]} == ON ]] && echo '"category-porn",' || true)
"category-ads-all"
],
"outbound": "block"
},
{
"ip_cidr": [
"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"
],
"outbound": "block"
},
{
"network": "tcp",
"port": [
25,
587,
465,
2525
],
"outbound": "block"
},
{
"domain": [
"pushnotificationws.com",
"sunlight-leds.com",
"icecyber.org"
],
"outbound": "block"
}
]
}
}
EOF
fi
if [[ ${config[core]} == 'xray' ]]; then
reality_object='"security":"reality",
"realitySettings":{
"show": false,
"dest": "'"${config[domain]}"':443",
"xver": 0,
"serverNames": ["'"${config[domain]}"'"],
"privateKey": "'"${config[private_key]}"'",
"maxTimeDiff": 60000,
"shortIds": ["'"${config[short_id]}"'"]
}'
tls_object='"security": "tls",
"tlsSettings": {
"certificates": [{
"oneTimeLoading": true,
"certificateFile": "/etc/xray/server.crt",
"keyFile": "/etc/xray/server.key"
}]
}'