[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [metrics-db/master] Extract certificates from votes while parsing them.



commit 4d74615040dc9b55ecee9f7beb4326275d006bbd
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Fri Apr 1 14:00:01 2011 +0200

    Extract certificates from votes while parsing them.
---
 src/org/torproject/ernie/db/ArchiveWriter.java     |   20 ++++++++++++--
 .../torproject/ernie/db/RelayDescriptorParser.java |   28 +++++++++++++++++---
 2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/src/org/torproject/ernie/db/ArchiveWriter.java b/src/org/torproject/ernie/db/ArchiveWriter.java
index 68cdbaa..e30cd1b 100644
--- a/src/org/torproject/ernie/db/ArchiveWriter.java
+++ b/src/org/torproject/ernie/db/ArchiveWriter.java
@@ -12,7 +12,7 @@ import org.apache.commons.codec.binary.*;
 public class ArchiveWriter {
   private Logger logger;
   private File outputDirectory;
-  private int storedConsensuses = 0, storedVotes = 0,
+  private int storedConsensuses = 0, storedVotes = 0, storedCerts = 0,
       storedServerDescriptors = 0, storedExtraInfoDescriptors = 0;
 
   public ArchiveWriter(File outputDirectory) {
@@ -68,6 +68,18 @@ public class ArchiveWriter {
     }
   }
 
+  public void storeCertificate(byte[] data, String fingerprint,
+      long published) {
+    SimpleDateFormat printFormat = new SimpleDateFormat(
+        "yyyy-MM-dd-HH-mm-ss");
+    printFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+    String filename = outputDirectory + "/certs/"
+        + fingerprint + "-" + printFormat.format(new Date(published));
+    if (this.store(data, filename)) {
+      this.storedCerts++;
+    }
+  }
+
   public void storeServerDescriptor(byte[] data, String digest,
       long published) {
     SimpleDateFormat printFormat = new SimpleDateFormat("yyyy/MM/");
@@ -99,11 +111,13 @@ public class ArchiveWriter {
   public void intermediateStats(String event) {
     intermediateStats.append("While " + event + ", we stored "
         + this.storedConsensuses + " consensus(es), " + this.storedVotes
-        + " vote(s), " + this.storedServerDescriptors
-        + " server descriptor(s), and " + this.storedExtraInfoDescriptors
+        + " vote(s), " + this.storedCerts + " certificate(s), "
+        + this.storedServerDescriptors + " server descriptor(s), and "
+        + this.storedExtraInfoDescriptors
         + " extra-info descriptor(s) to disk.\n");
     this.storedConsensuses = 0;
     this.storedVotes = 0;
+    this.storedCerts = 0;
     this.storedServerDescriptors = 0;
     this.storedExtraInfoDescriptors = 0;
   }
diff --git a/src/org/torproject/ernie/db/RelayDescriptorParser.java b/src/org/torproject/ernie/db/RelayDescriptorParser.java
index bfd0033..089b743 100644
--- a/src/org/torproject/ernie/db/RelayDescriptorParser.java
+++ b/src/org/torproject/ernie/db/RelayDescriptorParser.java
@@ -77,11 +77,21 @@ public class RelayDescriptorParser {
         boolean isConsensus = true;
         String validAfterTime = null, fingerprint = null,
             dirSource = null;
-        long validAfter = -1L;
+        long validAfter = -1L, dirKeyPublished = -1L;
         SortedSet<String> dirSources = new TreeSet<String>();
         SortedSet<String> serverDescriptors = new TreeSet<String>();
         SortedSet<String> hashedRelayIdentities = new TreeSet<String>();
+        StringBuilder certificateStringBuilder = null;
+        String certificateString = null;
         while ((line = br.readLine()) != null) {
+          if (certificateStringBuilder != null) {
+            if (line.startsWith("r ")) {
+              certificateString = certificateStringBuilder.toString();
+              certificateStringBuilder = null;
+            } else {
+              certificateStringBuilder.append(line + "\n");
+            }
+          }
           if (line.equals("vote-status vote")) {
             isConsensus = false;
           } else if (line.startsWith("valid-after ")) {
@@ -91,8 +101,16 @@ public class RelayDescriptorParser {
             dirSource = line.split(" ")[2];
           } else if (line.startsWith("vote-digest ")) {
             dirSources.add(dirSource);
+          } else if (line.startsWith("dir-key-certificate-version ")) {
+            certificateStringBuilder = new StringBuilder();
+            certificateStringBuilder.append(line + "\n");
           } else if (line.startsWith("fingerprint ")) {
             fingerprint = line.split(" ")[1];
+          } else if (line.startsWith("dir-key-published ")) {
+            String dirKeyPublishedTime = line.substring(
+                "dir-key-published ".length());
+            dirKeyPublished = parseFormat.parse(dirKeyPublishedTime).
+                getTime();
           } else if (line.startsWith("r ")) {
             String[] parts = line.split(" ");
             if (parts.length < 9) {
@@ -137,9 +155,11 @@ public class RelayDescriptorParser {
               byte[] forDigest = new byte[sig - start];
               System.arraycopy(data, start, forDigest, 0, sig - start);
               String digest = DigestUtils.shaHex(forDigest).toUpperCase();
-              if (this.aw != null) {
-                this.aw.storeVote(data, validAfter, dirSource, digest);
-              }
+              this.aw.storeVote(data, validAfter, dirSource, digest);
+            }
+            if (certificateString != null) {
+              this.aw.storeCertificate(certificateString.getBytes(),
+                  dirSource, dirKeyPublished);
             }
           }
         }

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits