plugin XEP-0198: Stream Management implementation:
- hooks can now be set in stream onElement and send methods - xmllog refactored to use new hooks - client.isConnected now uses transport.connected method - fixed reconnection, SàT will now try to reconnect indefinitely until it success, unresolvable failure happen (e.g. invalid certificate), or explicit disconnection is requested (or a plugin change this behaviour) - new triggers: "stream_hooks", "disconnecting", "disconnected", and "xml_init" (replace "XML Initialized")
This commit is contained in:
parent
604238e035
commit
05b59775d0
4 changed files with 641 additions and 113 deletions
|
|
@ -50,7 +50,7 @@ class Const(object):
|
|||
## Protocol ##
|
||||
XMPP_C2S_PORT = 5222
|
||||
XMPP_KEEP_ALIFE = 180
|
||||
XMPP_MAX_RETRIES = 2
|
||||
XMPP_MAX_RETRIES = None
|
||||
# default port used on Prosody, may differ on other servers
|
||||
XMPP_COMPONENT_PORT = 5347
|
||||
|
||||
|
|
@ -403,6 +403,11 @@ class Const(object):
|
|||
NO_LIMIT = -1 # used in bridge when a integer value is expected
|
||||
DEFAULT_MAX_AGE = 1209600 # default max age of cached files, in seconds
|
||||
HASH_SHA1_EMPTY = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
|
||||
STANZA_NAMES = (u"iq", u"message", u"presence")
|
||||
|
||||
# Stream Hooks
|
||||
STREAM_HOOK_SEND = u"send"
|
||||
STREAM_HOOK_RECEIVE = u"receive"
|
||||
|
||||
@classmethod
|
||||
def LOG_OPTIONS(cls):
|
||||
|
|
|
|||
|
|
@ -1,10 +1,19 @@
|
|||
from twisted.words.protocols.jabber import xmlstream
|
||||
from twisted.words.protocols.jabber import xmlstream, sasl, client as tclient
|
||||
from twisted.internet import ssl
|
||||
from wokkel import client
|
||||
from sat.core.constants import Const as C
|
||||
from sat.core.log import getLogger
|
||||
|
||||
"""This module apply monkey patches to Twisted and Wokkel to handle certificate validation
|
||||
during XMPP connection"""
|
||||
log = getLogger(__name__)
|
||||
|
||||
"""This module apply monkey patches to Twisted and Wokkel
|
||||
First part handle certificate validation during XMPP connectionand are temporary
|
||||
(until merged upstream).
|
||||
Second part add a trigger point to send and onElement method of XmlStream
|
||||
"""
|
||||
|
||||
|
||||
## certificate validation patches
|
||||
|
||||
class TLSInitiatingInitializer(xmlstream.TLSInitiatingInitializer):
|
||||
check_certificate = True
|
||||
|
|
@ -21,13 +30,15 @@ class TLSInitiatingInitializer(xmlstream.TLSInitiatingInitializer):
|
|||
|
||||
class XMPPClient(client.XMPPClient):
|
||||
|
||||
def __init__(self, jid, password, host=None, port=5222, check_certificate=True):
|
||||
def __init__(self, jid, password, host=None, port=5222,
|
||||
check_certificate=True):
|
||||
self.jid = jid
|
||||
self.domain = jid.host.encode('idna')
|
||||
self.host = host
|
||||
self.port = port
|
||||
|
||||
factory = HybridClientFactory(jid, password, check_certificate)
|
||||
factory = HybridClientFactory(
|
||||
jid, password, check_certificate=check_certificate)
|
||||
|
||||
client.StreamManager.__init__(self, factory)
|
||||
|
||||
|
|
@ -39,6 +50,7 @@ def HybridClientFactory(jid, password, check_certificate=True):
|
|||
|
||||
|
||||
class HybridAuthenticator(client.HybridAuthenticator):
|
||||
res_binding = True
|
||||
|
||||
def __init__(self, jid, password, check_certificate):
|
||||
xmlstream.ConnectAuthenticator.__init__(self, jid.host)
|
||||
|
|
@ -53,11 +65,83 @@ class HybridAuthenticator(client.HybridAuthenticator):
|
|||
tlsInit.check_certificate = self.check_certificate
|
||||
xs.initializers = [client.client.CheckVersionInitializer(xs),
|
||||
tlsInit,
|
||||
client.CheckAuthInitializer(xs)]
|
||||
CheckAuthInitializer(xs, self.res_binding)]
|
||||
|
||||
|
||||
# XmlStream triggers
|
||||
|
||||
|
||||
class XmlStream(xmlstream.XmlStream):
|
||||
"""XmlStream which allows to add hooks"""
|
||||
|
||||
def __init__(self, authenticator):
|
||||
xmlstream.XmlStream.__init__(self, authenticator)
|
||||
# hooks at this level should not modify content
|
||||
# so it's not needed to handle priority as with triggers
|
||||
self._onElementHooks = []
|
||||
self._sendHooks = []
|
||||
|
||||
def addHook(self, hook_type, callback):
|
||||
"""Add a send or receive hook"""
|
||||
conflict_msg = (u"Hook conflict: can't add {hook_type} hook {callback}"
|
||||
.format(hook_type=hook_type, callback=callback))
|
||||
if hook_type == C.STREAM_HOOK_RECEIVE:
|
||||
if callback not in self._onElementHooks:
|
||||
self._onElementHooks.append(callback)
|
||||
else:
|
||||
log.warning(conflict_msg)
|
||||
elif hook_type == C.STREAM_HOOK_SEND:
|
||||
if callback not in self._sendHooks:
|
||||
self._sendHooks.append(callback)
|
||||
else:
|
||||
log.warning(conflict_msg)
|
||||
else:
|
||||
raise ValueError(u"Invalid hook type: {hook_type}"
|
||||
.format(hook_type=hook_type))
|
||||
|
||||
def onElement(self, element):
|
||||
for hook in self._onElementHooks:
|
||||
hook(element)
|
||||
xmlstream.XmlStream.onElement(self, element)
|
||||
|
||||
def send(self, obj):
|
||||
for hook in self._sendHooks:
|
||||
hook(obj)
|
||||
xmlstream.XmlStream.send(self, obj)
|
||||
|
||||
|
||||
# Binding activation (needed for stream management, XEP-0198)
|
||||
|
||||
|
||||
class CheckAuthInitializer(client.CheckAuthInitializer):
|
||||
|
||||
def __init__(self, xs, res_binding):
|
||||
super(CheckAuthInitializer, self).__init__(xs)
|
||||
self.res_binding = res_binding
|
||||
|
||||
def initialize(self):
|
||||
# XXX: modification of client.CheckAuthInitializer which has optional
|
||||
# resource binding, and which doesn't do deprecated
|
||||
# SessionInitializer
|
||||
if (sasl.NS_XMPP_SASL, 'mechanisms') in self.xmlstream.features:
|
||||
inits = [(sasl.SASLInitiatingInitializer, True)]
|
||||
if self.res_binding:
|
||||
inits.append((tclient.BindInitializer, True)),
|
||||
|
||||
for initClass, required in inits:
|
||||
init = initClass(self.xmlstream)
|
||||
init.required = required
|
||||
self.xmlstream.initializers.append(init)
|
||||
elif (tclient.NS_IQ_AUTH_FEATURE, 'auth') in self.xmlstream.features:
|
||||
self.xmlstream.initializers.append(
|
||||
tclient.IQAuthInitializer(self.xmlstream))
|
||||
else:
|
||||
raise Exception("No available authentication method found")
|
||||
|
||||
|
||||
def apply():
|
||||
# certificate validation
|
||||
xmlstream.TLSInitiatingInitializer = TLSInitiatingInitializer
|
||||
client.XMPPClient = XMPPClient
|
||||
client.HybridClientFactory = HybridClientFactory
|
||||
client.HybridAuthenticator = HybridAuthenticator
|
||||
# XmlStream triggers
|
||||
xmlstream.XmlStreamFactory.protocol = XmlStream
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ import sys
|
|||
import os.path
|
||||
import uuid
|
||||
|
||||
|
||||
try:
|
||||
from collections import OrderedDict # only available from python 2.7
|
||||
except ImportError:
|
||||
|
|
|
|||
136
sat/core/xmpp.py
136
sat/core/xmpp.py
|
|
@ -49,20 +49,20 @@ NS_DISCO_INFO = u"http://jabber.org/protocol/disco#info"
|
|||
|
||||
class SatXMPPEntity(object):
|
||||
"""Common code for Client and Component"""
|
||||
_reason = None # reason of disconnection
|
||||
|
||||
def __init__(self, host_app, profile, max_retries):
|
||||
|
||||
self.factory.clientConnectionLost = self.connectionLost
|
||||
self.factory.maxRetries = max_retries
|
||||
# when self._connected is None, we are not connected
|
||||
factory = self.factory
|
||||
factory.maxRetries = max_retries
|
||||
factory.maxDelay = 30
|
||||
# when self._connected_d is None, we are not connected
|
||||
# else, it's a deferred which fire on disconnection
|
||||
self._connected = None
|
||||
self._connected_d = None
|
||||
self.profile = profile
|
||||
self.host_app = host_app
|
||||
self.cache = cache.Cache(host_app, profile)
|
||||
self._mess_id_uid = {} # map from message id to uid used in history.
|
||||
# Key: (full_jid,message_id) Value: uid
|
||||
# this Deferred fire when entity is connected
|
||||
self.conn_deferred = defer.Deferred()
|
||||
self._progress_cb = {} # callback called when a progress is requested
|
||||
# (key = progress id)
|
||||
|
|
@ -145,15 +145,12 @@ class SatXMPPEntity(object):
|
|||
"Password", "Connection", profile_key=profile
|
||||
)
|
||||
entity = host.profiles[profile] = cls(
|
||||
host,
|
||||
profile,
|
||||
jid.JID(host.memory.getParamA("JabberID", "Connection", profile_key=profile)),
|
||||
password,
|
||||
host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection", profile_key=profile)
|
||||
or None,
|
||||
port,
|
||||
max_retries,
|
||||
)
|
||||
host, profile, jid.JID(host.memory.getParamA("JabberID", "Connection",
|
||||
profile_key=profile)), password,
|
||||
host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection",
|
||||
profile_key=profile) or None,
|
||||
port, max_retries,
|
||||
)
|
||||
|
||||
entity._createSubProtocols()
|
||||
|
||||
|
|
@ -172,7 +169,7 @@ class SatXMPPEntity(object):
|
|||
|
||||
entity.startService()
|
||||
|
||||
yield entity.getConnectionDeferred()
|
||||
yield entity.conn_deferred
|
||||
|
||||
yield defer.maybeDeferred(entity.entityConnected)
|
||||
|
||||
|
|
@ -205,40 +202,16 @@ class SatXMPPEntity(object):
|
|||
# we finally send our presence
|
||||
entity.presence.available()
|
||||
|
||||
def getConnectionDeferred(self):
|
||||
"""Return a deferred which fire when the client is connected"""
|
||||
return self.conn_deferred
|
||||
|
||||
def _disconnectionCb(self, __):
|
||||
self._connected = None
|
||||
self._connected_d = None
|
||||
|
||||
def _disconnectionEb(self, failure_):
|
||||
log.error(_(u"Error while disconnecting: {}".format(failure_)))
|
||||
|
||||
def _authd(self, xmlstream):
|
||||
if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
|
||||
return
|
||||
super(SatXMPPEntity, self)._authd(xmlstream)
|
||||
|
||||
if self._reason is not None:
|
||||
# if we have had trouble to connect we can reset
|
||||
# the exception as the connection is now working.
|
||||
del self._reason
|
||||
|
||||
# the following Deferred is used to know when we are connected
|
||||
# so we need to be set it to None when connection is lost
|
||||
self._connected = defer.Deferred()
|
||||
self._connected.addCallback(self._cleanConnection)
|
||||
self._connected.addCallback(self._disconnectionCb)
|
||||
self._connected.addErrback(self._disconnectionEb)
|
||||
|
||||
log.info(
|
||||
_(u"********** [{profile}] CONNECTED **********").format(profile=self.profile)
|
||||
)
|
||||
log.debug(_(u"{profile} identified").format(profile=self.profile))
|
||||
self.streamInitialized()
|
||||
self.host_app.bridge.connected(
|
||||
self.profile, unicode(self.jid)
|
||||
) # we send the signal to the clients
|
||||
|
||||
def _finish_connection(self, __):
|
||||
self.conn_deferred.callback(None)
|
||||
|
|
@ -246,11 +219,32 @@ class SatXMPPEntity(object):
|
|||
def streamInitialized(self):
|
||||
"""Called after _authd"""
|
||||
log.debug(_(u"XML stream is initialized"))
|
||||
if not self.host_app.trigger.point("xml_init", self):
|
||||
return
|
||||
self.postStreamInit()
|
||||
|
||||
def postStreamInit(self):
|
||||
"""Workflow after stream initalisation."""
|
||||
log.info(
|
||||
_(u"********** [{profile}] CONNECTED **********").format(profile=self.profile)
|
||||
)
|
||||
|
||||
# the following Deferred is used to know when we are connected
|
||||
# so we need to be set it to None when connection is lost
|
||||
self._connected_d = defer.Deferred()
|
||||
self._connected_d.addCallback(self._cleanConnection)
|
||||
self._connected_d.addCallback(self._disconnectionCb)
|
||||
self._connected_d.addErrback(self._disconnectionEb)
|
||||
|
||||
self.host_app.bridge.connected(
|
||||
self.profile, unicode(self.jid)
|
||||
) # we send the signal to the clients
|
||||
|
||||
|
||||
self.keep_alife = task.LoopingCall(
|
||||
self.xmlstream.send, " "
|
||||
) # Needed to avoid disconnection (specially with openfire)
|
||||
self.keep_alife.start(C.XMPP_KEEP_ALIFE)
|
||||
|
||||
self.disco = SatDiscoProtocol(self)
|
||||
self.disco.setHandlerParent(self)
|
||||
self.discoHandler = disco.DiscoHandler()
|
||||
|
|
@ -278,21 +272,27 @@ class SatXMPPEntity(object):
|
|||
|
||||
## connection ##
|
||||
|
||||
def _disconnected(self, reason):
|
||||
# we have to save the reason of disconnection, otherwise it would be lost
|
||||
self._reason = reason
|
||||
super(SatXMPPEntity, self)._disconnected(reason)
|
||||
def _connected(self, xs):
|
||||
send_hooks = []
|
||||
receive_hooks = []
|
||||
self.host_app.trigger.point(
|
||||
"stream_hooks", self, receive_hooks, send_hooks)
|
||||
for hook in receive_hooks:
|
||||
xs.addHook(C.STREAM_HOOK_RECEIVE, hook)
|
||||
for hook in send_hooks:
|
||||
xs.addHook(C.STREAM_HOOK_SEND, hook)
|
||||
super(SatXMPPEntity, self)._connected(xs)
|
||||
|
||||
def connectionLost(self, connector, reason):
|
||||
def disconnectProfile(self, reason):
|
||||
try:
|
||||
self.keep_alife.stop()
|
||||
except AttributeError:
|
||||
log.debug(_("No keep_alife"))
|
||||
if self._connected is not None:
|
||||
if self._connected_d is not None:
|
||||
self.host_app.bridge.disconnected(
|
||||
self.profile
|
||||
) # we send the signal to the clients
|
||||
self._connected.callback(None)
|
||||
self._connected_d.callback(None)
|
||||
self.host_app.purgeEntity(
|
||||
self.profile
|
||||
) # and we remove references to this client
|
||||
|
|
@ -302,12 +302,10 @@ class SatXMPPEntity(object):
|
|||
)
|
||||
)
|
||||
if not self.conn_deferred.called:
|
||||
# FIXME: real error is not gotten here (e.g. if jid is not know by Prosody,
|
||||
# we should have the real error)
|
||||
if self._reason is None:
|
||||
if reason is None:
|
||||
err = error.StreamError(u"Server unexpectedly closed the connection")
|
||||
else:
|
||||
err = self._reason
|
||||
err = reason
|
||||
try:
|
||||
if err.value.args[0][0][2] == "certificate verify failed":
|
||||
err = exceptions.InvalidCertificate(
|
||||
|
|
@ -316,10 +314,17 @@ class SatXMPPEntity(object):
|
|||
u"This should never happen and may indicate that "
|
||||
u"somebody is trying to spy on you.\n"
|
||||
u"Please contact your server administrator."))
|
||||
self.factory.continueTrying = 0
|
||||
except (IndexError, TypeError):
|
||||
pass
|
||||
self.conn_deferred.errback(err)
|
||||
|
||||
def _disconnected(self, reason):
|
||||
super(SatXMPPEntity, self)._disconnected(reason)
|
||||
if not self.host_app.trigger.point("disconnected", self, reason):
|
||||
return
|
||||
self.disconnectProfile(reason)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _cleanConnection(self, __):
|
||||
"""method called on disconnection
|
||||
|
|
@ -333,13 +338,18 @@ class SatXMPPEntity(object):
|
|||
yield disconnected_cb(self)
|
||||
|
||||
def isConnected(self):
|
||||
return self._connected is not None
|
||||
try:
|
||||
return bool(self.xmlstream.transport.connected)
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
def entityDisconnect(self):
|
||||
if not self.host_app.trigger.point("disconnecting", self):
|
||||
return
|
||||
log.info(_(u"Disconnecting..."))
|
||||
self.stopService()
|
||||
if self._connected is not None:
|
||||
return self._connected
|
||||
if self._connected_d is not None:
|
||||
return self._connected_d
|
||||
else:
|
||||
return defer.succeed(None)
|
||||
|
||||
|
|
@ -735,16 +745,8 @@ class SatXMPPComponent(SatXMPPEntity, component.Component):
|
|||
) # XXX: set to True from entry plugin to keep messages in history for received
|
||||
# messages
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host_app,
|
||||
profile,
|
||||
component_jid,
|
||||
password,
|
||||
host=None,
|
||||
port=None,
|
||||
max_retries=C.XMPP_MAX_RETRIES,
|
||||
):
|
||||
def __init__(self, host_app, profile, component_jid, password, host=None, port=None,
|
||||
max_retries=C.XMPP_MAX_RETRIES):
|
||||
self.started = time.time()
|
||||
if port is None:
|
||||
port = C.XMPP_COMPONENT_PORT
|
||||
|
|
|
|||
Loading…
Reference in a new issue