plugin ip: local ip cache + DNS error detection
This commit is contained in:
parent
649ec27b3e
commit
7ca36bc79d
1 changed files with 24 additions and 8 deletions
|
|
@ -27,6 +27,7 @@ from twisted.internet import defer
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from twisted.internet import protocol
|
from twisted.internet import protocol
|
||||||
from twisted.internet import endpoints
|
from twisted.internet import endpoints
|
||||||
|
from twisted.internet import error as internet_error
|
||||||
import urlparse
|
import urlparse
|
||||||
try:
|
try:
|
||||||
import netifaces
|
import netifaces
|
||||||
|
|
@ -92,11 +93,13 @@ class IPPlugin(object):
|
||||||
|
|
||||||
# XXX: cache is kept until SàT is restarted
|
# XXX: cache is kept until SàT is restarted
|
||||||
# if IP may have changed, use self.refreshIP
|
# 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):
|
def refreshIP(self):
|
||||||
# FIXME: use a trigger instead ?
|
# FIXME: use a trigger instead ?
|
||||||
self._ip_cache = None
|
self._external_ip_cache = None
|
||||||
|
self._local_ip_cache = None
|
||||||
|
|
||||||
def _externalAllowed(self, profile):
|
def _externalAllowed(self, profile):
|
||||||
"""Return value of parameter with autorisation of user to do external requests
|
"""Return value of parameter with autorisation of user to do external requests
|
||||||
|
|
@ -112,7 +115,7 @@ class IPPlugin(object):
|
||||||
def setParam(allowed):
|
def setParam(allowed):
|
||||||
# FIXME: we need to use boolConst as setParam only manage str/unicode
|
# FIXME: we need to use boolConst as setParam only manage str/unicode
|
||||||
# need to be fixed when params will be refactored
|
# 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
|
return allowed
|
||||||
d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile)
|
d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile)
|
||||||
d.addCallback(setParam)
|
d.addCallback(setParam)
|
||||||
|
|
@ -146,7 +149,7 @@ class IPPlugin(object):
|
||||||
"""Get local IP by doing a connection on an external url
|
"""Get local IP by doing a connection on an external url
|
||||||
|
|
||||||
@param ext_utl(str): url to connect to
|
@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)
|
url = urlparse.urlparse(ext_url)
|
||||||
port = url.port
|
port = url.port
|
||||||
|
|
@ -182,6 +185,8 @@ class IPPlugin(object):
|
||||||
if there are several addresses, the one used with the server is put first
|
if there are several addresses, the one used with the server is put first
|
||||||
"""
|
"""
|
||||||
# TODO: manage permission requesting (e.g. for UMTS link)
|
# 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)
|
client = self.host.getClient(profile)
|
||||||
addresses = []
|
addresses = []
|
||||||
|
|
||||||
|
|
@ -221,7 +226,11 @@ class IPPlugin(object):
|
||||||
if not allow_get_ip:
|
if not allow_get_ip:
|
||||||
defer.returnValue(addresses)
|
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)
|
self._insertFirst(addresses, ip_tuple.local)
|
||||||
defer.returnValue(addresses)
|
defer.returnValue(addresses)
|
||||||
|
|
||||||
|
|
@ -233,16 +242,23 @@ class IPPlugin(object):
|
||||||
@param profile: %(doc_profile)s
|
@param profile: %(doc_profile)s
|
||||||
@return (deferred): external IP address or None if it can't be discovered
|
@return (deferred): external IP address or None if it can't be discovered
|
||||||
"""
|
"""
|
||||||
if self._ip_cache is not None:
|
if self._external_ip_cache is not None:
|
||||||
defer.returnValue(self._ip_cache)
|
defer.returnValue(self._external_ip_cache)
|
||||||
|
|
||||||
# we first try with NAT-Port
|
# we first try with NAT-Port
|
||||||
if self._nat is not None:
|
if self._nat is not None:
|
||||||
nat_ip = yield self._nat.getIP()
|
nat_ip = yield self._nat.getIP()
|
||||||
if nat_ip is not None:
|
if nat_ip is not None:
|
||||||
|
self._external_ip_cache = nat_ip
|
||||||
defer.returnValue(nat_ip)
|
defer.returnValue(nat_ip)
|
||||||
|
|
||||||
# then by requesting external website
|
# then by requesting external website
|
||||||
allow_get_ip = yield self._externalAllowed(profile)
|
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)
|
defer.returnValue(ip)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue