comparison src/plugins/plugin_misc_ip.py @ 2489:e2a7bb875957

plugin pipe/stream, file transfert: refactoring and improvments: this is a big patch as things had to be changed at the same time. - changed methods using profile argument to use client instead - move SatFile in a new tools.stream module, has it should be part of core, not a plugin - new IStreamProducer interface, to handler starting a pull producer - new FileStreamObject which create a stream producer/consumer from a SatFile - plugin pipe is no more using unix named pipe, as it complicate the thing, special care need to be taken to not block, and it's generally not necessary. Instead a socket is now used, so the plugin has been renomed to jingle stream. - bad connection/error should be better handler in jingle stream plugin, and code should not block anymore - jp pipe commands have been updated accordingly fix bug 237
author Goffi <goffi@goffi.org>
date Thu, 08 Feb 2018 00:37:42 +0100
parents 0046283a285d
children 67cc54b01a12
comparison
equal deleted inserted replaced
2488:78c7992a26ed 2489:e2a7bb875957
109 def refreshIP(self): 109 def refreshIP(self):
110 # FIXME: use a trigger instead ? 110 # FIXME: use a trigger instead ?
111 self._external_ip_cache = None 111 self._external_ip_cache = None
112 self._local_ip_cache = None 112 self._local_ip_cache = None
113 113
114 def _externalAllowed(self, profile): 114 def _externalAllowed(self, client):
115 """Return value of parameter with autorisation of user to do external requests 115 """Return value of parameter with autorisation of user to do external requests
116 116
117 if parameter is not set, a dialog is shown to use to get its confirmation, and parameted is set according to answer 117 if parameter is not set, a dialog is shown to use to get its confirmation, and parameted is set according to answer
118 @param profile: %(doc_profile)s
119 @return (defer.Deferred[bool]): True if external request is autorised 118 @return (defer.Deferred[bool]): True if external request is autorised
120 """ 119 """
121 allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False) 120 allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False)
122 121
123 if allow_get_ip is None: 122 if allow_get_ip is None:
125 def setParam(allowed): 124 def setParam(allowed):
126 # FIXME: we need to use boolConst as setParam only manage str/unicode 125 # FIXME: we need to use boolConst as setParam only manage str/unicode
127 # need to be fixed when params will be refactored 126 # need to be fixed when params will be refactored
128 self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY) 127 self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY)
129 return allowed 128 return allowed
130 d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile) 129 d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=client.profile)
131 d.addCallback(setParam) 130 d.addCallback(setParam)
132 return d 131 return d
133 132
134 return defer.succeed(allow_get_ip) 133 return defer.succeed(allow_get_ip)
135 134
183 d = endpoints.connectProtocol(point, protocol.Protocol()) 182 d = endpoints.connectProtocol(point, protocol.Protocol())
184 d.addCallback(gotConnection) 183 d.addCallback(gotConnection)
185 return d 184 return d
186 185
187 @defer.inlineCallbacks 186 @defer.inlineCallbacks
188 def getLocalIPs(self, profile): 187 def getLocalIPs(self, client):
189 """Try do discover local area network IPs 188 """Try do discover local area network IPs
190 189
191 @param profile): %(doc_profile)s
192 @return (deferred): list of lan IP addresses 190 @return (deferred): list of lan IP addresses
193 if there are several addresses, the one used with the server is put first 191 if there are several addresses, the one used with the server is put first
194 if no address is found, localhost IP will be in the list 192 if no address is found, localhost IP will be in the list
195 """ 193 """
196 # TODO: manage permission requesting (e.g. for UMTS link) 194 # TODO: manage permission requesting (e.g. for UMTS link)
197 if self._local_ip_cache is not None: 195 if self._local_ip_cache is not None:
198 defer.returnValue(self._local_ip_cache) 196 defer.returnValue(self._local_ip_cache)
199 client = self.host.getClient(profile)
200 addresses = [] 197 addresses = []
201 localhost = ['127.0.0.1'] 198 localhost = ['127.0.0.1']
202 199
203 # we first try our luck with netifaces 200 # we first try our luck with netifaces
204 if netifaces is not None: 201 if netifaces is not None:
229 226
230 if addresses: 227 if addresses:
231 defer.returnValue(addresses) 228 defer.returnValue(addresses)
232 229
233 # still not luck, we need to contact external website 230 # still not luck, we need to contact external website
234 allow_get_ip = yield self._externalAllowed(profile) 231 allow_get_ip = yield self._externalAllowed(client)
235 232
236 if not allow_get_ip: 233 if not allow_get_ip:
237 defer.returnValue(addresses or localhost) 234 defer.returnValue(addresses or localhost)
238 235
239 try: 236 try:
243 defer.returnValue(addresses or localhost) 240 defer.returnValue(addresses or localhost)
244 self._insertFirst(addresses, ip_tuple.local) 241 self._insertFirst(addresses, ip_tuple.local)
245 defer.returnValue(addresses) 242 defer.returnValue(addresses)
246 243
247 @defer.inlineCallbacks 244 @defer.inlineCallbacks
248 def getExternalIP(self, profile): 245 def getExternalIP(self, client):
249 """Try to discover external IP 246 """Try to discover external IP
250 247
251 @param profile: %(doc_profile)s
252 @return (deferred): external IP address or None if it can't be discovered 248 @return (deferred): external IP address or None if it can't be discovered
253 """ 249 """
254 if self._external_ip_cache is not None: 250 if self._external_ip_cache is not None:
255 defer.returnValue(self._external_ip_cache) 251 defer.returnValue(self._external_ip_cache)
256 252
257 client = self.host.getClient(profile)
258 253
259 # we first try with XEP-0279 254 # we first try with XEP-0279
260 ip_check = yield self.host.hasFeature(client, NS_IP_CHECK) 255 ip_check = yield self.host.hasFeature(client, NS_IP_CHECK)
261 if ip_check: 256 if ip_check:
262 log.debug(u"Server IP Check available, we use it to retrieve our IP") 257 log.debug(u"Server IP Check available, we use it to retrieve our IP")
263 client = self.host.getClient(profile)
264 iq_elt = client.IQ("get") 258 iq_elt = client.IQ("get")
265 iq_elt.addElement((NS_IP_CHECK, 'address')) 259 iq_elt.addElement((NS_IP_CHECK, 'address'))
266 try: 260 try:
267 result_elt = yield iq_elt.send() 261 result_elt = yield iq_elt.send()
268 address_elt = result_elt.elements(NS_IP_CHECK, 'address').next() 262 address_elt = result_elt.elements(NS_IP_CHECK, 'address').next()
285 if nat_ip is not None: 279 if nat_ip is not None:
286 self._external_ip_cache = nat_ip 280 self._external_ip_cache = nat_ip
287 defer.returnValue(nat_ip) 281 defer.returnValue(nat_ip)
288 282
289 # and finally by requesting external website 283 # and finally by requesting external website
290 allow_get_ip = yield self._externalAllowed(profile) 284 allow_get_ip = yield self._externalAllowed(client)
291 try: 285 try:
292 ip = (yield webclient.getPage(GET_IP_PAGE)) if allow_get_ip else None 286 ip = (yield webclient.getPage(GET_IP_PAGE)) if allow_get_ip else None
293 except (internet_error.DNSLookupError, internet_error.TimeoutError): 287 except (internet_error.DNSLookupError, internet_error.TimeoutError):
294 log.warning(u"Can't access Domain Name System") 288 log.warning(u"Can't access Domain Name System")
295 ip = None 289 ip = None