[or-cvs] [ernie/master] Download exit list and store it in local directory structure.

karsten at torproject.org karsten at torproject.org
Mon Feb 22 15:29:08 UTC 2010


Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Mon, 22 Feb 2010 16:28:45 +0100
Subject: Download exit list and store it in local directory structure.
Commit: a3d51065011116de5949980eaf76fa2890052a8a

---
 config                      |    5 +++-
 src/Configuration.java      |    8 +++++-
 src/ExitListDownloader.java |   48 +++++++++++++++++++++++++++++++++++++++++++
 src/Main.java               |    5 ++++
 4 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 src/ExitListDownloader.java

diff --git a/config b/config
index e783344..37fea66 100644
--- a/config
+++ b/config
@@ -65,5 +65,8 @@
 #DownloadProcessGetTorStats 0
 
 ## URL to download GetTor stats from
-#GetTorStatsURL = http://gettor.torproject.org:8080/~gettor/gettor_stats.txt
+#GetTorStatsURL http://gettor.torproject.org:8080/~gettor/gettor_stats.txt
+
+## Download exit list and store it to disk
+#DownloadExitList 0
 
diff --git a/src/Configuration.java b/src/Configuration.java
index 6e96636..f46751c 100644
--- a/src/Configuration.java
+++ b/src/Configuration.java
@@ -29,6 +29,7 @@ public class Configuration {
   private boolean downloadProcessGetTorStats = false;
   private String getTorStatsUrl = "http://gettor.torproject.org:8080/"
       + "~gettor/gettor_stats.txt";
+  private boolean downloadExitList = false;
   public Configuration() {
     Logger logger = Logger.getLogger(Configuration.class.getName());
     File configFile = new File("config");
@@ -105,6 +106,9 @@ public class Configuration {
           // test if URL has correct format
           new URL(newUrl);
           this.getTorStatsUrl = newUrl;
+        } else if (line.startsWith("DownloadExitList")) {
+          this.downloadExitList = Integer.parseInt(
+              line.split(" ")[1]) != 0;
         } else {
           logger.severe("Configuration file contains unrecognized "
               + "configuration key in line '" + line + "'! Exiting!");
@@ -178,8 +182,8 @@ public class Configuration {
   public String getGetTorStatsUrl() {
     return this.getTorStatsUrl;
   }
-  public static void main(String[] args) {
-    new Configuration();
+  public boolean getDownloadExitList() {
+    return this.downloadExitList;
   }
 }
 
diff --git a/src/ExitListDownloader.java b/src/ExitListDownloader.java
new file mode 100644
index 0000000..9fe7c6c
--- /dev/null
+++ b/src/ExitListDownloader.java
@@ -0,0 +1,48 @@
+import java.io.*;
+import java.net.*;
+import java.text.*;
+import java.util.*;
+import java.util.logging.*;
+
+public class ExitListDownloader {
+  public ExitListDownloader() {
+    Logger logger = Logger.getLogger(TorperfProcessor.class.getName());
+    try {
+      logger.info("Downloading exit list...");
+      String exitAddressesUrl =
+          "http://exitlist.torproject.org/exitAddresses";
+      URL u = new URL(exitAddressesUrl);
+      HttpURLConnection huc = (HttpURLConnection) u.openConnection();
+      huc.setRequestMethod("GET");
+      huc.connect();
+      int response = huc.getResponseCode();
+      if (response != 200) {
+        logger.warning("Could not download exit list. Response code " + 
+            response);
+        return;
+      }
+      BufferedInputStream in = new BufferedInputStream(
+          huc.getInputStream());
+      SimpleDateFormat printFormat =
+          new SimpleDateFormat("yyyy/MM/dd/yyyy-MM-dd-HH-mm-ss");
+      printFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+      File exitListFile = new File("exitlist/" + printFormat.format(
+          new Date()));
+      exitListFile.getParentFile().mkdirs();
+      BufferedWriter bw = new BufferedWriter(new FileWriter(
+          exitListFile));
+      int len;
+      byte[] data = new byte[1024];
+      while ((len = in.read(data, 0, 1024)) >= 0) {
+        bw.write(new String(data, 0, len));
+      }   
+      in.close();
+      bw.close();
+      logger.info("Finished downloading exit list.");
+    } catch (IOException e) {
+      logger.log(Level.WARNING, "Failed downloading exit list", e);
+      return;
+    }
+  }
+}
+
diff --git a/src/Main.java b/src/Main.java
index 98d4658..53221f4 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -96,6 +96,11 @@ public class Main {
       new GetTorProcessor(statsDirectory, config.getGetTorStatsUrl());
     }
 
+    // Download exit list and store it to disk
+    if (config.getDownloadExitList()) {
+      new ExitListDownloader();
+    }
+
     // Remove lock file
     lf.releaseLock();
 
-- 
1.6.5



More information about the tor-commits mailing list