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
|
/* Copyright 2011--2017 The Tor Project
* See LICENSE for licensing information */
package org.torproject.onionoo.server;
import org.torproject.onionoo.util.Time;
import org.torproject.onionoo.util.TimeFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceServlet extends HttpServlet {
private static final long serialVersionUID = 7236658979947465319L;
private boolean maintenanceMode = false;
/* Called by servlet container, not by test class. */
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.maintenanceMode = config.getInitParameter("maintenance") != null
&& config.getInitParameter("maintenance").equals("1");
}
private static final long INDEX_WAITING_TIME = 10L * 1000L;
@Override
public long getLastModified(HttpServletRequest request) {
if (this.maintenanceMode) {
return super.getLastModified(request);
} else {
return NodeIndexerFactory.getNodeIndexer().getLastIndexed(
INDEX_WAITING_TIME);
}
}
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
HttpServletRequestWrapper requestWrapper =
new HttpServletRequestWrapper(request);
HttpServletResponseWrapper responseWrapper =
new HttpServletResponseWrapper(response);
this.doGet(requestWrapper, responseWrapper);
}
private static final long CACHE_MIN_TIME = 5L * 60L * 1000L;
private static final long CACHE_MAX_TIME = 45L * 60L * 1000L;
private static final long CACHE_INTERVAL = 5L * 60L * 1000L;
private static Set<String> knownParameters = new HashSet<>(
Arrays.asList(("type,running,search,lookup,fingerprint,country,as,"
+ "flag,first_seen_days,last_seen_days,contact,order,limit,"
+ "offset,fields,family").split(",")));
private static Set<String> illegalSearchQualifiers =
new HashSet<>(Arrays.asList(("search,fingerprint,order,limit,"
+ "offset,fields").split(",")));
private static String ipv6AddressPatternString =
"^\\[?[0-9a-fA-F:\\.]{1,39}\\]?$";
private static Pattern ipv6AddressPattern =
Pattern.compile(ipv6AddressPatternString);
/** Handles the HTTP GET request in the wrapped <code>request</code> by
* writing an HTTP GET response to the likewise <code>response</code>,
* both of which are wrapped to facilitate testing. */
@SuppressWarnings("checkstyle:variabledeclarationusagedistance")
public void doGet(HttpServletRequestWrapper request,
HttpServletResponseWrapper response) throws IOException {
if (this.maintenanceMode) {
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
}
NodeIndex nodeIndex = NodeIndexerFactory.getNodeIndexer()
.getLatestNodeIndex(INDEX_WAITING_TIME);
if (nodeIndex == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
Time time = TimeFactory.getTime();
long receivedRequestMillis = time.currentTimeMillis();
String uri = request.getRequestURI();
if (uri.startsWith("/onionoo/")) {
uri = uri.substring("/onionoo".length());
}
String resourceType = null;
if (uri.startsWith("/summary")) {
resourceType = "summary";
} else if (uri.startsWith("/details")) {
resourceType = "details";
} else if (uri.startsWith("/bandwidth")) {
resourceType = "bandwidth";
} else if (uri.startsWith("/weights")) {
resourceType = "weights";
} else if (uri.startsWith("/clients")) {
resourceType = "clients";
} else if (uri.startsWith("/uptime")) {
resourceType = "uptime";
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
RequestHandler rh = new RequestHandler(nodeIndex);
rh.setResourceType(resourceType);
/* Extract parameters either from the old-style URI or from request
* parameters. */
Map<String, String> parameterMap = new HashMap<>();
for (Object parameterKey : request.getParameterMap().keySet()) {
String[] parameterValues =
request.getParameterValues((String) parameterKey);
parameterMap.put((String) parameterKey, parameterValues[0]);
}
/* Make sure that the request doesn't contain any unknown
* parameters. */
for (String parameterKey : parameterMap.keySet()) {
if (!knownParameters.contains(parameterKey)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
}
/* Filter relays and bridges matching the request. */
if (parameterMap.containsKey("search")) {
String[] searchTerms = parseSearchParameters(
request.getQueryString());
if (searchTerms == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
List<String> unqualifiedSearchTerms = new ArrayList<>();
for (String searchTerm : searchTerms) {
if (searchTerm.contains(":")) {
String[] parts = searchTerm.split(":", 2);
String parameterKey = parts[0];
if (!knownParameters.contains(parameterKey)
|| illegalSearchQualifiers.contains(parameterKey)) {
if (ipv6AddressPattern.matcher(parameterKey).matches()) {
unqualifiedSearchTerms.add(searchTerm);
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
}
if (!parameterMap.containsKey(parameterKey)) {
String parameterValue = parts[1];
parameterMap.put(parameterKey, parameterValue);
}
} else {
unqualifiedSearchTerms.add(searchTerm);
}
}
rh.setSearch(unqualifiedSearchTerms.toArray(
new String[unqualifiedSearchTerms.size()]));
}
if (parameterMap.containsKey("type")) {
String typeParameterValue = parameterMap.get("type").toLowerCase();
boolean relaysRequested = true;
if (typeParameterValue.equals("bridge")) {
relaysRequested = false;
} else if (!typeParameterValue.equals("relay")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setType(relaysRequested ? "relay" : "bridge");
}
if (parameterMap.containsKey("running")) {
String runningParameterValue =
parameterMap.get("running").toLowerCase();
boolean runningRequested = true;
if (runningParameterValue.equals("false")) {
runningRequested = false;
} else if (!runningParameterValue.equals("true")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setRunning(runningRequested ? "true" : "false");
}
if (parameterMap.containsKey("lookup")) {
String lookupParameter = this.parseFingerprintParameter(
parameterMap.get("lookup"));
if (lookupParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String fingerprint = lookupParameter.toUpperCase();
rh.setLookup(fingerprint);
}
if (parameterMap.containsKey("fingerprint")) {
String fingerprintParameter = this.parseFingerprintParameter(
parameterMap.get("fingerprint"));
if (fingerprintParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String fingerprint = fingerprintParameter.toUpperCase();
rh.setFingerprint(fingerprint);
}
if (parameterMap.containsKey("country")) {
String countryCodeParameter = this.parseCountryCodeParameter(
parameterMap.get("country"));
if (countryCodeParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setCountry(countryCodeParameter);
}
if (parameterMap.containsKey("as")) {
String asNumberParameter = this.parseAsNumberParameter(
parameterMap.get("as"));
if (asNumberParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setAs(asNumberParameter);
}
if (parameterMap.containsKey("flag")) {
String flagParameter = this.parseFlagParameter(
parameterMap.get("flag"));
if (flagParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setFlag(flagParameter);
}
if (parameterMap.containsKey("first_seen_days")) {
int[] days = this.parseDaysParameter(
parameterMap.get("first_seen_days"));
if (days == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setFirstSeenDays(days);
}
if (parameterMap.containsKey("last_seen_days")) {
int[] days = this.parseDaysParameter(
parameterMap.get("last_seen_days"));
if (days == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setLastSeenDays(days);
}
if (parameterMap.containsKey("contact")) {
String[] contactParts = this.parseContactParameter(
parameterMap.get("contact"));
if (contactParts == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setContact(contactParts);
}
if (parameterMap.containsKey("order")) {
String[] order = this.parseOrderParameter(parameterMap.get("order"));
if (order == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setOrder(order);
}
if (parameterMap.containsKey("offset")) {
String offsetParameter = parameterMap.get("offset");
if (offsetParameter.length() > 6) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
Integer.parseInt(offsetParameter);
} catch (NumberFormatException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setOffset(offsetParameter);
}
if (parameterMap.containsKey("limit")) {
String limitParameter = parameterMap.get("limit");
if (limitParameter.length() > 6) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
Integer.parseInt(limitParameter);
} catch (NumberFormatException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rh.setLimit(limitParameter);
}
if (parameterMap.containsKey("family")) {
String familyParameter = this.parseFingerprintParameter(
parameterMap.get("family"));
if (familyParameter == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String family = familyParameter.toUpperCase();
rh.setFamily(family);
}
rh.handleRequest();
long parsedRequestMillis = time.currentTimeMillis();
ResponseBuilder rb = new ResponseBuilder();
rb.setResourceType(resourceType);
rb.setRelaysPublishedString(rh.getRelaysPublishedString());
rb.setBridgesPublishedString(rh.getBridgesPublishedString());
rb.setOrderedRelays(rh.getOrderedRelays());
rb.setOrderedBridges(rh.getOrderedBridges());
rb.setRelaysSkipped(rh.getRelaysSkipped());
rb.setBridgesSkipped(rh.getBridgesSkipped());
rb.setRelaysTruncated(rh.getRelaysTruncated());
rb.setBridgesTruncated(rh.getBridgesTruncated());
String[] fields = null;
if (parameterMap.containsKey("fields")) {
fields = this.parseFieldsParameter(parameterMap.get("fields"));
if (fields == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
rb.setFields(fields);
}
long indexWrittenMillis =
NodeIndexerFactory.getNodeIndexer().getLastIndexed(
INDEX_WAITING_TIME);
long indexAgeMillis = receivedRequestMillis - indexWrittenMillis;
long cacheMaxAgeMillis = Math.max(CACHE_MIN_TIME,
((CACHE_MAX_TIME - indexAgeMillis)
/ CACHE_INTERVAL) * CACHE_INTERVAL);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.setHeader("Cache-Control", "public, max-age="
+ (cacheMaxAgeMillis / 1000L));
try (PrintWriter pw = response.getWriter()) {
rb.buildResponse(pw);
}
int relayDocumentsWritten = rh.getOrderedRelays().size();
int bridgeDocumentsWritten = rh.getOrderedBridges().size();
int charsWritten = rb.getCharsWritten();
long writtenResponseMillis = time.currentTimeMillis();
PerformanceMetrics.logStatistics(receivedRequestMillis, resourceType,
parameterMap.keySet(), parsedRequestMillis, relayDocumentsWritten,
bridgeDocumentsWritten, charsWritten, writtenResponseMillis);
}
private static Pattern searchQueryStringPattern =
Pattern.compile("(?:.*[\\?&])*?" // lazily skip other parameters
+ "search=([\\p{Graph} &&[^&]]+)" // capture parameter
+ "(?:&.*)*"); // skip remaining parameters
private static Pattern searchParameterPattern =
Pattern.compile("^\\$?[0-9a-fA-F]{1,40}$|" /* Hex fingerprint. */
+ "^[0-9a-zA-Z+/]{1,27}$|" /* Base64 fingerprint. */
+ "^[0-9a-zA-Z\\.]{1,19}$|" /* Nickname or IPv4 address. */
+ ipv6AddressPatternString + "|" /* IPv6 address. */
+ "^[a-zA-Z_]+:\\p{Graph}+$" /* Qualified search term. */);
protected static String[] parseSearchParameters(String queryString) {
Matcher searchQueryStringMatcher = searchQueryStringPattern.matcher(
queryString);
if (!searchQueryStringMatcher.matches()) {
/* Search query contains illegal character(s). */
return null;
}
String parameter = searchQueryStringMatcher.group(1);
String[] searchParameters =
parameter.replaceAll("%20", " ").split(" ");
for (String searchParameter : searchParameters) {
if (!searchParameterPattern.matcher(searchParameter).matches()) {
/* Illegal search term. */
return null;
}
}
return searchParameters;
}
private static Pattern fingerprintParameterPattern =
Pattern.compile("^[0-9a-zA-Z]{1,40}$");
private String parseFingerprintParameter(String parameter) {
if (!fingerprintParameterPattern.matcher(parameter).matches()) {
/* Fingerprint contains non-hex character(s). */
return null;
}
if (parameter.length() != 40) {
/* Only full fingerprints are accepted. */
return null;
}
return parameter;
}
private static Pattern countryCodeParameterPattern =
Pattern.compile("^[0-9a-zA-Z]{2}$");
private String parseCountryCodeParameter(String parameter) {
if (!countryCodeParameterPattern.matcher(parameter).matches()) {
/* Country code contains illegal characters or is shorter/longer
* than 2 characters. */
return null;
}
return parameter;
}
private static Pattern asNumberParameterPattern =
Pattern.compile("^[asAS]{0,2}[0-9]{1,10}$");
private String parseAsNumberParameter(String parameter) {
if (!asNumberParameterPattern.matcher(parameter).matches()) {
/* AS number contains illegal character(s). */
return null;
}
return parameter;
}
private static Pattern flagPattern =
Pattern.compile("^[a-zA-Z0-9]{1,20}$");
private String parseFlagParameter(String parameter) {
if (!flagPattern.matcher(parameter).matches()) {
/* Flag contains illegal character(s). */
return null;
}
return parameter;
}
private static Pattern daysPattern = Pattern.compile("^[0-9-]{1,10}$");
private int[] parseDaysParameter(String parameter) {
if (!daysPattern.matcher(parameter).matches()) {
/* Days contain illegal character(s). */
return null;
}
int fromDays = 0;
int toDays = Integer.MAX_VALUE;
try {
if (!parameter.contains("-")) {
fromDays = Integer.parseInt(parameter);
toDays = fromDays;
} else {
String[] parts = parameter.split("-", 2);
if (parts[0].length() > 0) {
fromDays = Integer.parseInt(parts[0]);
}
if (parts.length > 1 && parts[1].length() > 0) {
toDays = Integer.parseInt(parts[1]);
}
}
} catch (NumberFormatException e) {
/* Invalid format. */
return null;
}
if (fromDays > toDays) {
/* Second number or days must exceed first number. */
return null;
}
return new int[] { fromDays, toDays };
}
private String[] parseContactParameter(String parameter) {
for (char c : parameter.toCharArray()) {
if (c < 32 || c >= 127) {
/* Only accept printable ASCII. */
return null;
}
}
return parameter.split(" ");
}
private static Pattern orderParameterPattern =
Pattern.compile("^[0-9a-zA-Z_,-]*$");
private static HashSet<String> knownOrderParameters = new HashSet<>(
Arrays.asList(new String[] { OrderParameterValues.CONSENSUS_WEIGHT_ASC,
OrderParameterValues.CONSENSUS_WEIGHT_DES,
OrderParameterValues.FIRST_SEEN_ASC,
OrderParameterValues.FIRST_SEEN_DES }));
private String[] parseOrderParameter(String parameter) {
if (!orderParameterPattern.matcher(parameter).matches()) {
/* Orders contain illegal character(s). */
return null;
}
String[] orderParameters = parameter.toLowerCase().split(",");
Set<String> seenOrderParameters = new HashSet<>();
for (String orderParameter : orderParameters) {
if (!knownOrderParameters.contains(orderParameter)) {
/* Unknown order parameter. */
return null;
}
if (!seenOrderParameters.add(orderParameter.startsWith("-")
? orderParameter.substring(1) : orderParameter)) {
/* Duplicate parameter. */
return null;
}
}
return orderParameters;
}
private static Pattern fieldsParameterPattern =
Pattern.compile("^[0-9a-zA-Z_,]*$");
private String[] parseFieldsParameter(String parameter) {
if (!fieldsParameterPattern.matcher(parameter).matches()) {
/* Fields contain illegal character(s). */
return null;
}
return parameter.toLowerCase().split(",");
}
}
|