Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_misc_ip.py @ 4161:2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
add `cascade=delete-orphan` to History's `messages`, `subjects` and `thread`, to make
modification of those attributes easier.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 28 Nov 2023 17:35:20 +0100 |
parents | 4b842c1fb686 |
children | b86912d3fd33 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1534
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 |
3479 | 5 # Copyright (C) 2009-2021 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 |
3028 | 20 import urllib.parse |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
21 from libervia.backend.core.i18n import _, D_ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.tools import xml_tools |
3028 | 25 from wokkel import disco, iwokkel |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
26 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
|
27 from twisted.web import error as web_error |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
28 from twisted.internet import defer |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
29 from twisted.internet import reactor |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
30 from twisted.internet import protocol |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
31 from twisted.internet import endpoints |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
32 from twisted.internet import error as internet_error |
3028 | 33 from zope.interface import implementer |
1566
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 |
3028 | 36 |
37 log = getLogger(__name__) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
39 try: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
40 import netifaces |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
41 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 log.warning( |
3028 | 43 "netifaces is not available, it help discovering IPs, you can install it on https://pypi.python.org/pypi/netifaces" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 ) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
45 netifaces = None |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
49 C.PI_NAME: "IP discovery", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
50 C.PI_IMPORT_NAME: "IP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
51 C.PI_TYPE: C.PLUG_TYPE_MISC, |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
52 C.PI_MODES: C.PLUG_MODE_BOTH, |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
53 C.PI_PROTOCOLS: ["XEP-0279"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
54 C.PI_RECOMMENDATIONS: ["NAT-PORT"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
55 C.PI_MAIN: "IPPlugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
56 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 C.PI_DESCRIPTION: _("""This plugin help to discover our external IP address."""), |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 } |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
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
|
60 # TODO: GET_IP_PAGE should be configurable in sat.conf |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 GET_IP_PAGE = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 "http://salut-a-toi.org/whereami/" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 ) # This page must only return external IP of the requester |
3028 | 64 GET_IP_LABEL = D_("Allow external get IP") |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 GET_IP_CATEGORY = "General" |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 GET_IP_NAME = "allow_get_ip" |
3028 | 67 GET_IP_CONFIRM_TITLE = D_("Confirm external site request") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 GET_IP_CONFIRM = D_( |
3028 | 69 """To facilitate data transfer, we need to contact a website. |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 A request will be done on {page} |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 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
|
72 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 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
|
74 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 Do you agree to do this request ? |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 """ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ).format( |
3028 | 78 page=GET_IP_PAGE, domain=urllib.parse.urlparse(GET_IP_PAGE).netloc, app_name=C.APP_NAME |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 ) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
80 NS_IP_CHECK = "urn:xmpp:sic:1" |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 PARAMS = """ |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 <params> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 <general> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 <category name="{category}"> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 <param name="{name}" label="{label}" type="bool" /> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 </category> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 </general> |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 </params> |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 """.format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 category=GET_IP_CATEGORY, name=GET_IP_NAME, label=GET_IP_LABEL |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 ) |
1534
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 |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 class IPPlugin(object): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
96 # TODO: refresh IP if a new connection is detected |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
97 # 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
|
98 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 def __init__(self, host): |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 log.info(_("plugin IP discovery initialization")) |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
102 host.memory.update_params(PARAMS) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
104 # NAT-Port |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
105 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 self._nat = host.plugins["NAT-PORT"] |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
107 except KeyError: |
3028 | 108 log.debug("NAT port plugin not available") |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
109 self._nat = None |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
110 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
111 # XXX: cache is kept until SàT is restarted |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
112 # if IP may have changed, use self.refresh_ip |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
113 self._external_ip_cache = None |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
114 self._local_ip_cache = None |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
115 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
116 def get_handler(self, client): |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
117 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
|
118 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
119 def refresh_ip(self): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
120 # FIXME: use a trigger instead ? |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
121 self._external_ip_cache = None |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
122 self._local_ip_cache = None |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
123 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
124 def _external_allowed(self, client): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
125 """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
|
126 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
127 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
|
128 @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
|
129 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
130 allow_get_ip = self.host.memory.params.param_get_a( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
131 GET_IP_NAME, GET_IP_CATEGORY, use_default=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 ) |
1537
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 if allow_get_ip is None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
135 # we don't have autorisation from user yet to use get_ip, we ask him |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
136 def param_set(allowed): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
137 # FIXME: we need to use bool_const as param_set only manage str/unicode |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
138 # need to be fixed when params will be refactored |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
139 self.host.memory.param_set( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
140 GET_IP_NAME, C.bool_const(allowed), GET_IP_CATEGORY |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 ) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
142 return allowed |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
144 d = xml_tools.defer_confirm( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 self.host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 _(GET_IP_CONFIRM), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 _(GET_IP_CONFIRM_TITLE), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 profile=client.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 d.addCallback(param_set) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
151 return d |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
152 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
153 return defer.succeed(allow_get_ip) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
154 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
155 def _filter_addresse(self, ip_addr): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
156 """Filter acceptable addresses |
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 For now, just remove IPv4 local addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
159 @param ip_addr(str): IP addresse |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
160 @return (bool): True if addresse is acceptable |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
161 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 return not ip_addr.startswith("127.") |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
163 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
164 def _insert_first(self, addresses, ip_addr): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
165 """Insert ip_addr as first item in addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
166 |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
167 @param addresses(list): list of IP addresses |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
168 @param ip_addr(str): IP addresse |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
169 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
170 if ip_addr in addresses: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
171 if addresses[0] != ip_addr: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
172 addresses.remove(ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
173 addresses.insert(0, ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
174 else: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
175 addresses.insert(0, ip_addr) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
176 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
177 async def _get_ip_from_external(self, ext_url): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
178 """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
|
179 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
180 @param ext_utl(str): url to connect to |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
181 @return (str, None): return local IP, or None if it's not possible |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
182 """ |
3028 | 183 url = urllib.parse.urlparse(ext_url) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
184 port = url.port |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
185 if port is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
186 if url.scheme == "http": |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
187 port = 80 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
188 elif url.scheme == "https": |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
189 port = 443 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
190 else: |
3028 | 191 log.error("Unknown url scheme: {}".format(url.scheme)) |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
192 return None |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
193 if url.hostname is None: |
3028 | 194 log.error("Can't find url hostname for {}".format(GET_IP_PAGE)) |
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 point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
198 p = await endpoints.connectProtocol(point, protocol.Protocol()) |
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
199 local_ip = p.transport.getHost().host |
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
200 p.transport.loseConnection() |
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
201 return local_ip |
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 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
204 def get_local_i_ps(self, client): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
205 """Try do discover local area network IPs |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
206 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
207 @return (deferred): list of lan IP addresses |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
208 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
|
209 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
|
210 """ |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
211 # 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
|
212 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
|
213 defer.returnValue(self._local_ip_cache) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
214 addresses = [] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 localhost = ["127.0.0.1"] |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
216 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
217 # we first try our luck with netifaces |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
218 if netifaces is not None: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
219 addresses = [] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
220 for interface in netifaces.interfaces(): |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
221 if_addresses = netifaces.ifaddresses(interface) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
222 try: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
223 inet_list = if_addresses[netifaces.AF_INET] |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
224 except KeyError: |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
225 continue |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
226 for data in inet_list: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 addresse = data["addr"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
228 if self._filter_addresse(addresse): |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
229 addresses.append(addresse) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
230 |
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
|
231 # then we use our connection to server |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
232 ip = client.xmlstream.transport.getHost().host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
233 if self._filter_addresse(ip): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
234 self._insert_first(addresses, ip) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
235 defer.returnValue(addresses) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 |
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
|
237 # 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
|
238 if self._nat is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
239 nat_ip = yield self._nat.get_ip(local=True) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
240 if nat_ip is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
241 self._insert_first(addresses, nat_ip) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
242 defer.returnValue(addresses) |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
243 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
244 if addresses: |
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 |
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
|
247 # still not luck, we need to contact external website |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
248 allow_get_ip = yield self._external_allowed(client) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
249 |
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
|
250 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
|
251 defer.returnValue(addresses or localhost) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
252 |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
253 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
254 local_ip = yield defer.ensureDeferred(self._get_ip_from_external(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
|
255 except (internet_error.DNSLookupError, internet_error.TimeoutError): |
3028 | 256 log.warning("Can't access Domain Name System") |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
257 else: |
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
258 if local_ip is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
259 self._insert_first(addresses, local_ip) |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
260 |
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
261 defer.returnValue(addresses or localhost) |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
262 |
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
263 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
264 def get_external_ip(self, client): |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 """Try to discover external IP |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 |
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 @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
|
268 """ |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
269 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
|
270 defer.returnValue(self._external_ip_cache) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
272 # we first try with XEP-0279 |
2148
a543eda2c923
core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents:
2145
diff
changeset
|
273 ip_check = yield self.host.hasFeature(client, NS_IP_CHECK) |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
274 if ip_check: |
3028 | 275 log.debug("Server IP Check available, we use it to retrieve our IP") |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
276 iq_elt = client.IQ("get") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 iq_elt.addElement((NS_IP_CHECK, "address")) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
278 try: |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
279 result_elt = yield iq_elt.send() |
3028 | 280 address_elt = next(result_elt.elements(NS_IP_CHECK, "address")) |
281 ip_elt = next(address_elt.elements(NS_IP_CHECK, "ip")) | |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
282 except StopIteration: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
283 log.warning( |
3028 | 284 "Server returned invalid result on XEP-0279 request, we ignore it" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 ) |
1668
a9e86f660653
plugin ip: fixed ip check (XEP-0279) use
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
286 except StanzaError as e: |
3028 | 287 log.warning("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
|
288 else: |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
289 # 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
|
290 # 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
|
291 external_ip = str(ip_elt) |
3028 | 292 log.debug("External IP found: {}".format(external_ip)) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
293 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
|
294 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
|
295 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
296 # then with NAT-Port |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
297 if self._nat is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
298 nat_ip = yield self._nat.get_ip() |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
299 if nat_ip is not None: |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
300 self._external_ip_cache = nat_ip |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
301 defer.returnValue(nat_ip) |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
303 # and finally by requesting external website |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
304 allow_get_ip = yield self._external_allowed(client) |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
305 try: |
3028 | 306 ip = ((yield webclient.getPage(GET_IP_PAGE.encode('utf-8'))) |
307 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
|
308 except (internet_error.DNSLookupError, internet_error.TimeoutError): |
3028 | 309 log.warning("Can't access Domain Name System") |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
310 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
|
311 except web_error.Error as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
312 log.warning( |
3028 | 313 "Error while retrieving IP on {url}: {message}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 url=GET_IP_PAGE, message=e |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 ) |
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
|
317 ip = None |
1555
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
318 else: |
eb8aae35085b
plugin ip: local ip cache + DNS error detection
Goffi <goffi@goffi.org>
parents:
1537
diff
changeset
|
319 self._external_ip_cache = ip |
1537
6fa9e8c02c34
plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents:
1534
diff
changeset
|
320 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
|
321 |
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
322 |
3028 | 323 @implementer(iwokkel.IDisco) |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
324 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
|
325 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
326 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
327 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
|
328 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
329 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1555
diff
changeset
|
330 return [] |