예제 #1
0
        def startTLS(self, contextFactory, normal=True):
            """
			@see: L{ITLSTransport.startTLS}
			"""
            # Figure out which direction the SSL goes in.  If normal is True,
            # we'll go in the direction indicated by the subclass.  Otherwise,
            # we'll go the other way (client = not normal ^ _tlsClientDefault,
            # in other words).
            if normal:
                client = self._tlsClientDefault
            else:
                client = not self._tlsClientDefault

            tlsFactory = TLSMemoryBIOFactory(contextFactory, client, None)
            tlsProtocol = TLSMemoryBIOProtocol(tlsFactory, self.protocol,
                                               False)
            self.protocol = tlsProtocol

            self.getHandle = tlsProtocol.getHandle
            self.getPeerCertificate = tlsProtocol.getPeerCertificate

            # Mark the transport as secure.
            directlyProvides(self, interfaces.ISSLTransport)

            # Remember we did this so that write and writeSequence can send the
            # data to the right place.
            self._tls = True

            # Hook it up
            self.protocol.makeConnection(_BypassTLS(self))
예제 #2
0
파일: tcp.py 프로젝트: TheArchives/blockBox
		def startTLS(self, contextFactory, normal=True):
			"""
			@see: L{ITLSTransport.startTLS}
			"""
			# Figure out which direction the SSL goes in.  If normal is True,
			# we'll go in the direction indicated by the subclass.  Otherwise,
			# we'll go the other way (client = not normal ^ _tlsClientDefault,
			# in other words).
			if normal:
				client = self._tlsClientDefault
			else:
				client = not self._tlsClientDefault

			tlsFactory = TLSMemoryBIOFactory(contextFactory, client, None)
			tlsProtocol = TLSMemoryBIOProtocol(tlsFactory, self.protocol, False)
			self.protocol = tlsProtocol

			self.getHandle = tlsProtocol.getHandle
			self.getPeerCertificate = tlsProtocol.getPeerCertificate

			# Mark the transport as secure.
			directlyProvides(self, interfaces.ISSLTransport)

			# Remember we did this so that write and writeSequence can send the
			# data to the right place.
			self._tls = True

			# Hook it up
			self.protocol.makeConnection(_BypassTLS(self))
예제 #3
0
    def makeConnection(self, transport):
        """
		When a connection is made, register this wrapper with its factory,
		save the real transport, and connect the wrapped protocol to this
		L{ProtocolWrapper} to intercept any transport calls it makes.
		"""
        directlyProvides(self, providedBy(transport))
        Protocol.makeConnection(self, transport)
        self.factory.registerProtocol(self)
        self.wrappedProtocol.makeConnection(self)
예제 #4
0
	def makeConnection(self, transport):
		"""
		When a connection is made, register this wrapper with its factory,
		save the real transport, and connect the wrapped protocol to this
		L{ProtocolWrapper} to intercept any transport calls it makes.
		"""
		directlyProvides(self, providedBy(transport))
		Protocol.makeConnection(self, transport)
		self.factory.registerProtocol(self)
		self.wrappedProtocol.makeConnection(self)
예제 #5
0
파일: endpoints.py 프로젝트: samis/blockBox
	def __init__(self, connectedDeferred, wrappedProtocol):
		"""
		@param connectedDeferred: The L{Deferred} that will callback
			with the C{wrappedProtocol} when it is connected.

		@param wrappedProtocol: An L{IProtocol} provider that will be
			connected.
		"""
		self._connectedDeferred = connectedDeferred
		self._wrappedProtocol = wrappedProtocol

		if interfaces.IHalfCloseableProtocol.providedBy(
			self._wrappedProtocol):
			directlyProvides(self, interfaces.IHalfCloseableProtocol)
예제 #6
0
def upgradeWithIQResponseTracker(xs):
	"""
	Enhances an XmlStream for iq response tracking.

	This makes an L{XmlStream} object provide L{IIQResponseTracker}. When a
	response is an error iq stanza, the deferred has its errback invoked with a
	failure that holds a L{StanzaException<error.StanzaException>} that is
	easier to examine.
	"""
	def callback(iq):
		"""
		Handle iq response by firing associated deferred.
		"""
		if getattr(iq, 'handled', False):
			return

		try:
			d = xs.iqDeferreds[iq["id"]]
		except KeyError:
			pass
		else:
			del xs.iqDeferreds[iq["id"]]
			iq.handled = True
			if iq['type'] == 'error':
				d.errback(error.exceptionFromStanza(iq))
			else:
				d.callback(iq)


	def disconnected(_):
		"""
		Make sure deferreds do not linger on after disconnect.

		This errbacks all deferreds of iq's for which no response has been
		received with a L{ConnectionLost} failure. Otherwise, the deferreds
		will never be fired.
		"""
		iqDeferreds = xs.iqDeferreds
		xs.iqDeferreds = {}
		for d in iqDeferreds.itervalues():
			d.errback(ConnectionLost())

	xs.iqDeferreds = {}
	xs.iqDefaultTimeout = getattr(xs, 'iqDefaultTimeout', None)
	xs.addObserver(xmlstream.STREAM_END_EVENT, disconnected)
	xs.addObserver('/iq[@type="result"]', callback)
	xs.addObserver('/iq[@type="error"]', callback)
	directlyProvides(xs, ijabber.IIQResponseTracker)
예제 #7
0
def upgradeWithIQResponseTracker(xs):
    """
	Enhances an XmlStream for iq response tracking.

	This makes an L{XmlStream} object provide L{IIQResponseTracker}. When a
	response is an error iq stanza, the deferred has its errback invoked with a
	failure that holds a L{StanzaException<error.StanzaException>} that is
	easier to examine.
	"""
    def callback(iq):
        """
		Handle iq response by firing associated deferred.
		"""
        if getattr(iq, 'handled', False):
            return

        try:
            d = xs.iqDeferreds[iq["id"]]
        except KeyError:
            pass
        else:
            del xs.iqDeferreds[iq["id"]]
            iq.handled = True
            if iq['type'] == 'error':
                d.errback(error.exceptionFromStanza(iq))
            else:
                d.callback(iq)

    def disconnected(_):
        """
		Make sure deferreds do not linger on after disconnect.

		This errbacks all deferreds of iq's for which no response has been
		received with a L{ConnectionLost} failure. Otherwise, the deferreds
		will never be fired.
		"""
        iqDeferreds = xs.iqDeferreds
        xs.iqDeferreds = {}
        for d in iqDeferreds.itervalues():
            d.errback(ConnectionLost())

    xs.iqDeferreds = {}
    xs.iqDefaultTimeout = getattr(xs, 'iqDefaultTimeout', None)
    xs.addObserver(xmlstream.STREAM_END_EVENT, disconnected)
    xs.addObserver('/iq[@type="result"]', callback)
    xs.addObserver('/iq[@type="error"]', callback)
    directlyProvides(xs, ijabber.IIQResponseTracker)