X-Git-Url: http://git.ipfire.org/?p=ipfire.org.git;a=blobdiff_plain;f=src%2Fbackend%2Fmirrors.py;fp=src%2Fbackend%2Fmirrors.py;h=62f077656d550f6b49d6558e45bdfd5c4f6461b2;hp=5c30a0b0ff20c955cad9257185709348f6d1aeab;hb=440aba92db685306d6e7260781d084eb7365b5d4;hpb=7ccbc261b58882396794dde70e7a29fc5719055c 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