commit a6e08a33b95393dc29ace64bea8c50d808160e24 Author: Karsten Loesing karsten.loesing@gmx.net Date: Tue Jul 24 21:54:19 2018 +0200
Support comma-separated list in "as" parameter.
Implements the last part of #23713. --- CHANGELOG.md | 2 ++ .../torproject/onionoo/server/RequestHandler.java | 32 +++++++++------------- .../torproject/onionoo/server/ResourceServlet.java | 14 +++++++--- .../onionoo/server/ResourceServletTest.java | 6 ++++ 4 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b36eb..d024d83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Add "as" field as a copy of the "as_number" field in preparation of removing the "as_number" field in the future. - Add new "as_name" parameter to search relays by AS name. + - Support a comma-separated list of AS numbers in the "as" + parameter.
# Changes in version 6.1-1.15.0 - 2018-07-16 diff --git a/src/main/java/org/torproject/onionoo/server/RequestHandler.java b/src/main/java/org/torproject/onionoo/server/RequestHandler.java index e401ae9..33c35e1 100644 --- a/src/main/java/org/torproject/onionoo/server/RequestHandler.java +++ b/src/main/java/org/torproject/onionoo/server/RequestHandler.java @@ -72,9 +72,9 @@ public class RequestHandler { this.country = country; }
- private String as; + private String[] as;
- public void setAs(String as) { + public void setAs(String[] as) { this.as = as; }
@@ -408,29 +408,23 @@ public class RequestHandler { }
private void filterByAsNumber() { - if (this.as == null) { + if (this.as == null || 0 == this.as.length) { /* Not filtering by AS number. */ return; } - String asNumber = this.as.toUpperCase(); - if (!asNumber.startsWith("AS")) { - asNumber = "AS" + asNumber; - } - if (!this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) { - this.filteredRelays.clear(); - } else { - Set<String> relaysWithAsNumber = - this.nodeIndex.getRelaysByAsNumber().get(asNumber); - Set<String> removeRelays = new HashSet<>(); - for (String fingerprint : this.filteredRelays.keySet()) { - if (!relaysWithAsNumber.contains(fingerprint)) { - removeRelays.add(fingerprint); + Map<String, SummaryDocument> matchingRelays = new HashMap<>(); + for (String asNumber : this.as) { + if (this.nodeIndex.getRelaysByAsNumber().containsKey(asNumber)) { + for (String fingerprint + : this.nodeIndex.getRelaysByAsNumber().get(asNumber)) { + if (this.filteredRelays.containsKey(fingerprint)) { + matchingRelays.put(fingerprint, + this.filteredRelays.get(fingerprint)); + } } } - for (String fingerprint : removeRelays) { - this.filteredRelays.remove(fingerprint); - } } + this.filteredRelays = matchingRelays; this.filteredBridges.clear(); }
diff --git a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java index cf480d3..57c8f83 100644 --- a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java +++ b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java @@ -238,7 +238,7 @@ public class ResourceServlet extends HttpServlet { rh.setCountry(countryCodeParameter); } if (parameterMap.containsKey("as")) { - String asNumberParameter = this.parseAsNumberParameter( + String[] asNumberParameter = this.parseAsNumberParameter( parameterMap.get("as")); if (asNumberParameter == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); @@ -502,14 +502,20 @@ public class ResourceServlet extends HttpServlet { }
private static Pattern asNumberParameterPattern = - Pattern.compile("^[asAS]{0,2}[0-9]{1,10}$"); + Pattern.compile("((^|,)([aA][sS])?[1-9][0-9]{0,9})+$");
- private String parseAsNumberParameter(String parameter) { + private String[] parseAsNumberParameter(String parameter) { if (!asNumberParameterPattern.matcher(parameter).matches()) { /* AS number contains illegal character(s). */ return null; } - return parameter; + String[] parameterParts = parameter.toUpperCase().split(","); + String[] parsedParameter = new String[parameterParts.length]; + for (int i = 0; i < parameterParts.length; i++) { + parsedParameter[i] = (!parameterParts[i].startsWith("AS") ? "AS" : "") + + parameterParts[i]; + } + return parsedParameter; }
private String[] parseAsNameParameter(String parameter) { diff --git a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java index 37f7923..e176d9b 100644 --- a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java +++ b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java @@ -1174,6 +1174,12 @@ public class ResourceServletTest { }
@Test(timeout = 100) + public void testAs8767Or7922() { + this.assertSummaryDocument("/summary?as=8767,7922", 2, + new String[] { "TorkaZ", "Ferrari458" }, 0, null); + } + + @Test(timeout = 100) public void testAsNameComcast() { this.assertSummaryDocument("/summary?as_name=Comcast", 1, null, 0, null); }
tor-commits@lists.torproject.org