Beispiel #1
0
 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)
Beispiel #2
0
    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)
Beispiel #3
0
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)
Beispiel #4
0
 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)
Beispiel #5
0
 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)
Beispiel #6
0
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)
Beispiel #7
0
 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)
Beispiel #8
0
 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)
Beispiel #9
0
    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))
Beispiel #10
0
 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)
Beispiel #11
0
    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
Beispiel #12
0
 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)
Beispiel #13
0
 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))
Beispiel #14
0
 def handlePort(port):
     if port is None:
         self.deferred.cancel()
     serverAddr = I2PAddress(self.localDest)
     p = I2PListeningPort(port, wrappedFactory, serverAddr)
     return p
Beispiel #15
0
 def test_portString(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p', '81')
     self.assertEqual((addr.destination, addr.host, addr.port),
                      (TEST_B64, 'spam.i2p', 81))
Beispiel #16
0
 def test_reprWithHostPort(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p', 81)
     self.assertEqual(repr(addr), 'I2PAddress(spam.i2p, 81)')
Beispiel #17
0
 def test_reprWithPortString(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p', '81')
     self.assertEqual(repr(addr), 'I2PAddress(spam.i2p, 81)')
Beispiel #18
0
 def test_reprWithNoHostPort(self):
     addr = I2PAddress(TEST_B64, port=81)
     self.assertEqual(repr(addr), 'I2PAddress(%s, 81)' % TEST_B32)
Beispiel #19
0
 def test_noHost_port(self):
     addr = I2PAddress(TEST_B64, port=81)
     self.assertEqual((addr.destination, addr.host, addr.port),
                      (TEST_B64, TEST_B32, 81))
Beispiel #20
0
 def test_hashWithPortString(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p', '81')
     self.assertEqual(hash(addr), hash(('spam.i2p', 81)))
Beispiel #21
0
 def test_hashWithHostPort(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p', 81)
     self.assertEqual(hash(addr), hash(('spam.i2p', 81)))
Beispiel #22
0
 def test_hashWithNoHostPort(self):
     addr = I2PAddress(TEST_B64, port=81)
     self.assertEqual(hash(addr), hash((TEST_B32, 81)))
Beispiel #23
0
 def test_hashWithNoHostNoPort(self):
     addr = I2PAddress(TEST_B64)
     self.assertEqual(hash(addr), hash((TEST_B32, None)))
Beispiel #24
0
 def test_host_noPort(self):
     addr = I2PAddress(TEST_B64, 'spam.i2p')
     self.assertEqual((addr.destination, addr.host, addr.port),
                      (TEST_B64, 'spam.i2p', None))
Beispiel #25
0
 def test_reprWithNoHostNoPort(self):
     addr = I2PAddress(TEST_B64)
     self.assertEqual(repr(addr), 'I2PAddress(%s)' % TEST_B32)