Mercurial > libervia-backend
view src/plugins/plugin_misc_ip.py @ 1534:a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
- a dedicated plugin now manage IP discovery, for now it uses external website, but it should implement XEP-0279 soon
- a permission is requested to user for calling an external website. Once the permission is granted (it's global), it is not asked anymore.
- the ip is discovered on demand, and cache is kept for the session.
- memory.params.getParamA has a new parameter use_default, which allow to get None when a parameter is not set, instead of the default value.
This allow to detected when we need to do the first permission request. memory.getParamA don't have this parameter.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 29 Sep 2015 17:54:22 +0200 |
parents | |
children | 6fa9e8c02c34 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- # SAT plugin for IP address discovery # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # 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/>. 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 twisted.web.client import getPage import urlparse PLUGIN_INFO = { "name": "IP discovery", "import_name": "IP", "type": C.PLUG_TYPE_MISC, "main": "IPPlugin", "handler": "no", "description": _("""This plugin help to discover our external IP address.""") } GET_IP_PAGE = "http://www.goffi.org/sat_tools/get_ip.php" # This page must only return external IP of the requester GET_IP_LABEL = D_(u"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 = D_(u"""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. IP address is an identifier to locate you on Internet (similar to a phone number). Do you agree to do this request ? """).format( page = GET_IP_PAGE, domain = urlparse.urlparse(GET_IP_PAGE).netloc, app_name = C.APP_NAME) PARAMS = """ <params> <general> <category name="{category}"> <param name="{name}" label="{label}" type="bool" /> </category> </general> </params> """.format(category=GET_IP_CATEGORY, name=GET_IP_NAME, label=GET_IP_LABEL) class IPPlugin(object): def __init__(self, host): log.info(_("plugin IP discovery initialization")) self.host = host host.memory.updateParams(PARAMS) def profileConnected(self, profile): client = self.host.getClient(profile) # XXX: we keep cache only for profile session as ip can change between them client._ip_cache = None def getIP(self, profile): """Try to discover external IP @param profile: %(doc_profile)s @return (deferred): external IP address or None if it can't be discovered """ client = self.host.getClient(profile) if client._ip_cache is not None: return client._ip_cache allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False) if allow_get_ip is None: # we don't have autorisation from user yet to use get_ip, we ask him confirm_d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile) def setParam(allowed): # FIXME: we need to use boolConst as setParam only manage str/unicode # need to be fixed when params will be refactored self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY) return self.getIP(profile) return confirm_d.addCallback(setParam) return getPage(GET_IP_PAGE) if allow_get_ip else None