def initializeCallbackProtocol(self, peerPort, srcAddr, srcPort, dstAddr, dstPort, higherProtocol, d, retryCount=10): if not self.__uninitializedConnections.has_key(peerPort): if retryCount: callLater(.25, self.initializeCallbackProtocol, peerPort, srcAddr, srcPort, dstAddr, dstPort, higherProtocol, d, retryCount - 1) return # retry exhausted errReporter.warning( "Received 'init' for a connection %d that does not exist." % peerPort) d.errback(ConnectError) return protocol = self.__uninitializedConnections[peerPort] g_logger.info( "Callback protocol %s initialized for gate %s:%d to %s:%d" % (protocol, srcAddr, srcPort, dstAddr, dstPort)) del self.__uninitializedConnections[peerPort] self.__initializedConnections[protocol] = higherProtocol transport = GateTransport(srcAddr, srcPort, dstAddr, dstPort, protocol.transport) protocol.setHigherProtocol(higherProtocol) protocol.completeConnection(transport) connectionMade_d = protocol.waitForHigherConnection() connectionMade_d.addCallback(self.__higherProtocolConnected, d) connectionMade_d.addErrback(d.errback)
def maintainGates(hpFactory, port, stack, logerror, grabber, starter): for addr in grabber.latestpeers: g2gconn = starter.createHPGate(addr) if g2gconn: callLater(.25, connectEndpointToGate, port, g2gconn, stack, hpFactory, logerror) grabber.updateList() callLater(.75, maintainGates, hpFactory, port, networkStack, logerror, grabber, starter)
def makeHigherConnection(self, higherTransport): self.higherProtocol().makeConnection(higherTransport) if self.__higherConnectionDeferred: callLater(0, self.__higherConnectionDeferred.callback, self.higherProtocol())
def waitForHigherConnection(self): self.__higherConnectionDeferred = Deferred() if self.higherProtocol().transport: callLater(0, self.__higherConnectionDeferred.callback, self.higherProtocol()) return self.__higherConnectionDeferred
def connectionLost(self, reason=connectionDone): callLater(1.0, reactor.stop)
def __handleError(self, signal, data): errReporter.error("Entered error state on signal %s with data %s" % (signal, data)) callLater(0, self.close)
def writeMessageAndClose(self, msg): self.transport.write(msg.__serialize__()) callLater(0, self.close)
exec("import " + stack) networkStack = eval(stack) else: networkStack = None # Do logging things logger = logging.getLogger(__name__) logctx = playgroundlog.LoggingContext("GATESTARTER_") # Uncomment the next line to turn on "packet tracing" #logctx.doPacketTracing = True playgroundlog.startLogging(logctx) playgroundlog.UseStdErrHandler(True) # start grabber grabber = createLGService(chapAddr=chapAddr, chapPort=chapPort) # start gate starter starter = GateStarter(logctx, chapAddr, chapPort, addrFile) # This guy will be the server. Create an instance of the factory hpProtocolServerFactory = HoneypotServerFactory() # tell the playground client to connect to playground server and start running callLater(1, maintainGates, hpProtocolServerFactory, portNum, networkStack, logger.error, grabber, starter) TwistedShutdownErrorHandler.HandleRootFatalErrors() reactor.run()