annotate src/plugins/plugin_misc_ip.py @ 1537:6fa9e8c02c34

plugin IP discovery: better IP discovering: - locals and external IP addresses can be discovered - if netifaces is available, it is used - same thing for NAT Port plugin - cache is no more managed per profile but globally, a signal or observer will be used in the future to detect connection changes
author Goffi <goffi@goffi.org>
date Tue, 29 Sep 2015 17:54:25 +0200
parents a5e0393a06cd
children eb8aae35085b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
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
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
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
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
26 from twisted.internet import defer
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
27 from twisted.internet import reactor
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
28 from twisted.internet import protocol
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
29 from twisted.internet import endpoints
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import urlparse
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
31 try:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
32 import netifaces
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
33 except ImportError:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
34 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
35 netifaces = None
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 PLUGIN_INFO = {
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "name": "IP discovery",
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "import_name": "IP",
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "type": C.PLUG_TYPE_MISC,
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
42 "recommendations": ["NAT-PORT"],
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "main": "IPPlugin",
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "handler": "no",
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "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
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 GET_IP_PAGE = "http://www.goffi.org/sat_tools/get_ip.php" # This page must only return external IP of the requester
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 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
50 GET_IP_CATEGORY = "General"
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 GET_IP_NAME = "allow_get_ip"
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 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
53 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
54 A request will be done on {page}
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 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
56
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 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
58
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 Do you agree to do this request ?
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """).format(
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 page = GET_IP_PAGE,
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 domain = urlparse.urlparse(GET_IP_PAGE).netloc,
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 app_name = C.APP_NAME)
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 PARAMS = """
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 <params>
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 <general>
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 <category name="{category}">
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 <param name="{name}" label="{label}" type="bool" />
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 </category>
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 </general>
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 </params>
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """.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
74
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
75
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 class IPPlugin(object):
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
77 # TODO: refresh IP if a new connection is detected
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
78 # TODO: manage IPv6 when implemented in SàT
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
79 # TODO: implement XEP-0279
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def __init__(self, host):
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 log.info(_("plugin IP discovery initialization"))
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.host = host
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 host.memory.updateParams(PARAMS)
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
86 # NAT-Port
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
87 try:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
88 self._nat = host.plugins['NAT-PORT']
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
89 except KeyError:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
90 log.debug(u"NAT port plugin not available")
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
91 self._nat = None
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
92
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
93 # XXX: cache is kept until SàT is restarted
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
94 # if IP may have changed, use self.refreshIP
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
95 self._ip_cache = None
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
96
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
97 def refreshIP(self):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
98 # FIXME: use a trigger instead ?
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
99 self._ip_cache = 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 def _externalAllowed(self, profile):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
102 """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
103
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
104 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
105 @param profile: %(doc_profile)s
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
106 @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
107 """
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
108 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
109
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
110 if allow_get_ip is None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
111 # 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
112 def setParam(allowed):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
113 # 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
114 # need to be fixed when params will be refactored
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
115 self.host.memory.setParam(GET_IP_NAME, C.boolConst(allow_get_ip), GET_IP_CATEGORY)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
116 return allowed
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
117 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
118 d.addCallback(setParam)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
119 return d
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 return defer.succeed(allow_get_ip)
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 def _filterAddresse(self, ip_addr):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
124 """Filter acceptable addresses
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
125
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
126 For now, just remove IPv4 local addresses
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
127 @param ip_addr(str): IP addresse
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
128 @return (bool): True if addresse is acceptable
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
129 """
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
130 return not ip_addr.startswith('127.')
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
131
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
132 def _insertFirst(self, addresses, ip_addr):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
133 """Insert ip_addr as first item in addresses
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
134
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
135 @param ip_addr(str): IP addresse
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
136 @param addresses(list): list of IP addresses
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
137 """
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
138 if ip_addr in addresses:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
139 if addresses[0] != ip_addr:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
140 addresses.remove(ip_addr)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
141 addresses.insert(0, ip_addr)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
142 else:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
143 addresses.insert(0, ip_addr)
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 _getIPFromExternal(self, ext_url):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
146 """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
147
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
148 @param ext_utl(str): url to connect to
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
149 @return (defer.Deferred): return local IP
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 url = urlparse.urlparse(ext_url)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
152 port = url.port
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
153 if port is None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
154 if url.scheme=='http':
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
155 port = 80
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
156 elif url.scheme=='https':
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
157 port = 443
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
158 else:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
159 log.error(u"Unknown url scheme: {}".format(url.scheme))
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
160 defer.returnValue(None)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
161 if url.hostname is None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
162 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
163
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
164 point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
165 def gotConnection(p):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
166 local_ip = p.transport.getHost().host
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
167 p.transport.loseConnection()
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
168 return local_ip
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 d = endpoints.connectProtocol(point, protocol.Protocol())
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
171 d.addCallback(gotConnection)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
172 return d
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
173
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
174
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
175 @defer.inlineCallbacks
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
176 def getLocalIPs(self, profile):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
177 """Try do discover local area network IPs
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
178
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
179 @param profile): %(doc_profile)s
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
180 @return (deferred): list of lan IP addresses
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
181 or empty list if it can't be discovered
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
182 if there are several addresses, the one used with the server is put first
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
183 """
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
184 # TODO: manage permission requesting (e.g. for UMTS link)
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 client = self.host.getClient(profile)
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
186 addresses = []
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
187
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
188 # we first try our luck with netifaces
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
189 if netifaces is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
190 addresses = []
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
191 for interface in netifaces.interfaces():
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
192 if_addresses = netifaces.ifaddresses(interface)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
193 try:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
194 inet_list = if_addresses[netifaces.AF_INET]
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
195 except KeyError:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
196 continue
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
197 for data in inet_list:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
198 addresse = data['addr']
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
199 if self._filterAddresse(addresse):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
200 addresses.append(addresse)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
201
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
202 # we first try with our connection to server
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
203 ip = client.xmlstream.transport.getHost().host
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
204 if self._filterAddresse(ip):
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
205 self._insertFirst(addresses, ip)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
206 defer.returnValue(addresses)
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
208 # if not available, we try with NAT-Port
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
209 if self._nat is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
210 nat_ip = yield self._nat.getIP(local=True)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
211 if nat_ip is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
212 self._insertFirst(addresses, nat_ip)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
213 defer.returnValue(addresses)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
214
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
215 if addresses:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
216 defer.returnValue(addresses)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
217
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
218 # still not luck, we need to contact external website
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
219 allow_get_ip = yield self._externalAllowed(profile)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
220
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
221 if not allow_get_ip:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
222 defer.returnValue(addresses)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
223
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
224 ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
225 self._insertFirst(addresses, ip_tuple.local)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
226 defer.returnValue(addresses)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
227
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
228
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
229 @defer.inlineCallbacks
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
230 def getExternalIP(self, profile):
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 """Try to discover external IP
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 @param profile: %(doc_profile)s
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 @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
235 """
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
236 if self._ip_cache is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
237 defer.returnValue(self._ip_cache)
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
239 # we first try with NAT-Port
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
240 if self._nat is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
241 nat_ip = yield self._nat.getIP()
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
242 if nat_ip is not None:
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
243 defer.returnValue(nat_ip)
1534
a5e0393a06cd plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
1537
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
245 # then by requesting external website
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
246 allow_get_ip = yield self._externalAllowed(profile)
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
247 ip = webclient.getPage(GET_IP_PAGE) if allow_get_ip else None
6fa9e8c02c34 plugin IP discovery: better IP discovering:
Goffi <goffi@goffi.org>
parents: 1534
diff changeset
248 defer.returnValue(ip)