]> git.ipfire.org Git - ipfire.org.git/commitdiff
Migrate to libloc
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 May 2020 15:31:14 +0000 (15:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 May 2020 15:33:17 +0000 (15:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
16 files changed:
Makefile.am
src/backend/base.py
src/backend/fireinfo.py
src/backend/geoip.py [deleted file]
src/backend/mirrors.py
src/backend/misc.py
src/backend/resolver.py [new file with mode: 0644]
src/backend/util.py
src/templates/location/index.html
src/templates/location/lookup.html
src/templates/mirrors/mirror.html
src/web/__init__.py
src/web/base.py
src/web/donate.py
src/web/fireinfo.py
src/web/location.py

index f9657875eb1932c6671144835f25c90537a0d10b..c88e0cfecf1d1a1a622ac89039eaf154f4d18db7 100644 (file)
@@ -56,7 +56,6 @@ backend_PYTHON = \
        src/backend/database.py \
        src/backend/decorators.py \
        src/backend/fireinfo.py \
-       src/backend/geoip.py \
        src/backend/hwdata.py \
        src/backend/iuse.py \
        src/backend/memcached.py \
@@ -67,6 +66,7 @@ backend_PYTHON = \
        src/backend/nopaste.py \
        src/backend/ratelimit.py \
        src/backend/releases.py \
+       src/backend/resolver.py \
        src/backend/settings.py \
        src/backend/talk.py \
        src/backend/tweets.py \
index 0fdf2c436b219f53d465587eb1660eea309ae61c..6ee041dc212ada18f9e608df2b58783a2cdf84bf 100644 (file)
@@ -8,7 +8,6 @@ from . import accounts
 from . import blog
 from . import campaigns
 from . import database
-from . import geoip
 from . import fireinfo
 from . import iuse
 from . import memcached
@@ -18,9 +17,11 @@ from . import netboot
 from . import nopaste
 from . import ratelimit
 from . import releases
+from . import resolver
 from . import settings
 from . import talk
 from . import tweets
+from . import util
 from . import wiki
 from . import zeiterfassung
 from .decorators import *
@@ -57,7 +58,6 @@ class Backend(object):
 
                # Initialize backend modules.
                self.accounts = accounts.Accounts(self)
-               self.geoip = geoip.GeoIP(self)
                self.fireinfo = fireinfo.Fireinfo(self)
                self.iuse = iuse.IUse(self)
                self.mirrors = mirrors.Mirrors(self)
@@ -106,7 +106,6 @@ class Backend(object):
                        "scan-files"          : self.releases.scan_files,
                        "send-message"        : self.messages.send_cli,
                        "send-all-messages"   : self.messages.queue.send_all,
-                       "test-blacklist"      : self.geoip.test_blacklist,
                        "test-ldap"           : self.accounts.test_ldap,
                        "tweet"               : self.tweets.tweet,
                        "update-blog-feeds"   : self.blog.update_feeds,
@@ -141,6 +140,10 @@ class Backend(object):
        def ratelimiter(self):
                return ratelimit.RateLimiter(self)
 
+       @lazy_property
+       def resolver(self):
+               return resolver.Resolver(tries=2, timeout=2, domains=[])
+
        @lazy_property
        def tweets(self):
                return tweets.Tweets(self)
index e5099599d0b67a417de01d7f421ed61b54284aea..d02d774f8201a75c7a2c24775cab91e8139ea6ee 100644 (file)
@@ -5,6 +5,7 @@ import iso3166
 import logging
 import re
 
+from . import countries
 from . import database
 from . import hwdata
 from . import util
@@ -325,7 +326,7 @@ class Device(Object):
                        "11" : N_("Signal processing controller"),
                        "ff" : N_("Unassigned class"),
                },
-               
+
                "usb" : {
                        "00" : N_("Unclassified"),
                        "01" : N_("Multimedia"),
@@ -481,7 +482,7 @@ class Profile(Object):
        def time_updated(self):
                return self.data.time_updated
 
-       def updated(self, profile_parser=None, location=None, when=None):
+       def updated(self, profile_parser=None, country_code=None, when=None):
                valid = self.settings.get_int("fireinfo_profile_days_valid", 14)
 
                self.db.execute("UPDATE fireinfo_profiles \
@@ -497,8 +498,8 @@ class Profile(Object):
                                profile_parser.processor_bogomips,
                        )
 
-               if location:
-                       self.set_location(location)
+               if country_code:
+                       self.set_country_code(country_code)
 
                self.log_profile_update()
 
@@ -549,7 +550,8 @@ class Profile(Object):
 
        # Location
 
-       def get_location(self):
+       @property
+       def location(self):
                if not hasattr(self, "_location"):
                        res = self.db.get("SELECT location FROM fireinfo_profiles_locations \
                                WHERE profile_id = %s", self.id)
@@ -561,22 +563,20 @@ class Profile(Object):
 
                return self._location
 
-       def set_location(self, location):
-               if self.location == location:
+       def set_country_code(self, country_code):
+               if self.location == country_code:
                        return
 
                self.db.execute("DELETE FROM fireinfo_profiles_locations \
                        WHERE profile_id = %s", self.id)
                self.db.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \
-                       VALUES(%s, %s)", self.id, location)
+                       VALUES(%s, %s)", self.id, country_code)
 
-               self._location = location
-
-       location = property(get_location, set_location)
+               self._location = country_code
 
        @property
        def location_string(self):
-               return self.geoip.get_country_name(self.location)
+               return countries.get_name(self.location)
 
        # Devices
 
@@ -904,7 +904,7 @@ class Profile(Object):
                                self._kernel_id = None
 
                return self._kernel_id
-               
+
        def set_kernel_id(self, kernel_id):
                if self.kernel_id == kernel_id:
                        return
@@ -1727,7 +1727,7 @@ class Fireinfo(Object):
                else:
                        self.db.execute("COMMIT")
 
-       def _handle_profile(self, public_id, profile_blob, location=None, when=None):
+       def _handle_profile(self, public_id, profile_blob, country_code=None, when=None):
                private_id = profile_blob.get("private_id", None)
                assert private_id
 
@@ -1751,7 +1751,7 @@ class Fireinfo(Object):
                if profile:
                        # Check if the profile has changed. If so, update the data.
                        if profile_parser.equals(profile):
-                               profile.updated(profile_parser, location=location, when=when)
+                               profile.updated(profile_parser, country_code=country_code, when=when)
                                return
 
                        # If it does not match, we assume that it is expired and
@@ -1762,8 +1762,8 @@ class Fireinfo(Object):
                profile = self.fireinfo.create_profile(public_id, private_id, when=when)
                profile.parse(profile_parser)
 
-               if location:
-                       profile.set_location(location)
+               if country_code:
+                       profile.set_country_code(country_code)
 
                return profile
 
diff --git a/src/backend/geoip.py b/src/backend/geoip.py
deleted file mode 100644 (file)
index dffafa4..0000000
+++ /dev/null
@@ -1,237 +0,0 @@
-#!/usr/bin/python
-
-import ipaddress
-import logging
-import pycares
-import re
-import socket
-import tornado.gen
-import tornado.platform.caresresolver
-
-from . import countries
-
-from .decorators import *
-from .misc import Object
-
-# These lists are used to block access to the webapp
-BLOCKLISTS = (
-       "sbl.spamhaus.org",
-       "xbl.spamhaus.org",
-)
-
-BLACKLISTS = (
-       "b.barracudacentral.org",
-       "bl.spamcop.net",
-       "bl.blocklist.de",
-       "cbl.abuseat.org",
-       "dnsbl-1.uceprotect.net",
-       "dnsbl-2.uceprotect.net",
-       "dnsbl-3.uceprotect.net",
-       "dnsbl.abuse.ch",
-       "ix.dnsbl.manitu.net",
-       "pbl.spamhaus.org",
-       "sbl.spamhaus.org",
-       "xbl.spamhaus.org",
-       "zen.spamhaus.org",
-)
-
-class Resolver(tornado.platform.caresresolver.CaresResolver):
-       def initialize(self, **kwargs):
-               super().initialize()
-
-               # Overwrite Channel
-               self.channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
-
-       async def query(self, name, type=pycares.QUERY_TYPE_A):
-               # Create a new Future
-               fut = tornado.gen.Future()
-
-               # Perform the query
-               self.channel.query(name, type, lambda result, error: fut.set_result((result, error)))
-
-               # Wait for the response
-               result, error = await fut
-
-               # Handle any errors
-               if error:
-                       # NXDOMAIN
-                       if error == pycares.errno.ARES_ENOTFOUND:
-                               return
-
-                       # Ignore responses with no data
-                       elif error == pycares.errno.ARES_ENODATA:
-                               return
-
-                       raise IOError(
-                               "C-Ares returned error %s: %s while resolving %s"
-                               % (error, pycares.errno.strerror(error), name)
-                       )
-
-               # Return the result
-               return result
-
-
-class GeoIP(Object):
-       @lazy_property
-       def resolver(self):
-               return Resolver(tries=2, timeout=2, domains=[])
-
-       def lookup(self, address):
-               return Address(self.backend, address)
-
-       def guess_address_family(self, addr):
-               if ":" in addr:
-                       return 6
-
-               return 4
-
-       def get_country(self, addr):
-               ret = self.get_all(addr)
-
-               if ret:
-                       return ret.country
-
-       def get_location(self, addr):
-               query = "SELECT * FROM geoip \
-                       WHERE %s BETWEEN start_ip AND end_ip LIMIT 1"
-
-               return self.db.get(query, addr)
-
-       def get_asn(self, addr):
-               query = "SELECT asn FROM geoip_asn \
-                       WHERE %s BETWEEN start_ip AND end_ip LIMIT 1"
-
-               ret = self.db.get(query, addr)
-
-               if ret:
-                       return ret.asn
-
-       def get_all(self, addr):
-               location = self.get_location(addr)
-
-               if location:
-                       location["asn"] = self.get_asn(addr)
-
-               return location
-
-       _countries = {
-               "A1" : "Anonymous Proxy",
-               "A2" : "Satellite Provider",
-               "AP" : "Asia/Pacific Region",
-               "EU" : "Europe",
-       }
-
-       def get_country_name(self, code):
-               return countries.get_name(code)
-
-       async def test_blacklist(self, address):
-               address = self.lookup(address)
-
-               # Determne blacklist status
-               status = await address.is_blacklisted()
-
-               print("Blacklist status for %s: %s" % (address, status))
-
-
-class Address(Object):
-       def init(self, address):
-               self.address = ipaddress.ip_address(address)
-
-       def __str__(self):
-               return "%s" % self.address
-
-       @property
-       def family(self):
-               if isinstance(self.address, ipaddress.IPv6Address):
-                       return socket.AF_INET6
-               elif isinstance(self.address, ipaddress.IPv4Address):
-                       return socket.AF_INET
-
-       # Blacklist
-
-       def _make_blacklist_rr(self, blacklist):
-               if self.family == socket.AF_INET6:
-                       octets = list(self.address.exploded.replace(":", ""))
-               elif self.family == socket.AF_INET:
-                       octets = str(self.address).split(".")
-               else:
-                       raise NotImplementedError("Unknown IP protocol")
-
-               # Reverse the list
-               octets.reverse()
-
-               # Append suffix
-               octets.append(blacklist)
-
-               return ".".join(octets)
-
-       async def _resolve_blacklist(self, blacklist):
-               return_code = None
-
-               # Get resource record name
-               rr = self._make_blacklist_rr(blacklist)
-
-               # Get query type from IP protocol version
-               if self.family == socket.AF_INET6:
-                       type = pycares.QUERY_TYPE_AAAA
-               elif self.family == socket.AF_INET:
-                       type = pycares.QUERY_TYPE_A
-               else:
-                       raise NotImplementedError("Unknown IP protocol")
-
-               # Run query
-               try:
-                       res = await self.backend.geoip.resolver.query(rr, type=type)
-               except IOError as e:
-                       logging.warning(e)
-
-                       return return_code, "%s" % e
-
-               # Not found
-               if not res:
-                       logging.debug("%s is not blacklisted on %s" % (self, blacklist))
-                       return return_code, None
-
-               # Extract return code from DNS response
-               for row in res:
-                       return_code = row.host
-                       break
-
-               # If the IP address is on a blacklist, we will try to fetch the TXT record
-               reason = await self.backend.geoip.resolver.query(rr, type=pycares.QUERY_TYPE_TXT)
-
-               # Log result
-               logging.debug("%s is blacklisted on %s: %s" % (self, blacklist, reason or "N/A"))
-
-               # Take the first reason
-               if reason:
-                       for i in reason:
-                               return return_code, i.text
-
-               # Blocked, but no reason
-               return return_code, None
-
-       async def get_blacklists(self):
-               blacklists = { bl : self._resolve_blacklist(bl) for bl in BLACKLISTS }
-
-               return blacklists
-
-       async def is_blacklisted(self):
-               logging.debug("Checking if %s is blacklisted..." % self)
-
-               # Perform checks
-               blacklists = { bl : self._resolve_blacklist(bl) for bl in BLOCKLISTS }
-
-               # If we are blacklisted on one list, this one is screwed
-               for bl in blacklists:
-                       code, message = await blacklists[bl]
-
-                       logging.debug("Response from %s is: %s (%s)" % (bl, code, message))
-
-                       # Exclude matches on SBLCSS
-                       if bl == "sbl.spamhaus.org" and code == "127.0.0.3":
-                               continue
-
-                       # Consider the host blocked for any non-zero return code
-                       if code:
-                               return True
index 5c30a0b0ff20c955cad9257185709348f6d1aeab..62f077656d550f6b49d6558e45bdfd5c4f6461b2 100644 (file)
@@ -15,6 +15,7 @@ import tornado.netutil
 import urllib.parse
 
 from . import countries
+from . import util
 from .misc import Object
 from .decorators import *
 
@@ -100,6 +101,9 @@ class Mirror(Object):
                if isinstance(other, self.__class__):
                        return self.hostname < other.hostname
 
+       def __hash__(self):
+               return self.id
+
        @lazy_property
        def url(self):
                url = "%s://%s" % ("https" if self.supports_https else "http", self.hostname)
@@ -118,6 +122,14 @@ class Mirror(Object):
        def hostname(self):
                return self.data.hostname
 
+       @lazy_property
+       def address(self):
+               """
+                       Returns the stored address
+               """
+               if self.data.address:
+                       return util.Address(self.backend, self.data.address)
+
        @property
        def path(self):
                return self.data.path
@@ -126,51 +138,30 @@ class Mirror(Object):
        def supports_https(self):
                return self.data.supports_https
 
-       @property
-       def address(self):
-               for addr in self.addresses4:
-                       return addr
-
-               for addr in self.addresses6:
-                       return addr
-
        @property
        def owner(self):
                return self.data.owner
 
-       @lazy_property
-       def location(self):
-               return self.geoip.get_location(self.address)
-
-       @property
-       def latitude(self):
-               if self.location:
-                       return self.location.latitude
-
        @property
-       def longitude(self):
-               if self.location:
-                       return self.location.longitude
-
-       @lazy_property
        def country(self):
                return iso3166.countries.get(self.country_code)
 
        @property
        def country_code(self):
-               return self.data.country_code
+               if self.data.country_code:
+                       return self.data.country_code
 
-       @property
-       def country_name(self):
-               return self.geoip.get_country_name(self.country_code)
+               if self.address:
+                       return self.address.country_code
 
        @property
        def zone(self):
                return countries.get_zone(self.country_name)
 
-       @lazy_property
+       @property
        def asn(self):
-               return self.geoip.get_asn(self.address)
+               if self.address:
+                       return self.address.asn
 
        @property
        def filelist(self):
@@ -215,7 +206,7 @@ class Mirror(Object):
                logging.debug("Running check for mirror %s" % self.hostname)
 
                self.db.execute("UPDATE mirrors SET address = %s WHERE id = %s",
-                       self.address, self.id)
+                       await self.resolve(), self.id)
 
                success = await self.check_timestamp()
                if success:
@@ -332,24 +323,14 @@ class Mirror(Object):
        def mirrorlist(self):
                return self.data.get("mirrorlist", False)
 
-       @lazy_property
-       def addresses(self):
-               addrinfo = socket.getaddrinfo(self.hostname, 0, socket.AF_UNSPEC, socket.SOCK_STREAM)
-
-               ret = []
-               for family, socktype, proto, canonname, address in addrinfo:
-                       if family == socket.AF_INET:
-                               address, port = address
-                       elif family == socket.AF_INET6:
-                               address, port, flowid, scopeid = address
-                       ret.append((family, address))
+       async def resolve(self):
+               """
+                       Returns a single IP address of this mirror
+               """
+               addresses = await self.backend.resolver.resolve(self.hostname, 0)
 
-               return ret
+               # Return the first address
+               for family, address in addresses:
+                       host, port = address
 
-       @property
-       def addresses6(self):
-               return [address for family, address in self.addresses if family == socket.AF_INET6]
-
-       @property
-       def addresses4(self):
-               return [address for family, address in self.addresses if family == socket.AF_INET]
+                       return host
index c4b0480795c2973990f9566be8e58ca3b39af93d..f2f2e7532619fdb3872382fee74b61150f0bd0bb 100644 (file)
@@ -28,10 +28,6 @@ class Object(object):
        def fireinfo(self):
                return self.backend.fireinfo
 
-       @property
-       def geoip(self):
-               return self.backend.geoip
-
        @property
        def iuse(self):
                return self.backend.iuse
@@ -40,10 +36,6 @@ class Object(object):
        def memcache(self):
                return self.backend.memcache
 
-       @property
-       def planet(self):
-               return self.backend.planet
-
        @property
        def settings(self):
                return self.backend.settings
diff --git a/src/backend/resolver.py b/src/backend/resolver.py
new file mode 100644 (file)
index 0000000..2b37219
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+
+import ipaddress
+import pycares
+import tornado.gen
+import tornado.platform.caresresolver
+
+class Resolver(tornado.platform.caresresolver.CaresResolver):
+       def initialize(self, **kwargs):
+               super().initialize()
+
+               # Overwrite Channel
+               self.channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
+
+       async def query(self, name, type=pycares.QUERY_TYPE_A):
+               # Create a new Future
+               fut = tornado.gen.Future()
+
+               # Perform the query
+               self.channel.query(name, type, lambda result, error: fut.set_result((result, error)))
+
+               # Wait for the response
+               result, error = await fut
+
+               # Handle any errors
+               if error:
+                       # NXDOMAIN
+                       if error == pycares.errno.ARES_ENOTFOUND:
+                               return
+
+                       # Ignore responses with no data
+                       elif error == pycares.errno.ARES_ENODATA:
+                               return
+
+                       raise IOError(
+                               "C-Ares returned error %s: %s while resolving %s"
+                               % (error, pycares.errno.strerror(error), name)
+                       )
+
+               # Return the result
+               return result
index 04bde507a4480f6a91a2938a7fd7b94435028b96..99a9868a9fe1de8d09ca5253ba0ae29f9f4837f3 100644 (file)
@@ -4,12 +4,169 @@ import PIL.Image
 import PIL.ImageFilter
 import PIL.ImageOps
 import io
+import ipaddress
+import location
 import logging
+import pycares
 import random
 import re
+import socket
 import string
 import unicodedata
 
+from .decorators import *
+from .misc import Object
+
+# These lists are used to block access to the webapp
+BLOCKLISTS = (
+       "sbl.spamhaus.org",
+       "xbl.spamhaus.org",
+)
+
+BLACKLISTS = (
+       "b.barracudacentral.org",
+       "bl.spamcop.net",
+       "bl.blocklist.de",
+       "cbl.abuseat.org",
+       "dnsbl-1.uceprotect.net",
+       "dnsbl-2.uceprotect.net",
+       "dnsbl-3.uceprotect.net",
+       "dnsbl.abuse.ch",
+       "ix.dnsbl.manitu.net",
+       "pbl.spamhaus.org",
+       "sbl.spamhaus.org",
+       "xbl.spamhaus.org",
+       "zen.spamhaus.org",
+)
+
+# Open location database
+db = location.Database("/var/lib/location/database.db")
+
+class Address(Object):
+       def init(self, address):
+               self.address = ipaddress.ip_address(address)
+
+       def __str__(self):
+               return "%s" % self.address
+
+       @property
+       def family(self):
+               if isinstance(self.address, ipaddress.IPv6Address):
+                       return socket.AF_INET6
+               elif isinstance(self.address, ipaddress.IPv4Address):
+                       return socket.AF_INET
+
+       @lazy_property
+       def network(self):
+               return db.lookup("%s" % self.address)
+
+       @property
+       def country_code(self):
+               if self.network:
+                       return self.network.country_code
+
+       @lazy_property
+       def asn(self):
+               if self.network:
+                       return self.network.asn
+
+       # Blacklist
+
+       def _make_blacklist_rr(self, blacklist):
+               if self.family == socket.AF_INET6:
+                       octets = list(self.address.exploded.replace(":", ""))
+               elif self.family == socket.AF_INET:
+                       octets = str(self.address).split(".")
+               else:
+                       raise NotImplementedError("Unknown IP protocol")
+
+               # Reverse the list
+               octets.reverse()
+
+               # Append suffix
+               octets.append(blacklist)
+
+               return ".".join(octets)
+
+       async def _resolve_blacklist(self, blacklist):
+               return_code = None
+
+               # Get resource record name
+               rr = self._make_blacklist_rr(blacklist)
+
+               # Get query type from IP protocol version
+               if self.family == socket.AF_INET6:
+                       type = pycares.QUERY_TYPE_AAAA
+               elif self.family == socket.AF_INET:
+                       type = pycares.QUERY_TYPE_A
+               else:
+                       raise NotImplementedError("Unknown IP protocol")
+
+               # Run query
+               try:
+                       res = await self.backend.resolver.query(rr, type=type)
+               except IOError as e:
+                       logging.warning(e)
+
+                       return return_code, "%s" % e
+
+               # Not found
+               if not res:
+                       logging.debug("%s is not blacklisted on %s" % (self, blacklist))
+                       return return_code, None
+
+               # Extract return code from DNS response
+               for row in res:
+                       return_code = row.host
+                       break
+
+               # If the IP address is on a blacklist, we will try to fetch the TXT record
+               reason = await self.backend.resolver.query(rr, type=pycares.QUERY_TYPE_TXT)
+
+               # Log result
+               logging.debug("%s is blacklisted on %s: %s" % (self, blacklist, reason or "N/A"))
+
+               # Take the first reason
+               if reason:
+                       for i in reason:
+                               return return_code, i.text
+
+               # Blocked, but no reason
+               return return_code, None
+
+       async def get_blacklists(self):
+               blacklists = { bl : await self._resolve_blacklist(bl) for bl in BLACKLISTS }
+
+               return blacklists
+
+       async def is_blacklisted(self):
+               logging.debug("Checking if %s is blacklisted..." % self)
+
+               # Perform checks
+               blacklists = { bl : self._resolve_blacklist(bl) for bl in BLOCKLISTS }
+
+               # If we are blacklisted on one list, this one is screwed
+               for bl in blacklists:
+                       code, message = await blacklists[bl]
+
+                       logging.debug("Response from %s is: %s (%s)" % (bl, code, message))
+
+                       # Exclude matches on SBLCSS
+                       if bl == "sbl.spamhaus.org" and code == "127.0.0.3":
+                               continue
+
+                       # Consider the host blocked for any non-zero return code
+                       if code:
+                               return True
+
+def format_asn(asn):
+       network = db.get_as(asn)
+
+       if network:
+               return "%s" % network
+
+       return "AS%s" % asn
+
 def format_size(s, max_unit=None):
        units = ("B", "kB", "MB", "GB", "TB")
 
index 55e35e10b31228a84b1b0275cab8359ec5ded60f..97e8399e3dad7a34bc4415639ae8b374aecbd7fe 100644 (file)
@@ -13,7 +13,7 @@
                                <div class="col-12 col-md-6 my-5 text-center">
                                        <i class="fas fa-globe-europe fa-10x py-5"></i>
 
-                                       <h3>2001:678:b28::</h3>
+                                       <h3>{{ address }}</h3>
                                </div>
 
                                <div class="col-12 col-md-6 align-self-center px-3">
 
                                        <dl class="row">
                                                <dt class="col-sm-3">{{ _("Country") }}</dt>
-                                               <dd class="col-sm-9">Germany</dd>
+                                               <dd class="col-sm-9">
+                                                       {{ format_country_name(address.country_code) if address.country_code else _("N/A") }}
+                                               </dd>
 
                                                <dt class="col-sm-3">{{ _("Network") }}</dt>
-                                               <dd class="col-sm-9">AS204867 - Lightning Wire Labs GmbH</dd>
+                                               <dd class="col-sm-9">{{ address.asn or _("Unknown") }}</dd>
                                        </dl>
                                </div>
                        </div>
index 07d5ebfdf792350a80c10baf9a19644cdcb51f4b..5a7c5b1661d94c63bd1e6e738ce959d0546e99fd 100644 (file)
        {% end %}
 
        <div class="card mb-4">
-               {% if peer and peer.latitude and peer.longitude %}
-                       <div class="card-img-top">
-                               {% module Map(peer.latitude, peer.longitude) %}
-                       </div>
-               {% end %}
-
                <div class="card-body">
                        <dl>
-                               <dt>{{ _("Country") }}</dt>
-                               <dd>
-                                       {% if peer %}
-                                               {% if peer.country_name %}
-                                                       {{ peer.country_name }} ({{ peer.country }})
-                                               {% else %}
-                                                       {{ peer.country_name }}
-                                               {% end %}
-                                       {% else %}
-                                               <span class="text-muted">{{ _("Unkown") }}</span>
-                                       {% end %}
-                               </dd>
+                               {% if address.country_code %}
+                                       <dt>{{ _("Country") }}</dt>
+                                       <dd>
+                                               {{ format_country_name(address.country_code) }}
+                                       </dd>
+                               {% end %}
 
-                               {% if peer and peer.asn %}
+                               {% if address.asn %}
                                        <dt>{{ _("Autonomous System") }}</dt>
-                                       <dd>{{ peer.asn }}</dd>
+                                       <dd>{{ format_asn(address.asn) }}</dd>
                                {% end %}
                        </dl>
 
index 758d0be78a4152a1485cc20ef940ace997b6007b..cf9e2acef42138669b813ae61d083383a873c6c9 100644 (file)
        <div class="row justify-content-center">
                <div class="col-12 col-md-6">
                        <div class="card mb-4">
-                               {% if mirror.latitude and mirror.longitude %}
-                                       <div class="card-img-top">
-                                               {% module Map(mirror.latitude, mirror.longitude) %}
-                                       </div>
-                               {% end %}
+                               {# <div class="card-img-top">
+                                       {% module Map() %}
+                               </div> #}
 
                                <div class="card-body">
                                        <dl class="mb-0">
                                                {% if mirror.asn %}
                                                        <dt>{{ _("Autonomous System") }}</dt>
-                                                       <dd>{{ mirror.asn }}</dd>
+                                                       <dd>{{ format_asn(mirror.asn) }}</dd>
                                                {% end %}
 
                                                <dt>{{ _("Country") }}</dt>
-                                               <dd>{{ mirror.country_name }}</dd>
+                                               <dd>{{ mirror.country.name }}</dd>
                                        </dl>
                                </div>
 
index 4821c43824ec55176d6f6401b34bb66593558773..eec28f2dc1bf1e5423380fb976455f312319146b 100644 (file)
@@ -11,6 +11,7 @@ import tornado.web
 
 import ipfire
 import ipfire.countries
+from .. import util
 
 from .handlers import *
 
@@ -49,6 +50,7 @@ class Application(tornado.web.Application):
 
                        # UI Methods
                        "ui_methods" : {
+                               "format_asn"                   : self.format_asn,
                                "format_country_name"          : self.format_country_name,
                                "format_language_name"         : self.format_language_name,
                                "format_month_name"            : self.format_month_name,
@@ -152,7 +154,7 @@ class Application(tornado.web.Application):
                ])
 
                # blog.ipfire.org
-               self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"blog\.ipfire\.org", [
                        (r"/", blog.IndexHandler),
                        (r"/authors/(\w+)", blog.AuthorHandler),
                        (r"/compose", blog.ComposeHandler),
@@ -170,20 +172,20 @@ class Application(tornado.web.Application):
                ] + authentication_handlers)
 
                # downloads.ipfire.org
