[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [bridgedb/develop] Fixed some defects isis found in the previous patch
commit 4c5c35dca9ec768d8b5db031a9253eb50b3544f7
Author: Alden S. Page <pagea@xxxxxxxxxxxxx>
Date: Wed Jan 7 16:07:29 2015 -0500
Fixed some defects isis found in the previous patch
Changes in geo.py: Made sure we loaded GeoIP databases without a hitch before
looking up any IPs. Load compressed IP from IPAddr instead of exploded. Removed
unused import.
Changes in HTTPServer.py: Restored accidentally removed whitespace; validated
ip before calling getCountryCode.
Changes in Bridges.py: Fixed documentation whitespace issue, validated ip before
calling getCountryCode.
Signed-off-by: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Again, I removed the changes to bridgedb.Bridges.Bridge, since the class
has been rewritten for #9380 and now lives in bridgedb.bridges.Bridge.
---
lib/bridgedb/HTTPServer.py | 9 +++++++--
lib/bridgedb/geo.py | 21 ++++++++++++++-------
2 files changed, 21 insertions(+), 9 deletions(-)
diff --git a/lib/bridgedb/HTTPServer.py b/lib/bridgedb/HTTPServer.py
index 3a51f7c..258861e 100644
--- a/lib/bridgedb/HTTPServer.py
+++ b/lib/bridgedb/HTTPServer.py
@@ -675,8 +675,12 @@ class WebResourceBridges(resource.Resource):
else:
ip = request.getClientIP()
- # Record what country the client is in.
- countryCode = bridgedb.geo.getCountryCode(IPAddress(ip))
+ # Look up the country code of the input IP
+ if isIPAddress(ip):
+ countryCode = bridgedb.geo.getCountryCode(IPAddress(ip))
+ else:
+ logging.warn("Invalid IP detected; skipping country lookup.")
+ countryCode = None
# XXX separate function again
format = request.args.get("format", None)
@@ -786,6 +790,7 @@ class WebResourceBridges(resource.Resource):
return rendered
+
class WebRoot(resource.Resource):
"""The parent resource of all other documents hosted by the webserver."""
diff --git a/lib/bridgedb/geo.py b/lib/bridgedb/geo.py
index b2be72c..1e2e9e7 100644
--- a/lib/bridgedb/geo.py
+++ b/lib/bridgedb/geo.py
@@ -16,7 +16,6 @@ and geoip-database packages.
import logging
from os.path import isfile
-from bridgedb.safelog import logSafely
from ipaddr import IPv4Address, IPv6Address
# IPv4 database
@@ -37,16 +36,18 @@ try:
except Exception as err: # pragma: no cover
logging.warn("Error while loading geoip module: %r" % err)
geoip = None
+ geoipv6 = None
def getCountryCode(IPAddr):
- """Returns the two-letter country code of a given IP address.
+ """Return the two-letter country code of a given IP address.
:param IPAddr: (:class:`ipaddr.IPAddress`) An IPv4 OR IPv6 address.
"""
+
ip = None
version = None
try:
- ip = IPAddr.exploded
+ ip = IPAddr.compressed
version = IPAddr.version
except AttributeError as err:
logging.warn("Wrong type passed to getCountryCode. Offending call:"
@@ -55,11 +56,17 @@ def getCountryCode(IPAddr):
# GeoIP has two databases: one for IPv4 addresses, and one for IPv6
# addresses. This will ensure we use the correct one.
- db = None
- if version == 4:
- db = geoip
+ db = None
+ # First, make sure we loaded GeoIP properly.
+ if None in (geoip, geoipv6):
+ logging.warn("GeoIP databases failed to load; could not look up"\
+ " country code.")
+ return None
else:
- db = geoipv6
+ if version == 4:
+ db = geoip
+ else:
+ db = geoipv6
# Look up the country code of the address.
countryCode = db.country_code_by_addr(ip)
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits