def test_generateDestination(self): proto = proto_helpers.AccumulatingProtocol() samEndpoint = FakeEndpoint() samEndpoint.deferred = defer.succeed(None) samEndpoint.facDeferred = defer.succeed(I2PAddress(TEST_B64)) d = session.generateDestination('', samEndpoint) s = self.successResultOf(d) self.assertEqual(1, samEndpoint.called) self.assertEqual(I2PAddress(TEST_B64), s)
def i2pTunnelCreated(self): if self._writeKeypair: try: f = open(self._keyfile, 'w') f.write(self.keypair) f.close() except IOError: print 'Could not save keypair' # BOB will now forward data to a listener. # BOB only forwards to TCP4 (for now). serverEndpoint = TCP4ServerEndpoint(self._reactor, self.outport) # Wrap the server Factory. wrappedFactory = BOBServerFactoryWrapper(self._serverFactory, self._bobEndpoint, I2PAddress(self.localDest), self.tunnelNick, self.removeTunnelWhenFinished) d = serverEndpoint.listen(wrappedFactory) def handlePort(port): if port is None: self.deferred.cancel() serverAddr = I2PAddress(self.localDest) p = I2PListeningPort(port, wrappedFactory, serverAddr) return p d.addCallback(handlePort) # When the Deferred returns an IListeningPort, pass it on. d.chainDeferred(self.deferred)
def peerSAM(data): peerInfo = data.decode('utf-8').split('\n')[0].split(' ') peerOptions = { x: y for x, y in [x.split('=', 1) for x in peerInfo[1:] if x] } fromPort = peerOptions['FROM_PORT'] if 'FROM_PORT' in peerOptions else None return I2PAddress(peerInfo[0], port=fromPort)
def connectionMade(self): # Substitute transport for an I2P wrapper self.transport = I2PTunnelTransport(self.transport, self._clientAddr, I2PAddress(self.dest)) self.isConnected = False # First line sent must be the Destination to connect to. self.transport.write(self.dest + '\n') self.wrappedProto.makeConnection(self.transport)
def streamConnectionEstablished(self, streamProto): self.session.addStream(streamProto) peerAddress = I2PAddress(self.dest, self.host, self.port) proto = self._clientFactory.buildProtocol(peerAddress) if proto is None: self.deferred.cancel() return streamProto.wrapProto(proto, peerAddress) self.deferred.callback(proto)
def peerSAM(data): peerInfo = data.split('\n')[0].split(' ') peerOptions = { x: y for x, y in [x.split('=', 1) for x in peerInfo[1:] if x] } fromPort = peerOptions['FROM_PORT'] if peerOptions.has_key( 'FROM_PORT') else None return I2PAddress(peerInfo[0], port=fromPort)
def wrapProto(self, proto, peerAddress, invertTLS=False): self.wrappedProto = proto if hasattr(self.factory, 'localPort'): localAddress = I2PAddress(self.factory.session.address, port=self.factory.localPort) else: localAddress = self.factory.session.address self.transportWrapper = I2PTunnelTransport(self.sender.transport, localAddress, peerAddress, invertTLS) proto.makeConnection(self.transportWrapper)
def test_destGenerated(self): tmp = '/tmp/TestDestGenerateFactory.privKey' mreactor = proto_helpers.MemoryReactor() fac, proto = self.makeProto(tmp) # Shortcut to end of SAM dest generate protocol proto.receiver.currentRule = 'State_dest' proto._parser._setupInterp() proto.dataReceived('DEST REPLY PUB=%s PRIV=%s\n' % (TEST_B64, 'TEST_PRIV')) s = self.successResultOf(fac.deferred) self.assertEqual(I2PAddress(TEST_B64), s) os.remove(tmp)
def destGenerated(self, pubKey, privKey): if os.path.exists(self._keyfile): self.deferred.errback( failure.Failure(ValueError('The keyfile already exists'))) return try: f = open(self._keyfile, 'w') f.write(str(privKey)) f.close() self.deferred.callback(I2PAddress(pubKey)) except IOError as e: self.deferred.errback(failure.Failure(e))
def test_streamAcceptIncomingAfterPeerAddress(self): fac, proto = self.makeProto() fac.streamAcceptIncoming = Mock() fac.session = Mock() fac.session.id = 'foo' fac.forwardPort = 1337 # Shortcut to end of SAM stream accept protocol proto.receiver.currentRule = 'State_readData' proto._parser._setupInterp() proto.dataReceived('%s FROM_PORT=34444 TO_PORT=0\n' % TEST_B64) fac.streamAcceptIncoming.assert_called_with(proto.receiver) self.assertEquals(I2PAddress(TEST_B64, port=34444), proto.receiver.peer)
def createSession((samVersion, style, id, proto, pubKey, localPort)): s = SAMSession() s.nickname = nickname s.samEndpoint = samEndpoint s.samVersion = samVersion s.style = style s.id = id s.address = I2PAddress(pubKey, port=localPort) s._proto = proto s._autoClose = autoClose _sessions[nickname] = s waiting = _pending_sessions.pop(nickname, []) for d in waiting: d.callback(s) return s
def i2pTunnelCreated(self): # BOB is now listening for a tunnel. # BOB only listens on TCP4 (for now). clientEndpoint = TCP4ClientEndpoint(self._reactor, self.inhost, self.inport) # Wrap the client Factory. wrappedFactory = BOBClientFactoryWrapper(self._clientFactory, self._bobEndpoint, I2PAddress(self.localDest), self.tunnelNick, self.removeTunnelWhenFinished) wrappedFactory.setDest(self.dest) d = clientEndpoint.connect(wrappedFactory) def checkProto(proto): if proto is None: self.deferred.cancel() return proto d.addCallback(checkProto) # When the Deferred returns an IProtocol, pass it on. d.chainDeferred(self.deferred)
def test_address_host_port(self): addr = I2PAddress(TEST_B64, 'spam.i2p', 81) addr2 = I2PAddress(addr, host='eggs.i2p', port=82) self.assertEqual((addr2.destination, addr2.host, addr2.port), (TEST_B64, 'eggs.i2p', 82))
def handlePort(port): if port is None: self.deferred.cancel() serverAddr = I2PAddress(self.localDest) p = I2PListeningPort(port, wrappedFactory, serverAddr) return p
def test_portString(self): addr = I2PAddress(TEST_B64, 'spam.i2p', '81') self.assertEqual((addr.destination, addr.host, addr.port), (TEST_B64, 'spam.i2p', 81))
def test_reprWithHostPort(self): addr = I2PAddress(TEST_B64, 'spam.i2p', 81) self.assertEqual(repr(addr), 'I2PAddress(spam.i2p, 81)')
def test_reprWithPortString(self): addr = I2PAddress(TEST_B64, 'spam.i2p', '81') self.assertEqual(repr(addr), 'I2PAddress(spam.i2p, 81)')
def test_reprWithNoHostPort(self): addr = I2PAddress(TEST_B64, port=81) self.assertEqual(repr(addr), 'I2PAddress(%s, 81)' % TEST_B32)
def test_noHost_port(self): addr = I2PAddress(TEST_B64, port=81) self.assertEqual((addr.destination, addr.host, addr.port), (TEST_B64, TEST_B32, 81))
def test_hashWithPortString(self): addr = I2PAddress(TEST_B64, 'spam.i2p', '81') self.assertEqual(hash(addr), hash(('spam.i2p', 81)))
def test_hashWithHostPort(self): addr = I2PAddress(TEST_B64, 'spam.i2p', 81) self.assertEqual(hash(addr), hash(('spam.i2p', 81)))
def test_hashWithNoHostPort(self): addr = I2PAddress(TEST_B64, port=81) self.assertEqual(hash(addr), hash((TEST_B32, 81)))
def test_hashWithNoHostNoPort(self): addr = I2PAddress(TEST_B64) self.assertEqual(hash(addr), hash((TEST_B32, None)))
def test_host_noPort(self): addr = I2PAddress(TEST_B64, 'spam.i2p') self.assertEqual((addr.destination, addr.host, addr.port), (TEST_B64, 'spam.i2p', None))
def test_reprWithNoHostNoPort(self): addr = I2PAddress(TEST_B64) self.assertEqual(repr(addr), 'I2PAddress(%s)' % TEST_B32)