def create_file(self, filename):
     session = mock.Mock(spec=Session.Session)(None, None)
     hash_announcer = DHTHashAnnouncer.DHTHashAnnouncer(None, None)
     session.blob_manager = BlobManager.TempBlobManager(hash_announcer)
     session.db_dir = self.tmp_dir
     manager = mock.Mock(spec=EncryptedFileManager.EncryptedFileManager)()
     handle = mocks.GenFile(3 * MB, '1')
     key = '2' * AES.block_size
     return EncryptedFileCreator.create_lbry_file(session, manager,
                                                  filename, handle, key,
                                                  iv_generator())
Ejemplo n.º 2
0
 def create_file(self, filename):
     session = mock.Mock(spec=Session.Session)(None, None)
     hash_announcer = DHTHashAnnouncer.DHTHashAnnouncer(None, None)
     self.blob_manager = BlobManager.DiskBlobManager(
         hash_announcer, self.tmp_blob_dir, self.tmp_db_dir)
     session.blob_manager = self.blob_manager
     yield session.blob_manager.setup()
     session.db_dir = self.tmp_db_dir
     manager = mock.Mock(spec=EncryptedFileManager.EncryptedFileManager)()
     handle = mocks.GenFile(3 * MB, '1')
     key = '2' * AES.block_size
     out = yield EncryptedFileCreator.create_lbry_file(
         session, manager, filename, handle, key, iv_generator())
     defer.returnValue(out)
Ejemplo n.º 3
0
    def setUp(self):
        mocks.mock_conf_settings(self)
        self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir()

        self.session = mock.Mock(spec=Session.Session)(None, None)
        self.session.payment_rate_manager.min_blob_data_payment_rate = 0
        self.blob_manager = BlobManager.DiskBlobManager(
            self.tmp_blob_dir, SQLiteStorage(self.tmp_db_dir))
        self.session.blob_manager = self.blob_manager
        self.session.storage = self.session.blob_manager.storage
        self.file_manager = EncryptedFileManager.EncryptedFileManager(
            self.session, object())
        yield self.session.blob_manager.storage.setup()
        yield self.session.blob_manager.setup()
Ejemplo n.º 4
0
def reseed_file(input_file, sd_blob):
    sd_blob = SdBlob.new_instance(sd_blob)
    db_dir = conf.settings['data_dir']
    blobfile_dir = os.path.join(db_dir, "blobfiles")
    announcer = HashAnnouncer.DummyHashAnnouncer()
    blob_manager = BlobManager.DiskBlobManager(announcer, blobfile_dir, db_dir)
    yield blob_manager.setup()
    creator = CryptStreamCreator.CryptStreamCreator(blob_manager, None,
                                                    sd_blob.key(),
                                                    sd_blob.iv_generator())
    file_sender = basic.FileSender()
    with open(input_file) as f:
        yield file_sender.beginFileTransfer(f, creator)
        yield creator.stop()
    for blob_info in sd_blob.blob_infos():
        if 'blob_hash' not in blob_info:
            # the last blob is always empty and without a hash
            continue
        blob = yield blob_manager.get_blob(blob_info['blob_hash'], True)
        if not blob.verified:
            print "Blob {} is not verified".format(blob)
def main(args=None):
    conf.initialize_settings()

    parser = argparse.ArgumentParser()
    parser.add_argument('destination', type=conf.server_port, nargs='+')
    parser.add_argument('--names', nargs='*')
    parser.add_argument('--limit', type=int)
    args = parser.parse_args(args)

    log_support.configure_console(level='INFO')

    db_dir = appdirs.user_data_dir('lighthouse-uploader')
    safe_makedirs(db_dir)
    # no need to persist metadata info
    storage = Wallet.InMemoryStorage()
    wallet = Wallet.LBRYumWallet(storage)
    blob_dir = os.path.join(db_dir, 'blobfiles')
    safe_makedirs(blob_dir)
    # Don't set a hash_announcer, we have no need to tell anyone we
    # have these blobs
    blob_manager = BlobManager.DiskBlobManager(None, blob_dir, db_dir)
    # TODO: make it so that I can disable the BlobAvailabilityTracker
    #       or, in general, make the session more reusable for users
    #       that only want part of the functionality
    session = Session.Session(
        blob_data_payment_rate=0,
        db_dir=db_dir,
        node_id=utils.generate_id(),
        blob_dir=blob_dir,
        dht_node_port=4444,
        known_dht_nodes=conf.settings['known_dht_nodes'],
        peer_port=3333,
        use_upnp=False,
        wallet=wallet,
        blob_manager=blob_manager,
    )
    assert session.wallet
    run(session, args.destination, args.names, args.limit)
    reactor.run()
