[or-cvs] r10701: Documented GeoIP configuration in the code and added UniqueC (torflow/trunk/TorCtl)

renner at seul.org renner at seul.org
Fri Jun 29 11:00:43 UTC 2007


Author: renner
Date: 2007-06-29 07:00:42 -0400 (Fri, 29 Jun 2007)
New Revision: 10701

Modified:
   torflow/trunk/TorCtl/GeoIPSupport.py
   torflow/trunk/TorCtl/PathSupport.py
Log:

  Documented GeoIP configuration in the code and added UniqueContinentRestriction as an alternative to ContinentJumper.



Modified: torflow/trunk/TorCtl/GeoIPSupport.py
===================================================================
--- torflow/trunk/TorCtl/GeoIPSupport.py	2007-06-29 07:46:34 UTC (rev 10700)
+++ torflow/trunk/TorCtl/GeoIPSupport.py	2007-06-29 11:00:42 UTC (rev 10701)
@@ -64,7 +64,7 @@
   plog("WARN", country_code + " is not on any continent")
   return None
 
-# Get the country code out of a GeoLiteCity record
+# Get the country code out of a GeoLiteCity record (not used)
 def get_country_from_record(ip):
   record = geoip.record_by_addr(ip)
   if record != None:
@@ -72,7 +72,7 @@
 
 # Router class extended to GeoIP
 class GeoIPRouter(TorCtl.Router):  
-  def __init__(self, router): # Promotion constructor :)
+  def __init__(self, router):
     self.__dict__ = router.__dict__
     # Select method to get the country_code here
     self.country_code = geoip.country_code_by_addr(self.get_ip_dotted())
@@ -90,13 +90,20 @@
   """ Class to configure GeoIP-based path building """		    
   def __init__(self, unique_countries, entry_country, exit_country, max_crossings, excludes):    
     # TODO: Somehow ensure validity of the configuration
-    # Do not use a country twice in a route
+    
+    # Do not use a country twice in a route 
+    # [True --> unique, False --> same or None --> pass] 
     self.unique_countries = unique_countries
-    # entry in entry_country
+    
+    # entry in entry_country [single country code or None]
     self.entry_country = entry_country
-    # exit in exit_country
+    # exit in exit_country [single country code or None]
     self.exit_country = exit_country
-    # Configure max continent crossings in one path
+    
+    # Configure max continent crossings in one path 
+    # [integer number 0-n or None --> ContinentJumper/UniqueContinent]
     self.max_crossings = max_crossings
-    # List of countries to not use in routes
+    
+    # List of countries to not use in routes 
+    # [(empty) list of country codes or None]
     self.excludes = excludes

Modified: torflow/trunk/TorCtl/PathSupport.py
===================================================================
--- torflow/trunk/TorCtl/PathSupport.py	2007-06-29 07:46:34 UTC (rev 10700)
+++ torflow/trunk/TorCtl/PathSupport.py	2007-06-29 11:00:42 UTC (rev 10701)
@@ -20,7 +20,7 @@
 "PathBuilder", "SelectionManager", "CountryCodeRestriction", 
 "CountryRestriction", "UniqueCountryRestriction", 
 "SingleCountryRestriction", "ContinentRestriction", 
-"ContinentJumperRestriction"]
+"ContinentJumperRestriction", "UniqueContinentRestriction"]
 
 #################### Path Support Interfaces #####################
 
@@ -302,49 +302,48 @@
 
 #################### GeoIP Restrictions ###################
 
-# Ensure country_code is set
 class CountryCodeRestriction(NodeRestriction):
+  """ Ensure that the country_code is set """
   def r_is_ok(self, r):
     return r.country_code != None
 
-# Ensure a specific country_code
 class CountryRestriction(NodeRestriction):
+  """ Ensure a specific country_code for nodes """
   def __init__(self, country_code):
     self.country_code = country_code
 
   def r_is_ok(self, r):
     return r.country_code == self.country_code
 
-# Exclude a list of country_codes
 class ExcludeCountriesRestriction(NodeRestriction):
