Mercurial > libervia-backend
diff src/plugins/plugin_misc_ip.py @ 1555:eb8aae35085b
plugin ip: local ip cache + DNS error detection
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Nov 2015 22:02:41 +0100 |
parents | 6fa9e8c02c34 |
children | ec3848916ee8 |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_ip.py Mon Nov 02 22:02:41 2015 +0100 +++ b/src/plugins/plugin_misc_ip.py Mon Nov 02 22:02:41 2015 +0100 @@ -27,6 +27,7 @@ from twisted.internet import reactor from twisted.internet import protocol from twisted.internet import endpoints +from twisted.internet import error as internet_error import urlparse try: import netifaces @@ -92,11 +93,13 @@ # XXX: cache is kept until SàT is restarted # if IP may have changed, use self.refreshIP - self._ip_cache = None + self._external_ip_cache = None + self._local_ip_cache = None def refreshIP(self): # FIXME: use a trigger instead ? - self._ip_cache = None + self._external_ip_cache = None + self._local_ip_cache = None def _externalAllowed(self, profile): """Return value of parameter with autorisation of user to do external requests @@ -112,7 +115,7 @@ def setParam(allowed): # FIXME: we need to use boolConst as setParam only manage str/unicode # need to be fixed when params will be refactored - self.host.memory.setParam(GET_IP_NAME, C.boolConst(allow_get_ip), GET_IP_CATEGORY) + self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY) return allowed d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile) d.addCallback(setParam) @@ -146,7 +149,7 @@ """Get local IP by doing a connection on an external url @param ext_utl(str): url to connect to - @return (defer.Deferred): return local IP + @return (D(str)): return local IP """ url = urlparse.urlparse(ext_url) port = url.port @@ -182,6 +185,8 @@ if there are several addresses, the one used with the server is put first """ # TODO: manage permission requesting (e.g. for UMTS link) + if self._local_ip_cache is not None: + defer.returnValue(self._local_ip_cache) client = self.host.getClient(profile) addresses = [] @@ -221,7 +226,11 @@ if not allow_get_ip: defer.returnValue(addresses) - ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) + try: + ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) + except Exception as internet_error.DNSLookupError: + log.warning(u"Can't access Domain Name System") + defer.returnValue(addresses) self._insertFirst(addresses, ip_tuple.local) defer.returnValue(addresses) @@ -233,16 +242,23 @@ @param profile: %(doc_profile)s @return (deferred): external IP address or None if it can't be discovered """ - if self._ip_cache is not None: - defer.returnValue(self._ip_cache) + if self._external_ip_cache is not None: + defer.returnValue(self._external_ip_cache) # we first try with NAT-Port if self._nat is not None: nat_ip = yield self._nat.getIP() if nat_ip is not None: + self._external_ip_cache = nat_ip defer.returnValue(nat_ip) # then by requesting external website allow_get_ip = yield self._externalAllowed(profile) - ip = webclient.getPage(GET_IP_PAGE) if allow_get_ip else None + try: + ip = (yield webclient.getPage(GET_IP_PAGE)) if allow_get_ip else None + except internet_error.DNSLookupError: + log.warning(u"Can't access Domain Name System") + ip = None + else: + self._external_ip_cache = ip defer.returnValue(ip)