Ejemplo n.º 6
0
    def setUp(self):
        mocks.mock_conf_settings(self)
        self.session = None
        self.stream_info_manager = None
        self.lbry_file_manager = None
        self.server_blob_manager = None
        self.reflector_port = None
        self.port = None
        self.addCleanup(self.take_down_env)
        wallet = mocks.Wallet()
        peer_manager = PeerManager.PeerManager()
        peer_finder = mocks.PeerFinder(5553, peer_manager, 2)
        hash_announcer = mocks.Announcer()
        rate_limiter = RateLimiter.DummyRateLimiter()
        sd_identifier = StreamDescriptor.StreamDescriptorIdentifier()

        self.expected_blobs = [
            ('dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b'
             '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152),
            ('f4067522c1b49432a2a679512e3917144317caa1abba0c04'
             '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152),
            ('305486c434260484fcb2968ce0e963b72f81ba56c11b08b1'
             'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056),
        ]

        self.db_dir, self.blob_dir = mk_db_and_blob_dir()
        self.session = Session.Session(
            conf.settings['data_rate'],
            db_dir=self.db_dir,
            lbryid="abcd",
            peer_finder=peer_finder,
            hash_announcer=hash_announcer,
            blob_dir=self.blob_dir,
            peer_port=5553,
            use_upnp=False,
            rate_limiter=rate_limiter,
            wallet=wallet,
            blob_tracker_class=mocks.BlobAvailabilityTracker,
            dht_node_class=Node)

        self.stream_info_manager = EncryptedFileMetadataManager.DBEncryptedFileMetadataManager(
            self.db_dir)

        self.lbry_file_manager = EncryptedFileManager.EncryptedFileManager(
            self.session, self.stream_info_manager, sd_identifier)

        self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir()
        self.server_blob_manager = BlobManager.DiskBlobManager(
            hash_announcer, self.server_blob_dir, self.server_db_dir)

        d = self.session.setup()
        d.addCallback(lambda _: self.stream_info_manager.setup())
        d.addCallback(lambda _: EncryptedFileOptions.
                      add_lbry_file_to_sd_identifier(sd_identifier))
        d.addCallback(lambda _: self.lbry_file_manager.setup())
        d.addCallback(lambda _: self.server_blob_manager.setup())

        def verify_equal(sd_info):
            self.assertEqual(mocks.create_stream_sd_file, sd_info)

        def save_sd_blob_hash(sd_hash):
            self.expected_blobs.append((sd_hash, 923))

        def verify_stream_descriptor_file(stream_hash):
            self.stream_hash = stream_hash
            d = lbry_file.get_sd_info(
                self.lbry_file_manager.stream_info_manager, stream_hash, True)
            d.addCallback(verify_equal)
            d.addCallback(lambda _: lbry_file.publish_sd_blob(
                self.lbry_file_manager.stream_info_manager, self.session.
                blob_manager, stream_hash))
            d.addCallback(save_sd_blob_hash)
            return d

        def create_stream():
            test_file = mocks.GenFile(
                5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)]))
            d = EncryptedFileCreator.create_lbry_file(
                self.session,
                self.lbry_file_manager,
                "test_file",
                test_file,
                key="0123456701234567",
                iv_generator=iv_generator())
            return d

        def start_server():
            server_factory = reflector.ServerFactory(peer_manager,
                                                     self.server_blob_manager)
            from twisted.internet import reactor
            port = 8943
            while self.reflector_port is None:
                try:
                    self.reflector_port = reactor.listenTCP(
                        port, server_factory)
                    self.port = port
                except error.CannotListenError:
                    port += 1

        d.addCallback(lambda _: create_stream())
        d.addCallback(verify_stream_descriptor_file)
        d.addCallback(lambda _: start_server())
        return d
