Ticket #12872: 0002-Fixed-some-defects-isis-found-in-the-previous-patch.patch

File 0002-Fixed-some-defects-isis-found-in-the-previous-patch.patch, 4.3 KB (added by pagea, 5 years ago)

Made an amendment patch after isis reviewed my changes over IRC. Mostly doing more thorough error checking and fixing some small whitespace issues.

  • lib/bridgedb/Bridges.py

    From a262ebb01df910925801dce29dfe189b09664147 Mon Sep 17 00:00:00 2001
    From: "Alden S. Page" <pagea@allegheny.edu>
    Date: Wed, 7 Jan 2015 16:07:29 -0500
    Subject: [PATCH 2/2] 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.
    ---
     lib/bridgedb/Bridges.py    |  2 +-
     lib/bridgedb/HTTPServer.py | 10 ++++++++--
     lib/bridgedb/geo.py        | 21 ++++++++++++++-------
     3 files changed, 23 insertions(+), 10 deletions(-)
    
    diff --git a/lib/bridgedb/Bridges.py b/lib/bridgedb/Bridges.py
    index 4b80f12..543130a 100644
    a b class Bridge(object): 
    120120    :attr bool verified: Did we receive the descriptor for this bridge that
    121121        was specified in the networkstatus?
    122122    :attr str countryCode: The two-letter country code of this bridge as
    123     reported by GeoIP.
     123        reported by GeoIP.
    124124    """
    125125    def __init__(self, nickname, ip, orport, fingerprint=None, id_digest=None,
    126126                 or_addresses=None, transports=None):
  • lib/bridgedb/HTTPServer.py

    diff --git a/lib/bridgedb/HTTPServer.py b/lib/bridgedb/HTTPServer.py
    index 54d9112..b96889c 100644
    a b from bridgedb.Filters import filterBridgesByIP6 
    5151from bridgedb.Filters import filterBridgesByTransport
    5252from bridgedb.Filters import filterBridgesByNotBlockedIn
    5353from bridgedb.parse import headers
     54from bridgedb.parse.addr import isIPAddress
    5455from bridgedb.safelog import logSafely
    5556
    5657
    class WebResourceBridges(resource.Resource): 
    673674        else:
    674675            ip = request.getClientIP()
    675676
    676         # Record what country the client is in.
    677         countryCode = bridgedb.geo.getCountryCode(IPAddress(ip))
     677        # Look up the country code of the input IP
     678        if isIPAddress(ip):
     679            countryCode = bridgedb.geo.getCountryCode(IPAddress(ip))
     680        else:
     681            logging.warn("Invalid IP detected; skipping country lookup.")
     682            countryCode = None
    678683
    679684        # XXX separate function again
    680685        format = request.args.get("format", None)
    class WebResourceBridges(resource.Resource): 
    778783
    779784        return rendered
    780785
     786
    781787class WebRoot(resource.Resource):
    782788    """The parent resource of all other documents hosted by the webserver."""
    783789
  • lib/bridgedb/geo.py

    diff --git a/lib/bridgedb/geo.py b/lib/bridgedb/geo.py
    index b2be72c..1e2e9e7 100644
    a b and geoip-database packages. 
    1616import logging
    1717from os.path import isfile
    1818
    19 from bridgedb.safelog import logSafely
    2019from ipaddr import IPv4Address, IPv6Address
    2120
    2221# IPv4 database
    try: 
    3736except Exception as err:  # pragma: no cover
    3837    logging.warn("Error while loading geoip module: %r" % err)
    3938    geoip = None
     39    geoipv6 = None
    4040
    4141def getCountryCode(IPAddr):
    42     """Returns the two-letter country code of a given IP address.
     42    """Return the two-letter country code of a given IP address.
    4343
    4444    :param IPAddr: (:class:`ipaddr.IPAddress`) An IPv4 OR IPv6 address.
    4545    """
     46
    4647    ip = None
    4748    version = None
    4849    try:
    49         ip = IPAddr.exploded
     50        ip = IPAddr.compressed
    5051        version = IPAddr.version
    5152    except AttributeError as err:
    5253        logging.warn("Wrong type passed to getCountryCode. Offending call:"
    def getCountryCode(IPAddr): 
    5556
    5657    # GeoIP has two databases: one for IPv4 addresses, and one for IPv6
    5758    # addresses. This will ensure we use the correct one.
    58     db = None   
    59     if version == 4:
    60         db = geoip
     59    db = None
     60    # First, make sure we loaded GeoIP properly.
     61    if None in (geoip, geoipv6):
     62        logging.warn("GeoIP databases failed to load; could not look up"\
     63                     " country code.")
     64        return None
    6165    else:
    62         db = geoipv6
     66        if version == 4:
     67            db = geoip
     68        else:
     69            db = geoipv6
    6370
    6471    # Look up the country code of the address.
    6572    countryCode = db.country_code_by_addr(ip)