Mercurial > libervia-backend
annotate src/plugins/plugin_misc_ip.py @ 2138:6e509ee853a8
plugin OTR, core; use of new sendMessage + OTR mini refactoring:
- new client.sendMessage method is used instead of sendMessageToStream
- client.feedback is used in OTR
- OTR now add message processing hints and carbon private element as recommanded by XEP-0364. Explicit Message Encryption is still TODO
- OTR use the new sendMessageFinish trigger, this has a number of advantages:
* there is little risk that OTR is skipped by other plugins (they have to use client.sendMessage as recommanded)
* being at the end of the chain, OTR can check and remove any HTML or other leaking elements
* OTR doesn't have to skip other plugins anymore, this means that things like delivery receipts are now working with OTR
(but because there is not full stanza encryption, they can leak metadata)
* OTR can decide to follow storage hint by letting or deleting "history" key
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 05 Feb 2017 15:00:01 +0100 |
parents | a74047906dfd |
children | 1d3f73e065e1 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for IP address discovery |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _, D_ |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 log = getLogger(__name__) |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.tools import xml_tools |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
25 from twisted.web import client as webclient |
1953
f6d560cabeee
plugin misc ip: fixed exception when a web error happen while retrieving IP from external website
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
26 from twisted.web import error as web_error |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
27 from twisted.internet import defer |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
28 from twisted.internet import reactor |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
29 from twisted.internet import protocol |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
30 from twisted.internet import endpoints |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
31 from twisted.internet import error as internet_error |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
32 from zope.interface import implements |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
33 from wokkel import disco, iwokkel |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
34 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
35 from twisted.words.protocols.jabber.error import StanzaError |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 import urlparse |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
37 try: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
38 import netifaces |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
39 except ImportError: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
40 log.warning(u"netifaces is not available, it help discovering IPs, you can install it on https://pypi.python.org/pypi/netifaces") |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
41 netifaces = None |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 PLUGIN_INFO = { |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 "name": "IP discovery", |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 "import_name": "IP", |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 "type": C.PLUG_TYPE_MISC, |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
48 "protocols": ["XEP-0279"], |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
49 "recommendations": ["NAT-PORT"], |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 "main": "IPPlugin", |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
51 "handler": "yes", |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 "description": _("""This plugin help to discover our external IP address.""") |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 } |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
1953
f6d560cabeee
plugin misc ip: fixed exception when a web error happen while retrieving IP from external website
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
55 # TODO: GET_IP_PAGE should be configurable in sat.conf |
1954
a74047906dfd
plugin misc ip: changed URL of GET_IP_PAGE for the new one on salut-a-toi.org
Goffi <goffi@goffi.org>
parents:
1953
diff
changeset
|
56 GET_IP_PAGE = "http://salut-a-toi.org/whereami/" # This page must only return external IP of the requester |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 GET_IP_LABEL = D_(u"Allow external get IP") |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 GET_IP_CATEGORY = "General" |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 GET_IP_NAME = "allow_get_ip" |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 GET_IP_CONFIRM_TITLE = D_(u"Confirm external site request") |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 GET_IP_CONFIRM = D_(u"""To facilitate data transfer, we need to contact a website. |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 A request will be done on {page} |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 That means that administrators of {domain} can know that you use "{app_name}" and your IP Address. |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 IP address is an identifier to locate you on Internet (similar to a phone number). |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 Do you agree to do this request ? |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 """).format( |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 page = GET_IP_PAGE, |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 domain = urlparse.urlparse(GET_IP_PAGE).netloc, |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 app_name = C.APP_NAME) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
72 NS_IP_CHECK = "urn:xmpp:sic:1" |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 PARAMS = """ |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 <params> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 <general> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 <category name="{category}"> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 <param name="{name}" label="{label}" type="bool" /> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 </category> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 </general> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 </params> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 """.format(category=GET_IP_CATEGORY, name=GET_IP_NAME, label=GET_IP_LABEL) |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
84 |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 class IPPlugin(object): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
86 # TODO: refresh IP if a new connection is detected |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
87 # TODO: manage IPv6 when implemented in SàT |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 def __init__(self, host): |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 log.info(_("plugin IP discovery initialization")) |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 self.host = host |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 host.memory.updateParams(PARAMS) |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
94 # NAT-Port |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
95 try: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
96 self._nat = host.plugins['NAT-PORT'] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
97 except KeyError: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
98 log.debug(u"NAT port plugin not available") |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
99 self._nat = None |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
100 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
101 # XXX: cache is kept until SàT is restarted |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
102 # if IP may have changed, use self.refreshIP |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
103 self._external_ip_cache = None |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
104 self._local_ip_cache = None |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
105 |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
106 def getHandler(self, profile): |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
107 return IPPlugin_handler() |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
108 |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
109 def refreshIP(self): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
110 # FIXME: use a trigger instead ? |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
111 self._external_ip_cache = None |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
112 self._local_ip_cache = None |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
113 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
114 def _externalAllowed(self, profile): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
115 """Return value of parameter with autorisation of user to do external requests |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
116 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
117 if parameter is not set, a dialog is shown to use to get its confirmation, and parameted is set according to answer |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
118 @param profile: %(doc_profile)s |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
119 @return (defer.Deferred[bool]): True if external request is autorised |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
120 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
121 allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
122 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
123 if allow_get_ip is None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
124 # we don't have autorisation from user yet to use get_ip, we ask him |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
125 def setParam(allowed): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
126 # FIXME: we need to use boolConst as setParam only manage str/unicode |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
127 # need to be fixed when params will be refactored |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
128 self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
129 return allowed |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
130 d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
131 d.addCallback(setParam) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
132 return d |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
133 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
134 return defer.succeed(allow_get_ip) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
135 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
136 def _filterAddresse(self, ip_addr): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
137 """Filter acceptable addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
138 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
139 For now, just remove IPv4 local addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
140 @param ip_addr(str): IP addresse |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
141 @return (bool): True if addresse is acceptable |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
142 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
143 return not ip_addr.startswith('127.') |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
144 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
145 def _insertFirst(self, addresses, ip_addr): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
146 """Insert ip_addr as first item in addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
147 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
148 @param ip_addr(str): IP addresse |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
149 @param addresses(list): list of IP addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
150 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
151 if ip_addr in addresses: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
152 if addresses[0] != ip_addr: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
153 addresses.remove(ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
154 addresses.insert(0, ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
155 else: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
156 addresses.insert(0, ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
157 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
158 def _getIPFromExternal(self, ext_url): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
159 """Get local IP by doing a connection on an external url |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
160 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
161 @param ext_utl(str): url to connect to |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
162 @return (D(str)): return local IP |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
163 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
164 url = urlparse.urlparse(ext_url) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
165 port = url.port |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
166 if port is None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
167 if url.scheme=='http': |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
168 port = 80 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
169 elif url.scheme=='https': |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
170 port = 443 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
171 else: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
172 log.error(u"Unknown url scheme: {}".format(url.scheme)) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
173 defer.returnValue(None) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
174 if url.hostname is None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
175 log.error(u"Can't find url hostname for {}".format(GET_IP_PAGE)) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
176 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
177 point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
178 def gotConnection(p): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
179 local_ip = p.transport.getHost().host |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
180 p.transport.loseConnection() |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
181 return local_ip |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
182 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
183 d = endpoints.connectProtocol(point, protocol.Protocol()) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
184 d.addCallback(gotConnection) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
185 return d |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
186 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
187 @defer.inlineCallbacks |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
188 def getLocalIPs(self, profile): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
189 """Try do discover local area network IPs |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
190 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
191 @param profile): %(doc_profile)s |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
192 @return (deferred): list of lan IP addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
193 if there are several addresses, the one used with the server is put first |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
194 if no address is found, localhost IP will be in the list |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
195 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
196 # TODO: manage permission requesting (e.g. for UMTS link) |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
197 if self._local_ip_cache is not None: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
198 defer.returnValue(self._local_ip_cache) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 client = self.host.getClient(profile) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
200 addresses = [] |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
201 localhost = ['127.0.0.1'] |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
202 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
203 # we first try our luck with netifaces |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
204 if netifaces is not None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
205 addresses = [] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
206 for interface in netifaces.interfaces(): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
207 if_addresses = netifaces.ifaddresses(interface) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
208 try: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
209 inet_list = if_addresses[netifaces.AF_INET] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
210 except KeyError: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
211 continue |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
212 for data in inet_list: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
213 addresse = data['addr'] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
214 if self._filterAddresse(addresse): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
215 addresses.append(addresse) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
216 |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
217 # then we use our connection to server |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
218 ip = client.xmlstream.transport.getHost().host |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
219 if self._filterAddresse(ip): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
220 self._insertFirst(addresses, ip) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
221 defer.returnValue(addresses) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
223 # if server is local, we try with NAT-Port |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
224 if self._nat is not None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
225 nat_ip = yield self._nat.getIP(local=True) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
226 if nat_ip is not None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
227 self._insertFirst(addresses, nat_ip) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
228 defer.returnValue(addresses) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
229 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
230 if addresses: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
231 defer.returnValue(addresses) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
232 |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
233 # still not luck, we need to contact external website |
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
234 allow_get_ip = yield self._externalAllowed(profile) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
235 |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
236 if not allow_get_ip: |
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
237 defer.returnValue(addresses or localhost) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
238 |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
239 try: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
240 ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
241 except (internet_error.DNSLookupError, internet_error.TimeoutError): |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
242 log.warning(u"Can't access Domain Name System") |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1566
diff
changeset
|
243 defer.returnValue(addresses or localhost) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
244 self._insertFirst(addresses, ip_tuple.local) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
245 defer.returnValue(addresses) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
246 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
247 @defer.inlineCallbacks |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
248 def getExternalIP(self, profile): |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 """Try to discover external IP |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 @param profile: %(doc_profile)s |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 @return (deferred): external IP address or None if it can't be discovered |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 """ |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
254 if self._external_ip_cache is not None: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
255 defer.returnValue(self._external_ip_cache) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
257 # we first try with XEP-0279 |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
258 ip_check = yield self.host.hasFeature(NS_IP_CHECK, profile=profile) |
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
259 if ip_check: |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
260 log.debug(u"Server IP Check available, we use it to retrieve our IP") |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
261 client = self.host.getClient(profile) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
262 iq_elt = client.IQ("get") |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
263 iq_elt.addElement((NS_IP_CHECK, 'address')) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
264 try: |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
265 result_elt = yield iq_elt.send() |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
266 address_elt = result_elt.elements(NS_IP_CHECK, 'address').next() |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
267 ip_elt = address_elt.elements(NS_IP_CHECK,'ip').next() |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
268 except StopIteration: |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
269 log.warning(u"Server returned invalid result on XEP-0279 request, we ignore it") |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
270 except StanzaError as e: |
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
271 log.warning(u"error while requesting ip to server: {}".format(e)) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
272 else: |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
273 # FIXME: server IP may not be the same as external IP (server can be on local machine or network) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
274 # IP should be checked to see if we have a local one, and rejected in this case |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
275 external_ip = str(ip_elt) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
276 log.debug(u"External IP found: {}".format(external_ip)) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
277 self._external_ip_cache = external_ip |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
278 defer.returnValue(self._external_ip_cache) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
279 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
280 # then with NAT-Port |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
281 if self._nat is not None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
282 nat_ip = yield self._nat.getIP() |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
283 if nat_ip is not None: |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
284 self._external_ip_cache = nat_ip |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
285 defer.returnValue(nat_ip) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
287 # and finally by requesting external website |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
288 allow_get_ip = yield self._externalAllowed(profile) |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
289 try: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
290 ip = (yield webclient.getPage(GET_IP_PAGE)) if allow_get_ip else None |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
291 except (internet_error.DNSLookupError, internet_error.TimeoutError): |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
292 log.warning(u"Can't access Domain Name System") |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
293 ip = None |
1953
f6d560cabeee
plugin misc ip: fixed exception when a web error happen while retrieving IP from external website
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
294 except web_error.Error as e: |
f6d560cabeee
plugin misc ip: fixed exception when a web error happen while retrieving IP from external website
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
295 log.warning(u"Error while retrieving IP on {url}: {message}".format(url=GET_IP_PAGE, message=e)) |
f6d560cabeee
plugin misc ip: fixed exception when a web error happen while retrieving IP from external website
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
296 ip = None |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
297 else: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
298 self._external_ip_cache = ip |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
299 defer.returnValue(ip) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
300 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
301 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
302 class IPPlugin_handler(XMPPHandler): |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
303 implements(iwokkel.IDisco) |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
304 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
305 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
306 return [disco.DiscoFeature(NS_IP_CHECK)] |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
307 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
308 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
309 return [] |