-
Notifications
You must be signed in to change notification settings - Fork 188
/
reality-ezpz.sh
2662 lines (2583 loc) · 88.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.4"
image[sing-box]="gzxhwq/sing-box:1.8.14"
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"
image[wgcf]="virb3/wgcf:2.2.18"
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://cloudflare.com/cdn-cgi/trace | grep ip | cut -d '=' -f2)
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})*$"
regex[domain_port]="^[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*\.[a-zA-Z]{2,}(:[1-9][0-9]*)?$"
regex[file_path]="^[a-zA-Z0-9_/.-]+$"
regex[url]="^(http|https)://([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|[0-9]{1,3}(\.[0-9]{1,3}){3})(:[0-9]{1,5})?(/.*)?$"
function show_help {
echo ""
echo "Usage: reality-ezpz.sh [-t|--transport=tcp|http|grpc|ws|tuic|hysteria2|shadowtls] [-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>] [--backup] [--restore=<url|file>] [--backup-password=<password>] [-u|--uninstall]"
echo ""
echo " -t, --transport <tcp|http|grpc|ws|tuic|hysteria2|shadowtls> Transport protocol (tcp, http, grpc, ws, tuic, hysteria2, shadowtls, 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 " --backup Backup users and configuration and upload it to temp.sh"
echo " --restore <url|file> Restore backup from URL or file"
echo " --backup-password <password> Create/Restore password protected backup file"
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:,backup,restore:,backup-password:,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|tuic|hysteria2|shadowtls)
shift 2
;;
*)
echo "Invalid transport protocol: ${args[transport]}"
return 1
;;
esac
;;
-d|--domain)
args[domain]="$2"
if ! [[ ${args[domain]} =~ ${regex[domain_port]} ]]; 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 -m 3 "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
;;
--backup)
args[backup]=true
shift
;;
--restore)
args[restore]="$2"
if [[ ! ${args[restore]} =~ ${regex[file_path]} ]] && [[ ! ${args[restore]} =~ ${regex[url]} ]]; then
echo "Invalid: Backup file path or URL is not valid."
return 1
fi
shift 2
;;
--backup-password)
args[backup_password]="$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 backup {
local backup_name
local backup_password="$1"
local backup_file_url
local exit_code
backup_name="reality-ezpz-backup-$(date +%Y-%m-%d_%H-%M-%S).zip"
cd "${config_path}"
if [ -z "${backup_password}" ]; then
zip -r "/tmp/${backup_name}" . > /dev/null
else
zip -P "${backup_password}" -r "/tmp/${backup_name}" . > /dev/null
fi
if ! backup_file_url=$(curl -fsS -m 30 -F "file=@/tmp/${backup_name}" "https://temp.sh/upload"); then
rm -f "/tmp/${backup_name}"
echo "Error in uploading backup file" >&2
return 1
fi
rm -f "/tmp/${backup_name}"
echo "${backup_file_url}"
}
function restore {
local backup_file="$1"
local backup_password="$2"
local temp_file
local unzip_output
local unzip_exit_code
local current_state
if [[ ! -r ${backup_file} ]]; then
temp_file=$(mktemp -u)
if [[ "${backup_file}" =~ ^https?://temp\.sh/ ]]; then
if ! curl -fSsL -m 30 -X POST "${backup_file}" -o "${temp_file}"; then
echo "Cannot download or find backup file" >&2
return 1
fi
else
if ! curl -fSsL -m 30 "${backup_file}" -o "${temp_file}"; then
echo "Cannot download or find backup file" >&2
return 1
fi
fi
backup_file="${temp_file}"
fi
current_state=$(set +o)
set +e
if [[ -z "${backup_password}" ]]; then
unzip_output=$(unzip -P "" -t "${backup_file}" 2>&1)
else
unzip_output=$(unzip -P "${backup_password}" -t "${backup_file}" 2>&1)
fi
unzip_exit_code=$?
eval "$current_state"
if [[ ${unzip_exit_code} -eq 0 ]]; then
if ! echo "${unzip_output}" | grep -q 'config'; then
echo "The provided file is not a reality-ezpz backup file." >&2
rm -f "${temp_file}"
return 1
fi
else
if echo "${unzip_output}" | grep -q 'incorrect password'; then
echo "The provided password for backup file is incorrect." >&2
else
echo "An error occurred during zip file verification: ${unzip_output}" >&2
fi
rm -f "${temp_file}"
return 1
fi
rm -rf "${config_path}"
mkdir -p "${config_path}"
set +e
if [[ -z "${backup_password}" ]]; then
unzip_output=$(unzip -d "${config_path}" "${backup_file}" 2>&1)
else
unzip_output=$(unzip -P "${backup_password}" -d "${config_path}" "${backup_file}" 2>&1)
fi
unzip_exit_code=$?
eval "$current_state"
if [[ ${unzip_exit_code} -ne 0 ]]; then
echo "Error in backup restore: ${unzip_output}" >&2
rm -f "${temp_file}"
return 1
fi
rm -f "${temp_file}"
return
}
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[transport]} == 'tuic' && ${config[security]} == 'reality' ]]; then
echo 'You cannot use "tuic" transport with "reality" TLS certificate. Use other transports or change TLS certifcate to letsencrypt or selfsigned'
exit 1
fi
if [[ ${config[transport]} == 'tuic' && ${config[core]} == 'xray' ]]; then
echo 'You cannot use "tuic" transport with "xray" core. Use other transports or change core to sing-box'
exit 1
fi
if [[ ${config[transport]} == 'hysteria2' && ${config[security]} == 'reality' ]]; then
echo 'You cannot use "hysteria2" transport with "reality" TLS certificate. Use other transports or change TLS certifcate to letsencrypt or selfsigned'
exit 1
fi
if [[ ${config[transport]} == 'hysteria2' && ${config[core]} == 'xray' ]]; then
echo 'You cannot use "hysteria2" transport with "xray" core. Use other transports or change core to sing-box'
exit 1
fi
if [[ ${config[transport]} == 'shadowtls' && ${config[core]} == 'xray' ]]; then
echo 'You cannot use "shadowtls" transport with "xray" core. Use other transports or change core to sing-box'
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]}" || -n "${args[transport]}") && ("${args[security]}" == 'reality' || "${args[transport]}" == 'shadowtls') && ("${config_file[security]}" != 'reality' && "${config_file[transport]}" != 'shadowtls') ]]; then
config[domain]="${defaults[domain]}"
fi
if [[ (-n "${args[security]}" || -n "${args[transport]}") && ("${args[security]}" != 'reality' && "${args[transport]}" != 'shadowtls') && ("${config_file[security]}" == 'reality' || "${config_file[transport]}" == 'shadowtls') ]]; then
config[domain]="${config[server]}"
fi
if [[ -n "${args[server]}" && ("${config[security]}" != 'reality' && "${config[transport]}" != 'shadowtls') ]]; 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}"
echo "Reality-EZPZ uninstalled successfully."
exit 0
}
function install_packages {
if [[ -n $BOT_TOKEN ]]; then
return 0
fi
if ! which qrencode whiptail jq xxd zip unzip >/dev/null 2>&1; then
if which apt >/dev/null 2>&1; then
apt update
DEBIAN_FRONTEND=noninteractive apt install qrencode whiptail jq xxd zip unzip -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 vim-common zip unzip -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 -m 5 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 -m 30 https://github.com/docker/compose/releases/download/v2.28.0/docker-compose-linux-$(uname -m) -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
enable_ipv6: true
ipam:
config:
- subnet: fc11::1:0/112
services:
engine:
image: ${image[${config[core]}]}
$([[ ${config[security]} == 'reality' || ${config[transport]} == 'shadowtls' ]] && echo "ports:" || true)
$([[ (${config[security]} == 'reality' || ${config[transport]} == 'shadowtls') && ${config[port]} -eq 443 ]] && echo '- 80:8080' || true)
$([[ ${config[security]} == 'reality' || ${config[transport]} == 'shadowtls' ]] && echo "- ${config[port]}:8443" || true)
$([[ ${config[transport]} == 'tuic' || ${config[transport]} == 'hysteria2' ]] && echo "ports:" || true)
$([[ ${config[transport]} == 'tuic' || ${config[transport]} == 'hysteria2' ]] && echo "- ${config[port]}:8443/udp" || true)
$([[ ${config[security]} != 'reality' && ${config[transport]} != 'shadowtls' ]] && echo "expose:" || true)
$([[ ${config[security]} != 'reality' && ${config[transport]} != 'shadowtls' ]] && 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' ]] || [[ ${config[transport]} == 'tuic' ]] || [[ ${config[transport]} == 'hysteria2' ]]; } && echo "- ./${path[server_crt]#${config_path}/}:/etc/${config[core]}/server.crt" || true)
$([[ ${config[security]} != 'reality' ]] && { [[ ${config[transport]} == 'http' ]] || [[ ${config[transport]} == 'tcp' ]] || [[ ${config[transport]} == 'tuic' ]] || [[ ${config[transport]} == 'hysteria2' ]]; } && echo "- ./${path[server_key]#${config_path}/}:/etc/${config[core]}/server.key" || true)
networks:
- reality
$(if [[ ${config[security]} != 'reality' && ${config[transport]} != 'shadowtls' ]]; then
echo "
nginx:
image: ${image[nginx]}
expose:
- 80
restart: always
volumes:
- ./website:/usr/share/nginx/html
networks:
- reality
haproxy:
image: ${image[haproxy]}
ports:
$([[ ${config[security]} == 'letsencrypt' || ${config[port]} -eq 443 ]] && echo '- 80:8080' || true)
- ${config[port]}:8443
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' && ${config[transport]} != 'shadowtls' ]]; 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
- ./website:/website
networks:
- reality
entrypoint: /bin/sh
command: /startup.sh"
fi)
EOF
}
function generate_tgbot_compose {
cat >"${path[tgbot_compose]}" <<EOF
version: "3"
networks:
tgbot:
driver: bridge
enable_ipv6: true
ipam:
config:
- subnet: fc11::2:0/112
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
- ../:${config_path}
- /etc/docker/:/etc/docker/
networks:
- tgbot
EOF
}
function generate_haproxy_config {
echo "
global
ssl-default-bind-options ssl-min-ver TLSv1.2
defaults
option http-server-close
timeout connect 5s
timeout client 50s
timeout client-fin 1s
timeout server-fin 1s
timeout server 50s
timeout tunnel 50s
timeout http-keep-alive 1s
timeout queue 15s
frontend http
mode http
bind :::8080 v4v6
$(if [[ ${config[security]} == 'letsencrypt' ]]; then echo "
use_backend certbot if { path_beg /.well-known/acme-challenge }
acl letsencrypt-acl path_beg /.well-known/acme-challenge
redirect scheme https if !letsencrypt-acl
"; fi)
use_backend default
frontend tls
$(if [[ ${config[transport]} != 'tcp' ]]; then echo "
bind :::8443 v4v6 ssl crt /usr/local/etc/haproxy/server.pem alpn h2,http/1.1
mode http
http-request set-header Host ${config[server]}
$(if [[ ${config[security]} == 'letsencrypt' ]]; then echo "
use_backend certbot if { path_beg /.well-known/acme-challenge }
"; fi)
$(if [[ ${config[transport]} != 'tuic' && ${config[transport]} != 'hysteria2' ]]; then echo "
use_backend engine if { path_beg /${config[service_path]} }
"; fi)
use_backend default
"; else echo "
bind :::8443 v4v6
mode tcp
use_backend engine
"; fi)
$(if [[ ${config[transport]} != 'tuic' && ${config[transport]} != 'hysteria2' ]]; then echo "
backend engine
retry-on conn-failure empty-response response-timeout
$(if [[ ${config[transport]} != 'tcp' ]]; then echo "
mode http
"; else echo "
mode tcp
"; fi)
$(if [[ ${config[transport]} == 'grpc' ]]; then echo "
server engine engine:8443 check tfo proto h2
"; elif [[ ${config[transport]} == 'http' && ${config[core]} == 'sing-box' ]]; then echo "
server engine engine:8443 check tfo proto h2 ssl verify none
"; elif [[ ${config[transport]} == 'http' && ${config[core]} != 'sing-box' ]]; then echo "
server engine engine:8443 check tfo ssl verify none
"; else echo "
server engine engine:8443 check tfo
"; fi)
"; fi)
$(if [[ ${config[security]} == 'letsencrypt' ]]; then echo "
backend certbot
mode http
server certbot certbot:80
"; fi)
backend default
mode http
server nginx nginx:80
" | grep -vE '^\s*$' > "${path[haproxy]}"
}
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
ls -d /website/* | grep -E '^/website/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\$'|xargs rm -f
uuid=\$(uuidgen)
echo "\$uuid" > "/website/\$uuid"
response=\$(curl -skL --max-time 3 http://${config[server]}/\$uuid)
if echo "\$response" | grep \$uuid >/dev/null; then
break
fi
echo "Domain ${config[server]} is not pointing to the server"
sleep 5
done
ls -d /website/* | grep -E '^/website/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\$'|xargs rm -f
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 uuidgen
EOF
}
function generate_tgbot_dockerfile {
cat >"${path[tgbot_dockerfile]}" << EOF
FROM ${image[python]}
WORKDIR ${config_path}/tgbot
RUN apk add --no-cache docker-cli-compose curl bash newt libqrencode-tools sudo openssl jq zip unzip
RUN pip install --no-cache-dir python-telegram-bot==13.5 qrcode[pil]==7.4.2
CMD [ "python", "./tgbot.py" ]
EOF
}
function download_tgbot_script {
curl -fsSL -m 3 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 type="vless"
local users_object=""
local reality_object=""
local tls_object=""
local warp_object=""
local reality_port=443
local temp_file
if [[ ${config[transport]} == 'tuic' ]]; then