-               self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"downloads?\.ipfire\.org", [
                        (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
                        (r"/release/(.*)", download.ReleaseRedirectHandler),
                        (r"/(.*)", download.FileHandler),
                ])
 
                # mirrors.ipfire.org
-               self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"^mirrors\.ipfire\.org", [
                        (r"/", mirrors.IndexHandler),
                        (r"/mirrors/(.*)", mirrors.MirrorHandler),
                ])
 
                # planet.ipfire.org
-               self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"planet\.ipfire\.org", [
                        (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }),
                        (r"/post/([A-Za-z0-9_-]+)", handlers.PlanetPostHandler),
                        (r"/user/([a-z0-9_-]+)", handlers.PlanetUserHandler),
@@ -195,7 +197,7 @@ class Application(tornado.web.Application):
                ])
 
                # fireinfo.ipfire.org
-               self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"fireinfo\.ipfire\.org", [
                        (r"/", fireinfo.IndexHandler),
 
                        # Admin
@@ -221,7 +223,7 @@ class Application(tornado.web.Application):
                ] + authentication_handlers)
 
                # i-use.ipfire.org
-               self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"i-use\.ipfire\.org", [
                        (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
                        (r"/profile/([a-f0-9]{40})/([0-9]+).png", iuse.ImageHandler),
                ])
