<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import itertools <EOL> try : <EOL> from OpenSSL . SSL import Error as NativeOpenSSLError <EOL> except ImportError : <EOL> pass <EOL> from zope . interface import implementer , directlyProvides <EOL> from twisted . internet . endpoints import TCP4ClientEndpoint , TCP4ServerEndpoint <EOL> from twisted . internet . protocol import Factory , Protocol <EOL> from twisted . internet . error import ConnectionRefusedError <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet import error <EOL> from twisted . internet import interfaces <EOL> from . proto_helpers import MemoryReactorClock <EOL> class TLSNegotiation : <EOL> def __init__ ( self , obj , connectState ) : <EOL> self . obj = obj <EOL> self . connectState = connectState <EOL> self . sent = False <EOL> self . readyToSend = connectState <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . obj , ) <EOL> def pretendToVerify ( self , other , tpt ) : <EOL> if not self . obj . iosimVerify ( other . obj ) : <EOL> tpt . disconnectReason = NativeOpenSSLError ( ) <EOL> tpt . loseConnection ( ) <EOL> @ implementer ( interfaces . IAddress ) <EOL> class FakeAddress ( object ) : <EOL> """<STR_LIT>""" <EOL> @ implementer ( interfaces . ITransport , <EOL> interfaces . ITLSTransport ) <EOL> class FakeTransport : <EOL> """<STR_LIT>""" <EOL> _nextserial = staticmethod ( lambda counter = itertools . count ( ) : next ( counter ) ) <EOL> closed = <NUM_LIT:0> <EOL> disconnecting = <NUM_LIT:0> <EOL> disconnected = <NUM_LIT:0> <EOL> disconnectReason = error . ConnectionDone ( "<STR_LIT>" ) <EOL> producer = None <EOL> streamingProducer = <NUM_LIT:0> <EOL> tls = None <EOL> def __init__ ( self , protocol , isServer , hostAddress = None , peerAddress = None ) : <EOL> """<STR_LIT>""" <EOL> self . protocol = protocol <EOL> self . isServer = isServer <EOL> self . stream = [ ] <EOL> self . serial = self . _nextserial ( ) <EOL> if hostAddress is None : <EOL> hostAddress = FakeAddress ( ) <EOL> self . hostAddress = hostAddress <EOL> if peerAddress is None : <EOL> peerAddress = FakeAddress ( ) <EOL> self . peerAddress = peerAddress <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . isServer and '<STR_LIT:S>' or '<STR_LIT:C>' , self . serial , <EOL> self . protocol . __class__ . __name__ ) <EOL> def write ( self , data ) : <EOL> if self . tls is not None : <EOL> self . tlsbuf . append ( data ) <EOL> else : <EOL> self . stream . append ( data ) <EOL> def _checkProducer ( self ) : <EOL> if self . producer : <EOL> self . producer . resumeProducing ( ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> """<STR_LIT>""" <EOL> self . producer = producer <EOL> self . streamingProducer = streaming <EOL> if not streaming : <EOL> producer . resumeProducing ( ) <EOL> def unregisterProducer ( self ) : <EOL> self . producer = None <EOL> def stopConsuming ( self ) : <EOL> self . unregisterProducer ( ) <EOL> self . loseConnection ( ) <EOL> def writeSequence ( self , iovec ) : <EOL> self . write ( b"<STR_LIT>" . join ( iovec ) ) <EOL> def loseConnection ( self ) : <EOL> self . disconnecting = True <EOL> def abortConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> self . disconnecting = True <EOL> def reportDisconnect ( self ) : <EOL> if self . tls is not None : <EOL> err = NativeOpenSSLError ( ) <EOL> else : <EOL> err = self . disconnectReason <EOL> self . protocol . connectionLost ( Failure ( err ) ) <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def getPeer ( self ) : <EOL> return self . peerAddress <EOL> def getHost ( self ) : <EOL> return self . hostAddress <EOL> def resumeProducing ( self ) : <EOL> pass <EOL> def pauseProducing ( self ) : <EOL> pass <EOL> def stopProducing ( self ) : <EOL> self . loseConnection ( ) <EOL> def startTLS ( self , contextFactory , beNormal = True ) : <EOL> connectState = self . isServer ^ beNormal <EOL> self . tls = TLSNegotiation ( contextFactory , connectState ) <EOL> self . tlsbuf = [ ] <EOL> def getOutBuffer ( self ) : <EOL> """<STR_LIT>""" <EOL> S = self . stream <EOL> if S : <EOL> self . stream = [ ] <EOL> return b'<STR_LIT>' . join ( S ) <EOL> elif self . tls is not None : <EOL> if self . tls . readyToSend : <EOL> self . tls . sent = True <EOL> return self . tls <EOL> else : <EOL> return None <EOL> else : <EOL> return None <EOL> def bufferReceived ( self , buf ) : <EOL> if isinstance ( buf , TLSNegotiation ) : <EOL> assert self . tls is not None <EOL> if self . tls . sent : <EOL> self . tls . pretendToVerify ( buf , self ) <EOL> self . tls = None <EOL> b , self . tlsbuf = self . tlsbuf , None <EOL> self . writeSequence ( b ) <EOL> directlyProvides ( self , interfaces . ISSLTransport ) <EOL> else : <EOL> self . tls . readyToSend = True <EOL> else : <EOL> self . protocol . dataReceived ( buf ) <EOL> def makeFakeClient ( clientProtocol ) : <EOL> """<STR_LIT>""" <EOL> return FakeTransport ( clientProtocol , isServer = False ) <EOL> def makeFakeServer ( serverProtocol ) : <EOL> """<STR_LIT>""" <EOL> return FakeTransport ( serverProtocol , isServer = True ) <EOL> class IOPump : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , server , clientIO , serverIO , debug ) : <EOL> self . client = client <EOL> self . server = server <EOL> self . clientIO = clientIO <EOL> self . serverIO = serverIO <EOL> self . debug = debug <EOL> def flush ( self , debug = False ) : <EOL> """<STR_LIT>""" <EOL> result = False <EOL> for x in range ( <NUM_LIT:1000> ) : <EOL> if self . pump ( debug ) : <EOL> result = True <EOL> else : <EOL> break <EOL> else : <EOL> assert <NUM_LIT:0> , "<STR_LIT>" <EOL> return result <EOL> def pump ( self , debug = False ) : <EOL> """<STR_LIT>""" <EOL> if self . debug or debug : <EOL> print ( '<STR_LIT>' ) <EOL> sData = self . serverIO . getOutBuffer ( ) <EOL> cData = self . clientIO . getOutBuffer ( ) <EOL> self . clientIO . _checkProducer ( ) <EOL> self . serverIO . _checkProducer ( ) <EOL> if self . debug or debug : <EOL> print ( '<STR_LIT:.>' ) <EOL> if cData : <EOL> print ( '<STR_LIT>' + repr ( cData ) ) <EOL> if sData : <EOL> print ( '<STR_LIT>' + repr ( sData ) ) <EOL> if cData : <EOL> self . serverIO . bufferReceived ( cData ) <EOL> if sData : <EOL> self . clientIO . bufferReceived ( sData ) <EOL> if cData or sData : <EOL> return True <EOL> if ( self . serverIO . disconnecting and <EOL> not self . serverIO . disconnected ) : <EOL> if self . debug or debug : <EOL> print ( '<STR_LIT>' ) <EOL> self . serverIO . disconnected = True <EOL> self . clientIO . disconnecting = True <EOL> self . clientIO . reportDisconnect ( ) <EOL> return True <EOL> if self . clientIO . disconnecting and not self . clientIO . disconnected : <EOL> if self . debug or debug : <EOL> print ( '<STR_LIT>' ) <EOL> self . clientIO . disconnected = True <EOL> self . serverIO . disconnecting = True <EOL> self . serverIO . reportDisconnect ( ) <EOL> return True <EOL> return False <EOL> def connect ( serverProtocol , serverTransport , <EOL> clientProtocol , clientTransport , debug = False ) : <EOL> """<STR_LIT>""" <EOL> serverProtocol . makeConnection ( serverTransport ) <EOL> clientProtocol . makeConnection ( clientTransport ) <EOL> pump = IOPump ( <EOL> clientProtocol , serverProtocol , clientTransport , serverTransport , debug <EOL> ) <EOL> pump . flush ( ) <EOL> return pump <EOL> def connectedServerAndClient ( ServerClass , ClientClass , <EOL> clientTransportFactory = makeFakeClient , <EOL> serverTransportFactory = makeFakeServer , <EOL> debug = False ) : <EOL> """<STR_LIT>""" <EOL> c = ClientClass ( ) <EOL> s = ServerClass ( ) <EOL> cio = clientTransportFactory ( c ) <EOL> sio = serverTransportFactory ( s ) <EOL> return c , s , connect ( s , sio , c , cio , debug ) <EOL> def _factoriesShouldConnect ( clientInfo , serverInfo ) : <EOL> """<STR_LIT>""" <EOL> ( clientHost , clientPort , clientFactory , clientTimeout , <EOL> clientBindAddress ) = clientInfo <EOL> ( serverPort , serverFactory , serverBacklog , <EOL> serverInterface ) = serverInfo <EOL> if serverPort == clientPort : <EOL> return clientFactory , serverFactory <EOL> else : <EOL> return None <EOL> class ConnectionCompleter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , memoryReactor ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor = memoryReactor <EOL> def succeedOnce ( self , debug = False ) : <EOL> """<STR_LIT>""" <EOL> memoryReactor = self . _reactor <EOL> for clientIdx , clientInfo in enumerate ( memoryReactor . tcpClients ) : <EOL> for serverInfo in memoryReactor . tcpServers : <EOL> factories = _factoriesShouldConnect ( clientInfo , serverInfo ) <EOL> if factories : <EOL> memoryReactor . tcpClients . remove ( clientInfo ) <EOL> memoryReactor . connectors . pop ( clientIdx ) <EOL> clientFactory , serverFactory = factories <EOL> clientProtocol = clientFactory . buildProtocol ( None ) <EOL> serverProtocol = serverFactory . buildProtocol ( None ) <EOL> serverTransport = makeFakeServer ( serverProtocol ) <EOL> clientTransport = makeFakeClient ( clientProtocol ) <EOL> return connect ( serverProtocol , serverTransport , <EOL> clientProtocol , clientTransport , <EOL> debug ) <EOL> def failOnce ( self , reason = Failure ( ConnectionRefusedError ( ) ) ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor . tcpClients . pop ( <NUM_LIT:0> ) [ <NUM_LIT:2> ] . clientConnectionFailed ( <EOL> self . _reactor . connectors . pop ( <NUM_LIT:0> ) , reason <EOL> ) <EOL> def connectableEndpoint ( debug = False ) : <EOL> """<STR_LIT>""" <EOL> reactor = MemoryReactorClock ( ) <EOL> clientEndpoint = TCP4ClientEndpoint ( reactor , "<STR_LIT>" , <NUM_LIT> ) <EOL> serverEndpoint = TCP4ServerEndpoint ( reactor , <NUM_LIT> ) <EOL> serverEndpoint . listen ( Factory . forProtocol ( Protocol ) ) <EOL> return clientEndpoint , ConnectionCompleter ( reactor ) </s>
