forked from radio24/TorBox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu-onion
executable file
·1993 lines (1930 loc) · 97.6 KB
/
menu-onion
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
# shellcheck disable=SC2001,SC2010,SC2012,SC2016,SC2062,SC2128,SC2178
# This file is part of TorBox, an easy to use anonymizing router based on Raspberry Pi.
# Copyright (C) 2024 Patrick Truffer
# Contact: [email protected]
# Website: https://www.torbox.ch
# Github: https://github.com/radio24/TorBox
#
# The code in this script is an adaption from the incredible OnionJuggler project.
# Copyright (C) 2022 OnionJuggler developers
# Github: https://github.com/nyxnor/onionjuggler
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it is useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# DESCRIPTION
# This file will set up a public .onion site, optionally manage the client
# access authorization and help to share resources over Onion services.
#
# SYNTAX
# ./menu-onion [<SELECTION>]
# <SELECTION> will execute one of the routines, which configures
# TorBox to route the data from an internal to an external interface.
#
#
###### SET VARIABLES ######
#
# SIZE OF THE MENU
#
# How many items do you have in the main menu?
NO_ITEMS=13
#
# How many lines are only for decoration and spaces?
NO_SPACER=5
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_WIDTH_REDUX=60
MENU_HEIGHT_25=25
MENU_HEIGHT_20=20
MENU_HEIGHT_15=15
# MENU_HEIGHT should not exceed 26
MENU_HEIGHT=$((8+NO_ITEMS+NO_SPACER))
MENU_LIST_HEIGHT=$((NO_ITEMS+NO_SPACER))
#Colors
RED='\033[1;31m'
WHITE='\033[1;37m'
NOCOLOR='\033[0m'
#Other variables
TORRC="/etc/tor/torrc"
BAK="/etc/tor/torrc.bak"
TOR_USER="debian-tor"
DATA_DIR_ROOT="/var/lib"
DATA_DIR="${DATA_DIR_ROOT}/tor"
DATA_DIR_OS="${DATA_DIR}/services"
CLIENT_ONION_AUTH_DIR="${DATA_DIR}/onion_auth"
WEBSERVER="nginx"
WEBSITE_DIR="/var/www"
NGINX_DIR="/etc/nginx"
RUNFILE="/home/torbox/torbox/run/torbox.run"
SELECTION=$1
ENTRY_NUMBERS=0
##############################
######## FUNCTIONS ########
# include lib
. /home/torbox/torbox/lib/torbox.lib
# This function imports the configuration and makes some preparations
# TOGGLE16 / TOGGLE17 represents the status of the Onion Servcie mode
read_config()
{
MODE_OS=$(grep "^HiddenServiceDir" ${TORRC})
if [ -n "$MODE_OS" ]; then
TOGGLE16="Onion Service ON!"
TOGGLE16b="ON"
TOGGLE17b="OFF"
else
TOGGLE16="Onion Service OFF!"
TOGGLE16b="OFF"
TOGGLE17b="ON"
fi
}
# This function tests if a onion service exists.
# Syntax test_service_exists <service_name>
# Following variables can be used:
# $ONION_HOSTNAME
test_service_exists()
{
SERVICE_NAME="${1}"
ONION_HOSTNAME=$(sudo -u "${TOR_USER}" grep -s ".onion" "${DATA_DIR_OS}/${SERVICE_NAME}/hostname")
if [ "${ONION_HOSTNAME}" = "" ]; then
clear
echo -e "${WHITE}[!] ERROR: Service does not exist: ${SERVICE_NAME}${NOCOLOR}"
echo " "
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
# NEW - TorBox v.0.5.3
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
}
# This function creates a list of all onione services
# Syntax create_service_list [<only_with_clients>]
# [<only_with_clients>] will provide a list with services that have at least one client
# Following variables can be used:
# $SERVICE_NAME_LIST is a list with the onion service names (can be separate by newlines (/n))
create_service_list()
{
ONLY_WITH_CLIENTS=$1
SERVICE_NAME_LIST=$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}")
if [ "$ONLY_WITH_CLIENTS" == "only_with_clients" ]; then
i=0
for SERVICE_NAME in $SERVICE_NAME_LIST; do
CLIENT_EXIST="$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/" | sed "s/\.auth//g")"
if [ -n "${CLIENT_EXIST}" ]; then
i=$((i+1))
ONION_HOSTNAME="$(sudo -u "${TOR_USER}" grep -s ".onion" "${DATA_DIR_OS}/${SERVICE_NAME}/hostname")"
ONION_HOSTNAME_WITHOUT_ONION=${ONION_HOSTNAME%.onion}
if [ $i == 1 ]; then SERVICE_LIST="$(printf "%s\n" "${SERVICE_NAME}")"
else SERVICE_LIST="$(printf "%s\n%s\n" "${SERVICE_LIST}" "${SERVICE_NAME}")"; fi
fi
done
SERVICE_NAME_LIST=${SERVICE_LIST}
fi
clear
}
# This function saves the clients names that are inside the <HiddenServiceDir>/authorized_clients/ in list format (CLIENT1,CLIENT2,...)
# Syntax create_client_list [<SERVICE_NAME>]
# Following variables can be used:
# $CLIENT_NAME_LIST is a list with the onion service names (SERV1,SERV2,...)
# $CLIENT_COUNT number of items in the $CLIENT_NAME_LIST
create_client_list()
{
SERVICE_NAME="${1}"
CLIENT_COUNT=0
CLIENT_NAME_LIST="$(printf %s"$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/")" | sed "s/\.auth//g" | tr "\n" ", ")"
CLIENT_COUNT="$(printf %s"${CLIENT_NAME_LIST}" | tr -dc "," | wc -c)"
CLIENT_COUNT=$((CLIENT_COUNT+1))
}
# This function lists the services which have an nginx configuration
# Syntax create_web_service_list <TFS>
# <TFS> 0 -> only shared folders; 1 -> only TFS; 2 -> only TCS; else -> shared folders, TFS and TCS
# Following variables can be used:
# WEB_SERVICE_NAME_LIST is a list with the service names which shares a folder through Nginx (SERV1,SERV2,...)
# VIRTPORT_WEB_SERVICE_LIST is a list with shows on which port the folder is shared through Nginx
# WEB_SERVICE_NAME_FOR_MENU is a list which combines the service name with the port number, which is used by the menu
create_web_service_list(){
TFS="${1}"
WEB_SERVICE_NAME_LIST_WITH_PORT=""
if [ "$TFS" == "0" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion.conf" | sed "s/-onion.conf//g")
elif [ "$TFS" == "1" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-filesharing.conf" | sed "s/-onion-filesharing.conf//g")
elif [ "$TFS" == "2" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-chatsecure.conf" | sed "s/-onion-chatsecure.conf//g")
else WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion.conf" | sed "s/-onion.conf//g"; sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-filesharing.conf" | sed "s/-onion-filesharing.conf//g"; sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-chatsecure.conf" | sed "s/-onion-chatsecure.conf//g"); fi
i=0
for WEB_SERVICE_NAME in $WEB_SERVICE_NAME_LIST_WITH_PORT; do
i=$((i+1))
WEB_SERVICE_NAME=$(rev <<< ${WEB_SERVICE_NAME})
VIRTPORT_WEB_SERVICE=$(cut -d '-' -f1 <<< ${WEB_SERVICE_NAME} | rev)
WEB_SERVICE_NAME=$(cut -d '-' -f2- <<< ${WEB_SERVICE_NAME} | rev)
if [ $i == 1 ]; then
# % represent a space
WEB_SERVICE_NAME_FOR_MENU="$(printf "%s\n" "${WEB_SERVICE_NAME}%Port:%$VIRTPORT_WEB_SERVICE")"
WEB_SERVICE_NAME_LIST="$(printf "%s\n" "${WEB_SERVICE_NAME}")"
VIRTPORT_WEB_SERVICE_LIST="$(printf "%s\n" "${VIRTPORT_WEB_SERVICE}")"
else
WEB_SERVICE_NAME_FOR_MENU="$(printf "%s\n%s\n" "${WEB_SERVICE_NAME_FOR_MENU}" "${WEB_SERVICE_NAME}%Port:%$VIRTPORT_WEB_SERVICE")"
WEB_SERVICE_NAME_LIST="$(printf "%s\n%s\n" "${WEB_SERVICE_NAME_LIST}" "${WEB_SERVICE_NAME}")"
VIRTPORT_WEB_SERVICE_LIST="$(printf "%s\n%s\n" "${VIRTPORT_WEB_SERVICE_LIST}" "${VIRTPORT_WEB_SERVICE}")"
fi
done
}
# This function will generate a new key pair (public and private key) for an operator).
# As a client of an Onion Service, generate a key pair, from which you can give the public key to an Onion Operator.
# Syntax client_auth_on <ONION_HOSTNAME> [<CLIENT_PRIV_KEY>]
client_auth_on()
{
ONION_HOSTNAME="${1}"
CLIENT_PRIV_KEY="${2}"
## removes protocol such as http(s)://, ssh:// and git:// from the front of the address and trailing / at the end of the onion to clean it and only show the hostname (address.onion)
ONION_HOSTNAME="$(printf %s"${ONION_HOSTNAME}\n" | sed "s|.*://||" | sed "s|/$||")"
ONION_HOSTNAME_WITHOUT_ONION="${ONION_HOSTNAME%.onion}"
[ "${ONION_HOSTNAME_WITHOUT_ONION%%*[^a-z2-7]*}" ] || error_onion_invalid 1
[ "${#ONION_HOSTNAME_WITHOUT_ONION}" = "56" ] || error_onion_invalid 2
echo -e "${RED}[+] Activating client Onion Service authorization... ${SERVICE_NAME}...${NOCOLOR}"
(sudo sed -i "s/^#ClientOnionAuthDir/ClientOnionAuthDir/g" ${TORRC}) 2>/dev/null
[ -d "${CLIENT_ONION_AUTH_DIR}" ] || sudo -u "${TOR_USER}" mkdir -p "${CLIENT_ONION_AUTH_DIR}"
if [ -z "${CLIENT_PRIV_KEY}" ]; then
## Generate pem and derive pub and priv keys
openssl genpkey -algorithm x25519 -out /tmp/k1.prv.pem
grep -v "PRIVATE KEY" /tmp/k1.prv.pem | base64pem -d | tail --bytes=32 | base32 | sed 's/=//g' > /tmp/k1.prv.key
openssl pkey -in /tmp/k1.prv.pem -pubout | grep -v "PUBLIC KEY" | base64pem -d | tail -c 32 | base32 | sed 's/=//g' > /tmp/k1.pub.key
## save variables
CLIENT_PUB_KEY=$(cat /tmp/k1.pub.key)
CLIENT_PRIV_KEY=$(cat /tmp/k1.prv.key)
CLIENT_PRIV_KEY_CONFIG="${ONION_HOSTNAME_WITHOUT_ONION}:descriptor:x25519:${CLIENT_PRIV_KEY}"
CLIENT_PUB_KEY_CONFIG="descriptor:x25519:${CLIENT_PUB_KEY}"
## Delete pem and keys
sudo rm -f /tmp/k1.pub.key /tmp/k1.prv.key /tmp/k1.prv.pem
# Client side configuration
printf %s"${CLIENT_PRIV_KEY_CONFIG}\n" | sudo tee "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private" >/dev/null
sudo chown debian-tor:debian-tor "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private"
# Output
echo -e "${WHITE}[!] Client side authorization is now configured!${NOCOLOR}"
echo -e "${RED}[+] Below are the generated keys:${NOCOLOR}"
echo ""
echo -e "${WHITE}Service address = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
echo -e "${WHITE}Private key (saved) = ${RED}${CLIENT_PRIV_KEY}${NOCOLOR}"
echo -e "${WHITE}Private key configuration = ${RED}${CLIENT_PRIV_KEY_CONFIG}${NOCOLOR}"
echo -e "${WHITE}Public key (for the service operator) = ${RED}${CLIENT_PUB_KEY}${NOCOLOR}"
echo -e "${WHITE}Public key configuration = ${RED}${CLIENT_PUB_KEY_CONFIG}${NOCOLOR}"
echo ""
echo -e "${NOCOLOR}Now it depends on the service operator to authorize your client public key"
echo -e "${NOCOLOR}Send the public key to the onion service operator of ${WHITE}${ONION_HOSTNAME}${NOCOLOR} with following instruction:"
echo ""
echo -e ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo -e "Please, create a file with the client name (eg. alice) using the suffix '.auth' (eg. alice.auth) inside the folder"
echo -e "'<HiddenServiceDir>/authorized_clients/' where the service hostname is ${WHITE}${ONION_HOSTNAME}${NOCOLOR} and restart tor by using this command:"
echo -e "${WHITE}printf '${CLIENT_PUB_KEY_CONFIG}' | sudo tee /var/lib/tor/${ONION_HOSTNAME}/authorized_clients/alice.auth${NOCOLOR}"
echo -e "${WHITE}sudo chown -R debian-tor:debian-tor /var/lib/tor/${ONION_HOSTNAME}/authorized_clients/alice.auth${NOCOLOR}"
echo -e "${WHITE}sudo systemctl restart tor${NOCOLOR}"
printf "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
else
# Client side configuration
CLIENT_PRIV_KEY_CONFIG="${ONION_HOSTNAME_WITHOUT_ONION}:descriptor:x25519:${CLIENT_PRIV_KEY}"
printf %s"${CLIENT_PRIV_KEY_CONFIG}\n" | sudo tee "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private" >/dev/null
sudo chown debian-tor:debian-tor "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private"
# Output
echo -e "${WHITE}[!] Client side authorization is now configured!${NOCOLOR}"
echo ""
echo -e "${WHITE}Service address = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
echo -e "${WHITE}Private key (saved) = ${RED}${CLIENT_PRIV_KEY}${NOCOLOR}"
echo -e "${WHITE}Private key configuration = ${RED}${CLIENT_PRIV_KEY_CONFIG}${NOCOLOR}"
echo ""
echo -e "${NOCOLOR}As you inserted the private key manually, it ise expected that you have already sent/received the public key to/from the Onion Service operator."
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
fi
clear
echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
sudo systemctl restart tor
echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
sleep 2
}
# This function shows the .onion address and all necessary information to use it.
# Syntax show_onion_address <service_name>
show_onion_address()
{
SERVICE_NAME="${1}"
test_service_exists "${SERVICE_NAME}"
echo ""
echo -e "${WHITE}Service name = ${RED}${SERVICE_NAME}${NOCOLOR}"
echo ""
qrencode -m 2 -t ANSIUTF8 "${ONION_HOSTNAME}"
echo ""
echo -e "${WHITE}Service address = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
find_virtport "${SERVICE_NAME}"
echo -e "${WHITE}Virtual port = ${RED}${VIRTPORT}${NOCOLOR}"
if sudo grep -qc "^HiddenServiceDir .*/${SERVICE_NAME}$" "${TORRC}"; then
echo -e "${WHITE}Status = ${RED}active${NOCOLOR}"
else
echo -e "${WHITE}Status = ${WHITE}inactive${NOCOLOR}"
fi
create_client_list "${SERVICE_NAME}"
echo ""
echo -e "${WHITE}Client access information${NOCOLOR}"
if [ ! -z "${CLIENT_NAME_LIST}" ]; then
echo -e "${WHITE}Clients = ${RED}${CLIENT_NAME_LIST} (${CLIENT_COUNT})${NOCOLOR}"
for AUTH in $(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/"); do
echo -e "${WHITE}File name : ${RED}${AUTH}${NOCOLOR}"
echo -e "${WHITE}Content : ${RED}$(sudo -u "${TOR_USER}" grep "descriptor:x25519:" "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${AUTH}")${NOCOLOR}"
done
else
echo -e "${RED}If activated, currently, this Onion Service will be ${WHITE}PUBLIC${RED} available.${NOCOLOR}"
echo "If you want to restrict client's access, you have to control the client access by"
echo "generating a key pair (menu entry 7), sending the client his private key or"
echo "registering a client's public key (menu entry 8) if he is providing you with it."
fi
# This produces WEB_SERVICE_NAME_LIST with all available shared folder and TFS locations
create_web_service_list
echo ""
echo -e "${WHITE}Shared folder / TFS and TCS information${NOCOLOR}"
if [[ ! -z "$WEB_SERVICE_NAME_LIST" && ( -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" || -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" || -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ) ]]; then
[ -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ] && SHARED_FOLDER=$(grep "root" "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | cut -d ";" -f1)
[ -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ] && SHARED_FOLDER=$(grep "# Path to shared dir" "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | cut -d ";" -f1)
[ ! -z "$SHARED_FOLDER" ] && echo -e "${RED}Shared folder = ${WHITE}$SHARED_FOLDER${NOCOLOR}"
if grep -q -c "^TFS-${SERVICE_NAME}" ${RUNFILE}; then echo -e "${WHITE}TFS is configured ${RED}for this service address. You can change that with menu entry 18. If started, it is accessible${NOCOLOR}"
elif grep -q -c "^TCS-${SERVICE_NAME}" ${RUNFILE}; then echo -e "${WHITE}TCS is configured ${RED}for this service address. You can change that with menu entry 19. If started, it is accessible${NOCOLOR}"
else echo -e "${RED}The folder is shared by Nginx. You can change that with menu entry 17. If started, it is accessible${NOCOLOR}"; fi
if [ "$VIRTPORT" == "80" ]; then echo -e "${RED}through the service address ${WHITE}${ONION_HOSTNAME}${NOCOLOR}"
else echo -e "${RED}through the service address ${WHITE}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"; fi
else
echo -e "${RED}Currently, the Onion Service above is ${WHITE}not accessible${NOCOLOR}."
echo -e "To change that, you have to start sharing a folder, configuring TFS or TCS (menu entry 17, 18 or 19)."
fi
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
}
# This function configures the TFS in the run file and starts TFS.
# Syntax configure_tfs <TFS_MESSAGE>
# <choice>: from the selection if downloads and/or uploads are allowed
# This function needs the following special variables:
# CHOICE, TFS_PATH, TFS_MESSAGE, SERVICE_NAME
configure_tfs()
{
TFS_MESSAGE=$1
allow_ud=0
if [ ! -z "$CHOICE3" ]; then
mapfile -t CHOICE3 <<< "$CHOICE3"
for ARGUMENT in "${CHOICE3[@]}"; do
if [ $ARGUMENT = 1 ]; then allow_ud=$((allow_ud+1))
elif [ $ARGUMENT = 2 ]; then allow_ud=$((allow_ud+2))
fi
done
fi
# This is necessary to work with special characters in sed
TFS_PATH_STRING="$(<<< "$TFS_PATH" sed -e 's`[][\\/.*^$]`\\&`g')"
TFS_MESSAGE_TXT="$(<<< "$TFS_MESSAGE" sed -e 's`[][\\/.*^$]`\\&`g')"
# Using the Anchor in torbox.run
REPLACE_STRING=$(grep -m 1 "This will configure the TFS program" ${RUNFILE})
#This is necessary to work with special characters in sed
REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
# This will give $ONION_HOSTNAME
test_service_exists $SERVICE_NAME
# This will give VIRTPORT and HIDDENSSERVICEPORT
find_virtport $SERVICE_NAME
# allow_ud = 0 --> neither up- nor download
# allow_ud = 1 --> only upload
# allow_ud = 2 --> only download
# allow_ud = 3 --> up- and download
if [ "$allow_ud" == "0" ]; then
echo -e "${WHITE}[!] NEITHER UP- NOR DOWNLOAD ALLOWED!${NOCOLOR}"
echo -e "${RED}[+] We will configure TFS for $SERVICE_NAME, anyway.${NOCOLOR}"
echo -e "${RED}[+] You can change the configuration later with menu entry 2.${NOCOLOR}"
echo ""
echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE_TXT" ]; then TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -m \"$TFS_MESSAGE_TXT\" -od $ONION_HOSTNAME -ad 0 -au 0"
else TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME -ad 0 -au 0"
fi
sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
#Start TFS
echo -e "${RED}[+] Starting TFS (up- and downloading prohibited)...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE" ]; then (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -m "$TFS_MESSAGE" -od $ONION_HOSTNAME -ad 0 -au 0 &) &>/dev/null
else (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME -ad 0 -au 0 &) &>/dev/null
fi
elif [ "$allow_ud" == "1" ]; then
echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE_TXT" ]; then TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -m \"$TFS_MESSAGE_TXT\" -od $ONION_HOSTNAME -ad 0 -au 1"
else TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME -ad 0 -au 1"
fi
sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
#Start TFS
echo -e "${RED}[+] Starting TFS (only for uploading)...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE" ]; then (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -m "$TFS_MESSAGE" -od $ONION_HOSTNAME -ad 0 -au 1 &) &>/dev/null
else (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME -ad 0 -au 1 &) &>/dev/null
fi
elif [ "$allow_ud" == "2" ]; then
echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE_TXT" ]; then TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -m \"$TFS_MESSAGE_TXT\" -od $ONION_HOSTNAME -ad 1 -au 0"
else TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME -ad 1 -au 0"
fi
sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
# Start TFS
echo -e "${RED}[+] Starting TFS (only for downloading)...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE" ]; then (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -m "$TFS_MESSAGE" -od $ONION_HOSTNAME -ad 1 -au 0 &) &>/dev/null
else (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME -ad 1 -au 0 &) &>/dev/null
fi
elif [ "$allow_ud" == "3" ]; then
echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE_TXT" ]; then TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -m \"$TFS_MESSAGE_TXT\" -od $ONION_HOSTNAME -ad 1 -au 1"
else TFS_STRING="\nTFS-$SERVICE_NAME=lib\/filesharing\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME -ad 1 -au 1"
fi
sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
#Start TFS
echo -e "${RED}[+] Starting TFS (only for up- and downloading)...${NOCOLOR}"
if [ ! -z "$TFS_MESSAGE" ]; then (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -m "$TFS_MESSAGE" -od $ONION_HOSTNAME -ad 1 -au 1 &) &>/dev/null
else (nohup sudo ./lib/filesharing/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME -ad 1 -au 1 &) &>/dev/null
fi
fi
#Configure Nginx
echo ""
echo -e "${RED}[+] Sharing the folder ${WHITE}$TFS_PATH${RED} for the Onion Service named ${WHITE}$SERVICE_NAME${RED} on port ${WHITE}$VIRTPORT${RED}"
(cp "etc/nginx/sites-available/sample-onion-filesharing.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") >/dev/null
UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
#Bash specific, but should also work with other shells
HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
sed -i'' "s|FOLDER|$TFS_PATH|" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
# sudo systemctl reload nginx will not create new socks !!
# sudo ls /var/run | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
sudo systemctl reload nginx
(sudo rm -f "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") &>/dev/null
}
# This function configures the TCS in the run file and starts TCS.
# Syntax configure_tcs
# This function needs the following special variables:
# SERVICE_NAME
configure_tcs()
{
# Using the Anchor in torbox.run
REPLACE_STRING=$(grep -m 1 "This will configure the TCS program" ${RUNFILE})
#This is necessary to work with special characters in sed
REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
# This will give $ONION_HOSTNAME
test_service_exists $SERVICE_NAME
# This will give VIRTPORT and HIDDENSSERVICEPORT
find_virtport $SERVICE_NAME
echo -e "${RED}[+] Configuring TCS...${NOCOLOR}"
# NEW - TorBox v.0.5.3
(sudo rm $PID_PATH/$SERVICE_NAME.pid) &>/dev/null
(sudo rm $DB_PATH/$SERVICE_NAME.*) &>/dev/null
(sudo rm /var/run/tcs_$SERVICE_NAME.sock) &>/dev/null
#
TCS_STRING="\nTCS-$SERVICE_NAME=lib\/chatsecure\/tcs -n $SERVICE_NAME -od $ONION_HOSTNAME"
sudo sed -E -i "s/# This will configure the TCS program.*/$REPLACE_STRING$TCS_STRING/g" "${RUNFILE}"
echo -e "${RED}[+] Starting TCS...${NOCOLOR}"
(nohup sudo ./lib/chatsecure/tcs -n $SERVICE_NAME -od $ONION_HOSTNAME &) &>/dev/null
# Configure Nginx
echo ""
echo -e "${RED}[+] Starting TCS for the Onion Service named ${WHITE}$SERVICE_NAME${RED} on port ${WHITE}$VIRTPORT${RED}"
(cp "etc/nginx/sites-available/sample-onion-chatsecure.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf") >/dev/null
UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
#Bash specific, but should also work with other shells
HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
# sudo systemctl reload nginx will not create new socks !!
# sudo ls /var/run | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
sudo systemctl reload nginx
(sudo rm -f "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf") &>/dev/null
}
# This function creates WEB_SERVICE_NAME_LIST with all available shared folder, TFS and TCS configurations for Nginx
# config_shared_folders_TFS_and_TCS
config_shared_folders_TFS_and_TCS()
{
WEB_SERVICE_NAME_LIST=""
create_web_service_list 0
if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
echo -e "${RED}[+] ${WHITE}Found shared folder(s)... ${RED}Nginx reloaded to make them available.${NOCOLOR}"
# sudo systemctl reload nginx will not create new socks !!
# sudo ls /var/run | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
sudo systemctl reload nginx
fi
WEB_SERVICE_NAME_LIST=""
create_web_service_list 1
if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
sudo ./bin/start_tfs initial
echo -e "${RED}[+] ${WHITE}Found a TFS configuration... ${RED}Nginx reloaded to make them available.${NOCOLOR}"
fi
WEB_SERVICE_NAME_LIST=""
create_web_service_list 2
if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
sudo ./bin/start_tcs initial
echo -e "${RED}[+] ${WHITE}Found a TCS configuration... ${RED}Nginx reloaded to make them available.${NOCOLOR}"
fi
echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
sudo systemctl restart tor
echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
}
# This function checks if an Onion Service is configured and exit when this is not the case
# Syntax error_no_onion_service
error_no_onion_service()
{
if [ -z "${SERVICE_NAME_LIST}" ]; then
echo -e "${WHITE}[!] NO ONION SERVICES AVAILABLE!!${NOCOLOR}"
echo -e "${RED}[+] There are no onion services configured, yet. You have to create them first (menu entry 2).${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
}
# This function checks if there is an Onion Service with client authorization and exit when this is not the case
# Syntax error_no_authorization
error_no_authorization()
{
if [ -z "${SERVICE_NAME_LIST}" ]; then
echo -e "${WHITE}[!] NO ONION SERVICES WITH CLIENT AUTHORIZATION AVAILABLE!!${NOCOLOR}"
echo -e "${RED}[+] There are no onion services with client authorization configured, yet.${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
}
# This function displays an error message and exit if the Onion domain is already used
# Syntax error_already_used
error_already_used()
{
echo -e "${WHITE}[!] THE ONION DOMAIN IS ALREADY USED!${NOCOLOR}"
echo -e "${RED}[+] You cannot use the same Onion domain for two different sharing options!${NOCOLOR}"
echo -e "${RED}[+] You have to create another Onion domain.${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
# This function displays an error message and exit if the Onion domain is invalid
# Syntax error_onion_invalid <REASON>
# <REASON>: 1 -> it is not within base32 alphabet lower-case encoding
# 2 -> is different than 56 characters
error_onion_invalid()
{
REASON=$1
echo -e "${WHITE}[!] THE ONION DOMAIN IS INVALID!${NOCOLOR}"
[ "$REASON" = "1" ] && echo -e "${RED}[+] The entered Onion domain is invalid, it is not within base32 alphabet lower-case encoding [a-z][2-7]${NOCOLOR}"
[ "$REASON" = "2" ] && echo -e "${RED}[+] Onion domain is invalid, LENGTH=${#ONION_HOSTNAME_WITHOUT_ONION} is different than 56 characters (<56-char-base32>.onion)${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
# This function displays an error message and exit if there is no client authorization files
# Syntax error_no_client_authorization
error_no_client_authorization()
{
echo -e "${WHITE}[!] NO CLIENT AUTHORIZATION AVAILABLE!!${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
# This function displays an error message and exit if there is no folder actively shared
# Syntax error_no_folder_shared
error_no_folder_shared()
{
echo -e "${WHITE}[!] There are no folders actively shared, yet!${NOCOLOR}"
echo -e "${RED}[+] Start sharing a folder on an Onion domain, first (entry 1) ${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
# This function displays an error message and exit if TFS is not running
# Syntax error_no_folder_shared
error_no_tfs_running()
{
echo -e "${WHITE}[!] TORBOX FILESHARING (TFS) IS NOT RUNNING!!!${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
# This function displays an error message and exit if TCS is not running
# Syntax error_no_folder_shared
error_no_tcs_running()
{
echo -e "${WHITE}[!] There is no TCS started on an Onion domain, yet!${NOCOLOR}"
echo -e "${RED}[+] Start TCS on an Onion domain, first (entry 1) ${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
}
######## PREPARATIONS ########
read_config
# Resetting
shopt -s checkwinsize
[ -f nohup.out ] && sudo rm nohup.out
stty intr ^c
trap
# NEW v.0.5.4: To be sure that the necessary directories exist
if ! sudo test -d /var/lib/tor/services; then
(sudo mkdir /var/lib/tor/services) 2>/dev/null
sudo chown -R debian-tor:debian-tor /var/lib/tor/services
sudo chmod -R go-rwx /var/lib/tor/services
fi
if ! sudo test -d /var/lib/tor/onion_auth; then
(mkdir /var/lib/tor/onion_auth) 2>/dev/null
chown -R debian-tor:debian-tor /var/lib/tor/onion_auth
chmod -R go-rwx /var/lib/tor/onion_auth
fi
###### DISPLAY THE MENU ######
clear
#This is used, if the script is started with an defined menu selection
CHOICE=()
if [ ! -z "$SELECTION" ]; then
CHOICE=$SELECTION
else
CHOICE=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.3 - ONION SERVICES" --menu "Choose an option (ESC -> back to the main menu) ${TOGGLE16}" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
"==" "=============================================[Informational]===" \
" 1" "RUN AN ONION SERVICE - READ ME FIRST" \
"==" "============================================[Onion Services]===" \
" 2" "Toggle Onion Service Mode from $TOGGLE16b to $TOGGLE17b" \
" 3" "Create or reactivate an Onion Service" \
" 4" "List all Onion Services" \
" 5" "Delete or deactivate an Onion Service" \
" 6" "Enter the advanced tor configuration editor" \
"==" "===================================[Control client's access]===" \
" 7" "Generate a new key pair (public and private key) for a client" \
" 8" "Register a client with its public key" \
" 9" "Edit a client's authorization" \
"10" "List all clients for a particular Onion Service" \
"11" "Remove a client's authorization" \
"==" "===============================[Manage my access to a server]===" \
"12" "Generate a new key pair (public and private key) for a server" \
"13" "Register a server with its private key" \
"14" "Edit a server access authorization" \
"15" "List all server access authorizations" \
"16" "Remove a server access authorization" \
"==" "=================================================[Share it!]===" \
"17" "Start/stop sharing a folder on an Onion domain and list them" \
"18" "Start/stop upload or/and download files (TorBox File Sharing)" \
"19" "Start/stop TorBox Chat Secure" \
3>&1 1>&2 2>&3)
exitstatus=$?
# exitstatus == 255 means that the ESC key was pressed
[ "$exitstatus" == "255" ] && exit 0
fi
CHOICE=$(echo "$CHOICE" | tr -d ' ')
case "$CHOICE" in
# Displays the read.me
1)
INPUT=$(cat text/help-onion_service-text)
whiptail --title "TorBox - INFO (scroll down!)" --msgbox --scrolltext "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH
;;
# Toggle Onion Service Mode ON or OFF
2)
if [ -z "$MODE_OS" ]; then
INPUT=$(cat text/activate-onion_service-text)
if (whiptail --title "TorBox - INFO" --defaultno --no-button "DON'T CHANGE" --yes-button "ACTIVATE" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
if grep -q "#HiddenServiceDir" ${TORRC}; then
SERVICE_NAME_LIST=$(grep "^#HiddenServiceDir" ${TORRC} | sed "s/^#HiddenServiceDir \/var\/lib\/tor\/services\///g")
echo -e "${RED}[+] Activating Onion Service Mode...${NOCOLOR}"
(sudo sed -i "s/^#HiddenServiceDir/HiddenServiceDir/g" ${TORRC}) &>/dev/null
(sudo sed -i "s/^#HiddenServicePort/HiddenServicePort/g" ${TORRC}) &>/dev/null
(sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC}) &>/dev/null
(sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC}) &>/dev/null
echo -e "${RED}[+] Reactivating following Onion Service name(s):\n\n${WHITE}${SERVICE_NAME_LIST}\n${NOCOLOR}"
config_shared_folders_TFS_and_TCS
i=0
#This is necessary to work with special characters in sed
DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
SERVICE_NAME_LIST=$(grep "HiddenServiceDir" ${TORRC} | sed "s/^#HiddenServiceDir $DATA_DIR_OS_STRING\///g" | sed "s/^HiddenServiceDir $DATA_DIR_OS_STRING\///g")
for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
i=$((i+1))
clear
if [ "$i" -gt "1" ]; then
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
else
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
fi
show_onion_address $SERVICE_NAME
done
else
trap "bash menu-onion; exit 0" SIGINT
echo -e "${WHITE}[!] THERE ARE NO ONION SERVICES CONFIGURED!!${NOCOLOR}"
stty intr q
read -n 1 -s -r -p $'\e[1;31m[+] We will create now an Onion Service for you. If you agree, press ENTER, otherwise press q. \e[0m'
stty intr ^c
clear
bash menu-onion 3; exit 0
fi
fi
else
INPUT=$(cat text/deactivate-onion_server-text)
if (whiptail --title "TorBox - INFO" --defaultno --no-button "DON'T CHANGE" --yes-button "DEACTIVATE" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
clear
HIDDENSERVICENAMES=$(grep "^HiddenServiceDir" ${TORRC} | sed "s/^HiddenServiceDir \/var\/lib\/tor\/services\///g")
echo -e "${RED}[+] Deactivating Onion Service Mode...${NOCOLOR}"
(sudo sed -i "s/^#HiddenServiceDir/##HiddenServiceDir/g" ${TORRC}) 2>/dev/null
(sudo sed -i "s/^#HiddenServicePort/##HiddenServicePort/g" ${TORRC}) 2>/dev/null
(sudo sed -i "s/^HiddenServiceDir/#HiddenServiceDir/g" ${TORRC}) 2>/dev/null
(sudo sed -i "s/^HiddenServicePort/#HiddenServicePort/g" ${TORRC}) 2>/dev/null
if [ ! -z "$HIDDENSERVICENAMES" ]; then echo -e "${RED}[+] Deactivating following Onion Service name(s):\n\n${WHITE}${HIDDENSERVICENAMES}\n${NOCOLOR}"; fi
# This will only kill the TFS/TCS process, but not touch torbox.run and the Nginx configuration for later reactivation
TFS_NAME_LIST=$(ls lib/filesharing/pid/ | sed "s/.pid//")
if [ ! -z "$TFS_NAME_LIST" ]; then
for TFS_NAME in $TFS_NAME_LIST; do
echo -e "${RED}[+] Stopping TFS named ${WHITE}${TFS_NAME}${RED} on Onion domain...${NOCOLOR}"
PID=$(cat /home/torbox/torbox/lib/filesharing/pid/${TFS_NAME}.pid)
(sudo kill $PID) &>/dev/null
done
fi
TCS_NAME_LIST=$(ls lib/chatsecure/pid/ | sed "s/.pid//")
if [ ! -z "$TCS_NAME_LIST" ]; then
for TCS_NAME in $TCS_NAME_LIST; do
echo -e "${RED}[+] Stopping TCS named ${WHITE}${TCS_NAME}${RED} on Onion domain...${NOCOLOR}"
PID=$(cat /home/torbox/torbox/lib/chatsecure/pid/${TCS_NAME}.pid)
(sudo kill $PID) &>/dev/null
done
fi
echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
sudo systemctl restart tor
echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
fi
fi
read_config
;;
# Create or reactivate an Onion Service
3)
INPUT=$(cat text/enable-onion_service-text)
if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
#This is necessary to work with special characters in sed
DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
SERVICE_NAME_LIST=$(grep "#HiddenServiceDir" ${TORRC} | sed "s/^##HiddenServiceDir $DATA_DIR_OS_STRING\///g" | sed "s/^#HiddenServiceDir $DATA_DIR_OS_STRING\///g")
if [ ! -z "$SERVICE_NAME_LIST" ]; then
if (whiptail --title "TorBox - INFO" --yesno --no-button "CREATE A NEW ONE" --yes-button "REACTIVATE" "There are deactivated Onion Services configured.\n\nDo you want to reactivate an already configured Onion Service or create a new one?" $MENU_HEIGHT_15 $MENU_WIDTH); then
exitstatus=$?
service_menu checklist "$SERVICE_NAME_LIST"
if [ "$exitstatus" == "0" ]; then
i=0
for SERVICE_NAME in $SERVICE_NAME_LIST; do
i=$((i+1))
for SERVICE_NUMBER in $ENTRY_NUMBERS; do
if [ "$i" = "$SERVICE_NUMBER" ]; then
echo -e "${RED}[+] Reactivating ${WHITE}${SERVICE_NAME}${NOCOLOR}"
SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC} 2>/dev/null
sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC} 2>/dev/null
sudo sed -i "s/^#HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/g" ${TORRC} 2>/dev/null
# This gives VIRTPORT, HIDDENSSERVICEPORT
find_virtport $SERVICE_NAME
#This is necessary to work with special characters in sed
HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
sudo sed -i "s/^#$HIDDENSSERVICEPORT_STRING/$HIDDENSSERVICEPORT_STRING/g" ${TORRC}
fi
done
done
config_shared_folders_TFS_and_TCS
i=0
for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
i=$((i+1))
clear
if [ "$i" -gt "1" ]; then
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
else
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
fi
show_onion_address $SERVICE_NAME
done
trap "bash menu-onion; exit 0" EXIT
exit 0
else
trap "bash menu-onion; exit 0" EXIT
exit 0
fi
fi
fi
SERVICE_NAME=$(whiptail --title "TorBox - INFO" --inputbox "\n\nName your onion service directory in one string and no space (e.g.: torbox.ch):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
exitstatus=$?
if [ ! -z "${SERVICE_NAME}" ]; then
if [ ! "${SERVICE_NAME%%*[^a-z0-9_.]*}" ]; then
clear
echo -e "${WHITE}[!] THE SERVICE NAME IS INVALID!!${NOCOLOR}"
echo -e "${RED}[+] The name can only contain characters in range of: 'a-z', 'A-Z', '_', '.'.${NOCOLOR}"
sleep 5
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
if (grep "^HiddenServiceDir ${DATA_DIR_OS}/${SERVICE_NAME}" "${TORRC}"); then
clear
echo -e "${WHITE}[!] THE SERVICE NAME IS ALREADY USED AND ACTIVATED!!${NOCOLOR}"
echo -e "${RED}[+] If you want to replace the particular Onion Service, you must first delete it with menu entry 4.${NOCOLOR}"
sleep 5
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
if (grep "#HiddenServiceDir ${DATA_DIR_OS}/${SERVICE_NAME}" "${TORRC}"); then
clear
INPUT=$(cat text/reactivate-onion_service-text)
if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
exitstatus=$?
if [ "$exitstatus" == "0" ]; then
clear
echo -e "${RED}[+] Reactivating ${WHITE}${SERVICE_NAME}${NOCOLOR}"
#This is necessary to work with special characters in sed
SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC} 2>/dev/null
sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC} 2>/dev/null
sudo sed -i "s/^#HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/g" ${TORRC} 2>/dev/null
# This gives VIRTPORT, HIDDENSSERVICEPORT
find_virtport $SERVICE_NAME
#This is necessary to work with special characters in sed
HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
sudo sed -i "s/^#$HIDDENSSERVICEPORT_STRING/$HIDDENSSERVICEPORT_STRING/g" ${TORRC}
echo -e "${RED}[+] Reactivating following Onion Service name:\n\n${WHITE}${SERVICE_NAME}\n${NOCOLOR}"
config_shared_folders_TFS_and_TCS
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
show_onion_address $SERVICE_NAME
trap "bash menu-onion; exit 0" EXIT
exit 0
fi
else
clear
echo -e "${WHITE}[!] THE SERVICE NAME IS ALREADY USED!!${NOCOLOR}"
echo -e "${RED}[+] If you want to replace the particular Onion Service, you must first delete it with menu entry 4.${NOCOLOR}"
sleep 5
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
fi
SERVICE_PORTS=$(whiptail --title "Onion Service -- virtual ports" --inputbox "\n\nPlease, configure a virtual port. Usually 80 is just good enough: " $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 80 3>&1 1>&2 2>&3)
exitstatus=$?
if [ ! -z "${SERVICE_PORTS}" ]; then
clear
echo -e "${RED}[+] Configuring torrc...${NOCOLOR}"
VIRTPORT=$(cut -d ' ' -f1 <<< $SERVICE_PORTS)
is_integer "${VIRTPORT}"
exitstatus=$?
# exitstatus == 1 means that $VIRTPORT is not an integer
if [ "$exitstatus" == "1" ]; then
clear
echo -e "${WHITE}[!] WRONG INPUT - THIS IS NOT AN INTEGER!${NOCOLOR}"
sleep 5
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
# Using the Anchor in torrc
REPLACE_STRING=$(grep -m 1 "This will configure the Onion Services" ${TORRC})
#This is necessary to work with special characters in sed
REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
SOCK_FILE="${UNIX_PATH}-${VIRTPORT}.sock"
#This is necessary to work with special characters in sed
DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
SOCK_FILE_STRING="$(<<< "${SOCK_FILE}" sed -e 's`[][\\/.*^$]`\\&`g')"
NEW_STRING="\nHiddenServiceDir $DATA_DIR_OS_STRING\/$SERVICE_NAME_STRING\nHiddenServicePort ${VIRTPORT} $SOCK_FILE_STRING"
#Writing the necessary entries on the right place in torrc (not at the end, because there are already the bridges)
sudo sed -E -i "s/## This will configure the Onion Services.*/$REPLACE_STRING$NEW_STRING/g" "${TORRC}"
#Create a folder, which can be published through the Onion Services
echo -e "${RED}[+] Creating ${WHITE}$WEBSITE_DIR/$SERVICE_NAME ${RED}which can be shared through the Onion domain.${NOCOLOR}"
sudo mkdir "$WEBSITE_DIR/$SERVICE_NAME"
sudo chown torbox:torbox "$WEBSITE_DIR/$SERVICE_NAME"
echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
sudo systemctl restart tor
echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
echo ""
read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
clear
echo -e "${RED}[+] This is the information for your newly created Onion Service${NOCOLOR}"
show_onion_address $SERVICE_NAME
fi
else
# exitstatus == 255 means that the ESC key was pressed
if [ $exitstatus != 255 ]; then
clear
echo -e "${WHITE}[!] NO SERVICE NAME GIVEN!!${NOCOLOR}"
echo -e "${RED}[+] You didn't define a service name! You have to define a service name!${NOCOLOR}"
sleep 5
clear
trap "bash menu-onion; exit 0" EXIT
exit 1
fi
fi
fi
;;
# List all Onion Services
4)
create_service_list
error_no_onion_service
i=0
for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
i=$((i+1))
clear
if [ "$i" -gt "1" ]; then
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
else
echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
fi
show_onion_address $SERVICE_NAME
done
;;
# Delete or deactivate Onion Service
5)
create_service_list
error_no_onion_service
INPUT=$(cat text/disable-onion_service-text)
if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
exitstatus=$?
service_menu checklist "$SERVICE_NAME_LIST"
if [ "$exitstatus" == "0" ]; then
i=0
for SERVICE_NAME in $SERVICE_NAME_LIST; do
i=$((i+1))
for SERVICE_NUMBER in $ENTRY_NUMBERS; do
if [ "$i" = "$SERVICE_NUMBER" ]; then
INPUT="Would you DEACTIVATE or DELETE the Onion Service named ->$SERVICE_NAME<-?\n\nDEACTIVATE means that nothing will be deleted and the Onion Service can be reactivatet later.\n\nDELETE means that the Onion Service, client authorizations, configuration for TFS & TCS and the HOSTED DATA IN THE SHARED FOLDER will be deleted PERMANENTLY! The service CANNOT be reactivated and the same onion address CANNOT be used anymore.\n"
if (whiptail --title "TorBox - INFO" --defaultno --no-button "DEACTIVATE" --yes-button "DELETE" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
exitstatus=$?
else
exitstatus=$?
fi
clear
#This is necessary to work with special characters in sed
DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
# This gives VIRTPORT, HIDDENSSERVICEPORT and UNIX_PATH
find_virtport $SERVICE_NAME
if [ "$exitstatus" == "0" ]; then
echo -e "${RED}[+] Stopping TFS and TCS, if active${NOCOLOR}"
(TFS_NAME_LIST=$(ls lib/filesharing/pid/$SERVICE_NAME.pid)) >/dev/null
(TCS_NAME_LIST=$(ls lib/chatsecure/pid/$SERVICE_NAME.pid)) >/dev/null
if [ ! -z "$TFS_NAME_LIST" ]; then stopping_tfs $SERVICE_NAME; fi
if [ ! -z "$TCS_NAME_LIST" ]; then stopping_tcs $SERVICE_NAME; fi
echo -e "${RED}[+] Delete Onion Service named ${WHITE}$SERVICE_NAME${NOCOLOR}"
(sudo rm -rfv "${DATA_DIR_OS}/${SERVICE_NAME}") &>/dev/null
sudo sed -i "/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/d" ${TORRC}
#This is necessary to work with special characters in sed
HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
sudo sed -i "/$HIDDENSSERVICEPORT_STRING/d" ${TORRC}
echo -e "${RED}[+] Removing Nginx configuration for shared folders...${NOCOLOR}"
if [ -f "/etc/nginx/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ]; then sudo rm "/etc/nginx/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf"; fi
if [ -f "/etc/nginx/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ]; then sudo rm "/etc/nginx/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf"; fi
echo ""
if [ -d "${WEBSITE_DIR}/${SERVICE_NAME}" ]; then
echo -e "${RED}[+] Delete shared folder $WEBSITE_DIR/$SERVICE_NAME in 5 seconds...${NOCOLOR}"
echo -e "${WHITE} Last chance to abbort with q!${NOCOLOR}"
trap "bash menu-onion; exit 0" SIGINT
stty intr q
sleep 1
echo -e "1"
sleep 1
echo -e "2"
sleep 1
echo -e "3"
sleep 1
echo -e "4"
sleep 1
echo -e "5"
sleep 1