Ejemplo n.º 7
0
    def setUp(self):
        self.reflector_port = None
        self.port = None
        mocks.mock_conf_settings(self)
        self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir()
        self.client_db_dir, self.client_blob_dir = mk_db_and_blob_dir()
        prm = OnlyFreePaymentsManager()
        wallet = mocks.Wallet()
        peer_manager = PeerManager.PeerManager()
        peer_finder = mocks.PeerFinder(5553, peer_manager, 2)
        self.server_storage = SQLiteStorage(self.server_db_dir)
        self.server_blob_manager = BlobManager.DiskBlobManager(
            self.server_blob_dir, self.server_storage)
        self.client_storage = SQLiteStorage(self.client_db_dir)
        self.client_blob_manager = BlobManager.DiskBlobManager(
            self.client_blob_dir, self.client_storage)
        self.server_lbry_file_manager = EncryptedFileManager(
            peer_finder, DummyRateLimiter(), self.server_blob_manager, wallet,
            prm, self.server_storage,
            StreamDescriptor.StreamDescriptorIdentifier())
        self.client_lbry_file_manager = EncryptedFileManager(
            peer_finder, DummyRateLimiter(), self.client_blob_manager, wallet,
            prm, self.client_storage,
            StreamDescriptor.StreamDescriptorIdentifier())

        self.expected_blobs = [
            ('dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b'
             '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152),
            ('f4067522c1b49432a2a679512e3917144317caa1abba0c04'
             '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152),
            ('305486c434260484fcb2968ce0e963b72f81ba56c11b08b1'
             'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056),
        ]

        d = self.server_storage.setup()
        d.addCallback(lambda _: self.server_blob_manager.setup())
        d.addCallback(lambda _: self.server_lbry_file_manager.setup())
        d.addCallback(lambda _: self.client_storage.setup())
        d.addCallback(lambda _: self.client_blob_manager.setup())
        d.addCallback(lambda _: self.client_lbry_file_manager.setup())

        @defer.inlineCallbacks
        def verify_equal(sd_info, stream_hash):
            self.assertDictEqual(mocks.create_stream_sd_file, sd_info)
            sd_hash = yield self.client_storage.get_sd_blob_hash_for_stream(
                stream_hash)
            defer.returnValue(sd_hash)

        def save_sd_blob_hash(sd_hash):
            self.sd_hash = sd_hash
            self.expected_blobs.append((sd_hash, 923))

        def verify_stream_descriptor_file(stream_hash):
            self.stream_hash = stream_hash
            d = get_sd_info(self.client_storage, stream_hash, True)
            d.addCallback(verify_equal, stream_hash)
            d.addCallback(save_sd_blob_hash)
            return d

        def create_stream():
            test_file = mocks.GenFile(5209343,
                                      bytes((i + 3) for i in range(0, 64, 6)))
            d = EncryptedFileCreator.create_lbry_file(
                self.client_blob_manager,
                self.client_storage,
                prm,
                self.client_lbry_file_manager,
                "test_file",
                test_file,
                key=b"0123456701234567",
                iv_generator=iv_generator())
            d.addCallback(lambda lbry_file: lbry_file.stream_hash)
            return d

        def start_server():
            server_factory = reflector.ServerFactory(
                peer_manager, self.server_blob_manager,
                self.server_lbry_file_manager)
            from twisted.internet import reactor
            port = 8943
            while self.reflector_port is None:
                try:
                    self.reflector_port = reactor.listenTCP(
                        port, server_factory)
                    self.port = port
                except error.CannotListenError:
                    port += 1

        d.addCallback(lambda _: create_stream())
        d.addCallback(verify_stream_descriptor_file)
        d.addCallback(lambda _: start_server())
        return d
Ejemplo n.º 8
0
    def test_reflector(self):
        wallet = mocks.Wallet()
        peer_manager = PeerManager.PeerManager()
        peer_finder = mocks.PeerFinder(5553, peer_manager, 2)
        hash_announcer = mocks.Announcer()
        rate_limiter = RateLimiter.DummyRateLimiter()
        sd_identifier = StreamDescriptor.StreamDescriptorIdentifier()

        self.expected_blobs = [
            ('dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b'
             '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152),
            ('f4067522c1b49432a2a679512e3917144317caa1abba0c04'
             '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152),
            ('305486c434260484fcb2968ce0e963b72f81ba56c11b08b1'
             'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056),
        ]

        db_dir = "client"
        os.mkdir(db_dir)

        self.session = Session.Session(
            conf.MIN_BLOB_DATA_PAYMENT_RATE,
            db_dir=db_dir,
            lbryid="abcd",
            peer_finder=peer_finder,
            hash_announcer=hash_announcer,
            blob_dir=None,
            peer_port=5553,
            use_upnp=False,
            rate_limiter=rate_limiter,
            wallet=wallet,
            blob_tracker_class=mocks.DummyBlobAvailabilityTracker,
            dht_node_class=Node)

        self.stream_info_manager = EncryptedFileMetadataManager.TempEncryptedFileMetadataManager(
        )

        self.lbry_file_manager = EncryptedFileManager.EncryptedFileManager(
            self.session, self.stream_info_manager, sd_identifier)

        self.server_blob_manager = BlobManager.TempBlobManager(hash_announcer)

        d = self.session.setup()
        d.addCallback(lambda _: self.stream_info_manager.setup())
        d.addCallback(lambda _: EncryptedFileOptions.
                      add_lbry_file_to_sd_identifier(sd_identifier))
        d.addCallback(lambda _: self.lbry_file_manager.setup())
        d.addCallback(lambda _: self.server_blob_manager.setup())

        def verify_equal(sd_info):
            self.assertEqual(mocks.create_stream_sd_file, sd_info)

        def save_sd_blob_hash(sd_hash):
            self.expected_blobs.append((sd_hash, 923))

        def verify_stream_descriptor_file(stream_hash):
            d = lbryfile.get_sd_info(
                self.lbry_file_manager.stream_info_manager, stream_hash, True)
            d.addCallback(verify_equal)
            d.addCallback(lambda _: lbryfile.publish_sd_blob(
                self.lbry_file_manager.stream_info_manager, self.session.
                blob_manager, stream_hash))
            d.addCallback(save_sd_blob_hash)
            d.addCallback(lambda _: stream_hash)
            return d

        def create_stream():
            test_file = mocks.GenFile(
                5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)]))
            d = EncryptedFileCreator.create_lbry_file(
                self.session,
                self.lbry_file_manager,
                "test_file",
                test_file,
                key="0123456701234567",
                iv_generator=iv_generator())
            return d

        def start_server():
            server_factory = reflector.ServerFactory(peer_manager,
                                                     self.server_blob_manager)
            from twisted.internet import reactor
            port = 8943
            while self.reflector_port is None:
                try:
                    self.reflector_port = reactor.listenTCP(
                        port, server_factory)
                except error.CannotListenError:
                    port += 1
            return defer.succeed(port)

        def send_to_server(port, stream_hash):
            factory = reflector.ClientFactory(self.session.blob_manager,
                                              self.stream_info_manager,
                                              stream_hash)

            from twisted.internet import reactor
            reactor.connectTCP('localhost', port, factory)
            return factory.finished_deferred

        def verify_blob_completed(blob, blob_size):
            self.assertTrue(blob.is_validated())
            self.assertEqual(blob_size, blob.length)

        def verify_have_blob(blob_hash, blob_size):
            d = self.server_blob_manager.get_blob(blob_hash, True)
            d.addCallback(lambda blob: verify_blob_completed(blob, blob_size))
            return d

        def verify_data_on_reflector():
            check_blob_ds = []
            for blob_hash, blob_size in self.expected_blobs:
                check_blob_ds.append(verify_have_blob(blob_hash, blob_size))
            return defer.DeferredList(check_blob_ds)

        def upload_to_reflector(stream_hash):
            d = start_server()
            d.addCallback(lambda port: send_to_server(port, stream_hash))
            d.addCallback(lambda _: verify_data_on_reflector())
            return d

        d.addCallback(lambda _: create_stream())
        d.addCallback(verify_stream_descriptor_file)
        d.addCallback(upload_to_reflector)
        return d