This repository has been archived by the owner on May 24, 2024. It is now read-only.
/
myfam
executable file
·203 lines (168 loc) · 4.17 KB
/
myfam
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
#!/bin/bash -u
#
# Sort out Tor node fingerprints nicly for each node.
# Pascal K. 20170613
# Source: https://github.com/ryru/myfam
#
# This script is licensed under BSD 3-Clause License (see LICENSE for more information).
usage() {
echo "Usage: $0 FILE"
}
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
#######################################
# Script input file check
# Globals:
# none
# Arguments:
# First script file parameter
# Returns:
# Exit script on error
#######################################
check_data_input() {
local readonly data="$1"
if [ -z "$data" ]; then
err "no input file specified"
usage
exit 1
fi
if [[ -d "$data" ]]; then
err "$data is a directory"
usage
exit 1
elif [[ -f "$data" ]]; then
# file seems to be fine
:
else
err "invalid argument: $data"
usage
exit 1
fi
}
#######################################
# Return fingerprint from within line
# Globals:
# none
# Arguments:
# Comma seperated line (fingerprint on first position)
# Returns:
# Fingerprint string or empty string on error
#######################################
get_fingerprint_from_line() {
readonly local line="$1"
local fingerprint=""
fingerprint=$( echo "$line" | cut -d ',' -f 1 | tr -d '[:space:]' )
echo "$fingerprint"
}
#######################################
# Return nickname from within line
# Globals:
# none
# Arguments:
# Comma seperated line (nickname on first position)
# Returns:
# Nickname string or empty string on error
#######################################
get_nickname_from_line() {
readonly local line="$1"
local nickname=""
nickname=$( echo "$line" | cut -d ',' -f 2 | tr -d '[:space:]' )
echo "$nickname"
}
#######################################
# Print amount of found fingerprints and nicknames
# Globals:
# none
# Arguments:
# Array of input file lines
# Returns:
# none
#######################################
print_info() {
local data_array=("${!1}")
local total_nickname=0
local total_fingerprint=0
for entry in "${data_array[@]}"; do
local fingerprint
fingerprint=$( get_fingerprint_from_line "$entry" )
if [[ ! -z "$fingerprint" ]]; then
((total_fingerprint=total_fingerprint+1))
fi
local nickname
nickname=$( get_nickname_from_line "$entry" )
if [[ ! -z "$nickname" ]]; then
((total_nickname=total_nickname+1))
fi
done
echo "total nicknames $total_nickname and total fingerprints $total_fingerprint"
}
#######################################
# Print node record to stdout
# Globals:
# none
# Arguments:
# Name of node to print
# Array of fingerprints to print
# Returns:
# none
#######################################
print_family_record() {
local node="$1"
local family_members_array=("${!2}")
local family_string="MyFamily "
echo ""
echo "Nickname $node"
for entry in "${family_members_array[@]}"; do
family_string="$family_string$entry,"
done
family_string="${family_string%?}"
echo "$family_string"
}
#######################################
# Print family record for all members to stdout
# Globals:
# none
# Arguments:
# Array of input file lines
# Size of input file array
# Returns:
# none
#######################################
print_family() {
local data_array=("${!1}")
local data_array_len="$2"
for (( i=0; i<${data_array_len}; i++ )); do
local readonly nickname=$( get_nickname_from_line "${data_array[$i]}" )
local family_members["$((data_array_len-1))"]
k=0
for (( j=0; j<${data_array_len}; j++ )); do
if [[ "$i" == "$j" ]]; then
:
else
family_members["$k"]=$( get_fingerprint_from_line "${data_array[$j]}" )
((k=k+1))
fi
done
print_family_record "$nickname" family_members[@]
done
}
main() {
if [[ $# -eq 0 ]]; then
err "no input file supplied"
usage
exit 1
fi
readonly local input_file="$1"
check_data_input "$input_file"
input_size=$( wc -l < "$input_file" )
counter=0
# read file in
while read -r line || [[ -n "$line" ]]; do
data["$counter"]="$line"
((counter=counter+1))
done < "$input_file"
print_info data[@]
print_family data[@] "$counter"
}
main "$@"