comparison src/plugins/plugin_misc_ip.py @ 1576:d5f59ba166fe

plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 18:19:49 +0100
parents ec3848916ee8
children a9e86f660653
comparison
equal deleted inserted replaced
1575:833bdb227b16 1576:d5f59ba166fe
185 def getLocalIPs(self, profile): 185 def getLocalIPs(self, profile):
186 """Try do discover local area network IPs 186 """Try do discover local area network IPs
187 187
188 @param profile): %(doc_profile)s 188 @param profile): %(doc_profile)s
189 @return (deferred): list of lan IP addresses 189 @return (deferred): list of lan IP addresses
190 or empty list if it can't be discovered
191 if there are several addresses, the one used with the server is put first 190 if there are several addresses, the one used with the server is put first
191 if no address is found, localhost IP will be in the list
192 """ 192 """
193 # TODO: manage permission requesting (e.g. for UMTS link) 193 # TODO: manage permission requesting (e.g. for UMTS link)
194 if self._local_ip_cache is not None: 194 if self._local_ip_cache is not None:
195 defer.returnValue(self._local_ip_cache) 195 defer.returnValue(self._local_ip_cache)
196 client = self.host.getClient(profile) 196 client = self.host.getClient(profile)
197 addresses = [] 197 addresses = []
198 localhost = ['127.0.0.1']
198 199
199 # we first try our luck with netifaces 200 # we first try our luck with netifaces
200 if netifaces is not None: 201 if netifaces is not None:
201 addresses = [] 202 addresses = []
202 for interface in netifaces.interfaces(): 203 for interface in netifaces.interfaces():
208 for data in inet_list: 209 for data in inet_list:
209 addresse = data['addr'] 210 addresse = data['addr']
210 if self._filterAddresse(addresse): 211 if self._filterAddresse(addresse):
211 addresses.append(addresse) 212 addresses.append(addresse)
212 213
213 # we first try with our connection to server 214 # then we use our connection to server
214 ip = client.xmlstream.transport.getHost().host 215 ip = client.xmlstream.transport.getHost().host
215 if self._filterAddresse(ip): 216 if self._filterAddresse(ip):
216 self._insertFirst(addresses, ip) 217 self._insertFirst(addresses, ip)
217 defer.returnValue(addresses) 218 defer.returnValue(addresses)
218 219
219 # if not available, we try with NAT-Port 220 # if server is local, we try with NAT-Port
220 if self._nat is not None: 221 if self._nat is not None:
221 nat_ip = yield self._nat.getIP(local=True) 222 nat_ip = yield self._nat.getIP(local=True)
222 if nat_ip is not None: 223 if nat_ip is not None:
223 self._insertFirst(addresses, nat_ip) 224 self._insertFirst(addresses, nat_ip)
224 defer.returnValue(addresses) 225 defer.returnValue(addresses)
225 226
226 if addresses: 227 if addresses:
227 defer.returnValue(addresses) 228 defer.returnValue(addresses)
228 229
229 # still not luck, we need to contact external website 230 # still not luck, we need to contact external website
230 allow_get_ip = yield self._externalAllowed(profile) 231 allow_get_ip = yield self._externalAllowed(profile)
231 232
232 if not allow_get_ip: 233 if not allow_get_ip:
233 defer.returnValue(addresses) 234 defer.returnValue(addresses or localhost)
234 235
235 try: 236 try:
236 ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) 237 ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE)
237 except (internet_error.DNSLookupError, internet_error.TimeoutError): 238 except (internet_error.DNSLookupError, internet_error.TimeoutError):
238 log.warning(u"Can't access Domain Name System") 239 log.warning(u"Can't access Domain Name System")
239 defer.returnValue(addresses) 240 defer.returnValue(addresses or localhost)
240 self._insertFirst(addresses, ip_tuple.local) 241 self._insertFirst(addresses, ip_tuple.local)
241 defer.returnValue(addresses) 242 defer.returnValue(addresses)
242 243
243 @defer.inlineCallbacks 244 @defer.inlineCallbacks
244 def getExternalIP(self, profile): 245 def getExternalIP(self, profile):