/
torrc-parser
executable file
·136 lines (120 loc) · 4.83 KB
/
torrc-parser
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
#!/bin/bash
## Copyright (C) 2018 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## Copyright (C) 2018 Iry Koon <iry@riseup.net>
## See the file COPYING for copying conditions.
#### meta start
#### project Whonix
#### category tor
#### gateway_only yes
#### description
## helper script for <code>anon-verify</code>
#### meta end
#set -ex
temp_verbose_torrc=$(mktemp)
declare -a torrc_files
function parser() {
local abs_path
for abs_path in "$@"; do
true "abs_path: '$abs_path'"
if [[ -f "$abs_path" ]]; then
local base_name first_character file_extension
base_name="${abs_path##*/}"
first_character=${base_name:0:1}
file_extension="${base_name##*.}"
if [ "$first_character" = "." ]; then
echo "===> Ignoring file name starting with '.': $abs_path (Quote Tor manual: 'Files starting with a dot are ignored.')" | tee -a "$temp_verbose_torrc" >/dev/null
continue
fi
if [ "$abs_path" = "/usr/share/tor/tor-service-defaults-torrc" ]; then
true "good: $abs_path"
elif [ "$abs_path" = "/usr/share/tor/tor-service-defaults-torrc.anondist" ]; then
true "good: $abs_path"
elif [ "$abs_path" = "/etc/tor/torrc" ]; then
true "good: $abs_path"
else
if [ "$file_extension" = "conf" ]; then
true "good: $abs_path"
else
true "extraneous: $abs_path"
extraneous_torrc+=("$abs_path")
fi
fi
echo "===> Start parsing file $abs_path" | tee -a "$temp_verbose_torrc" >/dev/null
torrc_files+=("$abs_path")
while read -r line ; do
true "line: '$line'"
if [ "$line" = "" ]; then
## show empty lines only when $VERY_VERBOSE is "1"
if [ ! "$VERY_VERBOSE" = "1" ]; then
true "Skipping empty line."
continue
fi
fi
## only show comments in torrc files when $VERY_VERBOSE is "1"
if [[ "$line" =~ ^[[:space:]]*# ]]; then
true "line is a comment: yes"
if [ "$VERY_VERBOSE" = "1" ]; then
echo "$line" | tee -a "$temp_verbose_torrc" >/dev/null
fi
else
true "line is a comment: no"
echo "$line" | tee -a "$temp_verbose_torrc" >/dev/null
fi
true "Checking if the line includes '%include'..."
if echo "$line" | grep -q "%include" ; then
true "Yes, the line includes '%include'."
local second_word files_list
second_word="$(echo "$line" | awk '{print $2;}')"
## Do not quote because /etc/torrc.d/*.conf needs to be expanded.
parser $second_word
else
true "No, the line does not include '%include'."
fi
done < "$abs_path"
echo "===> Done parsing file $abs_path" | tee -a "$temp_verbose_torrc" >/dev/null
elif [[ -d "$abs_path" ]]; then
echo "===> Start parsing folder: $abs_path" | tee -a "$temp_verbose_torrc" >/dev/null
local fso
for fso in $(find "$abs_path" -mindepth 1 -maxdepth 1 | sort -g) ; do
if [ -d "$fso" ]; then
echo "===> Ignoring subfolder: $abs_path (Quote Tor manual: 'Files on subfolders are ignored.')" | tee -a "$temp_verbose_torrc" >/dev/null
continue
fi
parser "$fso"
done
echo "===> Done parsing folder : $abs_path" | tee -a "$temp_verbose_torrc" >/dev/null
else
echo "ERROR: invalid file system object (fso) (abs_path): '$abs_path'"
fi
done
}
function parsing_order_verbose(){
echo "The Tor configuration files are parsed in such order: "
cat "$temp_verbose_torrc"
}
function used_torrc_files(){
echo "${#torrc_files[@]} files are used as Tor configuration files: "
echo "${torrc_files[@]}"
}
function unknown_option_specifier(){
for unknown_option in "$@"; do
echo "The following files contain unkown option '$unknown_option': "
grep -iHnE --color "^([[:space:]])*$unknown_option" "${torrc_files[@]}"
echo "Please modify these files to fix the problem."
done
}
function extraneous_torrc_files(){
if [ "$extraneous_torrc" = "" ]; then
return 0
fi
echo "/===================================================================\\"
echo "| Extraneous Tor Configuration Files |"
echo "\\===================================================================/"
echo "${#extraneous_torrc[@]} files are do not end with file extension '.conf': "
echo "${extraneous_torrc[@]}"
echo "These should be probably removed."
}
#parser $@
#parsing_order_verbose
#used_torrc_files
#unknown_option_specifier "bridge" "control"