-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_guards.py
384 lines (289 loc) · 13.1 KB
/
select_guards.py
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
#!/usr/bin/env python3
"""
Download information about all running guards in order to sort
the list by some specific criterion and use it in our experiments.
"""
import csv
import argparse
from sys import exit as sysexit
from os import getuid
from os.path import abspath, join
from datetime import datetime
from random import shuffle
from numpy import random as nprandom
import requests
ONIONOO_BASE_URL = "https://onionoo.torproject.org"
ONIONOO_DOC_TYPE = "details"
ONIONOO_PARAM_RELAY = "type=relay"
ONIONOO_PARAM_RUNNING = "running=true"
ONIONOO_PARAM_FLAG_GUARD = "flag=guard"
ONIONOO_PARAM_GUARDS_FIELDS = "fields=nickname,fingerprint,"\
"or_addresses,dir_address,country,advertised_bandwidth,"\
"guard_probability"
ONIONOO_PARAM_RELAYS_FIELDS = "fields=nickname,fingerprint,"\
"flags,country,region_name,city_name,latitude,longitude,as,"\
"advertised_bandwidth,guard_probability,middle_probability,"\
"exit_probability"
def build_onionoo_url(is_guard_query):
"""Puts all parts of the Onionoo query URL together."""
if is_guard_query:
return "{}/{}?{}&{}&{}&{}".format(
ONIONOO_BASE_URL, ONIONOO_DOC_TYPE, ONIONOO_PARAM_RELAY,
ONIONOO_PARAM_RUNNING, ONIONOO_PARAM_FLAG_GUARD,
ONIONOO_PARAM_GUARDS_FIELDS)
else:
return "{}/{}?{}&{}&{}".format(
ONIONOO_BASE_URL, ONIONOO_DOC_TYPE, ONIONOO_PARAM_RELAY,
ONIONOO_PARAM_RUNNING, ONIONOO_PARAM_RELAYS_FIELDS)
def make_onionoo_req(url):
"""Issues the Onionoo request and returns the JSON response."""
try:
# Request the data from Onionoo endpoint.
resp = requests.get(url)
# Parse response as JSON.
resp_json = resp.json()
# Raise any exception, if occured.
resp.raise_for_status()
except requests.HTTPError as err:
print(err)
except Exception as err:
print(err)
else:
return resp_json
def select_guards_by_cumul_guard_prob(fraction, guards):
"""Returns a new list containing all guards sorted in descending
guard probability order until the supplied cumulative fraction
of guard probability is reached."""
guards_selected = list()
cumul_prob = 0.0
for guard in guards:
# Append current guard to new list and update cumulative
# guard probability with its individual value.
guards_selected.append(guard)
cumul_prob += guard["guard_probability"]
# Once we hit our target fraction, we exit.
if cumul_prob >= fraction:
break
return guards_selected
def select_guards_by_cumul_adv_band(fraction, guards):
"""Returns a new list containing all guards sorted in descending
advertised bandwidth order until the supplied cumulative fraction
of advertised bandwidth is reached."""
guards_selected = list()
total_band = 0.0
target_band = 0.0
cumul_band = 0.0
for guard in guards:
total_band += guard["advertised_bandwidth"]
# Once we know the total advertised bandwidth, we can
# calculate the bytes-per-second value that corresponds
# to the supplied fraction argument.
target_band = total_band * fraction
for guard in guards:
# Append current guard to new list and update cumulative
# advertised bandwidth with its individual value.
guards_selected.append(guard)
cumul_band += guard["advertised_bandwidth"]
# Once we hit our target bandwidth, we exit.
if cumul_band >= target_band:
break
return guards_selected
def write_guards_list_file(path, guards):
"""Writes the list of selected guards as CSV to file system location."""
with open(path, "w") as guards_fp:
guards_writer = csv.DictWriter(
guards_fp, fieldnames=[
"nickname", "fingerprint", "or_address_v4", "dir_address",
"country", "advertised_bandwidth", "guard_probability"])
guards_writer.writeheader()
for guard in guards:
or_address = guard["or_addresses"][0]
for addr in guard["or_addresses"]:
if (addr.count(".") == 3) and (addr.count(":") == 1):
or_address = addr
dir_address = "none"
if "dir_address" in guard:
dir_address = guard["dir_address"]
country = "unknown"
if "country" in guard:
country = guard["country"]
data = {
"nickname": guard["nickname"],
"fingerprint": guard["fingerprint"],
"or_address_v4": or_address,
"dir_address": dir_address,
"country": country,
"advertised_bandwidth": guard["advertised_bandwidth"],
"guard_probability": guard["guard_probability"],
}
guards_writer.writerow(data)
def write_relays_list_file(path, relays):
"""Writes the list of all relays as CSV to file system location."""
with open(path, "w") as relays_fp:
relays_writer = csv.DictWriter(
relays_fp, fieldnames=[
"nickname", "fingerprint", "is_guard", "is_exit", "country",
"region_name", "city_name", "latitude", "longitude", "as",
"advertised_bandwidth", "guard_probability",
"middle_probability", "exit_probability"])
relays_writer.writeheader()
for relay in relays:
is_guard = False
if "Guard" in relay["flags"]:
is_guard = True
is_exit = False
if "Exit" in relay["flags"]:
is_exit = True
country = "unknown"
if "country" in relay:
country = relay["country"]
region_name = "unknown"
if "region_name" in relay:
region_name = relay["region_name"]
city_name = "unknown"
if "city_name" in relay:
city_name = relay["city_name"]
latitude = "unknown"
if "latitude" in relay:
latitude = relay["latitude"]
longitude = "unknown"
if "longitude" in relay:
longitude = relay["longitude"]
as_name = "unknown"
if "as" in relay:
as_name = relay["as"]
data = {
"nickname": relay["nickname"],
"fingerprint": relay["fingerprint"],
"is_guard": is_guard,
"is_exit": is_exit,
"country": country,
"region_name": region_name,
"city_name": city_name,
"latitude": latitude,
"longitude": longitude,
"as": as_name,
"advertised_bandwidth": relay["advertised_bandwidth"],
"guard_probability": relay["guard_probability"],
"middle_probability": relay["middle_probability"],
"exit_probability": relay["exit_probability"],
}
relays_writer.writerow(data)
def select_guards(cumul_guard_prob_perc, cumul_adv_band_perc,
num_sampled_from_guard_prob, randomize, output_dir):
"""Accept parameters specifying the criterion on which to select
guards, download relay infos, sort the guard list, select the guards,
possibly permute the guards list, and finally store both lists."""
if getuid() == 0:
return "Do not run this program as root."
# Stash current time and use it for output file.
now = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
if (cumul_guard_prob_perc * cumul_adv_band_perc *
num_sampled_from_guard_prob) <= 0:
return "Exactly one selection criterion (cumul_guard_prob_perc, " \
"cumul_adv_band_perc, num_sampled_from_guard_prob) must be " \
"set to an appropriate value of its respective domain."
if ((cumul_guard_prob_perc > 0) and (cumul_adv_band_perc > 0)
and (num_sampled_from_guard_prob > 0)):
return "Exactly one selection criterion (cumul_guard_prob_perc, " \
"cumul_adv_band_perc, num_sampled_from_guard_prob) must be " \
"set to an appropriate value of its respective domain."
if cumul_guard_prob_perc > 0:
cumul_guard_prob_perc = (cumul_guard_prob_perc / 100.0)
elif cumul_adv_band_perc > 0:
cumul_adv_band_perc = (cumul_adv_band_perc / 100.0)
# Build the two (guards, all relays) file paths.
relay_file = join(output_dir, "{}_relays.csv".format(now))
if cumul_guard_prob_perc > 0:
guard_file = \
join(output_dir, "{}_guards_cumul_guard_prob_perc-{}.csv"
.format(now, cumul_guard_prob_perc))
elif cumul_adv_band_perc > 0:
guard_file = \
join(output_dir, "{}_guards_cumul_adv_band_perc-{}.csv"
.format(now, cumul_adv_band_perc))
elif num_sampled_from_guard_prob > 0:
guard_file = \
join(output_dir, "{}_guards_num_sampled_from_guard_prob-{}.csv"
.format(now, num_sampled_from_guard_prob))
# Construct the two request URLs.
onionoo_guards_url = build_onionoo_url(True)
onionoo_relays_url = build_onionoo_url(False)
# Issue the two requests and obtain the JSON responses.
guards_resp = make_onionoo_req(onionoo_guards_url)
relays_resp = make_onionoo_req(onionoo_relays_url)
if cumul_guard_prob_perc > 0:
# Sort list of relays by guard probability in descending order.
guards_sorted = sorted(
guards_resp["relays"],
key=lambda relay: relay["guard_probability"],
reverse=True)
# Create final list of guards selected by decreasing guard
# probability until target cumulative percentage is reached.
guards_selected = select_guards_by_cumul_guard_prob(
cumul_guard_prob_perc, guards_sorted)
elif cumul_adv_band_perc > 0:
# Sort list of relays by advertised bandwidth in descending order.
guards_sorted = sorted(
guards_resp["relays"],
key=lambda relay: relay["advertised_bandwidth"],
reverse=True)
# Create final list of guards selected by decreasing advertised
# bandwidth until target cumulative percentage is reached.
guards_selected = select_guards_by_cumul_adv_band(
cumul_adv_band_perc, guards_sorted)
elif num_sampled_from_guard_prob > 0:
guards = guards_resp["relays"]
# Extract weights of each guard in order of
# appearance of the guard in the list.
guards_weights = []
for guard in guards:
guards_weights.append(guard["guard_probability"])
# Sample the configured number of guards without
# replacement from the list of guards weighted
# by their individual guard probability.
guards_selected_unsorted = nprandom.choice(
guards, size=num_sampled_from_guard_prob,
replace=False, p=guards_weights)
# Sort sampled list of guards in descending
# order of their guard probability.
guards_selected = sorted(
guards_selected_unsorted,
key=lambda guard: guard["guard_probability"],
reverse=True)
if randomize:
shuffle(guards_selected)
# Write the two lists to files.
write_guards_list_file(guard_file, guards_selected)
write_relays_list_file(relay_file, relays_resp["relays"])
return "success"
if __name__ == "__main__":
# Define and parse command-line arguments.
parser = argparse.ArgumentParser()
parser.add_argument("--cumul_guard_prob_perc", type=float, default=-1.0,
help="Select all guards that collectively account for "
"X per cent of the total guard selection probability, "
"chosen by descending individual selection probability"
" (e.g., X = 75.0).")
parser.add_argument("--cumul_adv_band_perc", type=float, default=-1.0,
help="Select all guards that collectively account for "
"X per cent of the total advertised guard bandwidth, "
"chosen by descending individual advertised bandwidth "
"(e.g., X = 50.0).")
parser.add_argument("--num_sampled_from_guard_prob", type=int, default=-1,
help="Return this number of guards, sampled from their"
" guard probability distribution.")
parser.add_argument("--randomize", type=bool, default=False,
help="After selecting guards by means of the "
"cumulative criterion, shuffle the selected list "
"before storing it at the specified location.")
parser.add_argument("--output_dir", type=str,
default=abspath("./experiments_relay-lists"),
help="File system location of output directory.")
args = parser.parse_args()
selection_result = select_guards(
args.cumul_guard_prob_perc, args.cumul_adv_band_perc,
args.num_sampled_from_guard_prob, args.randomize, args.output_dir)
if selection_result != "success":
print(selection_result)
sysexit(1)