comparison libervia/backend/plugins/plugin_misc_ip.py @ 4180:b86912d3fd33

plugin IP: fix use of legacy URL + coroutine use: An https:/salut-a-toi.org URL was used to retrieve external IP, but it's not valid anymore, resulting in an exception. This feature is currently disabled. Also moved several methods from legacy inline callbacks to coroutines.
author Goffi <goffi@goffi.org>
date Sat, 09 Dec 2023 14:30:54 +0100
parents 4b842c1fb686
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4179:3b95704ab777 4180:b86912d3fd33
198 p = await endpoints.connectProtocol(point, protocol.Protocol()) 198 p = await endpoints.connectProtocol(point, protocol.Protocol())
199 local_ip = p.transport.getHost().host 199 local_ip = p.transport.getHost().host
200 p.transport.loseConnection() 200 p.transport.loseConnection()
201 return local_ip 201 return local_ip
202 202
203 @defer.inlineCallbacks 203 async def get_local_ips(self, client):
204 def get_local_i_ps(self, client):
205 """Try do discover local area network IPs 204 """Try do discover local area network IPs
206 205
207 @return (deferred): list of lan IP addresses 206 @return (deferred): list of lan IP addresses
208 if there are several addresses, the one used with the server is put first 207 if there are several addresses, the one used with the server is put first
209 if no address is found, localhost IP will be in the list 208 if no address is found, localhost IP will be in the list
210 """ 209 """
211 # TODO: manage permission requesting (e.g. for UMTS link) 210 # TODO: manage permission requesting (e.g. for UMTS link)
212 if self._local_ip_cache is not None: 211 if self._local_ip_cache is not None:
213 defer.returnValue(self._local_ip_cache) 212 return self._local_ip_cache
214 addresses = [] 213 addresses = []
215 localhost = ["127.0.0.1"] 214 localhost = ["127.0.0.1"]
216 215
217 # we first try our luck with netifaces 216 # we first try our luck with netifaces
218 if netifaces is not None: 217 if netifaces is not None:
230 229
231 # then we use our connection to server 230 # then we use our connection to server
232 ip = client.xmlstream.transport.getHost().host 231 ip = client.xmlstream.transport.getHost().host
233 if self._filter_addresse(ip): 232 if self._filter_addresse(ip):
234 self._insert_first(addresses, ip) 233 self._insert_first(addresses, ip)
235 defer.returnValue(addresses) 234 return addresses
236 235
237 # if server is local, we try with NAT-Port 236 # if server is local, we try with NAT-Port
238 if self._nat is not None: 237 if self._nat is not None:
239 nat_ip = yield self._nat.get_ip(local=True) 238 nat_ip = await self._nat.get_ip(local=True)
240 if nat_ip is not None: 239 if nat_ip is not None:
241 self._insert_first(addresses, nat_ip) 240 self._insert_first(addresses, nat_ip)
242 defer.returnValue(addresses) 241 return addresses
243 242
244 if addresses: 243 if addresses:
245 defer.returnValue(addresses) 244 return addresses
246 245
247 # still not luck, we need to contact external website 246 # still not luck, we need to contact external website
248 allow_get_ip = yield self._external_allowed(client) 247 allow_get_ip = await self._external_allowed(client)
249 248
250 if not allow_get_ip: 249 if not allow_get_ip:
251 defer.returnValue(addresses or localhost) 250 return addresses or localhost
252 251
253 try: 252 try:
254 local_ip = yield defer.ensureDeferred(self._get_ip_from_external(GET_IP_PAGE)) 253 local_ip = await defer.ensureDeferred(self._get_ip_from_external(GET_IP_PAGE))
255 except (internet_error.DNSLookupError, internet_error.TimeoutError): 254 except (internet_error.DNSLookupError, internet_error.TimeoutError):
256 log.warning("Can't access Domain Name System") 255 log.warning("Can't access Domain Name System")
257 else: 256 else:
258 if local_ip is not None: 257 if local_ip is not None:
259 self._insert_first(addresses, local_ip) 258 self._insert_first(addresses, local_ip)
260 259
261 defer.returnValue(addresses or localhost) 260 return addresses or localhost
262 261
263 @defer.inlineCallbacks 262 async def get_external_ip(self, client):
264 def get_external_ip(self, client):
265 """Try to discover external IP 263 """Try to discover external IP
266 264
267 @return (deferred): external IP address or None if it can't be discovered 265 @return (deferred): external IP address or None if it can't be discovered
268 """ 266 """
269 if self._external_ip_cache is not None: 267 if self._external_ip_cache is not None:
270 defer.returnValue(self._external_ip_cache) 268 return self._external_ip_cache
271 269
272 # we first try with XEP-0279 270 # we first try with XEP-0279
273 ip_check = yield self.host.hasFeature(client, NS_IP_CHECK) 271 ip_check = await self.host.hasFeature(client, NS_IP_CHECK)
274 if ip_check: 272 if ip_check:
275 log.debug("Server IP Check available, we use it to retrieve our IP") 273 log.debug("Server IP Check available, we use it to retrieve our IP")
276 iq_elt = client.IQ("get") 274 iq_elt = client.IQ("get")
277 iq_elt.addElement((NS_IP_CHECK, "address")) 275 iq_elt.addElement((NS_IP_CHECK, "address"))
278 try: 276 try:
279 result_elt = yield iq_elt.send() 277 result_elt = await iq_elt.send()
280 address_elt = next(result_elt.elements(NS_IP_CHECK, "address")) 278 address_elt = next(result_elt.elements(NS_IP_CHECK, "address"))
281 ip_elt = next(address_elt.elements(NS_IP_CHECK, "ip")) 279 ip_elt = next(address_elt.elements(NS_IP_CHECK, "ip"))
282 except StopIteration: 280 except StopIteration:
283 log.warning( 281 log.warning(
284 "Server returned invalid result on XEP-0279 request, we ignore it" 282 "Server returned invalid result on XEP-0279 request, we ignore it"
289 # FIXME: server IP may not be the same as external IP (server can be on local machine or network) 287 # FIXME: server IP may not be the same as external IP (server can be on local machine or network)
290 # IP should be checked to see if we have a local one, and rejected in this case 288 # IP should be checked to see if we have a local one, and rejected in this case
291 external_ip = str(ip_elt) 289 external_ip = str(ip_elt)
292 log.debug("External IP found: {}".format(external_ip)) 290 log.debug("External IP found: {}".format(external_ip))
293 self._external_ip_cache = external_ip 291 self._external_ip_cache = external_ip
294 defer.returnValue(self._external_ip_cache) 292 return self._external_ip_cache
295 293
296 # then with NAT-Port 294 # then with NAT-Port
297 if self._nat is not None: 295 if self._nat is not None:
298 nat_ip = yield self._nat.get_ip() 296 nat_ip = await self._nat.get_ip()
299 if nat_ip is not None: 297 if nat_ip is not None:
300 self._external_ip_cache = nat_ip 298 self._external_ip_cache = nat_ip
301 defer.returnValue(nat_ip) 299 return nat_ip
302 300
303 # and finally by requesting external website 301 # and finally by requesting external website
304 allow_get_ip = yield self._external_allowed(client) 302 allow_get_ip = await self._external_allowed(client)
305 try: 303
306 ip = ((yield webclient.getPage(GET_IP_PAGE.encode('utf-8'))) 304 ip = None
307 if allow_get_ip else None) 305
308 except (internet_error.DNSLookupError, internet_error.TimeoutError): 306 # FIXME: following code is deprecated, check it
309 log.warning("Can't access Domain Name System") 307 # try:
310 ip = None 308 # ip = ((await webclient.getPage(GET_IP_PAGE.encode('utf-8')))
311 except web_error.Error as e: 309 # if allow_get_ip else None)
312 log.warning( 310 # except (internet_error.DNSLookupError, internet_error.TimeoutError):
313 "Error while retrieving IP on {url}: {message}".format( 311 # log.warning("Can't access Domain Name System")
314 url=GET_IP_PAGE, message=e 312 # ip = None
315 ) 313 # except web_error.Error as e:
316 ) 314 # log.warning(
317 ip = None 315 # "Error while retrieving IP on {url}: {message}".format(
318 else: 316 # url=GET_IP_PAGE, message=e
319 self._external_ip_cache = ip 317 # )
320 defer.returnValue(ip) 318 # )
319 # ip = None
320 # else:
321 # self._external_ip_cache = ip
322 return ip
321 323
322 324
323 @implementer(iwokkel.IDisco) 325 @implementer(iwokkel.IDisco)
324 class IPPlugin_handler(XMPPHandler): 326 class IPPlugin_handler(XMPPHandler):
325 327