commit 395aedf194918afa8a436d34bb01f49d0187321f Author: Karsten Loesing karsten.loesing@gmx.net Date: Tue Jul 5 15:50:07 2016 +0200
Fix most of the style issues found by checkstyle.
Fixed the following number of issues per type:
1131 Indentation 431 EmptyLineSeparator 95 CustomImportOrder 76 OperatorWrap 54 SeparatorWrap 25 LocalVariableName 14 ModifierOrder 13 MultipleVariableDeclarations 5 WhitespaceAround 5 JavadocTagContinuationIndentation 4 EmptyCatchBlock 2 VariableDeclarationUsageDistance 2 MissingSwitchDefault 1 (... types with single occurrence omitted...) --- .../descriptor/DescriptorParseException.java | 2 + .../descriptor/DescriptorSourceFactory.java | 54 +- .../java/org/torproject/descriptor/ExitList.java | 2 +- .../descriptor/RelayNetworkStatusConsensus.java | 4 +- .../descriptor/RelayNetworkStatusVote.java | 4 +- .../descriptor/impl/BandwidthHistoryImpl.java | 21 +- .../descriptor/impl/BlockingIteratorImpl.java | 2 + .../impl/BridgeExtraInfoDescriptorImpl.java | 7 +- .../descriptor/impl/BridgeNetworkStatusImpl.java | 115 +++-- .../descriptor/impl/BridgePoolAssignmentImpl.java | 11 +- .../impl/BridgeServerDescriptorImpl.java | 7 +- .../descriptor/impl/DescriptorCollectorImpl.java | 21 +- .../descriptor/impl/DescriptorDownloaderImpl.java | 25 +- .../descriptor/impl/DescriptorFileImpl.java | 25 +- .../torproject/descriptor/impl/DescriptorImpl.java | 165 ++++--- .../descriptor/impl/DescriptorParseException.java | 5 +- .../descriptor/impl/DescriptorParserImpl.java | 7 +- .../descriptor/impl/DescriptorReaderImpl.java | 77 ++- .../descriptor/impl/DescriptorRequestImpl.java | 27 +- .../descriptor/impl/DirSourceEntryImpl.java | 76 +-- .../descriptor/impl/DirectoryDownloader.java | 13 +- .../impl/DirectoryKeyCertificateImpl.java | 146 +++--- .../descriptor/impl/DirectorySignatureImpl.java | 87 ++-- .../descriptor/impl/DownloadCoordinator.java | 1 + .../descriptor/impl/DownloadCoordinatorImpl.java | 87 ++-- .../descriptor/impl/ExitListEntryImpl.java | 23 +- .../torproject/descriptor/impl/ExitListImpl.java | 18 +- .../descriptor/impl/ExtraInfoDescriptorImpl.java | 548 ++++++++++++--------- .../descriptor/impl/MicrodescriptorImpl.java | 158 +++--- .../descriptor/impl/NetworkStatusEntryImpl.java | 133 ++--- .../descriptor/impl/NetworkStatusImpl.java | 35 +- .../torproject/descriptor/impl/ParseHelper.java | 63 ++- .../descriptor/impl/RelayDirectoryImpl.java | 241 ++++----- .../impl/RelayExtraInfoDescriptorImpl.java | 7 +- .../impl/RelayNetworkStatusConsensusImpl.java | 165 ++++--- .../descriptor/impl/RelayNetworkStatusImpl.java | 203 ++++---- .../impl/RelayNetworkStatusVoteImpl.java | 350 +++++++------ .../descriptor/impl/RelayServerDescriptorImpl.java | 7 +- .../descriptor/impl/RouterStatusEntryImpl.java | 5 + .../descriptor/impl/ServerDescriptorImpl.java | 421 +++++++++------- .../descriptor/impl/TorperfResultImpl.java | 208 ++++---- 41 files changed, 2028 insertions(+), 1548 deletions(-)
diff --git a/src/main/java/org/torproject/descriptor/DescriptorParseException.java b/src/main/java/org/torproject/descriptor/DescriptorParseException.java index 309d3f7..731db3e 100644 --- a/src/main/java/org/torproject/descriptor/DescriptorParseException.java +++ b/src/main/java/org/torproject/descriptor/DescriptorParseException.java @@ -12,7 +12,9 @@ package org.torproject.descriptor; @SuppressWarnings("deprecation") public class DescriptorParseException extends org.torproject.descriptor.impl.DescriptorParseException { + private static final long serialVersionUID = 100L; + public DescriptorParseException(String message) { super(message); } diff --git a/src/main/java/org/torproject/descriptor/DescriptorSourceFactory.java b/src/main/java/org/torproject/descriptor/DescriptorSourceFactory.java index af13f39..f51434f 100644 --- a/src/main/java/org/torproject/descriptor/DescriptorSourceFactory.java +++ b/src/main/java/org/torproject/descriptor/DescriptorSourceFactory.java @@ -44,7 +44,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String DOWNLOADER_DEFAULT = + public static final String DOWNLOADER_DEFAULT = "org.torproject.descriptor.impl.DescriptorDownloaderImpl";
/** @@ -53,7 +53,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String PARSER_DEFAULT = + public static final String PARSER_DEFAULT = "org.torproject.descriptor.impl.DescriptorParserImpl";
/** @@ -62,7 +62,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String READER_DEFAULT = + public static final String READER_DEFAULT = "org.torproject.descriptor.impl.DescriptorReaderImpl";
/** @@ -71,7 +71,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String COLLECTOR_DEFAULT = + public static final String COLLECTOR_DEFAULT = "org.torproject.descriptor.impl.DescriptorCollectorImpl";
/** @@ -81,7 +81,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String PARSER_PROPERTY = "descriptor.parser"; + public static final String PARSER_PROPERTY = "descriptor.parser";
/** * Property name for overriding the implementation of the @@ -90,7 +90,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String READER_PROPERTY = "descriptor.reader"; + public static final String READER_PROPERTY = "descriptor.reader";
/** * Property name for overriding the implementation of the @@ -99,7 +99,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String DOWNLOADER_PROPERTY = + public static final String DOWNLOADER_PROPERTY = "descriptor.downloader";
/** @@ -109,7 +109,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static String COLLECTOR_PROPERTY = "descriptor.collector"; + public static final String COLLECTOR_PROPERTY = "descriptor.collector";
/** * Create a new {@link DescriptorParser} by instantiating the class in @@ -117,7 +117,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static DescriptorParser createDescriptorParser() { + public static final DescriptorParser createDescriptorParser() { return (DescriptorParser) retrieve(PARSER_PROPERTY); }
@@ -127,7 +127,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static DescriptorReader createDescriptorReader() { + public static final DescriptorReader createDescriptorReader() { return (DescriptorReader) retrieve(READER_PROPERTY); }
@@ -137,7 +137,7 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static DescriptorDownloader createDescriptorDownloader() { + public static final DescriptorDownloader createDescriptorDownloader() { return (DescriptorDownloader) retrieve(DOWNLOADER_PROPERTY); }
@@ -147,30 +147,30 @@ public final class DescriptorSourceFactory { * * @since 1.0.0 */ - public final static DescriptorCollector createDescriptorCollector() { + public static final DescriptorCollector createDescriptorCollector() { return (DescriptorCollector) retrieve(COLLECTOR_PROPERTY); }
- private final static <T> Object retrieve(String type) { + private static final <T> Object retrieve(String type) { Object object; String clazzName = null; try { switch (type) { - case PARSER_PROPERTY: - clazzName = System.getProperty(type, PARSER_DEFAULT); - break; - case DOWNLOADER_PROPERTY: - clazzName = System.getProperty(type, DOWNLOADER_DEFAULT); - break; - case READER_PROPERTY: - clazzName = System.getProperty(type, READER_DEFAULT); - break; - case COLLECTOR_PROPERTY: - clazzName = System.getProperty(type, COLLECTOR_DEFAULT); - break; + case PARSER_PROPERTY: + clazzName = System.getProperty(type, PARSER_DEFAULT); + break; + case DOWNLOADER_PROPERTY: + clazzName = System.getProperty(type, DOWNLOADER_DEFAULT); + break; + case READER_PROPERTY: + clazzName = System.getProperty(type, READER_DEFAULT); + break; + case COLLECTOR_PROPERTY: + clazzName = System.getProperty(type, COLLECTOR_DEFAULT); + break; } - object = ClassLoader.getSystemClassLoader().loadClass(clazzName). - newInstance(); + object = ClassLoader.getSystemClassLoader().loadClass(clazzName) + .newInstance(); } catch (ClassNotFoundException ex) { throw new ImplementationNotAccessibleException("Cannot load class " + clazzName + "for type " + type, ex); diff --git a/src/main/java/org/torproject/descriptor/ExitList.java b/src/main/java/org/torproject/descriptor/ExitList.java index 2a5cb2e..181da0c 100644 --- a/src/main/java/org/torproject/descriptor/ExitList.java +++ b/src/main/java/org/torproject/descriptor/ExitList.java @@ -19,7 +19,7 @@ public interface ExitList extends Descriptor { * * @since 1.0.0 */ - public final static String EOL = "\n"; + public static final String EOL = "\n";
/** * Exit list entry containing results from a single exit scan. diff --git a/src/main/java/org/torproject/descriptor/RelayNetworkStatusConsensus.java b/src/main/java/org/torproject/descriptor/RelayNetworkStatusConsensus.java index 15fdaca..b38bd1d 100644 --- a/src/main/java/org/torproject/descriptor/RelayNetworkStatusConsensus.java +++ b/src/main/java/org/torproject/descriptor/RelayNetworkStatusConsensus.java @@ -187,8 +187,8 @@ public interface RelayNetworkStatusConsensus extends Descriptor { * directory protocol, encoded as 40 upper-case hexadecimal characters. * * @deprecated Replaced by {@link #getSignatures()} which permits an - * arbitrary number of signatures made by an authority using the same - * identity key digest and different algorithms. + * arbitrary number of signatures made by an authority using the + * same identity key digest and different algorithms. * * @since 1.0.0 */ diff --git a/src/main/java/org/torproject/descriptor/RelayNetworkStatusVote.java b/src/main/java/org/torproject/descriptor/RelayNetworkStatusVote.java index 1f77db6..abb7ff0 100644 --- a/src/main/java/org/torproject/descriptor/RelayNetworkStatusVote.java +++ b/src/main/java/org/torproject/descriptor/RelayNetworkStatusVote.java @@ -389,8 +389,8 @@ public interface RelayNetworkStatusVote extends Descriptor { * characters. * * @deprecated Replaced by {@link #getSignatures()} which permits an - * arbitrary number of signatures made by the authority using the same - * identity key digest and different algorithms. + * arbitrary number of signatures made by the authority using the + * same identity key digest and different algorithms. * * @since 1.0.0 */ diff --git a/src/main/java/org/torproject/descriptor/impl/BandwidthHistoryImpl.java b/src/main/java/org/torproject/descriptor/impl/BandwidthHistoryImpl.java index 295e0a4..0d998f6 100644 --- a/src/main/java/org/torproject/descriptor/impl/BandwidthHistoryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BandwidthHistoryImpl.java @@ -1,13 +1,14 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.BandwidthHistory; import org.torproject.descriptor.DescriptorParseException; + import java.util.SortedMap; import java.util.TreeMap;
-import org.torproject.descriptor.BandwidthHistory; - public class BandwidthHistoryImpl implements BandwidthHistory {
protected BandwidthHistoryImpl(String line, String lineNoOpt, @@ -18,17 +19,17 @@ public class BandwidthHistoryImpl implements BandwidthHistory { try { this.historyEndMillis = ParseHelper.parseTimestampAtIndex(line, partsNoOpt, 1, 2); - if (partsNoOpt[3].startsWith("(") && - partsNoOpt[4].startsWith("s)")) { - this.intervalLength = Long.parseLong(partsNoOpt[3]. - substring(1)); + if (partsNoOpt[3].startsWith("(") + && partsNoOpt[4].startsWith("s)")) { + this.intervalLength = Long.parseLong(partsNoOpt[3] + .substring(1)); if (this.intervalLength <= 0L) { throw new DescriptorParseException("Only positive interval " + "lengths are allowed in line '" + line + "'."); } String[] values = null; - if (partsNoOpt.length == 5 && - partsNoOpt[4].equals("s)")) { + if (partsNoOpt.length == 5 + && partsNoOpt[4].equals("s)")) { /* There are no bandwidth values to parse. */ isValid = true; } else if (partsNoOpt.length == 6) { @@ -66,24 +67,28 @@ public class BandwidthHistoryImpl implements BandwidthHistory { }
private String line; + @Override public String getLine() { return this.line; }
private long historyEndMillis; + @Override public long getHistoryEndMillis() { return this.historyEndMillis; }
private long intervalLength; + @Override public long getIntervalLength() { return this.intervalLength; }
private long[] bandwidthValues; + @Override public SortedMap<Long, Long> getBandwidthValues() { SortedMap<Long, Long> result = new TreeMap<>(); diff --git a/src/main/java/org/torproject/descriptor/impl/BlockingIteratorImpl.java b/src/main/java/org/torproject/descriptor/impl/BlockingIteratorImpl.java index 66426d8..c9e0590 100644 --- a/src/main/java/org/torproject/descriptor/impl/BlockingIteratorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BlockingIteratorImpl.java @@ -1,5 +1,6 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import java.util.Iterator; @@ -46,6 +47,7 @@ public class BlockingIteratorImpl<T> implements Iterator<T> {
/* Signalize that there won't be any further objects to be enqueued. */ private boolean outOfDescriptors = false; + protected synchronized void setOutOfDescriptors() { if (this.outOfDescriptors) { throw new IllegalStateException("Internal error: Sending " diff --git a/src/main/java/org/torproject/descriptor/impl/BridgeExtraInfoDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/BridgeExtraInfoDescriptorImpl.java index 15d40d8..81d0623 100644 --- a/src/main/java/org/torproject/descriptor/impl/BridgeExtraInfoDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BridgeExtraInfoDescriptorImpl.java @@ -1,14 +1,15 @@ /* Copyright 2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import java.util.ArrayList; -import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.BridgeExtraInfoDescriptor; import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.ExtraInfoDescriptor;
+import java.util.ArrayList; +import java.util.List; + public class BridgeExtraInfoDescriptorImpl extends ExtraInfoDescriptorImpl implements BridgeExtraInfoDescriptor {
diff --git a/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java index bf3804d..49abc5c 100644 --- a/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java @@ -1,8 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.BridgeNetworkStatus; import org.torproject.descriptor.DescriptorParseException; + import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -11,8 +14,6 @@ import java.util.Scanner; import java.util.SortedMap; import java.util.TimeZone;
-import org.torproject.descriptor.BridgeNetworkStatus; - /* Contains a bridge network status. */ public class BridgeNetworkStatusImpl extends NetworkStatusImpl implements BridgeNetworkStatus { @@ -31,9 +32,9 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl * "published" line. */ return; } - if (fileName.length() == - "20000101-000000-4A0CCD2DDC7995083D73F5D667100C8A5831F16D". - length()) { + if (fileName.length() + == "20000101-000000-4A0CCD2DDC7995083D73F5D667100C8A5831F16D" + .length()) { String publishedString = fileName.substring(0, "yyyyMMdd-HHmmss".length()); try { @@ -41,8 +42,8 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl "yyyyMMdd-HHmmss"); fileNameFormat.setLenient(false); fileNameFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - this.publishedMillis = fileNameFormat.parse(publishedString). - getTime(); + this.publishedMillis = fileNameFormat.parse(publishedString) + .getTime(); } catch (ParseException e) { } } @@ -74,22 +75,22 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "published": - this.parsePublishedLine(line, parts); - break; - case "flag-thresholds": - this.parseFlagThresholdsLine(line, parts); - break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in bridge network status."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "published": + this.parsePublishedLine(line, parts); + break; + case "flag-thresholds": + this.parseFlagThresholdsLine(line, parts); + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in bridge network status."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } } @@ -111,36 +112,36 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl try { for (Map.Entry<String, String> e : flagThresholds.entrySet()) { switch (e.getKey()) { - case "stable-uptime": - this.stableUptime = Long.parseLong(e.getValue()); - break; - case "stable-mtbf": - this.stableMtbf = Long.parseLong(e.getValue()); - break; - case "fast-speed": - this.fastBandwidth = Long.parseLong(e.getValue()); - break; - case "guard-wfu": - this.guardWfu = Double.parseDouble(e.getValue(). - replaceAll("%", "")); - break; - case "guard-tk": - this.guardTk = Long.parseLong(e.getValue()); - break; - case "guard-bw-inc-exits": - this.guardBandwidthIncludingExits = - Long.parseLong(e.getValue()); - break; - case "guard-bw-exc-exits": - this.guardBandwidthExcludingExits = - Long.parseLong(e.getValue()); - break; - case "enough-mtbf": - this.enoughMtbfInfo = Integer.parseInt(e.getValue()); - break; - case "ignoring-advertised-bws": - this.ignoringAdvertisedBws = Integer.parseInt(e.getValue()); - break; + case "stable-uptime": + this.stableUptime = Long.parseLong(e.getValue()); + break; + case "stable-mtbf": + this.stableMtbf = Long.parseLong(e.getValue()); + break; + case "fast-speed": + this.fastBandwidth = Long.parseLong(e.getValue()); + break; + case "guard-wfu": + this.guardWfu = Double.parseDouble(e.getValue() + .replaceAll("%", "")); + break; + case "guard-tk": + this.guardTk = Long.parseLong(e.getValue()); + break; + case "guard-bw-inc-exits": + this.guardBandwidthIncludingExits = + Long.parseLong(e.getValue()); + break; + case "guard-bw-exc-exits": + this.guardBandwidthExcludingExits = + Long.parseLong(e.getValue()); + break; + case "enough-mtbf": + this.enoughMtbfInfo = Integer.parseInt(e.getValue()); + break; + case "ignoring-advertised-bws": + this.ignoringAdvertisedBws = Integer.parseInt(e.getValue()); + break; } } } catch (NumberFormatException ex) { @@ -168,60 +169,70 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private long stableUptime; + @Override public long getStableUptime() { return this.stableUptime; }
private long stableMtbf; + @Override public long getStableMtbf() { return this.stableMtbf; }
private long fastBandwidth; + @Override public long getFastBandwidth() { return this.fastBandwidth; }
private double guardWfu; + @Override public double getGuardWfu() { return this.guardWfu; }
private long guardTk; + @Override public long getGuardTk() { return this.guardTk; }
private long guardBandwidthIncludingExits; + @Override public long getGuardBandwidthIncludingExits() { return this.guardBandwidthIncludingExits; }
private long guardBandwidthExcludingExits; + @Override public long getGuardBandwidthExcludingExits() { return this.guardBandwidthExcludingExits; }
private int enoughMtbfInfo; + @Override public int getEnoughMtbfInfo() { return this.enoughMtbfInfo; }
private int ignoringAdvertisedBws; + @Override public int getIgnoringAdvertisedBws() { return this.ignoringAdvertisedBws; diff --git a/src/main/java/org/torproject/descriptor/impl/BridgePoolAssignmentImpl.java b/src/main/java/org/torproject/descriptor/impl/BridgePoolAssignmentImpl.java index 99578e8..f28dcce 100644 --- a/src/main/java/org/torproject/descriptor/impl/BridgePoolAssignmentImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BridgePoolAssignmentImpl.java @@ -1,8 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.BridgePoolAssignment; import org.torproject.descriptor.DescriptorParseException; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -12,8 +15,6 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap;
-import org.torproject.descriptor.BridgePoolAssignment; - /* TODO Write a test class. */ public class BridgePoolAssignmentImpl extends DescriptorImpl implements BridgePoolAssignment { @@ -48,8 +49,8 @@ public class BridgePoolAssignmentImpl extends DescriptorImpl }
private void parseDescriptorBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.rawDescriptorBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.rawDescriptorBytes)) + .useDelimiter("\n"); while (s.hasNext()) { String line = s.next(); if (line.startsWith("bridge-pool-assignment ")) { @@ -85,12 +86,14 @@ public class BridgePoolAssignmentImpl extends DescriptorImpl }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private SortedMap<String, String> entries = new TreeMap<>(); + @Override public SortedMap<String, String> getEntries() { return new TreeMap<>(this.entries); diff --git a/src/main/java/org/torproject/descriptor/impl/BridgeServerDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/BridgeServerDescriptorImpl.java index eb2b933..f9e9ba8 100644 --- a/src/main/java/org/torproject/descriptor/impl/BridgeServerDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BridgeServerDescriptorImpl.java @@ -1,14 +1,15 @@ /* Copyright 2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import java.util.ArrayList; -import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.BridgeServerDescriptor; import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.ServerDescriptor;
+import java.util.ArrayList; +import java.util.List; + public class BridgeServerDescriptorImpl extends ServerDescriptorImpl implements BridgeServerDescriptor {
diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorCollectorImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorCollectorImpl.java index 1a030ef..5776827 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorCollectorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorCollectorImpl.java @@ -1,9 +1,12 @@ /* Copyright 2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
-import java.io.BufferedOutputStream; +import org.torproject.descriptor.DescriptorCollector; + import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; @@ -26,8 +29,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream;
-import org.torproject.descriptor.DescriptorCollector; - public class DescriptorCollectorImpl implements DescriptorCollector {
@Override @@ -51,8 +52,8 @@ public class DescriptorCollectorImpl implements DescriptorCollector { this.fetchRemoteDirectories(collecTorBaseUrl, remoteDirectories); SortedSet<String> parsedDirectories = new TreeSet<>(); SortedMap<String, Long> remoteFiles = new TreeMap<>(); - for (Map.Entry<String, String> e : - fetchedDirectoryListings.entrySet()) { + for (Map.Entry<String, String> e + : fetchedDirectoryListings.entrySet()) { String remoteDirectory = e.getKey(); String directoryListing = e.getValue(); SortedMap<String, Long> parsedRemoteFiles = @@ -174,9 +175,9 @@ public class DescriptorCollectorImpl implements DescriptorCollector { for (Map.Entry<String, Long> e : remoteFiles.entrySet()) { String filename = e.getKey(); long lastModifiedMillis = e.getValue(); - if (lastModifiedMillis < minLastModified || - (localFiles.containsKey(filename) && - localFiles.get(filename) >= lastModifiedMillis)) { + if (lastModifiedMillis < minLastModified + || (localFiles.containsKey(filename) + && localFiles.get(filename) >= lastModifiedMillis)) { continue; } String url = collecTorBaseUrl + filename; @@ -206,8 +207,8 @@ public class DescriptorCollectorImpl implements DescriptorCollector { int responseCode = huc.getResponseCode(); if (responseCode == 200) { InputStream is; - if (huc.getContentEncoding() != null && - huc.getContentEncoding().equalsIgnoreCase("gzip")) { + if (huc.getContentEncoding() != null + && huc.getContentEncoding().equalsIgnoreCase("gzip")) { is = new GZIPInputStream(huc.getInputStream()); } else { is = huc.getInputStream(); diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorDownloaderImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorDownloaderImpl.java index e726ce9..637c80e 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorDownloaderImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorDownloaderImpl.java @@ -1,16 +1,17 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorDownloader; +import org.torproject.descriptor.DescriptorRequest; + import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap;
-import org.torproject.descriptor.DescriptorRequest; -import org.torproject.descriptor.DescriptorDownloader; - public class DescriptorDownloaderImpl implements DescriptorDownloader {
@@ -18,6 +19,7 @@ public class DescriptorDownloaderImpl
private SortedMap<String, DirectoryDownloader> directoryAuthorities = new TreeMap<>(); + @Override public void addDirectoryAuthority(String nickname, String ip, int dirPort) { @@ -33,6 +35,7 @@ public class DescriptorDownloaderImpl
private SortedMap<String, DirectoryDownloader> directoryMirrors = new TreeMap<>(); + @Override public void addDirectoryMirror(String nickname, String ip, int dirPort) { @@ -67,14 +70,15 @@ public class DescriptorDownloaderImpl } /* TODO Relax the requirement for directory nicknames to be unique. * In theory, we can identify them by ip+port. */ - if (this.directoryAuthorities.containsKey(nickname) || - this.directoryMirrors.containsKey(nickname)) { + if (this.directoryAuthorities.containsKey(nickname) + || this.directoryMirrors.containsKey(nickname)) { throw new IllegalArgumentException("Directory nicknames must be " + "unique."); } }
private boolean downloadConsensus = false; + @Override public void setIncludeCurrentConsensus() { if (this.hasStartedDownloading) { @@ -85,6 +89,7 @@ public class DescriptorDownloaderImpl }
private boolean downloadConsensusFromAllAuthorities = false; + @Override public void setIncludeCurrentConsensusFromAllDirectoryAuthorities() { if (this.hasStartedDownloading) { @@ -95,6 +100,7 @@ public class DescriptorDownloaderImpl }
private boolean includeCurrentReferencedVotes = false; + @Override public void setIncludeCurrentReferencedVotes() { if (this.hasStartedDownloading) { @@ -105,6 +111,7 @@ public class DescriptorDownloaderImpl }
private Set<String> downloadVotes = new HashSet<>(); + @Override public void setIncludeCurrentVote(String fingerprint) { if (this.hasStartedDownloading) { @@ -207,6 +214,7 @@ public class DescriptorDownloaderImpl }
private long readTimeoutMillis = 60L * 1000L; + @Override public void setReadTimeout(long readTimeoutMillis) { if (this.hasStartedDownloading) { @@ -222,6 +230,7 @@ public class DescriptorDownloaderImpl }
private long connectTimeoutMillis = 60L * 1000L; + @Override public void setConnectTimeout(long connectTimeoutMillis) { if (this.hasStartedDownloading) { @@ -237,6 +246,7 @@ public class DescriptorDownloaderImpl }
private long globalTimeoutMillis = 60L * 60L * 1000L; + @Override public void setGlobalTimeout(long globalTimeoutMillis) { if (this.hasStartedDownloading) { @@ -252,6 +262,7 @@ public class DescriptorDownloaderImpl }
private boolean failUnrecognizedDescriptorLines = false; + @Override public void setFailUnrecognizedDescriptorLines() { if (this.hasStartedDownloading) { @@ -275,8 +286,8 @@ public class DescriptorDownloaderImpl this.includeCurrentReferencedVotes, this.connectTimeoutMillis, this.readTimeoutMillis, this.globalTimeoutMillis, this.failUnrecognizedDescriptorLines); - Iterator<DescriptorRequest> descriptorQueue = downloadCoordinator. - getDescriptorQueue(); + Iterator<DescriptorRequest> descriptorQueue = downloadCoordinator + .getDescriptorQueue(); return descriptorQueue; } } diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorFileImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorFileImpl.java index 801c546..479f855 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorFileImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorFileImpl.java @@ -1,75 +1,90 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.Descriptor; +import org.torproject.descriptor.DescriptorFile; + import java.io.File; import java.util.ArrayList; import java.util.List;
-import org.torproject.descriptor.Descriptor; -import org.torproject.descriptor.DescriptorFile; - public class DescriptorFileImpl implements DescriptorFile {
private File directory; + protected void setDirectory(File directory) { this.directory = directory; } + @Override public File getDirectory() { return this.directory; }
private File tarball; + protected void setTarball(File tarball) { this.tarball = tarball; } + @Override public File getTarball() { return this.tarball; }
private File file; + protected void setFile(File file) { this.file = file; } + @Override public File getFile() { return this.file; }
private String fileName; + protected void setFileName(String fileName) { this.fileName = fileName; } + @Override public String getFileName() { return this.fileName; }
private long lastModified; + protected void setLastModified(long lastModified) { this.lastModified = lastModified; } + @Override public long getLastModified() { return this.lastModified; }
private List<Descriptor> descriptors; + protected void setDescriptors(List<Descriptor> descriptors) { this.descriptors = descriptors; } + @Override public List<Descriptor> getDescriptors() { - return this.descriptors == null ? new ArrayList<Descriptor>() : - new ArrayList<>(this.descriptors); + return this.descriptors == null ? new ArrayList<Descriptor>() + : new ArrayList<>(this.descriptors); }
private Exception exception; + protected void setException(Exception exception) { this.exception = exception; } + @Override public Exception getException() { return this.exception; diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java index 5625b3f..a32b6cc 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java @@ -1,8 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.Descriptor; import org.torproject.descriptor.DescriptorParseException; + import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; @@ -11,8 +14,6 @@ import java.util.Map; import java.util.Scanner; import java.util.Set;
-import org.torproject.descriptor.Descriptor; - public abstract class DescriptorImpl implements Descriptor {
protected static List<Descriptor> parseDescriptors( @@ -28,21 +29,21 @@ public abstract class DescriptorImpl implements Descriptor { System.arraycopy(rawDescriptorBytes, 0, first100Chars, 0, first100Chars.length); String firstLines = new String(first100Chars); - if (firstLines.startsWith("@type network-status-consensus-3 1.") || - firstLines.startsWith("@type network-status-microdesc-" - + "consensus-3 1.") || - ((firstLines.startsWith("network-status-version 3") || - firstLines.contains("\nnetwork-status-version 3")) && - firstLines.contains("\nvote-status consensus\n"))) { - parsedDescriptors.addAll(RelayNetworkStatusConsensusImpl. - parseConsensuses(rawDescriptorBytes, + if (firstLines.startsWith("@type network-status-consensus-3 1.") + || firstLines.startsWith("@type network-status-microdesc-" + + "consensus-3 1.") + || ((firstLines.startsWith("network-status-version 3") + || firstLines.contains("\nnetwork-status-version 3")) + && firstLines.contains("\nvote-status consensus\n"))) { + parsedDescriptors.addAll(RelayNetworkStatusConsensusImpl + .parseConsensuses(rawDescriptorBytes, failUnrecognizedDescriptorLines)); } else if (firstLines.startsWith("@type network-status-vote-3 1.") - || ((firstLines.startsWith("network-status-version 3\n") || - firstLines.contains("\nnetwork-status-version 3\n")) && - firstLines.contains("\nvote-status vote\n"))) { - parsedDescriptors.addAll(RelayNetworkStatusVoteImpl. - parseVotes(rawDescriptorBytes, + || ((firstLines.startsWith("network-status-version 3\n") + || firstLines.contains("\nnetwork-status-version 3\n")) + && firstLines.contains("\nvote-status vote\n"))) { + parsedDescriptors.addAll(RelayNetworkStatusVoteImpl + .parseVotes(rawDescriptorBytes, failUnrecognizedDescriptorLines)); } else if (firstLines.startsWith("@type bridge-network-status 1.") || firstLines.startsWith("r ")) { @@ -50,56 +51,56 @@ public abstract class DescriptorImpl implements Descriptor { rawDescriptorBytes, fileName, failUnrecognizedDescriptorLines)); } else if (firstLines.startsWith( "@type bridge-server-descriptor 1.")) { - parsedDescriptors.addAll(BridgeServerDescriptorImpl. - parseDescriptors(rawDescriptorBytes, + parsedDescriptors.addAll(BridgeServerDescriptorImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type server-descriptor 1.") || - firstLines.startsWith("router ") || - firstLines.contains("\nrouter ")) { - parsedDescriptors.addAll(RelayServerDescriptorImpl. - parseDescriptors(rawDescriptorBytes, + } else if (firstLines.startsWith("@type server-descriptor 1.") + || firstLines.startsWith("router ") + || firstLines.contains("\nrouter ")) { + parsedDescriptors.addAll(RelayServerDescriptorImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); } else if (firstLines.startsWith("@type bridge-extra-info 1.")) { - parsedDescriptors.addAll(BridgeExtraInfoDescriptorImpl. - parseDescriptors(rawDescriptorBytes, + parsedDescriptors.addAll(BridgeExtraInfoDescriptorImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type extra-info 1.") || - firstLines.startsWith("extra-info ") || - firstLines.contains("\nextra-info ")) { - parsedDescriptors.addAll(RelayExtraInfoDescriptorImpl. - parseDescriptors(rawDescriptorBytes, + } else if (firstLines.startsWith("@type extra-info 1.") + || firstLines.startsWith("extra-info ") + || firstLines.contains("\nextra-info ")) { + parsedDescriptors.addAll(RelayExtraInfoDescriptorImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type microdescriptor 1.") || - firstLines.startsWith("onion-key\n") || - firstLines.contains("\nonion-key\n")) { - parsedDescriptors.addAll(MicrodescriptorImpl. - parseDescriptors(rawDescriptorBytes, + } else if (firstLines.startsWith("@type microdescriptor 1.") + || firstLines.startsWith("onion-key\n") + || firstLines.contains("\nonion-key\n")) { + parsedDescriptors.addAll(MicrodescriptorImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type bridge-pool-assignment 1.") || - firstLines.startsWith("bridge-pool-assignment ") || - firstLines.contains("\nbridge-pool-assignment ")) { - parsedDescriptors.addAll(BridgePoolAssignmentImpl. - parseDescriptors(rawDescriptorBytes, + } else if (firstLines.startsWith("@type bridge-pool-assignment 1.") + || firstLines.startsWith("bridge-pool-assignment ") + || firstLines.contains("\nbridge-pool-assignment ")) { + parsedDescriptors.addAll(BridgePoolAssignmentImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type dir-key-certificate-3 1.") || - firstLines.startsWith("dir-key-certificate-version ") || - firstLines.contains("\ndir-key-certificate-version ")) { - parsedDescriptors.addAll(DirectoryKeyCertificateImpl. - parseDescriptors(rawDescriptorBytes, + } else if (firstLines.startsWith("@type dir-key-certificate-3 1.") + || firstLines.startsWith("dir-key-certificate-version ") + || firstLines.contains("\ndir-key-certificate-version ")) { + parsedDescriptors.addAll(DirectoryKeyCertificateImpl + .parseDescriptors(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type tordnsel 1.") || - firstLines.startsWith("ExitNode ") || - firstLines.contains("\nExitNode ")) { + } else if (firstLines.startsWith("@type tordnsel 1.") + || firstLines.startsWith("ExitNode ") + || firstLines.contains("\nExitNode ")) { parsedDescriptors.add(new ExitListImpl(rawDescriptorBytes, fileName, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type network-status-2 1.") || - firstLines.startsWith("network-status-version 2\n") || - firstLines.contains("\nnetwork-status-version 2\n")) { + } else if (firstLines.startsWith("@type network-status-2 1.") + || firstLines.startsWith("network-status-version 2\n") + || firstLines.contains("\nnetwork-status-version 2\n")) { parsedDescriptors.add(new RelayNetworkStatusImpl(rawDescriptorBytes, failUnrecognizedDescriptorLines)); - } else if (firstLines.startsWith("@type directory 1.") || - firstLines.startsWith("signed-directory\n") || - firstLines.contains("\nsigned-directory\n")) { + } else if (firstLines.startsWith("@type directory 1.") + || firstLines.startsWith("signed-directory\n") + || firstLines.contains("\nsigned-directory\n")) { parsedDescriptors.add(new RelayDirectoryImpl(rawDescriptorBytes, failUnrecognizedDescriptorLines)); } else if (firstLines.startsWith("@type torperf 1.")) { @@ -122,10 +123,10 @@ public abstract class DescriptorImpl implements Descriptor { } catch (UnsupportedEncodingException e) { return rawDescriptors; } - int endAllDescriptors = rawDescriptorBytes.length, - startAnnotations = 0; - boolean containsAnnotations = ascii.startsWith("@") || - ascii.contains("\n@"); + int endAllDescriptors = rawDescriptorBytes.length; + int startAnnotations = 0; + boolean containsAnnotations = ascii.startsWith("@") + || ascii.contains("\n@"); while (startAnnotations < endAllDescriptors) { int startDescriptor; if (ascii.indexOf(startToken, startAnnotations) == 0) { @@ -159,6 +160,7 @@ public abstract class DescriptorImpl implements Descriptor { }
protected byte[] rawDescriptorBytes; + @Override public byte[] getRawDescriptorBytes() { return this.rawDescriptorBytes; @@ -167,10 +169,11 @@ public abstract class DescriptorImpl implements Descriptor { protected boolean failUnrecognizedDescriptorLines = false;
protected List<String> unrecognizedLines; + @Override public List<String> getUnrecognizedLines() { - return this.unrecognizedLines == null ? new ArrayList<String>() : - new ArrayList<>(this.unrecognizedLines); + return this.unrecognizedLines == null ? new ArrayList<String>() + : new ArrayList<>(this.unrecognizedLines); }
protected DescriptorImpl(byte[] rawDescriptorBytes, @@ -185,12 +188,13 @@ public abstract class DescriptorImpl implements Descriptor {
/* Parse annotation lines from the descriptor bytes. */ private List<String> annotations = new ArrayList<>(); + private void cutOffAnnotations(byte[] rawDescriptorBytes) throws DescriptorParseException { String ascii = new String(rawDescriptorBytes); int start = 0; - while ((start == 0 && ascii.startsWith("@")) || - (start > 0 && ascii.indexOf("\n@", start - 1) >= 0)) { + while ((start == 0 && ascii.startsWith("@")) + || (start > 0 && ascii.indexOf("\n@", start - 1) >= 0)) { int end = ascii.indexOf("\n", start); if (end < 0) { throw new DescriptorParseException("Annotation line does not " @@ -203,26 +207,31 @@ public abstract class DescriptorImpl implements Descriptor { int length = rawDescriptorBytes.length; byte[] rawDescriptor = new byte[length - start]; System.arraycopy(rawDescriptorBytes, start, rawDescriptor, 0, - length - start); + length - start); this.rawDescriptorBytes = rawDescriptor; } } + @Override public List<String> getAnnotations() { return new ArrayList<>(this.annotations); }
- /* Count parsed keywords for consistency checks by subclasses. */ - private String firstKeyword, lastKeyword; + private String firstKeyword; + + private String lastKeyword; + private Map<String, Integer> parsedKeywords = new HashMap<>(); + + /* Count parsed keywords for consistency checks by subclasses. */ private void countKeywords(byte[] rawDescriptorBytes, boolean blankLinesAllowed) throws DescriptorParseException { if (rawDescriptorBytes.length == 0) { throw new DescriptorParseException("Descriptor is empty."); } String descriptorString = new String(rawDescriptorBytes); - if (!blankLinesAllowed && (descriptorString.startsWith("\n") || - descriptorString.contains("\n\n"))) { + if (!blankLinesAllowed && (descriptorString.startsWith("\n") + || descriptorString.contains("\n\n"))) { throw new DescriptorParseException("Blank lines are not allowed."); } boolean skipCrypto = false; @@ -233,10 +242,10 @@ public abstract class DescriptorImpl implements Descriptor { skipCrypto = true; } else if (line.startsWith("-----END")) { skipCrypto = false; - } else if (!line.isEmpty() && !line.startsWith("@") && - !skipCrypto) { - String lineNoOpt = line.startsWith("opt ") ? - line.substring("opt ".length()) : line; + } else if (!line.isEmpty() && !line.startsWith("@") + && !skipCrypto) { + String lineNoOpt = line.startsWith("opt ") + ? line.substring("opt ".length()) : line; String keyword = lineNoOpt.split(" ", -1)[0]; if (keyword.equals("")) { throw new DescriptorParseException("Illegal keyword in line '" @@ -257,8 +266,8 @@ public abstract class DescriptorImpl implements Descriptor {
protected void checkFirstKeyword(String keyword) throws DescriptorParseException { - if (this.firstKeyword == null || - !this.firstKeyword.equals(keyword)) { + if (this.firstKeyword == null + || !this.firstKeyword.equals(keyword)) { throw new DescriptorParseException("Keyword '" + keyword + "' must " + "be contained in the first line."); } @@ -266,8 +275,8 @@ public abstract class DescriptorImpl implements Descriptor {
protected void checkLastKeyword(String keyword) throws DescriptorParseException { - if (this.lastKeyword == null || - !this.lastKeyword.equals(keyword)) { + if (this.lastKeyword == null + || !this.lastKeyword.equals(keyword)) { throw new DescriptorParseException("Keyword '" + keyword + "' must " + "be contained in the last line."); } @@ -301,8 +310,8 @@ public abstract class DescriptorImpl implements Descriptor { protected void checkAtMostOnceKeywords(Set<String> keywords) throws DescriptorParseException { for (String keyword : keywords) { - if (this.parsedKeywords.containsKey(keyword) && - this.parsedKeywords.get(keyword) > 1) { + if (this.parsedKeywords.containsKey(keyword) + && this.parsedKeywords.get(keyword) > 1) { throw new DescriptorParseException("Keyword '" + keyword + "' is " + "contained " + this.parsedKeywords.get(keyword) + " times, " + "but must be contained at most once."); @@ -313,8 +322,8 @@ public abstract class DescriptorImpl implements Descriptor { protected void checkKeywordsDependOn(Set<String> dependentKeywords, String dependingKeyword) throws DescriptorParseException { for (String dependentKeyword : dependentKeywords) { - if (this.parsedKeywords.containsKey(dependentKeyword) && - !this.parsedKeywords.containsKey(dependingKeyword)) { + if (this.parsedKeywords.containsKey(dependentKeyword) + && !this.parsedKeywords.containsKey(dependingKeyword)) { throw new DescriptorParseException("Keyword '" + dependentKeyword + "' is contained, but keyword '" + dependingKeyword + "' is " + "not."); diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorParseException.java b/src/main/java/org/torproject/descriptor/impl/DescriptorParseException.java index 0f9add2..9015471 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorParseException.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorParseException.java @@ -1,13 +1,16 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
/** * @deprecated Replaced by - * org.torproject.descriptor.DescriptorParseException + * org.torproject.descriptor.DescriptorParseException */ @Deprecated public class DescriptorParseException extends Exception { + private static final long serialVersionUID = 100L; + protected DescriptorParseException(String message) { super(message); } diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorParserImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorParserImpl.java index 6ac53f8..24cbf15 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorParserImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorParserImpl.java @@ -1,13 +1,14 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import org.torproject.descriptor.DescriptorParseException; -import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.Descriptor; +import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.DescriptorParser;
+import java.util.List; + public class DescriptorParserImpl implements DescriptorParser {
private boolean failUnrecognizedDescriptorLines; diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java index 8da88e9..b77b3b1 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java @@ -1,8 +1,19 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.Descriptor; +import org.torproject.descriptor.DescriptorFile; import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.DescriptorParser; +import org.torproject.descriptor.DescriptorReader; + +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; +import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; + import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -21,20 +32,12 @@ import java.util.SortedMap; import java.util.Stack; import java.util.TreeMap;
-import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; -import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; -import org.torproject.descriptor.Descriptor; -import org.torproject.descriptor.DescriptorFile; -import org.torproject.descriptor.DescriptorParser; -import org.torproject.descriptor.DescriptorReader; - public class DescriptorReaderImpl implements DescriptorReader {
private boolean hasStartedReading = false;
private List<File> directories = new ArrayList<>(); + @Override public void addDirectory(File directory) { if (this.hasStartedReading) { @@ -45,6 +48,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private List<File> tarballs = new ArrayList<>(); + @Override public void addTarball(File tarball) { if (this.hasStartedReading) { @@ -55,6 +59,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private File historyFile; + @Override public void setExcludeFiles(File historyFile) { if (this.hasStartedReading) { @@ -65,6 +70,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private SortedMap<String, Long> excludedFiles; + @Override public void setExcludedFiles(SortedMap<String, Long> excludedFiles) { if (this.hasStartedReading) { @@ -93,6 +99,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private boolean failUnrecognizedDescriptorLines = false; + @Override public void setFailUnrecognizedDescriptorLines() { if (this.hasStartedReading) { @@ -103,6 +110,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private Integer maxDescriptorFilesInQueue = null; + @Override public void setMaxDescriptorFilesInQueue(int max) { if (this.hasStartedReading) { @@ -113,6 +121,7 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private DescriptorReaderRunnable reader; + @Override public Iterator<DescriptorFile> readDescriptors() { if (this.hasStartedReading) { @@ -133,15 +142,25 @@ public class DescriptorReaderImpl implements DescriptorReader { }
private static class DescriptorReaderRunnable implements Runnable { + private List<File> directories; + private List<File> tarballs; + private BlockingIteratorImpl<DescriptorFile> descriptorQueue; + private File historyFile; - private SortedMap<String, Long> excludedFilesBefore = new TreeMap<>(), - excludedFilesAfter = new TreeMap<>(), - parsedFilesAfter = new TreeMap<>(); + + private SortedMap<String, Long> excludedFilesBefore = new TreeMap<>(); + + private SortedMap<String, Long> excludedFilesAfter = new TreeMap<>(); + + private SortedMap<String, Long> parsedFilesAfter = new TreeMap<>(); + private DescriptorParser descriptorParser; + private boolean hasFinishedReading = false; + private DescriptorReaderRunnable(List<File> directories, List<File> tarballs, BlockingIteratorImpl<DescriptorFile> descriptorQueue, @@ -158,6 +177,7 @@ public class DescriptorReaderImpl implements DescriptorReader { this.descriptorParser.setFailUnrecognizedDescriptorLines( failUnrecognizedDescriptorLines); } + public void run() { try { this.readOldHistory(); @@ -178,6 +198,7 @@ public class DescriptorReaderImpl implements DescriptorReader { this.writeNewHistory(); } } + private void readOldHistory() { if (this.historyFile == null) { return; @@ -203,6 +224,7 @@ public class DescriptorReaderImpl implements DescriptorReader { /* TODO Handle this exception. */ } } + private void writeNewHistory() { if (this.historyFile == null) { return; @@ -227,6 +249,7 @@ public class DescriptorReaderImpl implements DescriptorReader { /* TODO Handle this exception. */ } } + private void readDescriptors() { for (File directory : this.directories) { if (!directory.exists() || !directory.isDirectory()) { @@ -239,16 +262,16 @@ public class DescriptorReaderImpl implements DescriptorReader { File file = files.pop(); if (file.isDirectory()) { files.addAll(Arrays.asList(file.listFiles())); - } else if (file.getName().endsWith(".tar") || - file.getName().endsWith(".tar.bz2") || - file.getName().endsWith(".tar.xz")) { + } else if (file.getName().endsWith(".tar") + || file.getName().endsWith(".tar.bz2") + || file.getName().endsWith(".tar.xz")) { this.tarballs.add(file); } else { String absolutePath = file.getAbsolutePath(); long lastModifiedMillis = file.lastModified(); - if (this.excludedFilesBefore.containsKey(absolutePath) && - this.excludedFilesBefore.get(absolutePath) == - lastModifiedMillis) { + if (this.excludedFilesBefore.containsKey(absolutePath) + && this.excludedFilesBefore.get(absolutePath) + == lastModifiedMillis) { this.excludedFilesAfter.put(absolutePath, lastModifiedMillis); continue; @@ -272,21 +295,22 @@ public class DescriptorReaderImpl implements DescriptorReader { } } } + private void readTarballs() { List<File> files = new ArrayList<>(this.tarballs); boolean abortReading = false; while (!abortReading && !files.isEmpty()) { File tarball = files.remove(0); - if (!tarball.getName().endsWith(".tar") && - !tarball.getName().endsWith(".tar.bz2") && - !tarball.getName().endsWith(".tar.xz")) { + if (!tarball.getName().endsWith(".tar") + && !tarball.getName().endsWith(".tar.bz2") + && !tarball.getName().endsWith(".tar.xz")) { continue; } String absolutePath = tarball.getAbsolutePath(); long lastModifiedMillis = tarball.lastModified(); - if (this.excludedFilesBefore.containsKey(absolutePath) && - this.excludedFilesBefore.get(absolutePath) == - lastModifiedMillis) { + if (this.excludedFilesBefore.containsKey(absolutePath) + && this.excludedFilesBefore.get(absolutePath) + == lastModifiedMillis) { this.excludedFilesAfter.put(absolutePath, lastModifiedMillis); continue; } @@ -314,8 +338,8 @@ public class DescriptorReaderImpl implements DescriptorReader { new DescriptorFileImpl(); descriptorFile.setTarball(tarball); descriptorFile.setFileName(tae.getName()); - descriptorFile.setLastModified(tae.getLastModifiedDate(). - getTime()); + descriptorFile.setLastModified(tae.getLastModifiedDate() + .getTime()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; byte[] data = new byte[1024]; @@ -344,6 +368,7 @@ public class DescriptorReaderImpl implements DescriptorReader { } } } + private List<Descriptor> readFile(File file) throws IOException, DescriptorParseException { FileInputStream fis = new FileInputStream(file); diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorRequestImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorRequestImpl.java index 0238f24..f27796f 100644 --- a/src/main/java/org/torproject/descriptor/impl/DescriptorRequestImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DescriptorRequestImpl.java @@ -1,111 +1,134 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.Descriptor; import org.torproject.descriptor.DescriptorRequest;
+import java.util.List; + public class DescriptorRequestImpl implements DescriptorRequest {
private String requestedResource; + protected void setRequestedResource(String requestedResource) { this.requestedResource = requestedResource; } + protected String getRequestedResource() { return this.requestedResource; }
private String descriptorType; + protected void setDescriptorType(String descriptorType) { this.descriptorType = descriptorType; } + protected String getDescriptorType() { return this.descriptorType; }
private byte[] responseBytes; + protected byte[] getResponseBytes() { return this.responseBytes; } + protected void setResponseBytes(byte[] responseBytes) { this.responseBytes = responseBytes; }
private String requestUrl; + @Override public String getRequestUrl() { return this.requestUrl; }
private String directoryNickname; + protected void setDirectoryNickname(String directoryNickname) { this.directoryNickname = directoryNickname; } + @Override public String getDirectoryNickname() { return this.directoryNickname; }
private int responseCode; + protected void setResponseCode(int responseCode) { this.responseCode = responseCode; } + @Override public int getResponseCode() { return this.responseCode; }
private long requestStart; + protected void setRequestStart(long requestStart) { this.requestStart = requestStart; } + @Override public long getRequestStart() { return this.requestStart; }
private long requestEnd; + protected void setRequestEnd(long requestEnd) { this.requestEnd = requestEnd; } + @Override public long getRequestEnd() { return this.requestEnd; }
private boolean connectTimeoutHasExpired; + @Override public boolean connectTimeoutHasExpired() { return this.connectTimeoutHasExpired; }
private boolean readTimeoutHasExpired; + @Override public boolean readTimeoutHasExpired() { return this.readTimeoutHasExpired; }
private boolean globalTimeoutHasExpired; + @Override public boolean globalTimeoutHasExpired() { return this.globalTimeoutHasExpired; }
private List<Descriptor> descriptors; + protected void setDescriptors(List<Descriptor> descriptors) { this.descriptors = descriptors; } + @Override public List<Descriptor> getDescriptors() { return this.descriptors; }
private Exception exception; + protected void setException(Exception exception) { this.exception = exception; } + @Override public Exception getException() { return this.exception; diff --git a/src/main/java/org/torproject/descriptor/impl/DirSourceEntryImpl.java b/src/main/java/org/torproject/descriptor/impl/DirSourceEntryImpl.java index fb2f5ad..2782925 100644 --- a/src/main/java/org/torproject/descriptor/impl/DirSourceEntryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DirSourceEntryImpl.java @@ -1,26 +1,30 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.DirSourceEntry; + import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet;
-import org.torproject.descriptor.DirSourceEntry; - public class DirSourceEntryImpl implements DirSourceEntry {
private byte[] dirSourceEntryBytes; + @Override public byte[] getDirSourceEntryBytes() { return this.dirSourceEntryBytes; }
private boolean failUnrecognizedDescriptorLines; + private List<String> unrecognizedLines; + protected List<String> getAndClearUnrecognizedLines() { List<String> lines = this.unrecognizedLines; this.unrecognizedLines = null; @@ -38,7 +42,10 @@ public class DirSourceEntryImpl implements DirSourceEntry { this.checkAndClearKeywords(); }
- private SortedSet<String> exactlyOnceKeywords, atMostOnceKeywords; + private SortedSet<String> exactlyOnceKeywords; + + private SortedSet<String> atMostOnceKeywords; + private void initializeKeywords() { this.exactlyOnceKeywords = new TreeSet<>(); this.exactlyOnceKeywords.add("dir-source"); @@ -76,40 +83,40 @@ public class DirSourceEntryImpl implements DirSourceEntry {
private void parseDirSourceEntryBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.dirSourceEntryBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.dirSourceEntryBytes)) + .useDelimiter("\n"); boolean skipCrypto = false; while (s.hasNext()) { String line = s.next(); String[] parts = line.split(" "); switch (parts[0]) { - case "dir-source": - this.parseDirSourceLine(line); - break; - case "contact": - this.parseContactLine(line); - break; - case "vote-digest": - this.parseVoteDigestLine(line); - break; - case "-----BEGIN": - skipCrypto = true; - break; - case "-----END": - skipCrypto = false; - break; - default: - if (!skipCrypto) { - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in dir-source entry."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "dir-source": + this.parseDirSourceLine(line); + break; + case "contact": + this.parseContactLine(line); + break; + case "vote-digest": + this.parseVoteDigestLine(line); + break; + case "-----BEGIN": + skipCrypto = true; + break; + case "-----END": + skipCrypto = false; + break; + default: + if (!skipCrypto) { + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in dir-source entry."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -162,54 +169,63 @@ public class DirSourceEntryImpl implements DirSourceEntry { }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String identity; + @Override public String getIdentity() { return this.identity; }
private boolean isLegacy; + @Override public boolean isLegacy() { return this.isLegacy; }
private String hostname; + @Override public String getHostname() { return this.hostname; }
private String ip; + @Override public String getIp() { return this.ip; }
private int dirPort; + @Override public int getDirPort() { return this.dirPort; }
private int orPort; + @Override public int getOrPort() { return this.orPort; }
private String contactLine; + @Override public String getContactLine() { return this.contactLine; }
private String voteDigest; + @Override public String getVoteDigest() { return this.voteDigest; diff --git a/src/main/java/org/torproject/descriptor/impl/DirectoryDownloader.java b/src/main/java/org/torproject/descriptor/impl/DirectoryDownloader.java index a27ed76..d3a707a 100644 --- a/src/main/java/org/torproject/descriptor/impl/DirectoryDownloader.java +++ b/src/main/java/org/torproject/descriptor/impl/DirectoryDownloader.java @@ -1,16 +1,17 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParser; +import org.torproject.descriptor.DescriptorSourceFactory; + import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.zip.InflaterInputStream;
-import org.torproject.descriptor.DescriptorParser; -import org.torproject.descriptor.DescriptorSourceFactory; - /* Download descriptors from one directory authority or mirror. First, * ask the coordinator thread to create a request, run it, and deliver * the response. Repeat until the coordinator thread says there are no @@ -18,8 +19,11 @@ import org.torproject.descriptor.DescriptorSourceFactory; public class DirectoryDownloader implements Runnable {
private String nickname; + private String ipPort; + private DescriptorParser descriptorParser; + protected DirectoryDownloader(String nickname, String ip, int dirPort) { this.nickname = nickname; this.ipPort = ip + ":" + String.valueOf(dirPort); @@ -28,17 +32,20 @@ public class DirectoryDownloader implements Runnable { }
private DownloadCoordinator downloadCoordinator; + protected void setDownloadCoordinator( DownloadCoordinator downloadCoordinator) { this.downloadCoordinator = downloadCoordinator; }
private long connectTimeout; + protected void setConnectTimeout(long connectTimeout) { this.connectTimeout = connectTimeout; }
private long readTimeout; + protected void setReadTimeout(long readTimeout) { this.readTimeout = readTimeout; } diff --git a/src/main/java/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java b/src/main/java/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java index b62fc8e..d76b32c 100644 --- a/src/main/java/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java @@ -1,7 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.DirectoryKeyCertificate; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,9 +18,6 @@ import java.util.Set;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.DirectoryKeyCertificate; - /* TODO Add test class. */
public class DirectoryKeyCertificateImpl extends DescriptorImpl @@ -58,8 +59,8 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl }
private void parseDescriptorBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.rawDescriptorBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.rawDescriptorBytes)) + .useDelimiter("\n"); String nextCrypto = ""; StringBuilder crypto = null; while (s.hasNext()) { @@ -67,78 +68,78 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "dir-key-certificate-version": - this.parseDirKeyCertificateVersionLine(line, parts); - break; - case "dir-address": - this.parseDirAddressLine(line, parts); - break; - case "fingerprint": - this.parseFingerprintLine(line, parts); - break; - case "dir-identity-key": - this.parseDirIdentityKeyLine(line, parts); - nextCrypto = "dir-identity-key"; - break; - case "dir-key-published": - this.parseDirKeyPublishedLine(line, parts); - break; - case "dir-key-expires": - this.parseDirKeyExpiresLine(line, parts); - break; - case "dir-signing-key": - this.parseDirSigningKeyLine(line, parts); - nextCrypto = "dir-signing-key"; - break; - case "dir-key-crosscert": - this.parseDirKeyCrosscertLine(line, parts); - nextCrypto = "dir-key-crosscert"; - break; - case "dir-key-certification": - this.parseDirKeyCertificationLine(line, parts); - nextCrypto = "dir-key-certification"; - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - switch (nextCrypto) { + case "dir-key-certificate-version": + this.parseDirKeyCertificateVersionLine(line, parts); + break; + case "dir-address": + this.parseDirAddressLine(line, parts); + break; + case "fingerprint": + this.parseFingerprintLine(line, parts); + break; case "dir-identity-key": - this.dirIdentityKey = cryptoString; + this.parseDirIdentityKeyLine(line, parts); + nextCrypto = "dir-identity-key"; + break; + case "dir-key-published": + this.parseDirKeyPublishedLine(line, parts); + break; + case "dir-key-expires": + this.parseDirKeyExpiresLine(line, parts); break; case "dir-signing-key": - this.dirSigningKey = cryptoString; + this.parseDirSigningKeyLine(line, parts); + nextCrypto = "dir-signing-key"; break; case "dir-key-crosscert": - this.dirKeyCrosscert = cryptoString; + this.parseDirKeyCrosscertLine(line, parts); + nextCrypto = "dir-key-crosscert"; break; case "dir-key-certification": - this.dirKeyCertification = cryptoString; + this.parseDirKeyCertificationLine(line, parts); + nextCrypto = "dir-key-certification"; break; - default: - throw new DescriptorParseException("Unrecognized crypto " - + "block in directory key certificate."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "-----BEGIN": + crypto = new StringBuilder(); + crypto.append(line).append("\n"); + break; + case "-----END": crypto.append(line).append("\n"); - } else { - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in directory key certificate."); + String cryptoString = crypto.toString(); + crypto = null; + switch (nextCrypto) { + case "dir-identity-key": + this.dirIdentityKey = cryptoString; + break; + case "dir-signing-key": + this.dirSigningKey = cryptoString; + break; + case "dir-key-crosscert": + this.dirKeyCrosscert = cryptoString; + break; + case "dir-key-certification": + this.dirKeyCertification = cryptoString; + break; + default: + throw new DescriptorParseException("Unrecognized crypto " + + "block in directory key certificate."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in directory key certificate."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -225,8 +226,8 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.certificateDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -240,66 +241,77 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl }
private int dirKeyCertificateVersion; + @Override public int getDirKeyCertificateVersion() { return this.dirKeyCertificateVersion; }
private String address; + @Override public String getAddress() { return this.address; }
private int port = -1; + @Override public int getPort() { return this.port; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private String dirIdentityKey; + @Override public String getDirIdentityKey() { return this.dirIdentityKey; }
private long dirKeyPublishedMillis; + @Override public long getDirKeyPublishedMillis() { return this.dirKeyPublishedMillis; }
private long dirKeyExpiresMillis; + @Override public long getDirKeyExpiresMillis() { return this.dirKeyExpiresMillis; }
private String dirSigningKey; + @Override public String getDirSigningKey() { return this.dirSigningKey; }
private String dirKeyCrosscert; + @Override public String getDirKeyCrosscert() { return this.dirKeyCrosscert; }
private String dirKeyCertification; + @Override public String getDirKeyCertification() { return this.dirKeyCertification; }
private String certificateDigest; + @Override public String getCertificateDigest() { return this.certificateDigest; diff --git a/src/main/java/org/torproject/descriptor/impl/DirectorySignatureImpl.java b/src/main/java/org/torproject/descriptor/impl/DirectorySignatureImpl.java index a955f62..771b632 100644 --- a/src/main/java/org/torproject/descriptor/impl/DirectorySignatureImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DirectorySignatureImpl.java @@ -1,20 +1,23 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.DirectorySignature; + import java.util.ArrayList; import java.util.List; import java.util.Scanner;
-import org.torproject.descriptor.DirectorySignature; - public class DirectorySignatureImpl implements DirectorySignature {
private byte[] directorySignatureBytes;
private boolean failUnrecognizedDescriptorLines; + private List<String> unrecognizedLines; + protected List<String> getAndClearUnrecognizedLines() { List<String> lines = this.unrecognizedLines; this.unrecognizedLines = null; @@ -32,56 +35,56 @@ public class DirectorySignatureImpl implements DirectorySignature {
private void parseDirectorySignatureBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.directorySignatureBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.directorySignatureBytes)) + .useDelimiter("\n"); StringBuilder crypto = null; while (s.hasNext()) { String line = s.next(); String[] parts = line.split(" ", -1); String keyword = parts[0]; switch (keyword) { - case "directory-signature": - int algorithmOffset = 0; - switch (parts.length) { - case 4: - this.algorithm = parts[1]; - algorithmOffset = 1; + case "directory-signature": + int algorithmOffset = 0; + switch (parts.length) { + case 4: + this.algorithm = parts[1]; + algorithmOffset = 1; + break; + case 3: + break; + default: + throw new DescriptorParseException("Illegal line '" + line + + "'."); + } + this.identity = ParseHelper.parseHexString(line, + parts[1 + algorithmOffset]); + this.signingKeyDigest = ParseHelper.parseHexString( + line, parts[2 + algorithmOffset]); break; - case 3: + case "-----BEGIN": + crypto = new StringBuilder(); + crypto.append(line).append("\n"); break; - default: - throw new DescriptorParseException("Illegal line '" + line - + "'."); - } - this.identity = ParseHelper.parseHexString(line, - parts[1 + algorithmOffset]); - this.signingKeyDigest = ParseHelper.parseHexString( - line, parts[2 + algorithmOffset]); - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - this.signature = cryptoString; - break; - default: - if (crypto != null) { + case "-----END": crypto.append(line).append("\n"); - } else { - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in dir-source entry."); + String cryptoString = crypto.toString(); + crypto = null; + this.signature = cryptoString; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in dir-source entry."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -89,24 +92,28 @@ public class DirectorySignatureImpl implements DirectorySignature { static final String DEFAULT_ALGORITHM = "sha1";
private String algorithm; + @Override public String getAlgorithm() { return this.algorithm == null ? DEFAULT_ALGORITHM : this.algorithm; }
private String identity; + @Override public String getIdentity() { return this.identity; }
private String signingKeyDigest; + @Override public String getSigningKeyDigest() { return this.signingKeyDigest; }
private String signature; + @Override public String getSignature() { return this.signature; diff --git a/src/main/java/org/torproject/descriptor/impl/DownloadCoordinator.java b/src/main/java/org/torproject/descriptor/impl/DownloadCoordinator.java index 72cfeae..fbad62d 100644 --- a/src/main/java/org/torproject/descriptor/impl/DownloadCoordinator.java +++ b/src/main/java/org/torproject/descriptor/impl/DownloadCoordinator.java @@ -1,5 +1,6 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
public interface DownloadCoordinator { diff --git a/src/main/java/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java b/src/main/java/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java index a8e3731..0c31736 100644 --- a/src/main/java/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java @@ -1,7 +1,13 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.Descriptor; +import org.torproject.descriptor.DescriptorRequest; +import org.torproject.descriptor.DirSourceEntry; +import org.torproject.descriptor.RelayNetworkStatusConsensus; + import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -11,28 +17,32 @@ import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeSet;
-import org.torproject.descriptor.Descriptor; -import org.torproject.descriptor.DescriptorRequest; -import org.torproject.descriptor.DirSourceEntry; -import org.torproject.descriptor.RelayNetworkStatusConsensus; - /* TODO This whole download logic is a mess and needs a cleanup. */ public class DownloadCoordinatorImpl implements DownloadCoordinator {
private BlockingIteratorImpl<DescriptorRequest> descriptorQueue = new BlockingIteratorImpl<>(); + protected Iterator<DescriptorRequest> getDescriptorQueue() { return this.descriptorQueue; }
private SortedSet<String> runningDirectories; + private SortedMap<String, DirectoryDownloader> directoryAuthorities; + private SortedMap<String, DirectoryDownloader> directoryMirrors; + private boolean downloadConsensusFromAllAuthorities; + private boolean includeCurrentReferencedVotes; + private long connectTimeoutMillis; + private long readTimeoutMillis; + private long globalTimeoutMillis; + private boolean failUnrecognizedDescriptorLines;
protected DownloadCoordinatorImpl( @@ -58,8 +68,8 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { this.globalTimeoutMillis = globalTimeoutMillis; this.failUnrecognizedDescriptorLines = failUnrecognizedDescriptorLines; - if (this.directoryMirrors.isEmpty() && - this.directoryAuthorities.isEmpty()) { + if (this.directoryMirrors.isEmpty() + && this.directoryAuthorities.isEmpty()) { this.descriptorQueue.setOutOfDescriptors(); /* TODO Should we say anything if we don't have any directories * configured? */ @@ -68,8 +78,8 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { this); this.globalTimerThread = new Thread(globalTimer); this.globalTimerThread.start(); - for (DirectoryDownloader directoryMirror : - this.directoryMirrors.values()) { + for (DirectoryDownloader directoryMirror + : this.directoryMirrors.values()) { directoryMirror.setDownloadCoordinator(this); directoryMirror.setConnectTimeout(this.connectTimeoutMillis); directoryMirror.setReadTimeout(this.readTimeoutMillis); @@ -77,8 +87,8 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { this.failUnrecognizedDescriptorLines); new Thread(directoryMirror).start(); } - for (DirectoryDownloader directoryAuthority : - this.directoryAuthorities.values()) { + for (DirectoryDownloader directoryAuthority + : this.directoryAuthorities.values()) { directoryAuthority.setDownloadCoordinator(this); directoryAuthority.setConnectTimeout(this.connectTimeoutMillis); directoryAuthority.setReadTimeout(this.readTimeoutMillis); @@ -92,16 +102,22 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { /* Interrupt all downloads if the total download time exceeds a given * time. */ private Thread globalTimerThread; + private static class GlobalTimer implements Runnable { + private long timeoutMillis; + private DownloadCoordinatorImpl downloadCoordinator; + private GlobalTimer(long timeoutMillis, DownloadCoordinatorImpl downloadCoordinator) { this.timeoutMillis = timeoutMillis; this.downloadCoordinator = downloadCoordinator; } + public void run() { - long started = System.currentTimeMillis(), sleep; + long started = System.currentTimeMillis(); + long sleep; while ((sleep = started + this.timeoutMillis - System.currentTimeMillis()) > 0L) { try { @@ -148,10 +164,10 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { while (!this.hasFinishedDownloading) { DescriptorRequestImpl request = new DescriptorRequestImpl(); request.setDirectoryNickname(nickname); - if ((this.missingConsensus || - (this.downloadConsensusFromAllAuthorities && - this.directoryAuthorities.containsKey(nickname))) && - !this.requestedConsensuses.contains(nickname)) { + if ((this.missingConsensus + || (this.downloadConsensusFromAllAuthorities + && this.directoryAuthorities.containsKey(nickname))) + && !this.requestedConsensuses.contains(nickname)) { if (!this.downloadConsensusFromAllAuthorities) { this.missingConsensus = false; } @@ -162,12 +178,13 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { request.setDescriptorType("consensus"); return request; } - if (!this.missingVotes.isEmpty() && - this.directoryAuthorities.containsKey(nickname)) { + if (!this.missingVotes.isEmpty() + && this.directoryAuthorities.containsKey(nickname)) { String requestingVote = null; for (String missingVote : this.missingVotes) { - if (!this.requestedVotes.containsKey(nickname) || - !this.requestedVotes.get(nickname).contains(missingVote)) { + if (!this.requestedVotes.containsKey(nickname) + || !this.requestedVotes.get(nickname).contains( + missingVote)) { requestingVote = missingVote; } } @@ -208,26 +225,26 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { switch (response.getDescriptorType()) { case "consensus": this.requestingConsensuses.remove(nickname); - if (response.getResponseCode() == 200 && - response.getDescriptors() != null) { + if (response.getResponseCode() == 200 + && response.getDescriptors() != null) { if (this.includeCurrentReferencedVotes) { /* TODO Only add votes if the consensus is not older than one * hour. Or does that make no sense? */ - for (Descriptor parsedDescriptor : - response.getDescriptors()) { - if (!(parsedDescriptor instanceof - RelayNetworkStatusConsensus)) { + for (Descriptor parsedDescriptor + : response.getDescriptors()) { + if (!(parsedDescriptor + instanceof RelayNetworkStatusConsensus)) { continue; } RelayNetworkStatusConsensus parsedConsensus = (RelayNetworkStatusConsensus) parsedDescriptor; - for (DirSourceEntry dirSource : - parsedConsensus.getDirSourceEntries().values()) { + for (DirSourceEntry dirSource + : parsedConsensus.getDirSourceEntries().values()) { String identity = dirSource.getIdentity(); if (!this.missingVotes.contains(identity)) { boolean alreadyRequested = false; - for (Set<String> requestedBefore : - this.requestedVotes.values()) { + for (Set<String> requestedBefore + : this.requestedVotes.values()) { if (requestedBefore.contains(identity)) { alreadyRequested = true; break; @@ -255,11 +272,11 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator { this.descriptorQueue.add(response); } boolean doneDownloading = true; - if ((this.missingConsensus || - this.downloadConsensusFromAllAuthorities) && - (!this.requestedConsensuses.containsAll( - this.runningDirectories) || - !this.requestingConsensuses.isEmpty())) { + if ((this.missingConsensus + || this.downloadConsensusFromAllAuthorities) + && (!this.requestedConsensuses.containsAll( + this.runningDirectories) + || !this.requestingConsensuses.isEmpty())) { doneDownloading = false; } if (!this.requestingVotes.isEmpty()) { diff --git a/src/main/java/org/torproject/descriptor/impl/ExitListEntryImpl.java b/src/main/java/org/torproject/descriptor/impl/ExitListEntryImpl.java index efbf31c..e62fb91 100644 --- a/src/main/java/org/torproject/descriptor/impl/ExitListEntryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/ExitListEntryImpl.java @@ -1,9 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.ExitList; +import org.torproject.descriptor.ExitListEntry;
import java.util.ArrayList; import java.util.HashMap; @@ -13,14 +15,14 @@ import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet;
-import org.torproject.descriptor.ExitListEntry; - public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry {
private byte[] exitListEntryBytes;
private boolean failUnrecognizedDescriptorLines; + private List<String> unrecognizedLines; + protected List<String> getAndClearUnrecognizedLines() { List<String> lines = this.unrecognizedLines; this.unrecognizedLines = null; @@ -40,8 +42,8 @@ public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry { List<ExitListEntry> oldEntries() { List<ExitListEntry> result = new ArrayList<>(); if (this.exitAddresses.size() > 1) { - for (Map.Entry<String, Long> entry : - this.exitAddresses.entrySet()) { + for (Map.Entry<String, Long> entry + : this.exitAddresses.entrySet()) { result.add(new ExitListEntryImpl(this.fingerprint, this.publishedMillis, this.lastStatusMillis, entry.getKey(), entry.getValue())); @@ -64,6 +66,7 @@ public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry { }
private SortedSet<String> keywordCountingSet; + private void initializeKeywords() { this.keywordCountingSet = new TreeSet<>(); this.keywordCountingSet.add("ExitNode"); @@ -91,8 +94,8 @@ public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry {
private void parseExitListEntryBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.exitListEntryBytes)). - useDelimiter(ExitList.EOL); + Scanner s = new Scanner(new String(this.exitListEntryBytes)) + .useDelimiter(ExitList.EOL); while (s.hasNext()) { String line = s.next(); String[] parts = line.split(" "); @@ -169,24 +172,28 @@ public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry { }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private long lastStatusMillis; + @Override public long getLastStatusMillis() { return this.lastStatusMillis; }
private String exitAddress; + @Override public String getExitAddress() { if (null == exitAddress) { @@ -199,12 +206,14 @@ public class ExitListEntryImpl implements ExitListEntry, ExitList.Entry { }
private Map<String, Long> exitAddresses = new HashMap<>(); + @Override - public Map<String, Long> getExitAddresses(){ + public Map<String, Long> getExitAddresses() { return new HashMap<>(this.exitAddresses); }
private long scanMillis; + @Override public long getScanMillis() { if (null == exitAddress) { diff --git a/src/main/java/org/torproject/descriptor/impl/ExitListImpl.java b/src/main/java/org/torproject/descriptor/impl/ExitListImpl.java index 10619ba..ab3e516 100644 --- a/src/main/java/org/torproject/descriptor/impl/ExitListImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/ExitListImpl.java @@ -1,8 +1,12 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.ExitList; +import org.torproject.descriptor.ExitListEntry; + import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -12,9 +16,6 @@ import java.util.Scanner; import java.util.Set; import java.util.TimeZone;
-import org.torproject.descriptor.ExitList; -import org.torproject.descriptor.ExitListEntry; - public class ExitListImpl extends DescriptorImpl implements ExitList {
protected ExitListImpl(byte[] rawDescriptorBytes, String fileName, @@ -27,8 +28,8 @@ public class ExitListImpl extends DescriptorImpl implements ExitList {
private void setPublishedMillisFromFileName(String fileName) throws DescriptorParseException { - if (this.downloadedMillis == 0L && - fileName.length() == "2012-02-01-04-06-24".length()) { + if (this.downloadedMillis == 0L + && fileName.length() == "2012-02-01-04-06-24".length()) { try { SimpleDateFormat fileNameFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss"); @@ -110,8 +111,8 @@ public class ExitListImpl extends DescriptorImpl implements ExitList { exitListEntryBytes, this.failUnrecognizedDescriptorLines); this.exitListEntries.add(exitListEntry); this.oldExitListEntries.addAll(exitListEntry.oldEntries()); - List<String> unrecognizedExitListEntryLines = exitListEntry. - getAndClearUnrecognizedLines(); + List<String> unrecognizedExitListEntryLines = exitListEntry + .getAndClearUnrecognizedLines(); if (unrecognizedExitListEntryLines != null) { if (this.unrecognizedLines == null) { this.unrecognizedLines = new ArrayList<>(); @@ -121,12 +122,14 @@ public class ExitListImpl extends DescriptorImpl implements ExitList { }
private long downloadedMillis; + @Override public long getDownloadedMillis() { return this.downloadedMillis; }
private Set<ExitListEntry> oldExitListEntries = new HashSet<>(); + @Deprecated @Override public Set<ExitListEntry> getExitListEntries() { @@ -134,6 +137,7 @@ public class ExitListImpl extends DescriptorImpl implements ExitList { }
private Set<ExitList.Entry> exitListEntries = new HashSet<>(); + @Override public Set<ExitList.Entry> getEntries() { return new HashSet<>(this.exitListEntries); diff --git a/src/main/java/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java index 3f72616..f67c793 100644 --- a/src/main/java/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java @@ -1,7 +1,12 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.BandwidthHistory; +import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.ExtraInfoDescriptor; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -20,10 +25,6 @@ import java.util.TreeMap;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.BandwidthHistory; -import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.ExtraInfoDescriptor; - public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl implements ExtraInfoDescriptor {
@@ -77,216 +78,217 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private void parseDescriptorBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.rawDescriptorBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.rawDescriptorBytes)) + .useDelimiter("\n"); String nextCrypto = ""; List<String> cryptoLines = null; while (s.hasNext()) { String line = s.next(); - String lineNoOpt = line.startsWith("opt ") ? - line.substring("opt ".length()) : line; + String lineNoOpt = line.startsWith("opt ") + ? line.substring("opt ".length()) : line; String[] partsNoOpt = lineNoOpt.split("[ \t]+"); String keyword = partsNoOpt[0]; switch (keyword) { - case "extra-info": - this.parseExtraInfoLine(line, lineNoOpt, partsNoOpt); - break; - case "published": - this.parsePublishedLine(line, lineNoOpt, partsNoOpt); - break; - case "read-history": - this.parseReadHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "write-history": - this.parseWriteHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "geoip-db-digest": - this.parseGeoipDbDigestLine(line, lineNoOpt, partsNoOpt); - break; - case "geoip6-db-digest": - this.parseGeoip6DbDigestLine(line, lineNoOpt, partsNoOpt); - break; - case "geoip-start-time": - this.parseGeoipStartTimeLine(line, lineNoOpt, partsNoOpt); - break; - case "geoip-client-origins": - this.parseGeoipClientOriginsLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-stats-end": - this.parseDirreqStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-ips": - this.parseDirreqV2IpsLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-ips": - this.parseDirreqV3IpsLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-reqs": - this.parseDirreqV2ReqsLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-reqs": - this.parseDirreqV3ReqsLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-share": - this.parseDirreqV2ShareLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-share": - this.parseDirreqV3ShareLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-resp": - this.parseDirreqV2RespLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-resp": - this.parseDirreqV3RespLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-direct-dl": - this.parseDirreqV2DirectDlLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-direct-dl": - this.parseDirreqV3DirectDlLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v2-tunneled-dl": - this.parseDirreqV2TunneledDlLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-v3-tunneled-dl": - this.parseDirreqV3TunneledDlLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-read-history": - this.parseDirreqReadHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "dirreq-write-history": - this.parseDirreqWriteHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "entry-stats-end": - this.parseEntryStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "entry-ips": - this.parseEntryIpsLine(line, lineNoOpt, partsNoOpt); - break; - case "cell-stats-end": - this.parseCellStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "cell-processed-cells": - this.parseCellProcessedCellsLine(line, lineNoOpt, partsNoOpt); - break; - case "cell-queued-cells": - this.parseCellQueuedCellsLine(line, lineNoOpt, partsNoOpt); - break; - case "cell-time-in-queue": - this.parseCellTimeInQueueLine(line, lineNoOpt, partsNoOpt); - break; - case "cell-circuits-per-decile": - this.parseCellCircuitsPerDecileLine(line, lineNoOpt, partsNoOpt); - break; - case "conn-bi-direct": - this.parseConnBiDirectLine(line, lineNoOpt, partsNoOpt); - break; - case "exit-stats-end": - this.parseExitStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "exit-kibibytes-written": - this.parseExitKibibytesWrittenLine(line, lineNoOpt, partsNoOpt); - break; - case "exit-kibibytes-read": - this.parseExitKibibytesReadLine(line, lineNoOpt, partsNoOpt); - break; - case "exit-streams-opened": - this.parseExitStreamsOpenedLine(line, lineNoOpt, partsNoOpt); - break; - case "bridge-stats-end": - this.parseBridgeStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "bridge-ips": - this.parseBridgeStatsIpsLine(line, lineNoOpt, partsNoOpt); - break; - case "bridge-ip-versions": - this.parseBridgeIpVersionsLine(line, lineNoOpt, partsNoOpt); - break; - case "bridge-ip-transports": - this.parseBridgeIpTransportsLine(line, lineNoOpt, partsNoOpt); - break; - case "transport": - this.parseTransportLine(line, lineNoOpt, partsNoOpt); - break; - case "hidserv-stats-end": - this.parseHidservStatsEndLine(line, lineNoOpt, partsNoOpt); - break; - case "hidserv-rend-relayed-cells": - this.parseHidservRendRelayedCellsLine(line, lineNoOpt, - partsNoOpt); - break; - case "hidserv-dir-onions-seen": - this.parseHidservDirOnionsSeenLine(line, lineNoOpt, partsNoOpt); - break; - case "identity-ed25519": - this.parseIdentityEd25519Line(line, lineNoOpt, partsNoOpt); - nextCrypto = "identity-ed25519"; - break; - case "master-key-ed25519": - this.parseMasterKeyEd25519Line(line, lineNoOpt, partsNoOpt); - break; - case "router-sig-ed25519": - this.parseRouterSigEd25519Line(line, lineNoOpt, partsNoOpt); - break; - case "router-signature": - this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "router-signature"; - break; - case "router-digest": - this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); - break; - case "router-digest-sha256": - this.parseRouterDigestSha256Line(line, lineNoOpt, partsNoOpt); - break; - case "-----BEGIN": - cryptoLines = new ArrayList<>(); - cryptoLines.add(line); - break; - case "-----END": - cryptoLines.add(line); - StringBuilder sb = new StringBuilder(); - for (String cryptoLine : cryptoLines) { - sb.append("\n").append(cryptoLine); - } - String cryptoString = sb.toString().substring(1); - switch (nextCrypto) { - case "router-signature": - this.routerSignature = cryptoString; + case "extra-info": + this.parseExtraInfoLine(line, lineNoOpt, partsNoOpt); + break; + case "published": + this.parsePublishedLine(line, lineNoOpt, partsNoOpt); + break; + case "read-history": + this.parseReadHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "write-history": + this.parseWriteHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "geoip-db-digest": + this.parseGeoipDbDigestLine(line, lineNoOpt, partsNoOpt); + break; + case "geoip6-db-digest": + this.parseGeoip6DbDigestLine(line, lineNoOpt, partsNoOpt); + break; + case "geoip-start-time": + this.parseGeoipStartTimeLine(line, lineNoOpt, partsNoOpt); + break; + case "geoip-client-origins": + this.parseGeoipClientOriginsLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-stats-end": + this.parseDirreqStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-ips": + this.parseDirreqV2IpsLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-ips": + this.parseDirreqV3IpsLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-reqs": + this.parseDirreqV2ReqsLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-reqs": + this.parseDirreqV3ReqsLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-share": + this.parseDirreqV2ShareLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-share": + this.parseDirreqV3ShareLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-resp": + this.parseDirreqV2RespLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-resp": + this.parseDirreqV3RespLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-direct-dl": + this.parseDirreqV2DirectDlLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-direct-dl": + this.parseDirreqV3DirectDlLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v2-tunneled-dl": + this.parseDirreqV2TunneledDlLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-v3-tunneled-dl": + this.parseDirreqV3TunneledDlLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-read-history": + this.parseDirreqReadHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "dirreq-write-history": + this.parseDirreqWriteHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "entry-stats-end": + this.parseEntryStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "entry-ips": + this.parseEntryIpsLine(line, lineNoOpt, partsNoOpt); + break; + case "cell-stats-end": + this.parseCellStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "cell-processed-cells": + this.parseCellProcessedCellsLine(line, lineNoOpt, partsNoOpt); + break; + case "cell-queued-cells": + this.parseCellQueuedCellsLine(line, lineNoOpt, partsNoOpt); + break; + case "cell-time-in-queue": + this.parseCellTimeInQueueLine(line, lineNoOpt, partsNoOpt); + break; + case "cell-circuits-per-decile": + this.parseCellCircuitsPerDecileLine(line, lineNoOpt, + partsNoOpt); + break; + case "conn-bi-direct": + this.parseConnBiDirectLine(line, lineNoOpt, partsNoOpt); + break; + case "exit-stats-end": + this.parseExitStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "exit-kibibytes-written": + this.parseExitKibibytesWrittenLine(line, lineNoOpt, partsNoOpt); + break; + case "exit-kibibytes-read": + this.parseExitKibibytesReadLine(line, lineNoOpt, partsNoOpt); + break; + case "exit-streams-opened": + this.parseExitStreamsOpenedLine(line, lineNoOpt, partsNoOpt); + break; + case "bridge-stats-end": + this.parseBridgeStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "bridge-ips": + this.parseBridgeStatsIpsLine(line, lineNoOpt, partsNoOpt); + break; + case "bridge-ip-versions": + this.parseBridgeIpVersionsLine(line, lineNoOpt, partsNoOpt); + break; + case "bridge-ip-transports": + this.parseBridgeIpTransportsLine(line, lineNoOpt, partsNoOpt); + break; + case "transport": + this.parseTransportLine(line, lineNoOpt, partsNoOpt); + break; + case "hidserv-stats-end": + this.parseHidservStatsEndLine(line, lineNoOpt, partsNoOpt); + break; + case "hidserv-rend-relayed-cells": + this.parseHidservRendRelayedCellsLine(line, lineNoOpt, + partsNoOpt); + break; + case "hidserv-dir-onions-seen": + this.parseHidservDirOnionsSeenLine(line, lineNoOpt, partsNoOpt); break; case "identity-ed25519": - this.identityEd25519 = cryptoString; - this.parseIdentityEd25519CryptoBlock(cryptoString); + this.parseIdentityEd25519Line(line, lineNoOpt, partsNoOpt); + nextCrypto = "identity-ed25519"; break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized crypto " - + "block '" + cryptoString + "' in extra-info " - + "descriptor."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); - } - this.unrecognizedLines.addAll(cryptoLines); - } - cryptoLines = null; - nextCrypto = ""; - } - break; - default: - if (cryptoLines != null) { + case "master-key-ed25519": + this.parseMasterKeyEd25519Line(line, lineNoOpt, partsNoOpt); + break; + case "router-sig-ed25519": + this.parseRouterSigEd25519Line(line, lineNoOpt, partsNoOpt); + break; + case "router-signature": + this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "router-signature"; + break; + case "router-digest": + this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); + break; + case "router-digest-sha256": + this.parseRouterDigestSha256Line(line, lineNoOpt, partsNoOpt); + break; + case "-----BEGIN": + cryptoLines = new ArrayList<>(); cryptoLines.add(line); - } else { - ParseHelper.parseKeyword(line, partsNoOpt[0]); - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in extra-info descriptor."); + break; + case "-----END": + cryptoLines.add(line); + StringBuilder sb = new StringBuilder(); + for (String cryptoLine : cryptoLines) { + sb.append("\n").append(cryptoLine); + } + String cryptoString = sb.toString().substring(1); + switch (nextCrypto) { + case "router-signature": + this.routerSignature = cryptoString; + break; + case "identity-ed25519": + this.identityEd25519 = cryptoString; + this.parseIdentityEd25519CryptoBlock(cryptoString); + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized crypto " + + "block '" + cryptoString + "' in extra-info " + + "descriptor."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.addAll(cryptoLines); + } + cryptoLines = null; + nextCrypto = ""; + } + break; + default: + if (cryptoLines != null) { + cryptoLines.add(line); } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + ParseHelper.parseKeyword(line, partsNoOpt[0]); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in extra-info descriptor."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -365,11 +367,11 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl this.dirreqStatsIntervalLength = parsedStatsEndData[1]; }
- private long[] parseStatsEndLine(String line, String partsNoOpt[], + private long[] parseStatsEndLine(String line, String[] partsNoOpt, int partsNoOptExpectedLength) throws DescriptorParseException { - if (partsNoOpt.length != partsNoOptExpectedLength || - partsNoOpt[3].length() < 2 || !partsNoOpt[3].startsWith("(") || - !partsNoOpt[4].equals("s)")) { + if (partsNoOpt.length != partsNoOptExpectedLength + || partsNoOpt[3].length() < 2 || !partsNoOpt[3].startsWith("(") + || !partsNoOpt[4].equals("s)")) { throw new DescriptorParseException("Illegal line '" + line + "'."); } long[] result = new long[2]; @@ -422,8 +424,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl private double parseShareLine(String line, String[] partsNoOpt) throws DescriptorParseException { double share = -1.0; - if (partsNoOpt.length == 2 && partsNoOpt[1].length() >= 2 && - partsNoOpt[1].endsWith("%")) { + if (partsNoOpt.length == 2 && partsNoOpt[1].length() >= 2 + && partsNoOpt[1].endsWith("%")) { String shareString = partsNoOpt[1]; shareString = shareString.substring(0, shareString.length() - 1); try { @@ -516,8 +518,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl
private void parseCellProcessedCellsLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - this.cellProcessedCells = ParseHelper. - parseCommaSeparatedIntegerValueList(line, partsNoOpt, 1); + this.cellProcessedCells = ParseHelper + .parseCommaSeparatedIntegerValueList(line, partsNoOpt, 1); if (this.cellProcessedCells.length != 10) { throw new DescriptorParseException("There must be exact ten values " + "in line '" + line + "'."); @@ -536,8 +538,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl
private void parseCellTimeInQueueLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - this.cellTimeInQueue = ParseHelper. - parseCommaSeparatedIntegerValueList(line, partsNoOpt, 1); + this.cellTimeInQueue = ParseHelper + .parseCommaSeparatedIntegerValueList(line, partsNoOpt, 1); if (this.cellTimeInQueue.length != 10) { throw new DescriptorParseException("There must be exact ten values " + "in line '" + line + "'."); @@ -567,8 +569,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl 6); this.connBiDirectStatsEndMillis = parsedStatsEndData[0]; this.connBiDirectStatsIntervalLength = parsedStatsEndData[1]; - Integer[] parsedConnBiDirectStats = ParseHelper. - parseCommaSeparatedIntegerValueList(line, partsNoOpt, 5); + Integer[] parsedConnBiDirectStats = ParseHelper + .parseCommaSeparatedIntegerValueList(line, partsNoOpt, 5); if (parsedConnBiDirectStats.length != 4) { throw new DescriptorParseException("Illegal line '" + line + "' in " + "extra-info descriptor."); @@ -590,24 +592,24 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl private void parseExitKibibytesWrittenLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - this.exitKibibytesWritten = this.sortByPorts(ParseHelper. - parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); + this.exitKibibytesWritten = this.sortByPorts(ParseHelper + .parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); this.verifyPorts(line, this.exitKibibytesWritten.keySet()); this.verifyBytesOrStreams(line, this.exitKibibytesWritten.values()); }
private void parseExitKibibytesReadLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - this.exitKibibytesRead = this.sortByPorts(ParseHelper. - parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); + this.exitKibibytesRead = this.sortByPorts(ParseHelper + .parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); this.verifyPorts(line, this.exitKibibytesRead.keySet()); this.verifyBytesOrStreams(line, this.exitKibibytesRead.values()); }
private void parseExitStreamsOpenedLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - this.exitStreamsOpened = this.sortByPorts(ParseHelper. - parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); + this.exitStreamsOpened = this.sortByPorts(ParseHelper + .parseCommaSeparatedKeyLongValueList(line, partsNoOpt, 1, 0)); this.verifyPorts(line, this.exitStreamsOpened.keySet()); this.verifyBytesOrStreams(line, this.exitStreamsOpened.values()); } @@ -617,7 +619,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl SortedMap<String, Long> byPortNumber = new TreeMap<String, Long>(new Comparator<String>() { public int compare(String arg0, String arg1) { - int port0 = 0, port1 = 0; + int port0 = 0; + int port1 = 0; try { port1 = Integer.parseInt(arg1); } catch (NumberFormatException e) { @@ -635,7 +638,9 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl } else { return 0; } - }}); + } + } + ); byPortNumber.putAll(naturalOrder); return byPortNumber; } @@ -835,8 +840,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.extraInfoDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -866,8 +871,8 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.extraInfoDigestSha256 = DatatypeConverter.printBase64Binary( - MessageDigest.getInstance("SHA-256").digest(forDigest)). - replaceAll("=", ""); + MessageDigest.getInstance("SHA-256").digest(forDigest)) + .replaceAll("=", ""); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -881,72 +886,84 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String extraInfoDigest; + @Override public String getExtraInfoDigest() { return this.extraInfoDigest; }
private String extraInfoDigestSha256; + @Override public String getExtraInfoDigestSha256() { return this.extraInfoDigestSha256; }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private BandwidthHistory readHistory; + @Override public BandwidthHistory getReadHistory() { return this.readHistory; }
private BandwidthHistory writeHistory; + @Override public BandwidthHistory getWriteHistory() { return this.writeHistory; }
private String geoipDbDigest; + @Override public String getGeoipDbDigest() { return this.geoipDbDigest; }
private String geoip6DbDigest; + @Override public String getGeoip6DbDigest() { return this.geoip6DbDigest; }
private long dirreqStatsEndMillis = -1L; + @Override public long getDirreqStatsEndMillis() { return this.dirreqStatsEndMillis; }
private long dirreqStatsIntervalLength = -1L; + @Override public long getDirreqStatsIntervalLength() { return this.dirreqStatsIntervalLength; }
private String dirreqV2Ips; + @Override public SortedMap<String, Integer> getDirreqV2Ips() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -954,6 +971,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV3Ips; + @Override public SortedMap<String, Integer> getDirreqV3Ips() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -961,6 +979,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV2Reqs; + @Override public SortedMap<String, Integer> getDirreqV2Reqs() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -968,6 +987,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV3Reqs; + @Override public SortedMap<String, Integer> getDirreqV3Reqs() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -975,18 +995,21 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private double dirreqV2Share = -1.0; + @Override public double getDirreqV2Share() { return this.dirreqV2Share; }
private double dirreqV3Share = -1.0; + @Override public double getDirreqV3Share() { return this.dirreqV3Share; }
private String dirreqV2Resp; + @Override public SortedMap<String, Integer> getDirreqV2Resp() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -994,6 +1017,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV3Resp; + @Override public SortedMap<String, Integer> getDirreqV3Resp() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1001,6 +1025,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV2DirectDl; + @Override public SortedMap<String, Integer> getDirreqV2DirectDl() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1008,6 +1033,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV3DirectDl; + @Override public SortedMap<String, Integer> getDirreqV3DirectDl() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1015,6 +1041,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV2TunneledDl; + @Override public SortedMap<String, Integer> getDirreqV2TunneledDl() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1022,6 +1049,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String dirreqV3TunneledDl; + @Override public SortedMap<String, Integer> getDirreqV3TunneledDl() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1029,30 +1057,35 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private BandwidthHistory dirreqReadHistory; + @Override public BandwidthHistory getDirreqReadHistory() { return this.dirreqReadHistory; }
private BandwidthHistory dirreqWriteHistory; + @Override public BandwidthHistory getDirreqWriteHistory() { return this.dirreqWriteHistory; }
private long entryStatsEndMillis = -1L; + @Override public long getEntryStatsEndMillis() { return this.entryStatsEndMillis; }
private long entryStatsIntervalLength = -1L; + @Override public long getEntryStatsIntervalLength() { return this.entryStatsIntervalLength; }
private String entryIps; + @Override public SortedMap<String, Integer> getEntryIps() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1060,120 +1093,139 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private long cellStatsEndMillis = -1L; + @Override public long getCellStatsEndMillis() { return this.cellStatsEndMillis; }
private long cellStatsIntervalLength = -1L; + @Override public long getCellStatsIntervalLength() { return this.cellStatsIntervalLength; }
private Integer[] cellProcessedCells; + @Override public List<Integer> getCellProcessedCells() { - return this.cellProcessedCells == null ? null : - Arrays.asList(this.cellProcessedCells); + return this.cellProcessedCells == null ? null + : Arrays.asList(this.cellProcessedCells); }
private Double[] cellQueuedCells; + @Override public List<Double> getCellQueuedCells() { - return this.cellQueuedCells == null ? null : - Arrays.asList(this.cellQueuedCells); + return this.cellQueuedCells == null ? null + : Arrays.asList(this.cellQueuedCells); }
private Integer[] cellTimeInQueue; + @Override public List<Integer> getCellTimeInQueue() { - return this.cellTimeInQueue == null ? null : - Arrays.asList(this.cellTimeInQueue); + return this.cellTimeInQueue == null ? null + : Arrays.asList(this.cellTimeInQueue); }
private int cellCircuitsPerDecile = -1; + @Override public int getCellCircuitsPerDecile() { return this.cellCircuitsPerDecile; }
private long connBiDirectStatsEndMillis = -1L; + @Override public long getConnBiDirectStatsEndMillis() { return this.connBiDirectStatsEndMillis; }
private long connBiDirectStatsIntervalLength = -1L; + @Override public long getConnBiDirectStatsIntervalLength() { return this.connBiDirectStatsIntervalLength; }
private int connBiDirectBelow = -1; + @Override public int getConnBiDirectBelow() { return this.connBiDirectBelow; }
private int connBiDirectRead = -1; + @Override public int getConnBiDirectRead() { return this.connBiDirectRead; }
private int connBiDirectWrite = -1; + @Override public int getConnBiDirectWrite() { return this.connBiDirectWrite; }
private int connBiDirectBoth = -1; + @Override public int getConnBiDirectBoth() { return this.connBiDirectBoth; }
private long exitStatsEndMillis = -1L; + @Override public long getExitStatsEndMillis() { return this.exitStatsEndMillis; }
private long exitStatsIntervalLength = -1L; + @Override public long getExitStatsIntervalLength() { return this.exitStatsIntervalLength; }
private SortedMap<String, Long> exitKibibytesWritten; + @Override public SortedMap<String, Long> getExitKibibytesWritten() { - return this.exitKibibytesWritten == null ? null : - new TreeMap<>(this.exitKibibytesWritten); + return this.exitKibibytesWritten == null ? null + : new TreeMap<>(this.exitKibibytesWritten); }
private SortedMap<String, Long> exitKibibytesRead; + @Override public SortedMap<String, Long> getExitKibibytesRead() { - return this.exitKibibytesRead == null ? null : - new TreeMap<>(this.exitKibibytesRead); + return this.exitKibibytesRead == null ? null + : new TreeMap<>(this.exitKibibytesRead); }
private SortedMap<String, Long> exitStreamsOpened; + @Override public SortedMap<String, Long> getExitStreamsOpened() { - return this.exitStreamsOpened == null ? null : - new TreeMap<>(this.exitStreamsOpened); + return this.exitStreamsOpened == null ? null + : new TreeMap<>(this.exitStreamsOpened); }
private long geoipStartTimeMillis = -1L; + @Override public long getGeoipStartTimeMillis() { return this.geoipStartTimeMillis; }
private String geoipClientOrigins; + @Override public SortedMap<String, Integer> getGeoipClientOrigins() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1181,18 +1233,21 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private long bridgeStatsEndMillis = -1L; + @Override public long getBridgeStatsEndMillis() { return this.bridgeStatsEndMillis; }
private long bridgeStatsIntervalLength = -1L; + @Override public long getBridgeStatsIntervalLength() { return this.bridgeStatsIntervalLength; }
private String bridgeIps; + @Override public SortedMap<String, Integer> getBridgeIps() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1200,6 +1255,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String bridgeIpVersions; + @Override public SortedMap<String, Integer> getBridgeIpVersions() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1207,6 +1263,7 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private String bridgeIpTransports; + @Override public SortedMap<String, Integer> getBridgeIpTransports() { return ParseHelper.convertCommaSeparatedKeyIntegerValueList( @@ -1214,68 +1271,79 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl }
private List<String> transports = new ArrayList<>(); + @Override public List<String> getTransports() { return new ArrayList<>(this.transports); }
private long hidservStatsEndMillis = -1L; + @Override public long getHidservStatsEndMillis() { return this.hidservStatsEndMillis; }
private long hidservStatsIntervalLength = -1L; + @Override public long getHidservStatsIntervalLength() { return this.hidservStatsIntervalLength; }
private Double hidservRendRelayedCells; + @Override public Double getHidservRendRelayedCells() { return this.hidservRendRelayedCells; }
private Map<String, Double> hidservRendRelayedCellsParameters; + @Override public Map<String, Double> getHidservRendRelayedCellsParameters() { - return this.hidservRendRelayedCellsParameters == null ? null : - new HashMap<>(this.hidservRendRelayedCellsParameters); + return this.hidservRendRelayedCellsParameters == null ? null + : new HashMap<>(this.hidservRendRelayedCellsParameters); }
private Double hidservDirOnionsSeen; + @Override public Double getHidservDirOnionsSeen() { return this.hidservDirOnionsSeen; }
private Map<String, Double> hidservDirOnionsSeenParameters; + @Override public Map<String, Double> getHidservDirOnionsSeenParameters() { - return this.hidservDirOnionsSeenParameters == null ? null : - new HashMap<>(this.hidservDirOnionsSeenParameters); + return this.hidservDirOnionsSeenParameters == null ? null + : new HashMap<>(this.hidservDirOnionsSeenParameters); }
private String routerSignature; + @Override public String getRouterSignature() { return this.routerSignature; }
private String identityEd25519; + @Override public String getIdentityEd25519() { return this.identityEd25519; }
private String masterKeyEd25519; + @Override public String getMasterKeyEd25519() { return this.masterKeyEd25519; }
private String routerSignatureEd25519; + @Override public String getRouterSignatureEd25519() { return this.routerSignatureEd25519; diff --git a/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java index 4931c31..7f07da9 100644 --- a/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java @@ -1,7 +1,11 @@ /* Copyright 2014--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.Microdescriptor; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,9 +18,6 @@ import java.util.Set;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.Microdescriptor; - /* Contains a microdescriptor. */ public class MicrodescriptorImpl extends DescriptorImpl implements Microdescriptor { @@ -55,8 +56,8 @@ public class MicrodescriptorImpl extends DescriptorImpl }
private void parseDescriptorBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.rawDescriptorBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.rawDescriptorBytes)) + .useDelimiter("\n"); String nextCrypto = ""; StringBuilder crypto = null; while (s.hasNext()) { @@ -67,59 +68,59 @@ public class MicrodescriptorImpl extends DescriptorImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "onion-key": - this.parseOnionKeyLine(line, parts); - nextCrypto = "onion-key"; - break; - case "ntor-onion-key": - this.parseNtorOnionKeyLine(line, parts); - break; - case "a": - this.parseALine(line, parts); - break; - case "family": - this.parseFamilyLine(line, parts); - break; - case "p": - this.parsePLine(line, parts); - break; - case "p6": - this.parseP6Line(line, parts); - break; - case "id": - this.parseIdLine(line, parts); - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - if (nextCrypto.equals("onion-key")) { - this.onionKey = cryptoString; - } else { - throw new DescriptorParseException("Unrecognized crypto " - + "block in microdescriptor."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "onion-key": + this.parseOnionKeyLine(line, parts); + nextCrypto = "onion-key"; + break; + case "ntor-onion-key": + this.parseNtorOnionKeyLine(line, parts); + break; + case "a": + this.parseALine(line, parts); + break; + case "family": + this.parseFamilyLine(line, parts); + break; + case "p": + this.parsePLine(line, parts); + break; + case "p6": + this.parseP6Line(line, parts); + break; + case "id": + this.parseIdLine(line, parts); + break; + case "-----BEGIN": + crypto = new StringBuilder(); crypto.append(line).append("\n"); - } else { - ParseHelper.parseKeyword(line, parts[0]); - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in microdescriptor."); + break; + case "-----END": + crypto.append(line).append("\n"); + String cryptoString = crypto.toString(); + crypto = null; + if (nextCrypto.equals("onion-key")) { + this.onionKey = cryptoString; } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + throw new DescriptorParseException("Unrecognized crypto " + + "block in microdescriptor."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); + } else { + ParseHelper.parseKeyword(line, parts[0]); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in microdescriptor."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -195,18 +196,18 @@ public class MicrodescriptorImpl extends DescriptorImpl isValid = false; } else { switch (parts[1]) { - case "accept": - case "reject": - String[] ports = parts[2].split(",", -1); - for (int i = 0; i < ports.length; i++) { - if (ports[i].length() < 1) { - isValid = false; - break; + case "accept": + case "reject": + String[] ports = parts[2].split(",", -1); + for (int i = 0; i < ports.length; i++) { + if (ports[i].length() < 1) { + isValid = false; + break; + } } - } - break; - default: - isValid = false; + break; + default: + isValid = false; } } if (!isValid) { @@ -219,7 +220,7 @@ public class MicrodescriptorImpl extends DescriptorImpl if (parts.length != 3) { throw new DescriptorParseException("Illegal line '" + line + "'."); } else { - switch (parts[1]) { + switch (parts[1]) { case "ed25519": ParseHelper.parseThirtyTwoByteBase64String(line, parts[2]); this.ed25519Identity = parts[2]; @@ -229,8 +230,9 @@ public class MicrodescriptorImpl extends DescriptorImpl this.rsa1024Identity = parts[2]; break; default: - throw new DescriptorParseException("Illegal line '" + line + "'."); - } + throw new DescriptorParseException("Illegal line '" + line + + "'."); + } } }
@@ -245,8 +247,8 @@ public class MicrodescriptorImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, end - start); this.microdescriptorDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-256").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-256").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -260,66 +262,78 @@ public class MicrodescriptorImpl extends DescriptorImpl }
private String microdescriptorDigest; + @Override public String getMicrodescriptorDigest() { return this.microdescriptorDigest; }
private String onionKey; + @Override public String getOnionKey() { return this.onionKey; }
private String ntorOnionKey; + @Override public String getNtorOnionKey() { return this.ntorOnionKey; }
private List<String> orAddresses = new ArrayList<>(); + @Override public List<String> getOrAddresses() { return new ArrayList<>(this.orAddresses); }
private String[] familyEntries; + @Override public List<String> getFamilyEntries() { - return this.familyEntries == null ? null : - Arrays.asList(this.familyEntries); + return this.familyEntries == null ? null + : Arrays.asList(this.familyEntries); } + private String defaultPolicy; + @Override public String getDefaultPolicy() { return this.defaultPolicy; }
private String portList; + @Override public String getPortList() { return this.portList; }
private String ipv6DefaultPolicy; + @Override public String getIpv6DefaultPolicy() { return this.ipv6DefaultPolicy; }
private String ipv6PortList; + @Override public String getIpv6PortList() { return this.ipv6PortList; }
private String rsa1024Identity; + @Override public String getRsa1024Identity() { return this.rsa1024Identity; }
private String ed25519Identity; + @Override public String getEd25519Identity() { return this.ed25519Identity; diff --git a/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java b/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java index b73d211..6ffaf62 100644 --- a/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java @@ -1,8 +1,11 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.NetworkStatusEntry; + import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; @@ -15,11 +18,10 @@ import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeSet;
-import org.torproject.descriptor.NetworkStatusEntry; - public class NetworkStatusEntryImpl implements NetworkStatusEntry {
private byte[] statusEntryBytes; + @Override public byte[] getStatusEntryBytes() { return this.statusEntryBytes; @@ -28,7 +30,9 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { private boolean microdescConsensus;
private boolean failUnrecognizedDescriptorLines; + private List<String> unrecognizedLines; + protected List<String> getAndClearUnrecognizedLines() { List<String> lines = this.unrecognizedLines; this.unrecognizedLines = null; @@ -48,6 +52,7 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { }
private SortedSet<String> atMostOnceKeywords; + private void initializeKeywords() { this.atMostOnceKeywords = new TreeSet<>(); this.atMostOnceKeywords.add("s"); @@ -66,8 +71,8 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { }
private void parseStatusEntryBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.statusEntryBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.statusEntryBytes)) + .useDelimiter("\n"); String line = null; if (!s.hasNext() || !(line = s.next()).startsWith("r ")) { throw new DescriptorParseException("Status entry must start with " @@ -77,49 +82,49 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { this.parseRLine(line, rLineParts); while (s.hasNext()) { line = s.next(); - String[] parts = !line.startsWith("opt ") ? line.split("[ \t]+") : - line.substring("opt ".length()).split("[ \t]+"); + String[] parts = !line.startsWith("opt ") ? line.split("[ \t]+") + : line.substring("opt ".length()).split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "a": - this.parseALine(line, parts); - break; - case "s": - this.parseSLine(line, parts); - break; - case "v": - this.parseVLine(line, parts); - break; - case "w": - this.parseWLine(line, parts); - break; - case "p": - this.parsePLine(line, parts); - break; - case "m": - this.parseMLine(line, parts); - break; - case "id": - this.parseIdLine(line, parts); - break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in status entry."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "a": + this.parseALine(line, parts); + break; + case "s": + this.parseSLine(line, parts); + break; + case "v": + this.parseVLine(line, parts); + break; + case "w": + this.parseWLine(line, parts); + break; + case "p": + this.parsePLine(line, parts); + break; + case "m": + this.parseMLine(line, parts); + break; + case "id": + this.parseIdLine(line, parts); + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in status entry."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } }
private void parseRLine(String line, String[] parts) throws DescriptorParseException { - if ((!this.microdescConsensus && parts.length != 9) || - (this.microdescConsensus && parts.length != 8)) { + if ((!this.microdescConsensus && parts.length != 9) + || (this.microdescConsensus && parts.length != 8)) { throw new DescriptorParseException("r line '" + line + "' has " + "fewer space-separated elements than expected."); } @@ -154,6 +159,7 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { }
private static Map<String, Integer> flagIndexes = new HashMap<>(); + private static Map<Integer, String> flagStrings = new HashMap<>();
private void parseSLine(String line, String[] parts) @@ -215,22 +221,22 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { if (parts.length != 3) { isValid = false; } else { - switch (parts[1]) { - case "accept": - case "reject": - this.defaultPolicy = parts[1]; - this.portList = parts[2]; - String[] ports = parts[2].split(",", -1); - for (int i = 0; i < ports.length; i++) { - if (ports[i].length() < 1) { - isValid = false; - break; - } + switch (parts[1]) { + case "accept": + case "reject": + this.defaultPolicy = parts[1]; + this.portList = parts[2]; + String[] ports = parts[2].split(",", -1); + for (int i = 0; i < ports.length; i++) { + if (ports[i].length() < 1) { + isValid = false; + break; } - break; - default: - isValid = false; - } + } + break; + default: + isValid = false; + } } if (!isValid) { throw new DescriptorParseException("Illegal line '" + line + "'."); @@ -270,61 +276,71 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private String descriptor; + @Override public String getDescriptor() { return this.descriptor; }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private String address; + @Override public String getAddress() { return this.address; }
private int orPort; + @Override public int getOrPort() { return this.orPort; }
private int dirPort; + @Override public int getDirPort() { return this.dirPort; }
private Set<String> microdescriptorDigests; + @Override public Set<String> getMicrodescriptorDigests() { - return this.microdescriptorDigests == null ? null : - new HashSet<>(this.microdescriptorDigests); + return this.microdescriptorDigests == null ? null + : new HashSet<>(this.microdescriptorDigests); }
private List<String> orAddresses = new ArrayList<>(); + @Override public List<String> getOrAddresses() { return new ArrayList<>(this.orAddresses); }
private BitSet flags; + @Override public SortedSet<String> getFlags() { SortedSet<String> result = new TreeSet<>(); @@ -338,42 +354,49 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry { }
private String version; + @Override public String getVersion() { return this.version; }
private long bandwidth = -1L; + @Override public long getBandwidth() { return this.bandwidth; }
private long measured = -1L; + @Override public long getMeasured() { return this.measured; }
private boolean unmeasured = false; + @Override public boolean getUnmeasured() { return this.unmeasured; }
private String defaultPolicy; + @Override public String getDefaultPolicy() { return this.defaultPolicy; }
private String portList; + @Override public String getPortList() { return this.portList; }
private String masterKeyEd25519; + @Override public String getMasterKeyEd25519() { return this.masterKeyEd25519; diff --git a/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java index 5fa22c7..4c99aca 100644 --- a/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java @@ -1,17 +1,18 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.DirSourceEntry; +import org.torproject.descriptor.DirectorySignature; +import org.torproject.descriptor.NetworkStatusEntry; + import java.util.ArrayList; import java.util.List; import java.util.SortedMap; import java.util.TreeMap;
-import org.torproject.descriptor.DirSourceEntry; -import org.torproject.descriptor.DirectorySignature; -import org.torproject.descriptor.NetworkStatusEntry; - /* Parse the common parts of v3 consensuses, v3 votes, v3 microdesc * consensuses, v2 statuses, and sanitized bridge network statuses and * delegate the specific parts to the subclasses. */ @@ -34,8 +35,8 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { } String descriptorString = new String(rawDescriptorBytes); int startIndex = 0; - int firstDirSourceIndex = !containsDirSourceEntries ? -1 : - this.findFirstIndexOfKeyword(descriptorString, "dir-source"); + int firstDirSourceIndex = !containsDirSourceEntries ? -1 + : this.findFirstIndexOfKeyword(descriptorString, "dir-source"); int firstRIndex = this.findFirstIndexOfKeyword(descriptorString, "r"); int directoryFooterIndex = this.findFirstIndexOfKeyword( descriptorString, "directory-footer"); @@ -164,8 +165,8 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { dirSourceBytes, this.failUnrecognizedDescriptorLines); this.dirSourceEntries.put(dirSourceEntry.getIdentity(), dirSourceEntry); - List<String> unrecognizedDirSourceLines = dirSourceEntry. - getAndClearUnrecognizedLines(); + List<String> unrecognizedDirSourceLines = dirSourceEntry + .getAndClearUnrecognizedLines(); if (unrecognizedDirSourceLines != null) { if (this.unrecognizedLines == null) { this.unrecognizedLines = new ArrayList<>(); @@ -191,8 +192,8 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { } break; default: - throw new DescriptorParseException("Illegal versions line '" + line - + "'."); + throw new DescriptorParseException("Illegal versions line '" + + line + "'."); } return result; } @@ -202,8 +203,8 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { NetworkStatusEntryImpl statusEntry = new NetworkStatusEntryImpl( statusEntryBytes, false, this.failUnrecognizedDescriptorLines); this.statusEntries.put(statusEntry.getFingerprint(), statusEntry); - List<String> unrecognizedStatusEntryLines = statusEntry. - getAndClearUnrecognizedLines(); + List<String> unrecognizedStatusEntryLines = statusEntry + .getAndClearUnrecognizedLines(); if (unrecognizedStatusEntryLines != null) { if (this.unrecognizedLines == null) { this.unrecognizedLines = new ArrayList<>(); @@ -223,8 +224,8 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { DirectorySignatureImpl signature = new DirectorySignatureImpl( directorySignatureBytes, failUnrecognizedDescriptorLines); this.signatures.add(signature); - List<String> unrecognizedStatusEntryLines = signature. - getAndClearUnrecognizedLines(); + List<String> unrecognizedStatusEntryLines = signature + .getAndClearUnrecognizedLines(); if (unrecognizedStatusEntryLines != null) { if (this.unrecognizedLines == null) { this.unrecognizedLines = new ArrayList<>(); @@ -235,27 +236,33 @@ public abstract class NetworkStatusImpl extends DescriptorImpl {
protected SortedMap<String, DirSourceEntry> dirSourceEntries = new TreeMap<>(); + public SortedMap<String, DirSourceEntry> getDirSourceEntries() { return new TreeMap<>(this.dirSourceEntries); }
protected SortedMap<String, NetworkStatusEntry> statusEntries = new TreeMap<>(); + public SortedMap<String, NetworkStatusEntry> getStatusEntries() { return new TreeMap<>(this.statusEntries); } + public boolean containsStatusEntry(String fingerprint) { return this.statusEntries.containsKey(fingerprint); } + public NetworkStatusEntry getStatusEntry(String fingerprint) { return this.statusEntries.get(fingerprint); }
protected List<DirectorySignature> signatures; + public List<DirectorySignature> getSignatures() { return this.signatures == null ? null : new ArrayList<>(this.signatures); } + public SortedMap<String, DirectorySignature> getDirectorySignatures() { SortedMap<String, DirectorySignature> directorySignatures = null; if (this.signatures != null) { diff --git a/src/main/java/org/torproject/descriptor/impl/ParseHelper.java b/src/main/java/org/torproject/descriptor/impl/ParseHelper.java index 82c0813..a40a351 100644 --- a/src/main/java/org/torproject/descriptor/impl/ParseHelper.java +++ b/src/main/java/org/torproject/descriptor/impl/ParseHelper.java @@ -1,7 +1,10 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; + import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -16,12 +19,11 @@ import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; - public class ParseHelper {
private static Pattern keywordPattern = Pattern.compile("^[A-Za-z0-9-]+$"); + protected static String parseKeyword(String line, String keyword) throws DescriptorParseException { if (!keywordPattern.matcher(keyword).matches()) { @@ -33,6 +35,7 @@ public class ParseHelper {
private static Pattern ipv4Pattern = Pattern.compile("^[0-9\.]{7,15}$"); + protected static String parseIpv4Address(String line, String address) throws DescriptorParseException { boolean isValid = true; @@ -141,20 +144,25 @@ public class ParseHelper { }
private static ThreadLocal<Map<String, DateFormat>> dateFormats = - new ThreadLocal<Map<String, DateFormat>> () { + new ThreadLocal<Map<String, DateFormat>>() { + public Map<String, DateFormat> get() { return super.get(); } + protected Map<String, DateFormat> initialValue() { return new HashMap<>(); } + public void remove() { super.remove(); } + public void set(Map<String, DateFormat> value) { super.set(value); } }; + static DateFormat getDateFormat(String format) { Map<String, DateFormat> threadDateFormats = dateFormats.get(); if (!threadDateFormats.containsKey(format)) { @@ -218,11 +226,13 @@ public class ParseHelper { }
private static Pattern hexPattern = Pattern.compile("^[0-9a-fA-F]*$"); + private static String parseHexString(String line, String hexString, int expectedLength) throws DescriptorParseException { - if (!hexPattern.matcher(hexString).matches() || - hexString.length() % 2 != 0 || - (expectedLength >= 0 && hexString.length() != expectedLength)) { + if (!hexPattern.matcher(hexString).matches() + || hexString.length() % 2 != 0 + || (expectedLength >= 0 + && hexString.length() != expectedLength)) { throw new DescriptorParseException("Illegal hex string in line '" + line + "'."); } @@ -265,6 +275,7 @@ public class ParseHelper {
private static Pattern nicknamePattern = Pattern.compile("^[0-9a-zA-Z]{1,19}$"); + protected static String parseNickname(String line, String nickname) throws DescriptorParseException { if (!nicknamePattern.matcher(nickname).matches()) { @@ -277,17 +288,19 @@ public class ParseHelper { protected static boolean parseBoolean(String b, String line) throws DescriptorParseException { switch (b) { - case "1": - return true; - case "0": - return false; - default: - throw new DescriptorParseException("Illegal line '" + line + "'."); + case "1": + return true; + case "0": + return false; + default: + throw new DescriptorParseException("Illegal line '" + line + + "'."); } }
private static Pattern twentyByteBase64Pattern = Pattern.compile("^[0-9a-zA-Z+/]{27}$"); + protected static String parseTwentyByteBase64String(String line, String base64String) throws DescriptorParseException { if (!twentyByteBase64Pattern.matcher(base64String).matches()) { @@ -296,12 +309,13 @@ public class ParseHelper { + "20-byte value."); } return DatatypeConverter.printHexBinary( - DatatypeConverter.parseBase64Binary(base64String + "=")). - toUpperCase(); + DatatypeConverter.parseBase64Binary(base64String + "=")) + .toUpperCase(); }
private static Pattern thirtyTwoByteBase64Pattern = Pattern.compile("^[0-9a-zA-Z+/]{43}$"); + protected static String parseThirtyTwoByteBase64String(String line, String base64String) throws DescriptorParseException { if (!thirtyTwoByteBase64Pattern.matcher(base64String).matches()) { @@ -310,12 +324,13 @@ public class ParseHelper { + "32-byte value."); } return DatatypeConverter.printHexBinary( - DatatypeConverter.parseBase64Binary(base64String + "=")). - toUpperCase(); + DatatypeConverter.parseBase64Binary(base64String + "=")) + .toUpperCase(); }
private static Map<Integer, Pattern> commaSeparatedKeyValueListPatterns = new HashMap<>(); + protected static String parseCommaSeparatedKeyIntegerValueList( String line, String[] partsNoOpt, int index, int keyLength) throws DescriptorParseException { @@ -382,8 +397,8 @@ public class ParseHelper { String[] keyAndValue = listElement.split("="); String key = null; long value = -1; - if (keyAndValue.length == 2 && (keyLength == 0 || - keyAndValue[0].length() == keyLength)) { + if (keyAndValue.length == 2 && (keyLength == 0 + || keyAndValue[0].length() == keyLength)) { try { value = Long.parseLong(keyAndValue[1]); key = keyAndValue[0]; @@ -496,8 +511,8 @@ public class ParseHelper { String identityEd25519CryptoBlock) throws DescriptorParseException { String identityEd25519CryptoBlockNoNewlines = identityEd25519CryptoBlock.replaceAll("\n", ""); - String beginEd25519CertLine = "-----BEGIN ED25519 CERT-----", - endEd25519CertLine = "-----END ED25519 CERT-----"; + String beginEd25519CertLine = "-----BEGIN ED25519 CERT-----"; + String endEd25519CertLine = "-----END ED25519 CERT-----"; if (!identityEd25519CryptoBlockNoNewlines.startsWith( beginEd25519CertLine)) { throw new DescriptorParseException("Illegal start of " @@ -510,8 +525,8 @@ public class ParseHelper { + "identity-ed25519 crypto block '" + identityEd25519CryptoBlock + "'."); } - String identityEd25519Base64 = identityEd25519CryptoBlockNoNewlines. - substring(beginEd25519CertLine.length(), + String identityEd25519Base64 = identityEd25519CryptoBlockNoNewlines + .substring(beginEd25519CertLine.length(), identityEd25519CryptoBlock.length() - endEd25519CertLine.length()).replaceAll("=", ""); byte[] identityEd25519 = DatatypeConverter.parseBase64Binary( @@ -551,8 +566,8 @@ public class ParseHelper { byte[] masterKeyEd25519 = new byte[32]; System.arraycopy(identityEd25519, extensionStart + 4, masterKeyEd25519, 0, masterKeyEd25519.length); - String masterKeyEd25519Base64 = DatatypeConverter. - printBase64Binary(masterKeyEd25519).replaceAll("=", ""); + String masterKeyEd25519Base64 = DatatypeConverter + .printBase64Binary(masterKeyEd25519).replaceAll("=", ""); String masterKeyEd25519Base64NoTrailingEqualSigns = masterKeyEd25519Base64.replaceAll("=", ""); return masterKeyEd25519Base64NoTrailingEqualSigns; diff --git a/src/main/java/org/torproject/descriptor/impl/RelayDirectoryImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayDirectoryImpl.java index 1ff15cb..1df9c55 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayDirectoryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayDirectoryImpl.java @@ -1,7 +1,13 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.RelayDirectory; +import org.torproject.descriptor.RouterStatusEntry; +import org.torproject.descriptor.ServerDescriptor; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,11 +20,6 @@ import java.util.Set;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.RelayDirectory; -import org.torproject.descriptor.RouterStatusEntry; -import org.torproject.descriptor.ServerDescriptor; - /* TODO Write unit tests. */
public class RelayDirectoryImpl extends DescriptorImpl @@ -73,8 +74,8 @@ public class RelayDirectoryImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.directoryDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -173,8 +174,8 @@ public class RelayDirectoryImpl extends DescriptorImpl to += 1; } int toNoNewline = to; - while (toNoNewline > from && - descriptorString.charAt(toNoNewline - 1) == '\n') { + while (toNoNewline > from + && descriptorString.charAt(toNoNewline - 1) == '\n') { toNoNewline--; } byte[] part = new byte[toNoNewline - from]; @@ -189,99 +190,102 @@ public class RelayDirectoryImpl extends DescriptorImpl private void parseHeader(byte[] headerBytes) throws DescriptorParseException { Scanner s = new Scanner(new String(headerBytes)).useDelimiter("\n"); - String publishedLine = null, nextCrypto = "", - runningRoutersLine = null, routerStatusLine = null; + String publishedLine = null; + String nextCrypto = ""; + String runningRoutersLine = null; + String routerStatusLine = null; StringBuilder crypto = null; while (s.hasNext()) { String line = s.next(); if (line.isEmpty() || line.startsWith("@")) { continue; } - String lineNoOpt = line.startsWith("opt ") ? - line.substring("opt ".length()) : line; + String lineNoOpt = line.startsWith("opt ") + ? line.substring("opt ".length()) : line; String[] partsNoOpt = lineNoOpt.split("[ \t]+"); String keyword = partsNoOpt[0]; switch (keyword) { - case "signed-directory": - this.parseSignedDirectoryLine(line, lineNoOpt, partsNoOpt); - break; - case "published": - if (publishedLine != null) { - throw new DescriptorParseException("Keyword 'published' is " - + "contained more than once, but must be contained exactly " - + "once."); - } else { - publishedLine = line; - } - break; - case "dir-signing-key": - this.parseDirSigningKeyLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "dir-signing-key"; - break; - case "recommended-software": - this.parseRecommendedSoftwareLine(line, lineNoOpt, partsNoOpt); - break; - case "running-routers": - runningRoutersLine = line; - break; - case "router-status": - routerStatusLine = line; - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - if (nextCrypto.equals("dir-signing-key") && - this.dirSigningKey == null) { - this.dirSigningKey = cryptoString; - } else { - throw new DescriptorParseException("Unrecognized crypto " - + "block in v1 directory."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "signed-directory": + this.parseSignedDirectoryLine(line, lineNoOpt, partsNoOpt); + break; + case "published": + if (publishedLine != null) { + throw new DescriptorParseException("Keyword 'published' is " + + "contained more than once, but must be contained " + + "exactly once."); + } else { + publishedLine = line; + } + break; + case "dir-signing-key": + this.parseDirSigningKeyLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "dir-signing-key"; + break; + case "recommended-software": + this.parseRecommendedSoftwareLine(line, lineNoOpt, partsNoOpt); + break; + case "running-routers": + runningRoutersLine = line; + break; + case "router-status": + routerStatusLine = line; + break; + case "-----BEGIN": + crypto = new StringBuilder(); crypto.append(line).append("\n"); - } else { - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in v1 directory."); + break; + case "-----END": + crypto.append(line).append("\n"); + String cryptoString = crypto.toString(); + crypto = null; + if (nextCrypto.equals("dir-signing-key") + && this.dirSigningKey == null) { + this.dirSigningKey = cryptoString; } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + throw new DescriptorParseException("Unrecognized crypto " + + "block in v1 directory."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); + } else { + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in v1 directory."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } if (publishedLine == null) { throw new DescriptorParseException("Keyword 'published' is " + "contained 0 times, but must be contained exactly once."); } else { - String publishedLineNoOpt = publishedLine.startsWith("opt ") ? - publishedLine.substring("opt ".length()) : publishedLine; + String publishedLineNoOpt = publishedLine.startsWith("opt ") + ? publishedLine.substring("opt ".length()) : publishedLine; String[] publishedPartsNoOpt = publishedLineNoOpt.split("[ \t]+"); this.parsePublishedLine(publishedLine, publishedLineNoOpt, publishedPartsNoOpt); } if (routerStatusLine != null) { - String routerStatusLineNoOpt = routerStatusLine.startsWith("opt ") ? - routerStatusLine.substring("opt ".length()) : routerStatusLine; + String routerStatusLineNoOpt = routerStatusLine.startsWith("opt ") + ? routerStatusLine.substring("opt ".length()) + : routerStatusLine; String[] routerStatusPartsNoOpt = routerStatusLineNoOpt.split("[ \t]+"); this.parseRouterStatusLine(routerStatusLine, routerStatusLineNoOpt, routerStatusPartsNoOpt); } else if (runningRoutersLine != null) { String runningRoutersLineNoOpt = - runningRoutersLine.startsWith("opt ") ? - runningRoutersLine.substring("opt ".length()) : - runningRoutersLine; + runningRoutersLine.startsWith("opt ") + ? runningRoutersLine.substring("opt ".length()) + : runningRoutersLine; String[] runningRoutersPartsNoOpt = runningRoutersLineNoOpt.split("[ \t]+"); this.parseRunningRoutersLine(runningRoutersLine, @@ -305,49 +309,49 @@ public class RelayDirectoryImpl extends DescriptorImpl
private void parseDirectorySignature(byte[] directorySignatureBytes) throws DescriptorParseException { - Scanner s = new Scanner(new String(directorySignatureBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(directorySignatureBytes)) + .useDelimiter("\n"); String nextCrypto = ""; StringBuilder crypto = null; while (s.hasNext()) { String line = s.next(); - String lineNoOpt = line.startsWith("opt ") ? - line.substring("opt ".length()) : line; + String lineNoOpt = line.startsWith("opt ") + ? line.substring("opt ".length()) : line; String[] partsNoOpt = lineNoOpt.split("[ \t]+"); String keyword = partsNoOpt[0]; switch (keyword) { - case "directory-signature": - this.parseDirectorySignatureLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "directory-signature"; - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - if (nextCrypto.equals("directory-signature")) { - this.directorySignature = cryptoString; - } else { - throw new DescriptorParseException("Unrecognized crypto " - + "block in v2 network status."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "directory-signature": + this.parseDirectorySignatureLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "directory-signature"; + break; + case "-----BEGIN": + crypto = new StringBuilder(); crypto.append(line).append("\n"); - } else if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in v2 network status."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + break; + case "-----END": + crypto.append(line).append("\n"); + String cryptoString = crypto.toString(); + crypto = null; + if (nextCrypto.equals("directory-signature")) { + this.directorySignature = cryptoString; + } else { + throw new DescriptorParseException("Unrecognized crypto " + + "block in v2 network status."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); + } else if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in v2 network status."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } } @@ -419,7 +423,8 @@ public class RelayDirectoryImpl extends DescriptorImpl part = part.substring(1); } boolean isVerified; - String fingerprint = null, nickname = null; + String fingerprint = null; + String nickname = null; if (part.startsWith("$")) { isVerified = false; fingerprint = ParseHelper.parseTwentyByteHexString(debugLine, @@ -442,7 +447,8 @@ public class RelayDirectoryImpl extends DescriptorImpl if (part.contains("=")) { String[] partParts = part.split("="); if (partParts.length == 2) { - boolean isVerified = true, isLive; + boolean isVerified = true; + boolean isLive; String nickname; if (partParts[0].startsWith("!")) { isLive = false; @@ -458,8 +464,10 @@ public class RelayDirectoryImpl extends DescriptorImpl isVerified); } } else { - boolean isVerified = false, isLive; - String nickname = null, fingerprint; + boolean isVerified = false; + boolean isLive; + String nickname = null; + String fingerprint; if (part.startsWith("!")) { isLive = false; fingerprint = ParseHelper.parseTwentyByteHexString( @@ -489,37 +497,43 @@ public class RelayDirectoryImpl extends DescriptorImpl }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private String dirSigningKey; + @Override public String getDirSigningKey() { return this.dirSigningKey; }
private List<String> recommendedSoftware; + @Override public List<String> getRecommendedSoftware() { - return this.recommendedSoftware == null ? null : - new ArrayList<>(this.recommendedSoftware); + return this.recommendedSoftware == null ? null + : new ArrayList<>(this.recommendedSoftware); }
private String directorySignature; + @Override public String getDirectorySignature() { return this.directorySignature; }
private List<RouterStatusEntry> statusEntries = new ArrayList<>(); + @Override public List<RouterStatusEntry> getRouterStatusEntries() { return new ArrayList<>(this.statusEntries); }
private List<ServerDescriptor> serverDescriptors = new ArrayList<>(); + @Override public List<ServerDescriptor> getServerDescriptors() { return new ArrayList<>(this.serverDescriptors); @@ -527,18 +541,21 @@ public class RelayDirectoryImpl extends DescriptorImpl
private List<Exception> serverDescriptorParseExceptions = new ArrayList<>(); + @Override public List<Exception> getServerDescriptorParseExceptions() { return new ArrayList<>(this.serverDescriptorParseExceptions); }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String directoryDigest; + @Override public String getDirectoryDigest() { return this.directoryDigest; diff --git a/src/main/java/org/torproject/descriptor/impl/RelayExtraInfoDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayExtraInfoDescriptorImpl.java index 73d4dfa..6fe3fc6 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayExtraInfoDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayExtraInfoDescriptorImpl.java @@ -1,14 +1,15 @@ /* Copyright 2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import java.util.ArrayList; -import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.ExtraInfoDescriptor; import org.torproject.descriptor.RelayExtraInfoDescriptor;
+import java.util.ArrayList; +import java.util.List; + public class RelayExtraInfoDescriptorImpl extends ExtraInfoDescriptorImpl implements RelayExtraInfoDescriptor {
diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java index fe045c1..eae3ace 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java @@ -1,7 +1,11 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.RelayNetworkStatusConsensus; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -18,9 +22,6 @@ import java.util.TreeSet;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.RelayNetworkStatusConsensus; - /* Contains a network status consensus or microdesc consensus. */ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl implements RelayNetworkStatusConsensus { @@ -74,8 +75,8 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.consensusDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -96,65 +97,66 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "network-status-version": - this.parseNetworkStatusVersionLine(line, parts); - break; - case "vote-status": - this.parseVoteStatusLine(line, parts); - break; - case "consensus-method": - this.parseConsensusMethodLine(line, parts); - break; - case "valid-after": - this.parseValidAfterLine(line, parts); - break; - case "fresh-until": - this.parseFreshUntilLine(line, parts); - break; - case "valid-until": - this.parseValidUntilLine(line, parts); - break; - case "voting-delay": - this.parseVotingDelayLine(line, parts); - break; - case "client-versions": - this.parseClientVersionsLine(line, parts); - break; - case "server-versions": - this.parseServerVersionsLine(line, parts); - break; - case "package": - this.parsePackageLine(line, parts); - break; - case "known-flags": - this.parseKnownFlagsLine(line, parts); - break; - case "params": - this.parseParamsLine(line, parts); - break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in consensus."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "network-status-version": + this.parseNetworkStatusVersionLine(line, parts); + break; + case "vote-status": + this.parseVoteStatusLine(line, parts); + break; + case "consensus-method": + this.parseConsensusMethodLine(line, parts); + break; + case "valid-after": + this.parseValidAfterLine(line, parts); + break; + case "fresh-until": + this.parseFreshUntilLine(line, parts); + break; + case "valid-until": + this.parseValidUntilLine(line, parts); + break; + case "voting-delay": + this.parseVotingDelayLine(line, parts); + break; + case "client-versions": + this.parseClientVersionsLine(line, parts); + break; + case "server-versions": + this.parseServerVersionsLine(line, parts); + break; + case "package": + this.parsePackageLine(line, parts); + break; + case "known-flags": + this.parseKnownFlagsLine(line, parts); + break; + case "params": + this.parseParamsLine(line, parts); + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in consensus."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } }
private boolean microdescConsensus = false; + protected void parseStatusEntry(byte[] statusEntryBytes) throws DescriptorParseException { NetworkStatusEntryImpl statusEntry = new NetworkStatusEntryImpl( statusEntryBytes, this.microdescConsensus, this.failUnrecognizedDescriptorLines); this.statusEntries.put(statusEntry.getFingerprint(), statusEntry); - List<String> unrecognizedStatusEntryLines = statusEntry. - getAndClearUnrecognizedLines(); + List<String> unrecognizedStatusEntryLines = statusEntry + .getAndClearUnrecognizedLines(); if (unrecognizedStatusEntryLines != null) { if (this.unrecognizedLines == null) { this.unrecognizedLines = new ArrayList<>(); @@ -171,21 +173,21 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "directory-footer": - break; - case "bandwidth-weights": - this.parseBandwidthWeightsLine(line, parts); - break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in consensus."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "directory-footer": + break; + case "bandwidth-weights": + this.parseBandwidthWeightsLine(line, parts); + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in consensus."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } } @@ -317,74 +319,86 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl }
private String consensusDigest; + @Override public String getConsensusDigest() { return this.consensusDigest; }
private int networkStatusVersion; + @Override public int getNetworkStatusVersion() { return this.networkStatusVersion; }
private String consensusFlavor; + @Override public String getConsensusFlavor() { return this.consensusFlavor; }
private int consensusMethod; + @Override public int getConsensusMethod() { return this.consensusMethod; }
private long validAfterMillis; + @Override public long getValidAfterMillis() { return this.validAfterMillis; }
private long freshUntilMillis; + @Override public long getFreshUntilMillis() { return this.freshUntilMillis; }
private long validUntilMillis; + @Override public long getValidUntilMillis() { return this.validUntilMillis; }
private long voteSeconds; + @Override public long getVoteSeconds() { return this.voteSeconds; }
private long distSeconds; + @Override public long getDistSeconds() { return this.distSeconds; }
private String[] recommendedClientVersions; + @Override public List<String> getRecommendedClientVersions() { - return this.recommendedClientVersions == null ? null : - Arrays.asList(this.recommendedClientVersions); + return this.recommendedClientVersions == null ? null + : Arrays.asList(this.recommendedClientVersions); }
private String[] recommendedServerVersions; + @Override public List<String> getRecommendedServerVersions() { - return this.recommendedServerVersions == null ? null : - Arrays.asList(this.recommendedServerVersions); + return this.recommendedServerVersions == null ? null + : Arrays.asList(this.recommendedServerVersions); }
private List<String> packageLines; + @Override public List<String> getPackageLines() { return this.packageLines == null ? null @@ -392,23 +406,26 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl }
private String[] knownFlags; + @Override public SortedSet<String> getKnownFlags() { return new TreeSet<>(Arrays.asList(this.knownFlags)); }
private SortedMap<String, Integer> consensusParams; + @Override public SortedMap<String, Integer> getConsensusParams() { - return this.consensusParams == null ? null: - new TreeMap<>(this.consensusParams); + return this.consensusParams == null ? null + : new TreeMap<>(this.consensusParams); }
private SortedMap<String, Integer> bandwidthWeights; + @Override public SortedMap<String, Integer> getBandwidthWeights() { - return this.bandwidthWeights == null ? null : - new TreeMap<>(this.bandwidthWeights); + return this.bandwidthWeights == null ? null + : new TreeMap<>(this.bandwidthWeights); } }
diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java index a5469db..2ea9fdf 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java @@ -1,7 +1,11 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.RelayNetworkStatus; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -16,9 +20,6 @@ import java.util.TreeSet;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.RelayNetworkStatus; - /* TODO Write unit tests. */
public class RelayNetworkStatusImpl extends NetworkStatusImpl @@ -71,8 +72,8 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.statusDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -98,61 +99,61 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "network-status-version": - this.parseNetworkStatusVersionLine(line, parts); - break; - case "dir-source": - this.parseDirSourceLine(line, parts); - break; - case "fingerprint": - this.parseFingerprintLine(line, parts); - break; - case "contact": - this.parseContactLine(line, parts); - break; - case "dir-signing-key": - this.parseDirSigningKeyLine(line, parts); - nextCrypto = "dir-signing-key"; - break; - case "client-versions": - this.parseClientVersionsLine(line, parts); - break; - case "server-versions": - this.parseServerVersionsLine(line, parts); - break; - case "published": - this.parsePublishedLine(line, parts); - break; - case "dir-options": - this.parseDirOptionsLine(line, parts); - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - if (nextCrypto.equals("dir-signing-key")) { - this.dirSigningKey = cryptoString; - } else { - throw new DescriptorParseException("Unrecognized crypto " - + "block in v2 network status."); - } - nextCrypto = ""; - default: - if (crypto != null) { + case "network-status-version": + this.parseNetworkStatusVersionLine(line, parts); + break; + case "dir-source": + this.parseDirSourceLine(line, parts); + break; + case "fingerprint": + this.parseFingerprintLine(line, parts); + break; + case "contact": + this.parseContactLine(line, parts); + break; + case "dir-signing-key": + this.parseDirSigningKeyLine(line, parts); + nextCrypto = "dir-signing-key"; + break; + case "client-versions": + this.parseClientVersionsLine(line, parts); + break; + case "server-versions": + this.parseServerVersionsLine(line, parts); + break; + case "published": + this.parsePublishedLine(line, parts); + break; + case "dir-options": + this.parseDirOptionsLine(line, parts); + break; + case "-----BEGIN": + crypto = new StringBuilder(); crypto.append(line).append("\n"); - } else if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in v2 network status."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + break; + case "-----END": + crypto.append(line).append("\n"); + String cryptoString = crypto.toString(); + crypto = null; + if (nextCrypto.equals("dir-signing-key")) { + this.dirSigningKey = cryptoString; + } else { + throw new DescriptorParseException("Unrecognized crypto " + + "block in v2 network status."); + } + nextCrypto = ""; + default: + if (crypto != null) { + crypto.append(line).append("\n"); + } else if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in v2 network status."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } } @@ -165,8 +166,8 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
protected void parseDirectorySignature(byte[] directorySignatureBytes) throws DescriptorParseException { - Scanner s = new Scanner(new String(directorySignatureBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(directorySignatureBytes)) + .useDelimiter("\n"); String nextCrypto = ""; StringBuilder crypto = null; while (s.hasNext()) { @@ -174,38 +175,38 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "directory-signature": - this.parseDirectorySignatureLine(line, parts); - nextCrypto = "directory-signature"; - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - if (nextCrypto.equals("directory-signature")) { - this.directorySignature = cryptoString; - } else { - throw new DescriptorParseException("Unrecognized crypto " - + "block in v2 network status."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "directory-signature": + this.parseDirectorySignatureLine(line, parts); + nextCrypto = "directory-signature"; + break; + case "-----BEGIN": + crypto = new StringBuilder(); + crypto.append(line).append("\n"); + break; + case "-----END": crypto.append(line).append("\n"); - } else if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" + line - + "' in v2 network status."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + String cryptoString = crypto.toString(); + crypto = null; + if (nextCrypto.equals("directory-signature")) { + this.directorySignature = cryptoString; + } else { + throw new DescriptorParseException("Unrecognized crypto " + + "block in v2 network status."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); + } else if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in v2 network status."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); - } } } } @@ -296,86 +297,100 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl }
private String statusDigest; + @Override public String getStatusDigest() { return this.statusDigest; }
private int networkStatusVersion; + @Override public int getNetworkStatusVersion() { return this.networkStatusVersion; }
private String hostname; + @Override public String getHostname() { return this.hostname; }
private String address; + @Override public String getAddress() { return this.address; }
private int dirPort; + @Override public int getDirport() { return this.dirPort; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private String contactLine; + @Override public String getContactLine() { return this.contactLine; }
private String dirSigningKey; + @Override public String getDirSigningKey() { return this.dirSigningKey; }
private String[] recommendedClientVersions; + @Override public List<String> getRecommendedClientVersions() { - return this.recommendedClientVersions == null ? null : - Arrays.asList(this.recommendedClientVersions); + return this.recommendedClientVersions == null ? null + : Arrays.asList(this.recommendedClientVersions); }
private String[] recommendedServerVersions; + @Override public List<String> getRecommendedServerVersions() { - return this.recommendedServerVersions == null ? null : - Arrays.asList(this.recommendedServerVersions); + return this.recommendedServerVersions == null ? null + : Arrays.asList(this.recommendedServerVersions); }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private String[] dirOptions; + @Override public SortedSet<String> getDirOptions() { return new TreeSet<>(Arrays.asList(this.dirOptions)); }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String directorySignature; + @Override public String getDirectorySignature() { return this.directorySignature; diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java index 384ad1f..a9bb928 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java @@ -1,9 +1,11 @@ /* Copyright 2011--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.DirectorySignature; +import org.torproject.descriptor.RelayNetworkStatusVote;
import java.util.ArrayList; import java.util.Arrays; @@ -17,8 +19,6 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet;
-import org.torproject.descriptor.RelayNetworkStatusVote; - /* Contains a network status vote. */ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl implements RelayNetworkStatusVote { @@ -53,8 +53,8 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl Set<String> atMostOnceKeywords = new HashSet<>(Arrays.asList(( "consensus-methods,client-versions,server-versions," + "flag-thresholds,params,contact," - + "legacy-key,dir-key-crosscert,dir-address,directory-footer"). - split(","))); + + "legacy-key,dir-key-crosscert,dir-address,directory-footer") + .split(","))); this.checkAtMostOnceKeywords(atMostOnceKeywords); Set<String> atLeastOnceKeywords = new HashSet<>(Arrays.asList( "directory-signature")); @@ -87,129 +87,129 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl String[] parts = line.split("[ \t]+"); String keyword = parts[0]; switch (keyword) { - case "network-status-version": - this.parseNetworkStatusVersionLine(line, parts); - break; - case "vote-status": - this.parseVoteStatusLine(line, parts); - break; - case "consensus-methods": - this.parseConsensusMethodsLine(line, parts); - break; - case "published": - this.parsePublishedLine(line, parts); - break; - case "valid-after": - this.parseValidAfterLine(line, parts); - break; - case "fresh-until": - this.parseFreshUntilLine(line, parts); - break; - case "valid-until": - this.parseValidUntilLine(line, parts); - break; - case "voting-delay": - this.parseVotingDelayLine(line, parts); - break; - case "client-versions": - this.parseClientVersionsLine(line, parts); - break; - case "server-versions": - this.parseServerVersionsLine(line, parts); - break; - case "package": - this.parsePackageLine(line, parts); - break; - case "known-flags": - this.parseKnownFlagsLine(line, parts); - break; - case "flag-thresholds": - this.parseFlagThresholdsLine(line, parts); - break; - case "params": - this.parseParamsLine(line, parts); - break; - case "dir-source": - this.parseDirSourceLine(line, parts); - break; - case "contact": - this.parseContactLine(line, parts); - break; - case "dir-key-certificate-version": - this.parseDirKeyCertificateVersionLine(line, parts); - break; - case "dir-address": - this.parseDirAddressLine(line, parts); - break; - case "fingerprint": - this.parseFingerprintLine(line, parts); - break; - case "legacy-dir-key": - this.parseLegacyDirKeyLine(line, parts); - break; - case "dir-key-published": - this.parseDirKeyPublished(line, parts); - break; - case "dir-key-expires": - this.parseDirKeyExpiresLine(line, parts); - break; - case "dir-identity-key": - this.parseDirIdentityKeyLine(line, parts); - nextCrypto = "dir-identity-key"; - break; - case "dir-signing-key": - this.parseDirSigningKeyLine(line, parts); - nextCrypto = "dir-signing-key"; - break; - case "dir-key-crosscert": - this.parseDirKeyCrosscertLine(line, parts); - nextCrypto = "dir-key-crosscert"; - break; - case "dir-key-certification": - this.parseDirKeyCertificationLine(line, parts); - nextCrypto = "dir-key-certification"; - break; - case "-----BEGIN": - crypto = new StringBuilder(); - crypto.append(line).append("\n"); - break; - case "-----END": - crypto.append(line).append("\n"); - String cryptoString = crypto.toString(); - crypto = null; - switch (nextCrypto) { + case "network-status-version": + this.parseNetworkStatusVersionLine(line, parts); + break; + case "vote-status": + this.parseVoteStatusLine(line, parts); + break; + case "consensus-methods": + this.parseConsensusMethodsLine(line, parts); + break; + case "published": + this.parsePublishedLine(line, parts); + break; + case "valid-after": + this.parseValidAfterLine(line, parts); + break; + case "fresh-until": + this.parseFreshUntilLine(line, parts); + break; + case "valid-until": + this.parseValidUntilLine(line, parts); + break; + case "voting-delay": + this.parseVotingDelayLine(line, parts); + break; + case "client-versions": + this.parseClientVersionsLine(line, parts); + break; + case "server-versions": + this.parseServerVersionsLine(line, parts); + break; + case "package": + this.parsePackageLine(line, parts); + break; + case "known-flags": + this.parseKnownFlagsLine(line, parts); + break; + case "flag-thresholds": + this.parseFlagThresholdsLine(line, parts); + break; + case "params": + this.parseParamsLine(line, parts); + break; + case "dir-source": + this.parseDirSourceLine(line, parts); + break; + case "contact": + this.parseContactLine(line, parts); + break; + case "dir-key-certificate-version": + this.parseDirKeyCertificateVersionLine(line, parts); + break; + case "dir-address": + this.parseDirAddressLine(line, parts); + break; + case "fingerprint": + this.parseFingerprintLine(line, parts); + break; + case "legacy-dir-key": + this.parseLegacyDirKeyLine(line, parts); + break; + case "dir-key-published": + this.parseDirKeyPublished(line, parts); + break; + case "dir-key-expires": + this.parseDirKeyExpiresLine(line, parts); + break; case "dir-identity-key": - this.dirIdentityKey = cryptoString; + this.parseDirIdentityKeyLine(line, parts); + nextCrypto = "dir-identity-key"; break; case "dir-signing-key": - this.dirSigningKey = cryptoString; + this.parseDirSigningKeyLine(line, parts); + nextCrypto = "dir-signing-key"; break; case "dir-key-crosscert": - this.dirKeyCrosscert = cryptoString; + this.parseDirKeyCrosscertLine(line, parts); + nextCrypto = "dir-key-crosscert"; break; case "dir-key-certification": - this.dirKeyCertification = cryptoString; + this.parseDirKeyCertificationLine(line, parts); + nextCrypto = "dir-key-certification"; break; - default: - throw new DescriptorParseException("Unrecognized crypto " - + "block in vote."); - } - nextCrypto = ""; - break; - default: - if (crypto != null) { + case "-----BEGIN": + crypto = new StringBuilder(); crypto.append(line).append("\n"); - } else { - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in vote."); + break; + case "-----END": + crypto.append(line).append("\n"); + String cryptoString = crypto.toString(); + crypto = null; + switch (nextCrypto) { + case "dir-identity-key": + this.dirIdentityKey = cryptoString; + break; + case "dir-signing-key": + this.dirSigningKey = cryptoString; + break; + case "dir-key-crosscert": + this.dirKeyCrosscert = cryptoString; + break; + case "dir-key-certification": + this.dirKeyCertification = cryptoString; + break; + default: + throw new DescriptorParseException("Unrecognized crypto " + + "block in vote."); + } + nextCrypto = ""; + break; + default: + if (crypto != null) { + crypto.append(line).append("\n"); } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in vote."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -341,38 +341,38 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl try { for (Map.Entry<String, String> e : flagThresholds.entrySet()) { switch (e.getKey()) { - case "stable-uptime": - this.stableUptime = Long.parseLong(e.getValue()); - break; - case "stable-mtbf": - this.stableMtbf = Long.parseLong(e.getValue()); - break; - case "fast-speed": - this.fastBandwidth = Long.parseLong(e.getValue()); - break; - case "guard-wfu": - this.guardWfu = Double.parseDouble(e.getValue(). - replaceAll("%", "")); - break; - case "guard-tk": - this.guardTk = Long.parseLong(e.getValue()); - break; - case "guard-bw-inc-exits": - this.guardBandwidthIncludingExits = - Long.parseLong(e.getValue()); - break; - case "guard-bw-exc-exits": - this.guardBandwidthExcludingExits = - Long.parseLong(e.getValue()); - break; - case "enough-mtbf": - this.enoughMtbfInfo = Integer.parseInt(e.getValue()); - break; - case "ignoring-advertised-bws": - this.ignoringAdvertisedBws = Integer.parseInt(e.getValue()); - break; - default: - // empty + case "stable-uptime": + this.stableUptime = Long.parseLong(e.getValue()); + break; + case "stable-mtbf": + this.stableMtbf = Long.parseLong(e.getValue()); + break; + case "fast-speed": + this.fastBandwidth = Long.parseLong(e.getValue()); + break; + case "guard-wfu": + this.guardWfu = Double.parseDouble(e.getValue() + .replaceAll("%", "")); + break; + case "guard-tk": + this.guardTk = Long.parseLong(e.getValue()); + break; + case "guard-bw-inc-exits": + this.guardBandwidthIncludingExits = + Long.parseLong(e.getValue()); + break; + case "guard-bw-exc-exits": + this.guardBandwidthExcludingExits = + Long.parseLong(e.getValue()); + break; + case "enough-mtbf": + this.enoughMtbfInfo = Integer.parseInt(e.getValue()); + break; + case "ignoring-advertised-bws": + this.ignoringAdvertisedBws = Integer.parseInt(e.getValue()); + break; + default: + // empty } } } catch (NumberFormatException ex) { @@ -454,7 +454,8 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl if (parts.length != 2) { throw new DescriptorParseException("Illegal line '" + line + "'."); } - this.legacyDirKey = ParseHelper.parseTwentyByteHexString(line, parts[1]); + this.legacyDirKey = ParseHelper.parseTwentyByteHexString(line, + parts[1]); }
private void parseDirKeyPublished(String line, String[] parts) @@ -517,90 +518,105 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String identity; + @Override public String getIdentity() { return this.identity; }
private String hostname; + @Override public String getHostname() { return this.hostname; }
private String address; + @Override public String getAddress() { return this.address; }
private int dirPort; + @Override public int getDirport() { return this.dirPort; }
private int orPort; + @Override public int getOrport() { return this.orPort; }
private String contactLine; + @Override public String getContactLine() { return this.contactLine; }
private int dirKeyCertificateVersion; + @Override public int getDirKeyCertificateVersion() { return this.dirKeyCertificateVersion; }
private String legacyDirKey; + @Override public String getLegacyDirKey() { return this.legacyDirKey; }
private long dirKeyPublishedMillis; + @Override public long getDirKeyPublishedMillis() { return this.dirKeyPublishedMillis; }
private long dirKeyExpiresMillis; + @Override public long getDirKeyExpiresMillis() { return this.dirKeyExpiresMillis; }
private String dirIdentityKey; + @Override public String getDirIdentityKey() { return this.dirIdentityKey; }
private String dirSigningKey; + @Override public String getDirSigningKey() { return this.dirSigningKey; }
private String dirKeyCrosscert; + @Override public String getDirKeyCrosscert() { return this.dirKeyCrosscert; }
private String dirKeyCertification; + @Override public String getDirKeyCertification() { return this.dirKeyCertification; @@ -622,69 +638,80 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl }
private int networkStatusVersion; + @Override public int getNetworkStatusVersion() { return this.networkStatusVersion; }
private Integer[] consensusMethods; + @Override public List<Integer> getConsensusMethods() { - return this.consensusMethods == null ? null : - Arrays.asList(this.consensusMethods); + return this.consensusMethods == null ? null + : Arrays.asList(this.consensusMethods); }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private long validAfterMillis; + @Override public long getValidAfterMillis() { return this.validAfterMillis; }
private long freshUntilMillis; + @Override public long getFreshUntilMillis() { return this.freshUntilMillis; }
private long validUntilMillis; + @Override public long getValidUntilMillis() { return this.validUntilMillis; }
private long voteSeconds; + @Override public long getVoteSeconds() { return this.voteSeconds; }
private long distSeconds; + @Override public long getDistSeconds() { return this.distSeconds; }
private String[] recommendedClientVersions; + @Override public List<String> getRecommendedClientVersions() { - return this.recommendedClientVersions == null ? null : - Arrays.asList(this.recommendedClientVersions); + return this.recommendedClientVersions == null ? null + : Arrays.asList(this.recommendedClientVersions); }
private String[] recommendedServerVersions; + @Override public List<String> getRecommendedServerVersions() { - return this.recommendedServerVersions == null ? null : - Arrays.asList(this.recommendedServerVersions); + return this.recommendedServerVersions == null ? null + : Arrays.asList(this.recommendedServerVersions); }
private List<String> packageLines; + @Override public List<String> getPackageLines() { return this.packageLines == null ? null @@ -692,70 +719,81 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl }
private String[] knownFlags; + @Override public SortedSet<String> getKnownFlags() { return new TreeSet<>(Arrays.asList(this.knownFlags)); }
private long stableUptime; + @Override public long getStableUptime() { return this.stableUptime; }
private long stableMtbf; + @Override public long getStableMtbf() { return this.stableMtbf; }
private long fastBandwidth; + @Override public long getFastBandwidth() { return this.fastBandwidth; }
private double guardWfu; + @Override public double getGuardWfu() { return this.guardWfu; }
private long guardTk; + @Override public long getGuardTk() { return this.guardTk; }
private long guardBandwidthIncludingExits; + @Override public long getGuardBandwidthIncludingExits() { return this.guardBandwidthIncludingExits; }
private long guardBandwidthExcludingExits; + @Override public long getGuardBandwidthExcludingExits() { return this.guardBandwidthExcludingExits; }
private int enoughMtbfInfo; + @Override public int getEnoughMtbfInfo() { return this.enoughMtbfInfo; }
private int ignoringAdvertisedBws; + @Override public int getIgnoringAdvertisedBws() { return this.ignoringAdvertisedBws; }
private SortedMap<String, Integer> consensusParams; + @Override public SortedMap<String, Integer> getConsensusParams() { - return this.consensusParams == null ? null: - new TreeMap<>(this.consensusParams); + return this.consensusParams == null ? null + : new TreeMap<>(this.consensusParams); } }
diff --git a/src/main/java/org/torproject/descriptor/impl/RelayServerDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayServerDescriptorImpl.java index 4957072..b40423d 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayServerDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayServerDescriptorImpl.java @@ -1,14 +1,15 @@ /* Copyright 2015 The Tor Project * See LICENSE for licensing information */ -package org.torproject.descriptor.impl;
-import java.util.ArrayList; -import java.util.List; +package org.torproject.descriptor.impl;
import org.torproject.descriptor.DescriptorParseException; import org.torproject.descriptor.RelayServerDescriptor; import org.torproject.descriptor.ServerDescriptor;
+import java.util.ArrayList; +import java.util.List; + public class RelayServerDescriptorImpl extends ServerDescriptorImpl implements RelayServerDescriptor {
diff --git a/src/main/java/org/torproject/descriptor/impl/RouterStatusEntryImpl.java b/src/main/java/org/torproject/descriptor/impl/RouterStatusEntryImpl.java index a359c50..1ada050 100644 --- a/src/main/java/org/torproject/descriptor/impl/RouterStatusEntryImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RouterStatusEntryImpl.java @@ -1,5 +1,6 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
import org.torproject.descriptor.RouterStatusEntry; @@ -15,24 +16,28 @@ public class RouterStatusEntryImpl implements RouterStatusEntry { }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private boolean isLive; + @Override public boolean isLive() { return this.isLive; }
private boolean isVerified; + @Override public boolean isVerified() { return this.isVerified; diff --git a/src/main/java/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/ServerDescriptorImpl.java index 1805dca..47d1b5b 100644 --- a/src/main/java/org/torproject/descriptor/impl/ServerDescriptorImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/ServerDescriptorImpl.java @@ -1,7 +1,12 @@ /* Copyright 2012--2015 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.BandwidthHistory; +import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.ServerDescriptor; + import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -14,10 +19,6 @@ import java.util.Set;
import javax.xml.bind.DatatypeConverter;
-import org.torproject.descriptor.BandwidthHistory; -import org.torproject.descriptor.DescriptorParseException; -import org.torproject.descriptor.ServerDescriptor; - /* Contains a server descriptor. */ public abstract class ServerDescriptorImpl extends DescriptorImpl implements ServerDescriptor { @@ -43,8 +44,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl + "router-digest").split(","))); this.checkAtMostOnceKeywords(atMostOnceKeywords); this.checkFirstKeyword("router"); - if (this.getKeywordCount("accept") == 0 && - this.getKeywordCount("reject") == 0) { + if (this.getKeywordCount("accept") == 0 + && this.getKeywordCount("reject") == 0) { throw new DescriptorParseException("Either keyword 'accept' or " + "'reject' must be contained at least once."); } @@ -53,8 +54,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl }
private void parseDescriptorBytes() throws DescriptorParseException { - Scanner s = new Scanner(new String(this.rawDescriptorBytes)). - useDelimiter("\n"); + Scanner s = new Scanner(new String(this.rawDescriptorBytes)) + .useDelimiter("\n"); String nextCrypto = ""; List<String> cryptoLines = null; while (s.hasNext()) { @@ -62,179 +63,179 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl if (line.startsWith("@")) { continue; } - String lineNoOpt = line.startsWith("opt ") ? - line.substring("opt ".length()) : line; + String lineNoOpt = line.startsWith("opt ") + ? line.substring("opt ".length()) : line; String[] partsNoOpt = lineNoOpt.split("[ \t]+"); String keyword = partsNoOpt[0]; switch (keyword) { - case "router": - this.parseRouterLine(line, lineNoOpt, partsNoOpt); - break; - case "or-address": - this.parseOrAddressLine(line, lineNoOpt, partsNoOpt); - break; - case "bandwidth": - this.parseBandwidthLine(line, lineNoOpt, partsNoOpt); - break; - case "platform": - this.parsePlatformLine(line, lineNoOpt, partsNoOpt); - break; - case "published": - this.parsePublishedLine(line, lineNoOpt, partsNoOpt); - break; - case "fingerprint": - this.parseFingerprintLine(line, lineNoOpt, partsNoOpt); - break; - case "hibernating": - this.parseHibernatingLine(line, lineNoOpt, partsNoOpt); - break; - case "uptime": - this.parseUptimeLine(line, lineNoOpt, partsNoOpt); - break; - case "onion-key": - this.parseOnionKeyLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "onion-key"; - break; - case "signing-key": - this.parseSigningKeyLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "signing-key"; - break; - case "accept": - this.parseAcceptLine(line, lineNoOpt, partsNoOpt); - break; - case "reject": - this.parseRejectLine(line, lineNoOpt, partsNoOpt); - break; - case "router-signature": - this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt); - nextCrypto = "router-signature"; - break; - case "contact": - this.parseContactLine(line, lineNoOpt, partsNoOpt); - break; - case "family": - this.parseFamilyLine(line, lineNoOpt, partsNoOpt); - break; - case "read-history": - this.parseReadHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "write-history": - this.parseWriteHistoryLine(line, lineNoOpt, partsNoOpt); - break; - case "eventdns": - this.parseEventdnsLine(line, lineNoOpt, partsNoOpt); - break; - case "caches-extra-info": - this.parseCachesExtraInfoLine(line, lineNoOpt, partsNoOpt); - break; - case "extra-info-digest": - this.parseExtraInfoDigestLine(line, lineNoOpt, partsNoOpt); - break; - case "hidden-service-dir": - this.parseHiddenServiceDirLine(line, lineNoOpt, partsNoOpt); - break; - case "protocols": - this.parseProtocolsLine(line, lineNoOpt, partsNoOpt); - break; - case "allow-single-hop-exits": - this.parseAllowSingleHopExitsLine(line, lineNoOpt, partsNoOpt); - break; - case "dircacheport": - this.parseDircacheportLine(line, lineNoOpt, partsNoOpt); - break; - case "router-digest": - this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); - break; - case "router-digest-sha256": - this.parseRouterDigestSha256Line(line, lineNoOpt, partsNoOpt); - break; - case "ipv6-policy": - this.parseIpv6PolicyLine(line, lineNoOpt, partsNoOpt); - break; - case "ntor-onion-key": - this.parseNtorOnionKeyLine(line, lineNoOpt, partsNoOpt); - break; - case "identity-ed25519": - this.parseIdentityEd25519Line(line, lineNoOpt, partsNoOpt); - nextCrypto = "identity-ed25519"; - break; - case "master-key-ed25519": - this.parseMasterKeyEd25519Line(line, lineNoOpt, partsNoOpt); - break; - case "router-sig-ed25519": - this.parseRouterSigEd25519Line(line, lineNoOpt, partsNoOpt); - break; - case "onion-key-crosscert": - this.parseOnionKeyCrosscert(line, lineNoOpt, partsNoOpt); - nextCrypto = "onion-key-crosscert"; - break; - case "ntor-onion-key-crosscert": - this.parseNtorOnionKeyCrosscert(line, lineNoOpt, partsNoOpt); - nextCrypto = "ntor-onion-key-crosscert"; - break; - case "tunnelled-dir-server": - this.parseTunnelledDirServerLine(line, lineNoOpt, partsNoOpt); - break; - case "-----BEGIN": - cryptoLines = new ArrayList<>(); - cryptoLines.add(line); - break; - case "-----END": - cryptoLines.add(line); - StringBuilder sb = new StringBuilder(); - for (String cryptoLine : cryptoLines) { - sb.append("\n").append(cryptoLine); - } - String cryptoString = sb.toString().substring(1); - switch (nextCrypto) { + case "router": + this.parseRouterLine(line, lineNoOpt, partsNoOpt); + break; + case "or-address": + this.parseOrAddressLine(line, lineNoOpt, partsNoOpt); + break; + case "bandwidth": + this.parseBandwidthLine(line, lineNoOpt, partsNoOpt); + break; + case "platform": + this.parsePlatformLine(line, lineNoOpt, partsNoOpt); + break; + case "published": + this.parsePublishedLine(line, lineNoOpt, partsNoOpt); + break; + case "fingerprint": + this.parseFingerprintLine(line, lineNoOpt, partsNoOpt); + break; + case "hibernating": + this.parseHibernatingLine(line, lineNoOpt, partsNoOpt); + break; + case "uptime": + this.parseUptimeLine(line, lineNoOpt, partsNoOpt); + break; case "onion-key": - this.onionKey = cryptoString; + this.parseOnionKeyLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "onion-key"; break; case "signing-key": - this.signingKey = cryptoString; + this.parseSigningKeyLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "signing-key"; + break; + case "accept": + this.parseAcceptLine(line, lineNoOpt, partsNoOpt); + break; + case "reject": + this.parseRejectLine(line, lineNoOpt, partsNoOpt); break; case "router-signature": - this.routerSignature = cryptoString; + this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt); + nextCrypto = "router-signature"; + break; + case "contact": + this.parseContactLine(line, lineNoOpt, partsNoOpt); + break; + case "family": + this.parseFamilyLine(line, lineNoOpt, partsNoOpt); + break; + case "read-history": + this.parseReadHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "write-history": + this.parseWriteHistoryLine(line, lineNoOpt, partsNoOpt); + break; + case "eventdns": + this.parseEventdnsLine(line, lineNoOpt, partsNoOpt); + break; + case "caches-extra-info": + this.parseCachesExtraInfoLine(line, lineNoOpt, partsNoOpt); + break; + case "extra-info-digest": + this.parseExtraInfoDigestLine(line, lineNoOpt, partsNoOpt); + break; + case "hidden-service-dir": + this.parseHiddenServiceDirLine(line, lineNoOpt, partsNoOpt); + break; + case "protocols": + this.parseProtocolsLine(line, lineNoOpt, partsNoOpt); + break; + case "allow-single-hop-exits": + this.parseAllowSingleHopExitsLine(line, lineNoOpt, partsNoOpt); + break; + case "dircacheport": + this.parseDircacheportLine(line, lineNoOpt, partsNoOpt); + break; + case "router-digest": + this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); + break; + case "router-digest-sha256": + this.parseRouterDigestSha256Line(line, lineNoOpt, partsNoOpt); + break; + case "ipv6-policy": + this.parseIpv6PolicyLine(line, lineNoOpt, partsNoOpt); + break; + case "ntor-onion-key": + this.parseNtorOnionKeyLine(line, lineNoOpt, partsNoOpt); break; case "identity-ed25519": - this.identityEd25519 = cryptoString; - this.parseIdentityEd25519CryptoBlock(cryptoString); + this.parseIdentityEd25519Line(line, lineNoOpt, partsNoOpt); + nextCrypto = "identity-ed25519"; + break; + case "master-key-ed25519": + this.parseMasterKeyEd25519Line(line, lineNoOpt, partsNoOpt); + break; + case "router-sig-ed25519": + this.parseRouterSigEd25519Line(line, lineNoOpt, partsNoOpt); break; case "onion-key-crosscert": - this.onionKeyCrosscert = cryptoString; + this.parseOnionKeyCrosscert(line, lineNoOpt, partsNoOpt); + nextCrypto = "onion-key-crosscert"; break; case "ntor-onion-key-crosscert": - this.ntorOnionKeyCrosscert = cryptoString; + this.parseNtorOnionKeyCrosscert(line, lineNoOpt, partsNoOpt); + nextCrypto = "ntor-onion-key-crosscert"; break; - default: - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized crypto " - + "block '" + cryptoString + "' in server descriptor."); - } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); - } - this.unrecognizedLines.addAll(cryptoLines); - } - } - cryptoLines = null; - nextCrypto = ""; - break; - default: - if (cryptoLines != null) { + case "tunnelled-dir-server": + this.parseTunnelledDirServerLine(line, lineNoOpt, partsNoOpt); + break; + case "-----BEGIN": + cryptoLines = new ArrayList<>(); cryptoLines.add(line); - } else { - ParseHelper.parseKeyword(line, partsNoOpt[0]); - if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized line '" - + line + "' in server descriptor."); + break; + case "-----END": + cryptoLines.add(line); + StringBuilder sb = new StringBuilder(); + for (String cryptoLine : cryptoLines) { + sb.append("\n").append(cryptoLine); + } + String cryptoString = sb.toString().substring(1); + switch (nextCrypto) { + case "onion-key": + this.onionKey = cryptoString; + break; + case "signing-key": + this.signingKey = cryptoString; + break; + case "router-signature": + this.routerSignature = cryptoString; + break; + case "identity-ed25519": + this.identityEd25519 = cryptoString; + this.parseIdentityEd25519CryptoBlock(cryptoString); + break; + case "onion-key-crosscert": + this.onionKeyCrosscert = cryptoString; + break; + case "ntor-onion-key-crosscert": + this.ntorOnionKeyCrosscert = cryptoString; + break; + default: + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized crypto " + + "block '" + cryptoString + "' in server descriptor."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.addAll(cryptoLines); + } + } + cryptoLines = null; + nextCrypto = ""; + break; + default: + if (cryptoLines != null) { + cryptoLines.add(line); } else { - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + ParseHelper.parseKeyword(line, partsNoOpt[0]); + if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + + line + "' in server descriptor."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + this.unrecognizedLines.add(line); } - this.unrecognizedLines.add(line); } - } } } } @@ -276,8 +277,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl if (partsNoOpt.length == 4) { this.bandwidthObserved = Integer.parseInt(partsNoOpt[3]); } - if (this.bandwidthRate >= 0 && this.bandwidthBurst >= 0 && - this.bandwidthObserved >= 0) { + if (this.bandwidthRate >= 0 && this.bandwidthBurst >= 0 + && this.bandwidthObserved >= 0) { isValid = true; } if (partsNoOpt.length < 4) { @@ -438,7 +439,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl if (partsNoOpt.length != 2) { throw new DescriptorParseException("Illegal line '" + line + "'."); } - this.usesEnhancedDnsLogic = ParseHelper.parseBoolean(partsNoOpt[1], line); + this.usesEnhancedDnsLogic = ParseHelper.parseBoolean(partsNoOpt[1], + line); }
private void parseCachesExtraInfoLine(String line, String lineNoOpt, @@ -482,17 +484,18 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
private void parseProtocolsLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { - int linkIndex = -1, circuitIndex = -1; + int linkIndex = -1; + int circuitIndex = -1; for (int i = 1; i < partsNoOpt.length; i++) { switch (partsNoOpt[i]) { - case "Link": - linkIndex = i; - break; - case "Circuit": - circuitIndex = i; - break; - default: - // empty + case "Link": + linkIndex = i; + break; + case "Circuit": + circuitIndex = i; + break; + default: + // empty } } if (linkIndex < 0 || circuitIndex < 0 || circuitIndex < linkIndex) { @@ -555,7 +558,7 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl if (partsNoOpt.length != 3) { isValid = false; } else { - switch (partsNoOpt[1]) { + switch (partsNoOpt[1]) { case "accept": case "reject": this.ipv6DefaultPolicy = partsNoOpt[1]; @@ -570,7 +573,7 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl break; default: isValid = false; - } + } } if (!isValid) { throw new DescriptorParseException("Illegal line '" + line + "'."); @@ -680,8 +683,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl System.arraycopy(this.getRawDescriptorBytes(), start, forDigest, 0, sig - start); this.serverDescriptorDigest = DatatypeConverter.printHexBinary( - MessageDigest.getInstance("SHA-1").digest(forDigest)). - toLowerCase(); + MessageDigest.getInstance("SHA-1").digest(forDigest)) + .toLowerCase(); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -712,8 +715,8 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl 0, sig - start); this.serverDescriptorDigestSha256 = DatatypeConverter.printBase64Binary( - MessageDigest.getInstance("SHA-256").digest(forDigest)). - replaceAll("=", ""); + MessageDigest.getInstance("SHA-256").digest(forDigest)) + .replaceAll("=", ""); } } catch (UnsupportedEncodingException e) { /* Handle below. */ @@ -727,256 +730,298 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl }
private String serverDescriptorDigest; + @Override public String getServerDescriptorDigest() { return this.serverDescriptorDigest; }
private String serverDescriptorDigestSha256; + @Override public String getServerDescriptorDigestSha256() { return this.serverDescriptorDigestSha256; }
private String nickname; + @Override public String getNickname() { return this.nickname; }
private String address; + @Override public String getAddress() { return this.address; }
private int orPort; + @Override public int getOrPort() { return this.orPort; }
private int socksPort; + @Override public int getSocksPort() { return this.socksPort; }
private int dirPort; + @Override public int getDirPort() { return this.dirPort; }
private List<String> orAddresses = new ArrayList<>(); + @Override public List<String> getOrAddresses() { return new ArrayList<>(this.orAddresses); }
private int bandwidthRate; + @Override public int getBandwidthRate() { return this.bandwidthRate; }
private int bandwidthBurst; + @Override public int getBandwidthBurst() { return this.bandwidthBurst; }
private int bandwidthObserved; + @Override public int getBandwidthObserved() { return this.bandwidthObserved; }
private String platform; + @Override public String getPlatform() { return this.platform; }
private long publishedMillis; + @Override public long getPublishedMillis() { return this.publishedMillis; }
private String fingerprint; + @Override public String getFingerprint() { return this.fingerprint; }
private boolean hibernating; + @Override public boolean isHibernating() { return this.hibernating; }
private Long uptime; + @Override public Long getUptime() { return this.uptime; }
private String onionKey; + @Override public String getOnionKey() { return this.onionKey; }
private String signingKey; + @Override public String getSigningKey() { return this.signingKey; }
private List<String> exitPolicyLines = new ArrayList<>(); + @Override public List<String> getExitPolicyLines() { return new ArrayList<>(this.exitPolicyLines); }
private String routerSignature; + @Override public String getRouterSignature() { return this.routerSignature; }
private String contact; + @Override public String getContact() { return this.contact; }
private String[] familyEntries; + @Override public List<String> getFamilyEntries() { - return this.familyEntries == null ? null : - Arrays.asList(this.familyEntries); + return this.familyEntries == null ? null + : Arrays.asList(this.familyEntries); }
private BandwidthHistory readHistory; + @Override public BandwidthHistory getReadHistory() { return this.readHistory; }
private BandwidthHistory writeHistory; + @Override public BandwidthHistory getWriteHistory() { return this.writeHistory; }
private boolean usesEnhancedDnsLogic; + @Override public boolean getUsesEnhancedDnsLogic() { return this.usesEnhancedDnsLogic; }
private boolean cachesExtraInfo; + @Override public boolean getCachesExtraInfo() { return this.cachesExtraInfo; }
private String extraInfoDigest; + @Override public String getExtraInfoDigest() { return this.extraInfoDigest; }
private String extraInfoDigestSha256; + @Override public String getExtraInfoDigestSha256() { return this.extraInfoDigestSha256; }
private Integer[] hiddenServiceDirVersions; + @Override public List<Integer> getHiddenServiceDirVersions() { - return this.hiddenServiceDirVersions == null ? null : - Arrays.asList(this.hiddenServiceDirVersions); + return this.hiddenServiceDirVersions == null ? null + : Arrays.asList(this.hiddenServiceDirVersions); }
private Integer[] linkProtocolVersions; + @Override public List<Integer> getLinkProtocolVersions() { - return this.linkProtocolVersions == null ? null : - Arrays.asList(this.linkProtocolVersions); + return this.linkProtocolVersions == null ? null + : Arrays.asList(this.linkProtocolVersions); }
private Integer[] circuitProtocolVersions; + @Override public List<Integer> getCircuitProtocolVersions() { - return this.circuitProtocolVersions == null ? null : - Arrays.asList(this.circuitProtocolVersions); + return this.circuitProtocolVersions == null ? null + : Arrays.asList(this.circuitProtocolVersions); }
private boolean allowSingleHopExits; + @Override public boolean getAllowSingleHopExits() { return this.allowSingleHopExits; }
private String ipv6DefaultPolicy; + @Override public String getIpv6DefaultPolicy() { return this.ipv6DefaultPolicy; }
private String ipv6PortList; + @Override public String getIpv6PortList() { return this.ipv6PortList; }
private String ntorOnionKey; + @Override public String getNtorOnionKey() { return this.ntorOnionKey; }
private String identityEd25519; + @Override public String getIdentityEd25519() { return this.identityEd25519; }
private String masterKeyEd25519; + @Override public String getMasterKeyEd25519() { return this.masterKeyEd25519; }
private String routerSignatureEd25519; + @Override public String getRouterSignatureEd25519() { return this.routerSignatureEd25519; }
private String onionKeyCrosscert; + @Override public String getOnionKeyCrosscert() { return this.onionKeyCrosscert; }
private String ntorOnionKeyCrosscert; + @Override public String getNtorOnionKeyCrosscert() { return this.ntorOnionKeyCrosscert; }
private int ntorOnionKeyCrosscertSign = -1; + @Override public int getNtorOnionKeyCrosscertSign() { return ntorOnionKeyCrosscertSign; }
private boolean tunnelledDirServer; + @Override public boolean getTunnelledDirServer() { return this.tunnelledDirServer; diff --git a/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java b/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java index 0800de0..71c762f 100644 --- a/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java @@ -1,8 +1,12 @@ /* Copyright 2012--2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.descriptor.impl;
+import org.torproject.descriptor.Descriptor; import org.torproject.descriptor.DescriptorParseException; +import org.torproject.descriptor.TorperfResult; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -12,9 +16,6 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap;
-import org.torproject.descriptor.Descriptor; -import org.torproject.descriptor.TorperfResult; - public class TorperfResultImpl extends DescriptorImpl implements TorperfResult {
@@ -80,100 +81,102 @@ public class TorperfResultImpl extends DescriptorImpl this.markKeyAsParsed(key, line); String value = keyValueParts[1]; switch (key) { - case "SOURCE": - this.parseSource(value, keyValue, line); - break; - case "FILESIZE": - this.parseFileSize(value, keyValue, line); - break; - case "START": - this.parseStart(value, keyValue, line); - break; - case "SOCKET": - this.parseSocket(value, keyValue, line); - break; - case "CONNECT": - this.parseConnect(value, keyValue, line); - break; - case "NEGOTIATE": - this.parseNegotiate(value, keyValue, line); - break; - case "REQUEST": - this.parseRequest(value, keyValue, line); - break; - case "RESPONSE": - this.parseResponse(value, keyValue, line); - break; - case "DATAREQUEST": - this.parseDataRequest(value, keyValue, line); - break; - case "DATARESPONSE": - this.parseDataResponse(value, keyValue, line); - break; - case "DATACOMPLETE": - this.parseDataComplete(value, keyValue, line); - break; - case "WRITEBYTES": - this.parseWriteBytes(value, keyValue, line); - break; - case "READBYTES": - this.parseReadBytes(value, keyValue, line); - break; - case "DIDTIMEOUT": - this.parseDidTimeout(value, keyValue, line); - break; - case "LAUNCH": - this.parseLaunch(value, keyValue, line); - break; - case "USED_AT": - this.parseUsedAt(value, keyValue, line); - break; - case "PATH": - this.parsePath(value, keyValue, line); - break; - case "BUILDTIMES": - this.parseBuildTimes(value, keyValue, line); - break; - case "TIMEOUT": - this.parseTimeout(value, keyValue, line); - break; - case "QUANTILE": - this.parseQuantile(value, keyValue, line); - break; - case "CIRC_ID": - this.parseCircId(value, keyValue, line); - break; - case "USED_BY": - this.parseUsedBy(value, keyValue, line); - break; - default: - if (key.startsWith("DATAPERC")) { - this.parseDataPercentile(value, keyValue, line); - } else if (this.failUnrecognizedDescriptorLines) { - throw new DescriptorParseException("Unrecognized key '" + key - + "' in line '" + line + "'."); - } else { - if (this.unrecognizedKeys == null) { - this.unrecognizedKeys = new TreeMap<>(); - } - this.unrecognizedKeys.put(key, value); - if (this.unrecognizedLines == null) { - this.unrecognizedLines = new ArrayList<>(); + case "SOURCE": + this.parseSource(value, keyValue, line); + break; + case "FILESIZE": + this.parseFileSize(value, keyValue, line); + break; + case "START": + this.parseStart(value, keyValue, line); + break; + case "SOCKET": + this.parseSocket(value, keyValue, line); + break; + case "CONNECT": + this.parseConnect(value, keyValue, line); + break; + case "NEGOTIATE": + this.parseNegotiate(value, keyValue, line); + break; + case "REQUEST": + this.parseRequest(value, keyValue, line); + break; + case "RESPONSE": + this.parseResponse(value, keyValue, line); + break; + case "DATAREQUEST": + this.parseDataRequest(value, keyValue, line); + break; + case "DATARESPONSE": + this.parseDataResponse(value, keyValue, line); + break; + case "DATACOMPLETE": + this.parseDataComplete(value, keyValue, line); + break; + case "WRITEBYTES": + this.parseWriteBytes(value, keyValue, line); + break; + case "READBYTES": + this.parseReadBytes(value, keyValue, line); + break; + case "DIDTIMEOUT": + this.parseDidTimeout(value, keyValue, line); + break; + case "LAUNCH": + this.parseLaunch(value, keyValue, line); + break; + case "USED_AT": + this.parseUsedAt(value, keyValue, line); + break; + case "PATH": + this.parsePath(value, keyValue, line); + break; + case "BUILDTIMES": + this.parseBuildTimes(value, keyValue, line); + break; + case "TIMEOUT": + this.parseTimeout(value, keyValue, line); + break; + case "QUANTILE": + this.parseQuantile(value, keyValue, line); + break; + case "CIRC_ID": + this.parseCircId(value, keyValue, line); + break; + case "USED_BY": + this.parseUsedBy(value, keyValue, line); + break; + default: + if (key.startsWith("DATAPERC")) { + this.parseDataPercentile(value, keyValue, line); + } else if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized key '" + key + + "' in line '" + line + "'."); + } else { + if (this.unrecognizedKeys == null) { + this.unrecognizedKeys = new TreeMap<>(); + } + this.unrecognizedKeys.put(key, value); + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<>(); + } + if (!this.unrecognizedLines.contains(line)) { + this.unrecognizedLines.add(line); + } } - if (!this.unrecognizedLines.contains(line)) { - this.unrecognizedLines.add(line); - } - } } } this.checkAllRequiredKeysParsed(line); }
private Set<String> parsedKeys = new HashSet<>(); + private Set<String> requiredKeys = new HashSet<>(Arrays.asList( ("SOURCE,FILESIZE,START,SOCKET,CONNECT,NEGOTIATE,REQUEST,RESPONSE," - + "DATAREQUEST,DATARESPONSE,DATACOMPLETE,WRITEBYTES,READBYTES"). - split(","))); + + "DATAREQUEST,DATARESPONSE,DATACOMPLETE,WRITEBYTES,READBYTES") + .split(","))); + private void markKeyAsParsed(String key, String line) throws DescriptorParseException { if (this.parsedKeys.contains(key)) { @@ -184,6 +187,7 @@ public class TorperfResultImpl extends DescriptorImpl this.parsedKeys.add(key); this.requiredKeys.remove(key); } + private void checkAllRequiredKeysParsed(String line) throws DescriptorParseException { for (String key : this.requiredKeys) { @@ -397,6 +401,7 @@ public class TorperfResultImpl extends DescriptorImpl }
private SortedMap<String, String> unrecognizedKeys; + @Override public SortedMap<String, String> getUnrecognizedKeys() { return this.unrecognizedKeys == null ? null @@ -404,90 +409,105 @@ public class TorperfResultImpl extends DescriptorImpl }
private String source; + @Override public String getSource() { return this.source; }
private int fileSize; + @Override public int getFileSize() { return this.fileSize; }
private long startMillis; + @Override public long getStartMillis() { return this.startMillis; }
private long socketMillis; + @Override public long getSocketMillis() { return this.socketMillis; }
private long connectMillis; + @Override public long getConnectMillis() { return this.connectMillis; }
private long negotiateMillis; + @Override public long getNegotiateMillis() { return this.negotiateMillis; }
private long requestMillis; + @Override public long getRequestMillis() { return this.requestMillis; }
private long responseMillis; + @Override public long getResponseMillis() { return this.responseMillis; }
private long dataRequestMillis; + @Override public long getDataRequestMillis() { return this.dataRequestMillis; }
private long dataResponseMillis; + @Override public long getDataResponseMillis() { return this.dataResponseMillis; }
private long dataCompleteMillis; + @Override public long getDataCompleteMillis() { return this.dataCompleteMillis; }
private int writeBytes; + @Override public int getWriteBytes() { return this.writeBytes; }
private int readBytes; + @Override public int getReadBytes() { return this.readBytes; }
private boolean didTimeout; + @Override public Boolean didTimeout() { return this.didTimeout; }
private SortedMap<Integer, Long> dataPercentiles; + @Override public SortedMap<Integer, Long> getDataPercentiles() { return this.dataPercentiles == null ? null @@ -495,49 +515,57 @@ public class TorperfResultImpl extends DescriptorImpl }
private long launchMillis = -1L; + @Override public long getLaunchMillis() { return this.launchMillis; }
private long usedAtMillis = -1L; + @Override public long getUsedAtMillis() { return this.usedAtMillis; }
private String[] path; + @Override public List<String> getPath() { return this.path == null ? null : Arrays.asList(this.path); }
private Long[] buildTimes; + @Override public List<Long> getBuildTimes() { - return this.buildTimes == null ? null : - Arrays.asList(this.buildTimes); + return this.buildTimes == null ? null + : Arrays.asList(this.buildTimes); }
private long timeout = -1L; + @Override public long getTimeout() { return this.timeout; }
private double quantile = -1.0; + @Override public double getQuantile() { return this.quantile; }
private int circId = -1; + @Override public int getCircId() { return this.circId; }
private int usedBy = -1; + @Override public int getUsedBy() { return this.usedBy;
tor-commits@lists.torproject.org