+  """ Exclude a list of countries """
   def __init__(self, countries):
     self.countries = countries
 
   def r_is_ok(self, r):
     return not (r.country_code in self.countries)
 
-# Ensure every router to have distinct country
 class UniqueCountryRestriction(PathRestriction):
+  """ Ensure every router to have a distinct country_code """
   def r_is_ok(self, path, router):
     for r in path:
       if router.country_code == r.country_code:
         return False
     return True
 
-# Ensure every router to have the same country
 class SingleCountryRestriction(PathRestriction):
+  """ Ensure every router to have the same country_code """
   def r_is_ok(self, path, router):
     for r in path:
       if router.country_code != r.country_code:
         return False
     return True
 
-# Do not more than n continent crossings
 class ContinentRestriction(PathRestriction):
+  """ Do not more than n continent crossings """
   def __init__(self, n):
     self.n = n
 
-  # TODO: Include our location
   def r_is_ok(self, path, router):
     crossings = 0
     last = None
@@ -362,13 +361,21 @@
     if crossings > self.n: return False
     else: return True
 
-# Continent crossings between all hops
 class ContinentJumperRestriction(PathRestriction):
+  """ Ensure continent crossings between all hops """
   def r_is_ok(self, path, router):
     if len(path) > 0 and path[len(path)-1].continent == router.continent:
       return False
     else: return True
 
+class UniqueContinentRestriction(PathRestriction):
+  """ Ensure every hop to be on a different continent """
+  def r_is_ok(self, path, router):
+    for r in path:
+      if router.continent == r.continent:
+        return False
+    return True    
+
 #################### Node Generators ######################
 
 class UniformGenerator(NodeGenerator):
@@ -513,14 +520,14 @@
       entry_rstr.add_restriction(CountryCodeRestriction())
       mid_rstr.add_restriction(CountryCodeRestriction())
       self.exit_rstr.add_restriction(CountryCodeRestriction())
+      
       # First hop in a specified country?
-      entry_country = self.geoip_config.entry_country
-      if entry_country:  
-	entry_rstr.add_restriction(CountryRestriction(entry_country))
+      if self.geoip_config.entry_country:  
+	entry_rstr.add_restriction(CountryRestriction(self.geoip_config.entry_country))
       # Last hop in a specified country?
-      exit_country = self.geoip_config.exit_country
-      if exit_country:
-        self.exit_rstr.add_restriction(CountryRestriction(exit_country))
+      if self.geoip_config.exit_country:
+        self.exit_rstr.add_restriction(CountryRestriction(self.geoip_config.exit_country))
+
       # Excluded countries
       if self.geoip_config.excludes:
         plog("INFO", "Excluded countries: " + str(self.geoip_config.excludes))
@@ -528,6 +535,7 @@
           entry_rstr.add_restriction(ExcludeCountriesRestriction(self.geoip_config.excludes))
           mid_rstr.add_restriction(ExcludeCountriesRestriction(self.geoip_config.excludes))
           self.exit_rstr.add_restriction(ExcludeCountriesRestriction(self.geoip_config.excludes))      
+      
       # Unique countries set? None --> pass
       if self.geoip_config.unique_countries != None:
         if self.geoip_config.unique_countries:
@@ -536,11 +544,11 @@
         else:
 	  # False: use the same country for all nodes in a path
 	  self.path_rstr.add_restriction(SingleCountryRestriction())
-      # Specify max number of crossings here, None means ContinentJumper
-      n = self.geoip_config.max_crossings
-      if n == None:
-        self.path_rstr.add_restriction(ContinentJumperRestriction())
-      else: self.path_rstr.add_restriction(ContinentRestriction(n))
+      
+      # Specify max number of crossings here, None means ContinentJumper/UniqueContinents
+      if self.geoip_config.max_crossings == None:
+        self.path_rstr.add_restriction(UniqueContinentRestriction())
+      else: self.path_rstr.add_restriction(ContinentRestriction(self.geoip_config.max_crossings))
 
     # This is kind of hokey..
     if self.order_exits:



More information about the tor-commits mailing list