@@ -242,14 +244,14 @@ class Application(tornado.web.Application):
                ])
 
                # nopaste.ipfire.org
-               self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"nopaste\.ipfire\.org", [
                        (r"/", nopaste.CreateHandler),
                        (r"/raw/(.*)", nopaste.RawHandler),
                        (r"/view/(.*)", nopaste.ViewHandler),
                ] + authentication_handlers)
 
                # location.ipfire.org
-               self.add_handlers(r"location(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"location\.ipfire\.org", [
                        (r"/", location.IndexHandler),
                        (r"/how\-to\-use", StaticHandler, { "template" : "../location/how-to-use.html" }),
                        (r"/lookup/(.+)/blacklists", location.BlacklistsHandler),
@@ -257,17 +259,17 @@ class Application(tornado.web.Application):
                ])
 
                # geoip.ipfire.org
-               self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"geoip\.ipfire\.org", [
                        (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }),
                ])
 
                # talk.ipfire.org
-               self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"talk\.ipfire\.org", [
                        (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }),
                ])
 
                # people.ipfire.org
-               self.add_handlers(r"people(\.dev)?\.ipfire\.org", [
+               self.add_handlers(r"people\.ipfire\.org", [
                        (r"/", people.IndexHandler),
                        (r"/activate/([a-z_][a-z0-9_-]{0,31})/(\w+)", auth.ActivateHandler),
                        (r"/conferences", people.ConferencesHandler),
@@ -304,7 +306,7 @@ class Application(tornado.web.Application):
                ]  + authentication_handlers)
 
                # wiki.ipfire.org
-               self.add_handlers(r"wiki(\.dev)?\.ipfire\.org",
+               self.add_handlers(r"wiki\.ipfire\.org",
                        authentication_handlers + [
 
                        # Actions
@@ -335,6 +337,9 @@ class Application(tornado.web.Application):
 
                logging.info("Successfully initialied application")
 
+       def format_asn(self, handler, asn):
+               return util.format_asn(asn)
+
        def format_country_name(self, handler, country_code):
                return ipfire.countries.get_name(country_code)
 
index e000ca33718516c59b5d827e4bdd1fb532c4837a..6b25ded18d99a417ed5a77623523ab6ad1e4d5ba 100644 (file)
@@ -119,26 +119,16 @@ class BaseHandler(tornado.web.RequestHandler):
                return remote_ips.pop()
 
        @lazy_property
-       def remote(self):
+       def current_address(self):
                address = self.get_remote_ip()
 
                if address:
-                       return self.backend.geoip.lookup(address)
+                       return util.Address(self.backend, address)
 
        @lazy_property
        def current_country_code(self):
-               remote_ip = self.get_remote_ip()
-
-               if remote_ip:
-                       return self.backend.geoip.get_country(remote_ip)
-
-       def get_remote_location(self):
-               if not hasattr(self, "__remote_location"):
-                       remote_ip = self.get_remote_ip()
-
-                       self.__remote_location = self.geoip.get_location(remote_ip)
-
-               return self.__remote_location
+               if self.current_address:
+                       return self.current_address.country_code
 
        def get_argument_int(self, *args, **kwargs):
                arg = self.get_argument(*args, **kwargs)
@@ -237,10 +227,6 @@ class BaseHandler(tornado.web.RequestHandler):
        def releases(self):
                return self.backend.releases
 
-       @property
-       def geoip(self):
-               return self.backend.geoip
-
        @property
        def talk(self):
                return self.backend.talk
index 1e31865dd2d8c675b5e172221818495fce1038b3..26b0ad6bb3d926ac62f12a861c6ba766e3b15106 100644 (file)
@@ -7,12 +7,7 @@ from . import base
 
 class DonateHandler(base.BaseHandler):
        def get(self):
-               location = self.get_remote_location()
-
-               if location:
-                       country = location.country
-               else:
-                       country = None
+               country = self.current_country_code
 
                # Get defaults
                first_name = self.get_argument("first_name", None)
index 4fa5b968f2a88f05eebb714699b3ce7160cc2d05..2662ed4667b8a5ef3b642111ef37d551e9c05410 100644 (file)
@@ -136,15 +136,11 @@ class ProfileSendHandler(BaseHandler):
                profile_blob = self.get_profile_blob()
                #self.check_profile_blob(profile_blob)
 
-               # Get location
-               location = self.get_remote_location()
-               if location:
-                       location = location.country
-
                # Handle the profile.
                with self.db.transaction():
                        try:
-                               self.fireinfo.handle_profile(public_id, profile_blob, location=location)
+                               self.fireinfo.handle_profile(public_id, profile_blob,
+                                       country_code=self.current_country_code)
 
                        except fireinfo.ProfileParserError as e:
                                raise tornado.web.HTTPError(400, "Could not parse profile: %s" % e)
index 5807542a8fa0550392adb3cbd7eb27f05c17c986..9d874e3f79053498efb6d5e2f91dfb797eaf8df1 100644 (file)
@@ -4,35 +4,31 @@
 import logging
 import tornado.web
 
+from .. import util
+
 from . import base
 
 class IndexHandler(base.BaseHandler):
        def get(self):
-               self.render("location/index.html")
+               self.render("location/index.html", address=self.current_address)
 
 
 class LookupHandler(base.BaseHandler):
        async def get(self, address):
-               peer = self.geoip.get_all(address)
-               if peer:
-                       peer["country_name"] = self.geoip.get_country_name(peer.country)
-
                # Lookup address
-               address = self.geoip.lookup(address)
+               address = util.Address(self.backend, address)
 
                # Lookup blacklists
                is_blacklisted = await address.is_blacklisted()
 
                self.render("location/lookup.html",
-                       address=address, is_blacklisted=is_blacklisted, peer=peer)
+                       address=address, is_blacklisted=is_blacklisted)
 
 
 class BlacklistsHandler(base.BaseHandler):
        async def get(self, address):
-               peer = self.geoip.get_all(address)
-
                # Lookup address
-               address = self.geoip.lookup(address)
+               address = util.Address(self.backend, address)
 
                # Lookup blacklists
                blacklists = await address.get_blacklists()