From 440aba92db685306d6e7260781d084eb7365b5d4 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 19 May 2020 15:31:14 +0000 Subject: [PATCH] Migrate to libloc Signed-off-by: Michael Tremer --- Makefile.am | 2 +- src/backend/base.py | 9 +- src/backend/fireinfo.py | 34 ++--- src/backend/geoip.py | 237 ----------------------------- src/backend/mirrors.py | 77 ++++------ src/backend/misc.py | 8 - src/backend/resolver.py | 41 +++++ src/backend/util.py | 157 +++++++++++++++++++ src/templates/location/index.html | 8 +- src/templates/location/lookup.html | 28 +--- src/templates/mirrors/mirror.html | 12 +- src/web/__init__.py | 29 ++-- src/web/base.py | 22 +-- src/web/donate.py | 7 +- src/web/fireinfo.py | 8 +- src/web/location.py | 16 +- 16 files changed, 299 insertions(+), 396 deletions(-) delete mode 100644 src/backend/geoip.py create mode 100644 src/backend/resolver.py diff --git a/Makefile.am b/Makefile.am index f9657875..c88e0cfe 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/backend/base.py b/src/backend/base.py index 0fdf2c43..6ee041dc 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -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) diff --git a/src/backend/fireinfo.py b/src/backend/fireinfo.py index e5099599..d02d774f 100644 --- a/src/backend/fireinfo.py +++ b/src/backend/fireinfo.py @@ -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 index dffafa4f..00000000 --- a/src/backend/geoip.py +++ /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 diff --git a/src/backend/mirrors.py b/src/backend/mirrors.py index 5c30a0b0..62f07765 100644 --- a/src/backend/mirrors.py +++ b/src/backend/mirrors.py @@ -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 diff --git a/src/backend/misc.py b/src/backend/misc.py index c4b04807..f2f2e753 100644 --- a/src/backend/misc.py +++ b/src/backend/misc.py @@ -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 index 00000000..2b37219f --- /dev/null +++ b/src/backend/resolver.py @@ -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 diff --git a/src/backend/util.py b/src/backend/util.py index 04bde507..99a9868a 100644 --- a/src/backend/util.py +++ b/src/backend/util.py @@ -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") diff --git a/src/templates/location/index.html b/src/templates/location/index.html index 55e35e10..97e8399e 100644 --- a/src/templates/location/index.html +++ b/src/templates/location/index.html @@ -13,7 +13,7 @@
-

2001:678:b28::

+

{{ address }}

@@ -23,10 +23,12 @@
{{ _("Country") }}
-
Germany
+
+ {{ format_country_name(address.country_code) if address.country_code else _("N/A") }} +
{{ _("Network") }}
-
AS204867 - Lightning Wire Labs GmbH
+
{{ address.asn or _("Unknown") }}
diff --git a/src/templates/location/lookup.html b/src/templates/location/lookup.html index 07d5ebfd..5a7c5b16 100644 --- a/src/templates/location/lookup.html +++ b/src/templates/location/lookup.html @@ -18,30 +18,18 @@ {% end %}
- {% if peer and peer.latitude and peer.longitude %} -
- {% module Map(peer.latitude, peer.longitude) %} -
- {% end %} -
-
{{ _("Country") }}
-
- {% if peer %} - {% if peer.country_name %} - {{ peer.country_name }} ({{ peer.country }}) - {% else %} - {{ peer.country_name }} - {% end %} - {% else %} - {{ _("Unkown") }} - {% end %} -
+ {% if address.country_code %} +
{{ _("Country") }}
+
+ {{ format_country_name(address.country_code) }} +
+ {% end %} - {% if peer and peer.asn %} + {% if address.asn %}
{{ _("Autonomous System") }}
-
{{ peer.asn }}
+
{{ format_asn(address.asn) }}
{% end %}
diff --git a/src/templates/mirrors/mirror.html b/src/templates/mirrors/mirror.html index 758d0be7..cf9e2ace 100644 --- a/src/templates/mirrors/mirror.html +++ b/src/templates/mirrors/mirror.html @@ -16,21 +16,19 @@
- {% if mirror.latitude and mirror.longitude %} -
- {% module Map(mirror.latitude, mirror.longitude) %} -
- {% end %} + {#
+ {% module Map() %} +
#}
{% if mirror.asn %}
{{ _("Autonomous System") }}
-
{{ mirror.asn }}
+
{{ format_asn(mirror.asn) }}
{% end %}
{{ _("Country") }}
-
{{ mirror.country_name }}
+
{{ mirror.country.name }}
diff --git a/src/web/__init__.py b/src/web/__init__.py index 4821c438..eec28f2d 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -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) diff --git a/src/web/base.py b/src/web/base.py index e000ca33..6b25ded1 100644 --- a/src/web/base.py +++ b/src/web/base.py @@ -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 diff --git a/src/web/donate.py b/src/web/donate.py index 1e31865d..26b0ad6b 100644 --- a/src/web/donate.py +++ b/src/web/donate.py @@ -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) diff --git a/src/web/fireinfo.py b/src/web/fireinfo.py index 4fa5b968..2662ed46 100644 --- a/src/web/fireinfo.py +++ b/src/web/fireinfo.py @@ -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) diff --git a/src/web/location.py b/src/web/location.py index 5807542a..9d874e3f 100644 --- a/src/web/location.py +++ b/src/web/location.py @@ -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() -- 2.39.2