扭曲:将ReconnectingClientFactory与SSL混合并使write()“可靠”

时间:2013-01-02 21:24:50

标签: python ssl buffer twisted

我对Python和Twisted都相当新,所以我可能只是没有正确地理解事情,但我似乎陷入了需要帮助的地步。

我想要做的是在SSL连接上使用ReconnectingClientFactory。我已经全部运行了,但是如果连接被丢弃,那么发送到传输的write()方法的所有数据都会被删除,而不会出现任何错误。调用的实际方法是twisted.protocols.tls.TLSMemoryBIOProtocol.write()

以下是我的想法(从工作连接开始):

  • 连接丢失
  • 使用一些数据调用write()方法(source code here
  • self.disconnecting为False,因此数据会传递到_write() method
  • _write方法获取_lostTLSConnection,True然后只运行return
  • 重新获得连接,但没有发送数据,因为它没有缓存任何地方

以下是客户端的简化版本:

from OpenSSL import SSL
from twisted.internet.protocol import (Protocol, ReconnectingClientFactory)
from twisted.internet import (reactor, ssl)
import struct

class MetricsServer(Protocol):
    streambuffer = bytearray()

    def connectionMade(self):
        self.transport.setTcpKeepAlive(True) # maintain the TCP connection
        self.transport.setTcpNoDelay(False) # allow Nagle algorithm
        print("connected to server")            

    def dataReceived(self, data):
        print("from server:", data)

    def connectionLost(self, reason):
        self.connected = 0
        print("server connection lost:", reason)

class MetricsServerFactory(ReconnectingClientFactory):
    protocol = MetricsServer
    maxDelay = 300 # maximum seconds between retries
    factor = 1.6180339887498948
    packet_sequence_number = 0
    active_connection = None

    def buildProtocol(self, addr):
        self.resetDelay()
        if self.active_connection == None:
            self.active_connection = self.protocol()
        return self.active_connection

    def get_packet_sequence_number(self):
        self.packet_sequence_number += 1
        return self.packet_sequence_number

    def send_data(self):
        print ("sending ssl packet")
        packet = struct.pack("!I", self.get_packet_sequence_number())
        self.active_connection.transport.write(packet)
        reactor.callLater(1.0, metrics_server.send_data)

class CtxFactory(ssl.ClientContextFactory):
    def getContext(self):
        self.method = SSL.TLSv1_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.use_certificate_file('keys/client.crt')
        ctx.use_privatekey_file('keys/client.key')

        def verifyCallback(connection, x509, errnum, errdepth, ok):
            return bool(ok)
        ctx.set_verify(SSL.VERIFY_PEER, verifyCallback)
        ctx.load_verify_locations("keys/ca.pem")
        return ctx

if __name__ == "__main__":
    metrics_server = MetricsServerFactory()
    reactor.connectSSL('localhost', 8000, metrics_server, CtxFactory())
    reactor.callLater(3.0, metrics_server.send_data)
    reactor.run()

这是一个简单的服务器,输出它收到的数据:

from OpenSSL import SSL
from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol

class Echo(Protocol):
    sent_back_data = False

    def dataReceived(self, data):
        print(' '.join("{0:02x}".format(x) for x in data))

def verifyCallback(connection, x509, errnum, errdepth, ok):
    return bool(ok)

if __name__ == '__main__':
    factory = Factory()
    factory.protocol = Echo

    myContextFactory = ssl.DefaultOpenSSLContextFactory(
        'keys/server.key', 'keys/server.crt'
        )
    ctx = myContextFactory.getContext()
    ctx.set_verify(
        SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
        verifyCallback
        )

    ctx.load_verify_locations("keys/ca.pem")
    reactor.listenSSL(8000, factory, myContextFactory)
    reactor.run()

重新创建问题的过程:

  • 首先,您需要生成自己的证书和CA才能使其正常工作
  • 首先运行服务器
  • 运行客户端代码
  • 等待服务器端的某些输出,然后结束程序
  • 注意客户端继续尝试发送数据
  • 重启服务器端
  • 注意服务器端将继续接收数据包,但在连接丢失时发送的数据包将被删除

作为一种解决方法,我尝试实现自己的缓冲区来重新连接时发送数据,但遇到了另一个问题。我希望它在重新建立连接时发送数据,我能看到的唯一钩子是Protocol.connectionMade()。但是,该方法在实际完成TLS握手之前被称为,因此最终被exception handler in _write()捕获并放入另一个缓冲区以便稍后发送。 但是,缓冲区only seems to be sent if data is received from the other end(在我的情况下经常不会发生,也意味着数据可能以错误的顺序到达另一端,因为写()可以在收到数据之前调用)。我还认为在收到数据之前再次断开连接也会导致数据缓冲区被擦除。

编辑:添加了第一期的示例代码。我在工厂里有active_connection可能很奇怪,但我试图让它作为单身人士工作。

1 个答案:

答案 0 :(得分:0)

好的,我弄清楚了我的解决方法的问题......我正在传递一个bytearray要写入传输,然后立即清除它,之后没有意识到写入被推迟到我清除之后缓冲。所以,我传递了bytearray的副本,现在似乎正在运作。

对于写入的每个调用都必须通过检查来查看它是否已连接,因为ReconnectingClientFactory的整个想法是它处理为您维护连接,这似乎仍然是正确的。此外,我认为可能会失去if语句与实际运行write()之间的联系,因此仍有可能丢失数据。

from OpenSSL import SSL
from twisted.internet.protocol import (Protocol, ReconnectingClientFactory)
from twisted.internet import (reactor, ssl)
import struct

class MetricsServer(Protocol):
    streambuffer = bytearray()

    def connectionMade(self):
        self.transport.setTcpKeepAlive(True) # maintain the TCP connection
        self.transport.setTcpNoDelay(False) # allow Nagle algorithm
        print("connected to server")
        if len(self.transport.factory.wrappedFactory.send_buffer) > 0:
            self.transport.write(bytes(self.transport.factory.wrappedFactory.send_buffer))
            self.transport.factory.wrappedFactory.send_buffer.clear()

    def dataReceived(self, data):
        print("from server:", data)

    def connectionLost(self, reason):
        self.connected = 0
        print("server connection lost:", reason)


class MetricsServerFactory(ReconnectingClientFactory):
    protocol = MetricsServer
    maxDelay = 300 # maximum seconds between retries
    factor = 1.6180339887498948
    packet_sequence_number = 0
    active_connection = None

    send_buffer = bytearray()

    def buildProtocol(self, addr):
        self.resetDelay()
        if self.active_connection == None:
            self.active_connection = self.protocol()
        return self.active_connection

    def get_packet_sequence_number(self):
        self.packet_sequence_number += 1
        return self.packet_sequence_number

    def send_data(self):
        print ("sending ssl packet")
        packet = struct.pack("!I", self.get_packet_sequence_number())
        if self.active_connection and self.active_connection.connected:
            self.active_connection.transport.write(packet)
        else:
            self.send_buffer.extend(packet)
        reactor.callLater(1.0, metrics_server.send_data)


class CtxFactory(ssl.ClientContextFactory):
    def getContext(self):
        self.method = SSL.TLSv1_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.use_certificate_file('keys/client.crt')
        ctx.use_privatekey_file('keys/client.key')

        def verifyCallback(connection, x509, errnum, errdepth, ok):
            return bool(ok)
        ctx.set_verify(SSL.VERIFY_PEER, verifyCallback)
        ctx.load_verify_locations("keys/ca.pem")
        return ctx

if __name__ == "__main__":
    metrics_server = MetricsServerFactory()
    reactor.connectSSL('localhost', 8000, metrics_server, CtxFactory())
    reactor.callLater(3.0, metrics_server.send_data)
    reactor.run()