Mercurial > libervia-backend
diff sat/plugins/plugin_misc_ip.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | b64dd7c1496d |
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_ip.py Wed Jul 31 11:31:22 2019 +0200 +++ b/sat/plugins/plugin_misc_ip.py Tue Aug 13 19:08:41 2019 +0200 @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # SAT plugin for IP address discovery @@ -17,12 +17,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import urllib.parse from sat.core.i18n import _, D_ from sat.core.constants import Const as C from sat.core.log import getLogger - -log = getLogger(__name__) from sat.tools import xml_tools +from wokkel import disco, iwokkel from twisted.web import client as webclient from twisted.web import error as web_error from twisted.internet import defer @@ -30,17 +30,17 @@ from twisted.internet import protocol from twisted.internet import endpoints from twisted.internet import error as internet_error -from zope.interface import implements -from wokkel import disco, iwokkel +from zope.interface import implementer from twisted.words.protocols.jabber.xmlstream import XMPPHandler from twisted.words.protocols.jabber.error import StanzaError -import urlparse + +log = getLogger(__name__) try: import netifaces except ImportError: log.warning( - u"netifaces is not available, it help discovering IPs, you can install it on https://pypi.python.org/pypi/netifaces" + "netifaces is not available, it help discovering IPs, you can install it on https://pypi.python.org/pypi/netifaces" ) netifaces = None @@ -61,12 +61,12 @@ GET_IP_PAGE = ( "http://salut-a-toi.org/whereami/" ) # This page must only return external IP of the requester -GET_IP_LABEL = D_(u"Allow external get IP") +GET_IP_LABEL = D_("Allow external get IP") GET_IP_CATEGORY = "General" GET_IP_NAME = "allow_get_ip" -GET_IP_CONFIRM_TITLE = D_(u"Confirm external site request") +GET_IP_CONFIRM_TITLE = D_("Confirm external site request") GET_IP_CONFIRM = D_( - u"""To facilitate data transfer, we need to contact a website. + """To facilitate data transfer, we need to contact a website. A request will be done on {page} That means that administrators of {domain} can know that you use "{app_name}" and your IP Address. @@ -75,7 +75,7 @@ Do you agree to do this request ? """ ).format( - page=GET_IP_PAGE, domain=urlparse.urlparse(GET_IP_PAGE).netloc, app_name=C.APP_NAME + page=GET_IP_PAGE, domain=urllib.parse.urlparse(GET_IP_PAGE).netloc, app_name=C.APP_NAME ) NS_IP_CHECK = "urn:xmpp:sic:1" @@ -105,7 +105,7 @@ try: self._nat = host.plugins["NAT-PORT"] except KeyError: - log.debug(u"NAT port plugin not available") + log.debug("NAT port plugin not available") self._nat = None # XXX: cache is kept until SàT is restarted @@ -180,7 +180,7 @@ @param ext_utl(str): url to connect to @return (D(str)): return local IP """ - url = urlparse.urlparse(ext_url) + url = urllib.parse.urlparse(ext_url) port = url.port if port is None: if url.scheme == "http": @@ -188,10 +188,10 @@ elif url.scheme == "https": port = 443 else: - log.error(u"Unknown url scheme: {}".format(url.scheme)) + log.error("Unknown url scheme: {}".format(url.scheme)) defer.returnValue(None) if url.hostname is None: - log.error(u"Can't find url hostname for {}".format(GET_IP_PAGE)) + log.error("Can't find url hostname for {}".format(GET_IP_PAGE)) point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port) @@ -257,7 +257,7 @@ try: ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) except (internet_error.DNSLookupError, internet_error.TimeoutError): - log.warning(u"Can't access Domain Name System") + log.warning("Can't access Domain Name System") defer.returnValue(addresses or localhost) self._insertFirst(addresses, ip_tuple.local) defer.returnValue(addresses) @@ -274,24 +274,25 @@ # we first try with XEP-0279 ip_check = yield self.host.hasFeature(client, NS_IP_CHECK) if ip_check: - log.debug(u"Server IP Check available, we use it to retrieve our IP") + log.debug("Server IP Check available, we use it to retrieve our IP") iq_elt = client.IQ("get") + iq_elt['to'] = client.host iq_elt.addElement((NS_IP_CHECK, "address")) try: result_elt = yield iq_elt.send() - address_elt = result_elt.elements(NS_IP_CHECK, "address").next() - ip_elt = address_elt.elements(NS_IP_CHECK, "ip").next() + address_elt = next(result_elt.elements(NS_IP_CHECK, "address")) + ip_elt = next(address_elt.elements(NS_IP_CHECK, "ip")) except StopIteration: log.warning( - u"Server returned invalid result on XEP-0279 request, we ignore it" + "Server returned invalid result on XEP-0279 request, we ignore it" ) except StanzaError as e: - log.warning(u"error while requesting ip to server: {}".format(e)) + log.warning("error while requesting ip to server: {}".format(e)) else: # FIXME: server IP may not be the same as external IP (server can be on local machine or network) # IP should be checked to see if we have a local one, and rejected in this case external_ip = str(ip_elt) - log.debug(u"External IP found: {}".format(external_ip)) + log.debug("External IP found: {}".format(external_ip)) self._external_ip_cache = external_ip defer.returnValue(self._external_ip_cache) @@ -305,13 +306,14 @@ # and finally by requesting external website allow_get_ip = yield self._externalAllowed(client) try: - ip = (yield webclient.getPage(GET_IP_PAGE)) if allow_get_ip else None + ip = ((yield webclient.getPage(GET_IP_PAGE.encode('utf-8'))) + if allow_get_ip else None) except (internet_error.DNSLookupError, internet_error.TimeoutError): - log.warning(u"Can't access Domain Name System") + log.warning("Can't access Domain Name System") ip = None except web_error.Error as e: log.warning( - u"Error while retrieving IP on {url}: {message}".format( + "Error while retrieving IP on {url}: {message}".format( url=GET_IP_PAGE, message=e ) ) @@ -321,8 +323,8 @@ defer.returnValue(ip) +@implementer(iwokkel.IDisco) class IPPlugin_handler(XMPPHandler): - implements(iwokkel.IDisco) def getDiscoInfo(self, requestor, target, nodeIdentifier=""): return [disco.DiscoFeature(NS_IP_CHECK)]