微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

扭曲的手柄连接丢失

如何解决扭曲的手柄连接丢失

我尝试使用ldaptor proxybase和Twisted创建ldap代理应用程序。 代理应用程序处理来自客户端(C)->代理(P)-> LDAPserver(S)的连接请求

我的问题是,一旦P-> S之间的连接关闭并且C-> P之间的连接仍然打开,如果客户端发送ldap请求,则代理返回错误

所以我尝试处理P-> S中的连接丢失事件,并触发一个方法关闭C-> P之间的连接

这是我的代码

#! /usr/bin/env python

class LoggingProxy(ProxyBase):
    """
    A simple example of using `ProxyBase` to log requests and responses.
    """
    def handleProxiedResponse(self,response,request,controls):
        """
        Log the representation of the responses received.
        """
        log.msg("Request => " + repr(request))
        log.msg("Response => " + repr(response))
        return defer.succeed(response)
        
    def connectionLost(self,reason):
        if self.client is not None and self.client.connected:
            if not self.unbound:
                self.client.unbind()
                self.unbound = True
            else:
                self.client.transport.loseConnection()
        self.client = None
        ldapserver.BaseLDAPServer.connectionLost(self,reason)
        log.msg("C -> P CONN LOST: {} {}".format(
            *tcp_lost_conn_repr(self.transport)))
    def connectionClose(self):
        log.msg("C -> P CONN LOST: {} {}".format(
            *tcp_lost_conn_repr(self.transport)))
        self.transport.loseConnection()
    
class MyLDAPClient(LDAPClient):
    """An LDAP client that connect to the proxied server."""
    def connectionLost(self,reason=protocol.connectionDone):
        """Called when TCP connection has been lost"""
        self.connected = 0
        log.msg("P -> S CONN LOST: {} {}".format(
            *tcp_lost_conn_repr(self.transport)))
        while self.onwire:
            k,v = self.onwire.popitem()
            d,_,_ = v
            d.errback(reason)
        # Terminate C -> P connection
        LoggingProxy.connectionClose(self)

class LoggingProxyService(Service):
    endpointStr = "tcp:port=10389"
    proxiedEndpointStr = 'tcp:host=localhost:port=389'
    debug = True
    
    def startService(self):
        factory = protocol.ServerFactory()
        use_tls = False
 
        #proxiedEndpointStr = 'tcp:host=localhost:port=389'
        clientConnector = partial(
            connectToLDAPEndpoint,reactor,self.proxiedEndpointStr,MyLDAPClient)

        def buildProtocol():
            proto = LoggingProxy()
            proto.clientConnector = clientConnector
            proto.use_tls = use_tls
            return proto

        factory.protocol = buildProtocol
        ep = serverFromString(reactor,self.endpointStr)
        d = ep.listen(factory)
        d.addCallback(self.setListeningPort)
        d.addErrback(log.err)
        
    def setListeningPort(self,port):
        self.port_ = port

    def stopService(self):
        # If there are asynchronous cleanup tasks that need to
        # be performed,add deferreds for them to `async_tasks`.
        async_tasks = []
        if self.port_ is not None:
            async_tasks.append(self.port_.stopListening())
        if len(async_tasks) > 0:
            return defer.DeferredList(async_tasks,consumeErrors=True)


application = Application("Logging LDAP Proxy")
service = LoggingProxyService()
service.setServiceParent(application)

问题是LoggingProxy.connectionClose(self)没有引用发起连接的原始实例。

如何指向原始LoggingProxy()实例并关闭连接?

解决方法

在您的 PagerController controller= PageController(viewportFraction: 1,initialPage: 1); TransformationController _transformationController = TransformationController(); var images=['https://asia.olympus-imaging.com/content/000107506.jpg']; PageView.builder( onPageChanged: (int page){ setState(() { _transformationController.value = Matrix4.identity(); }); },controller: controller,itemCount: images.length,itemBuilder: (BuildContext context,int index) { return Center( child :SizedBox( height: 250,child: Container( width: double.infinity,child: images[index]?.length !=0 ? InteractiveViewer( panEnabled: false,// Set it to false to prevent panning. boundaryMargin: EdgeInsets.all(10),transformationController: _transformationController,minScale: 0.5,maxScale: 3.0,child : Image.network(mages[index],fit: BoxFit.fill,loadingBuilder:(BuildContext context,Widget child,ImageChunkEvent loadingProgress) { if (loadingProgress == null) return child; return Center( child: CircularProgressIndicator( value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes : null,),); },):Container(),) ) ); },); 中,您并不总是关闭LoggingProxy.connectionLost连接:

C -> P

如果 def connectionLost(self,reason): if self.client is not None and self.client.connected: if not self.unbound: self.client.unbind() self.unbound = True else: self.client.transport.loseConnection() self.client = None ldapserver.BaseLDAPServer.connectionLost(self,reason) log.msg("C -> P CONN LOST: {} {}".format( *tcp_lost_conn_repr(self.transport))) ,则永远不会调用not self.unbound。相反,self.client.transport.loseConnection会被丢弃,因此永远无法调用。

更加努力地始终呼叫self.client以响应loseConnection

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。