2019-01-22 18:54:17 +01:00
|
|
|
import asyncio
|
2022-03-05 08:15:04 +01:00
|
|
|
import ipaddress
|
|
|
|
import time
|
2019-01-22 18:54:17 +01:00
|
|
|
import typing
|
|
|
|
import logging
|
2019-03-31 03:07:43 +02:00
|
|
|
import binascii
|
2019-09-25 17:39:34 +02:00
|
|
|
|
2021-07-20 15:50:24 +02:00
|
|
|
from lbry.dht.node import get_kademlia_peers_from_hosts
|
2019-11-19 19:57:14 +01:00
|
|
|
from lbry.error import DownloadSDTimeoutError
|
2021-07-20 15:50:24 +02:00
|
|
|
from lbry.utils import lru_cache_concurrent
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.stream.descriptor import StreamDescriptor
|
|
|
|
from lbry.blob_exchange.downloader import BlobDownloader
|
2022-03-08 03:35:12 +01:00
|
|
|
from lbry.torrent.tracker import subscribe_hash
|
2022-03-05 08:15:04 +01:00
|
|
|
|
2019-01-22 18:54:17 +01:00
|
|
|
if typing.TYPE_CHECKING:
|
2022-03-08 03:35:12 +01:00
|
|
|
from lbry.torrent.tracker import AnnounceResponse
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.conf import Config
|
|
|
|
from lbry.dht.node import Node
|
|
|
|
from lbry.blob.blob_manager import BlobManager
|
|
|
|
from lbry.blob.blob_file import AbstractBlob
|
|
|
|
from lbry.blob.blob_info import BlobInfo
|
2019-01-22 18:54:17 +01:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-03-31 03:07:43 +02:00
|
|
|
class StreamDownloader:
|
2019-08-05 15:24:23 +02:00
|
|
|
def __init__(self, loop: asyncio.AbstractEventLoop, config: 'Config', blob_manager: 'BlobManager', sd_hash: str,
|
2019-03-31 03:07:43 +02:00
|
|
|
descriptor: typing.Optional[StreamDescriptor] = None):
|
|
|
|
self.loop = loop
|
2019-01-30 20:57:09 +01:00
|
|
|
self.config = config
|
2019-03-31 03:07:43 +02:00
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.sd_hash = sd_hash
|
|
|
|
self.search_queue = asyncio.Queue(loop=loop) # blob hashes to feed into the iterative finder
|
|
|
|
self.peer_queue = asyncio.Queue(loop=loop) # new peers to try
|
|
|
|
self.blob_downloader = BlobDownloader(self.loop, self.config, self.blob_manager, self.peer_queue)
|
|
|
|
self.descriptor: typing.Optional[StreamDescriptor] = descriptor
|
2019-01-30 20:57:09 +01:00
|
|
|
self.node: typing.Optional['Node'] = None
|
2019-03-31 03:07:43 +02:00
|
|
|
self.accumulate_task: typing.Optional[asyncio.Task] = None
|
2019-01-30 20:57:09 +01:00
|
|
|
self.fixed_peers_handle: typing.Optional[asyncio.Handle] = None
|
2019-03-14 20:08:26 +01:00
|
|
|
self.fixed_peers_delay: typing.Optional[float] = None
|
|
|
|
self.added_fixed_peers = False
|
2019-03-31 19:42:27 +02:00
|
|
|
self.time_to_descriptor: typing.Optional[float] = None
|
|
|
|
self.time_to_first_bytes: typing.Optional[float] = None
|
2019-01-30 20:57:09 +01:00
|
|
|
|
2019-05-24 04:40:02 +02:00
|
|
|
async def cached_read_blob(blob_info: 'BlobInfo') -> bytes:
|
|
|
|
return await self.read_blob(blob_info, 2)
|
|
|
|
|
2020-12-23 22:37:31 +01:00
|
|
|
if self.blob_manager.decrypted_blob_lru_cache is not None:
|
2019-05-24 04:40:02 +02:00
|
|
|
cached_read_blob = lru_cache_concurrent(override_lru_cache=self.blob_manager.decrypted_blob_lru_cache)(
|
|
|
|
cached_read_blob
|
|
|
|
)
|
|
|
|
|
|
|
|
self.cached_read_blob = cached_read_blob
|
|
|
|
|
2019-03-31 03:07:43 +02:00
|
|
|
async def add_fixed_peers(self):
|
2021-07-20 15:50:24 +02:00
|
|
|
def _add_fixed_peers(fixed_peers):
|
|
|
|
self.peer_queue.put_nowait(fixed_peers)
|
2019-03-31 03:07:43 +02:00
|
|
|
self.added_fixed_peers = True
|
|
|
|
|
2020-05-08 16:58:29 +02:00
|
|
|
if not self.config.fixed_peers:
|
2019-03-31 03:07:43 +02:00
|
|
|
return
|
|
|
|
if 'dht' in self.config.components_to_skip or not self.node or not \
|
2020-01-03 06:35:38 +01:00
|
|
|
len(self.node.protocol.routing_table.get_peers()) > 0:
|
2019-03-31 03:07:43 +02:00
|
|
|
self.fixed_peers_delay = 0.0
|
|
|
|
else:
|
|
|
|
self.fixed_peers_delay = self.config.fixed_peer_delay
|
2021-07-20 15:50:24 +02:00
|
|
|
fixed_peers = await get_kademlia_peers_from_hosts(self.config.fixed_peers)
|
|
|
|
self.fixed_peers_handle = self.loop.call_later(self.fixed_peers_delay, _add_fixed_peers, fixed_peers)
|
2019-03-31 03:07:43 +02:00
|
|
|
|
2022-03-08 03:35:12 +01:00
|
|
|
async def _process_announcement(self, announcement: 'AnnounceResponse'):
|
|
|
|
peers = [(str(ipaddress.ip_address(peer.address)), peer.port) for peer in announcement.peers]
|
|
|
|
peers = await get_kademlia_peers_from_hosts(peers)
|
|
|
|
log.info("Found %d peers from tracker for %s", len(peers), self.sd_hash[:8])
|
|
|
|
self.peer_queue.put_nowait(peers)
|
2022-03-05 08:15:04 +01:00
|
|
|
|
2019-05-06 02:22:10 +02:00
|
|
|
async def load_descriptor(self, connection_id: int = 0):
|
2019-03-31 03:07:43 +02:00
|
|
|
# download or get the sd blob
|
|
|
|
sd_blob = self.blob_manager.get_blob(self.sd_hash)
|
|
|
|
if not sd_blob.get_is_verified():
|
2019-03-31 19:42:27 +02:00
|
|
|
try:
|
|
|
|
now = self.loop.time()
|
|
|
|
sd_blob = await asyncio.wait_for(
|
2019-05-06 02:22:10 +02:00
|
|
|
self.blob_downloader.download_blob(self.sd_hash, connection_id),
|
2019-03-31 19:42:27 +02:00
|
|
|
self.config.blob_download_timeout, loop=self.loop
|
|
|
|
)
|
|
|
|
log.info("downloaded sd blob %s", self.sd_hash)
|
|
|
|
self.time_to_descriptor = self.loop.time() - now
|
|
|
|
except asyncio.TimeoutError:
|
2019-11-19 19:57:14 +01:00
|
|
|
raise DownloadSDTimeoutError(self.sd_hash)
|
2019-03-31 03:07:43 +02:00
|
|
|
|
|
|
|
# parse the descriptor
|
|
|
|
self.descriptor = await StreamDescriptor.from_stream_descriptor_blob(
|
|
|
|
self.loop, self.blob_manager.blob_dir, sd_blob
|
|
|
|
)
|
|
|
|
log.info("loaded stream manifest %s", self.sd_hash)
|
|
|
|
|
2021-10-20 07:18:34 +02:00
|
|
|
async def start(self, node: typing.Optional['Node'] = None, connection_id: int = 0, save_stream=True):
|
2019-03-31 03:07:43 +02:00
|
|
|
# set up peer accumulation
|
2020-01-29 17:49:14 +01:00
|
|
|
self.node = node or self.node # fixme: this shouldnt be set here!
|
|
|
|
if self.node:
|
2019-05-13 18:14:08 +02:00
|
|
|
if self.accumulate_task and not self.accumulate_task.done():
|
|
|
|
self.accumulate_task.cancel()
|
2019-01-30 20:57:09 +01:00
|
|
|
_, self.accumulate_task = self.node.accumulate_peers(self.search_queue, self.peer_queue)
|
2019-03-31 03:07:43 +02:00
|
|
|
await self.add_fixed_peers()
|
2022-03-08 04:58:18 +01:00
|
|
|
subscribe_hash(
|
|
|
|
bytes.fromhex(self.sd_hash), lambda result: asyncio.ensure_future(self._process_announcement(result)))
|
2019-03-31 03:07:43 +02:00
|
|
|
# start searching for peers for the sd hash
|
2019-01-30 20:57:09 +01:00
|
|
|
self.search_queue.put_nowait(self.sd_hash)
|
2019-03-31 03:07:43 +02:00
|
|
|
log.info("searching for peers for stream %s", self.sd_hash)
|
|
|
|
|
|
|
|
if not self.descriptor:
|
2019-05-06 02:22:10 +02:00
|
|
|
await self.load_descriptor(connection_id)
|
2019-01-30 20:57:09 +01:00
|
|
|
|
2021-10-20 07:18:34 +02:00
|
|
|
if not await self.blob_manager.storage.stream_exists(self.sd_hash) and save_stream:
|
2019-03-31 03:07:43 +02:00
|
|
|
await self.blob_manager.storage.store_stream(
|
|
|
|
self.blob_manager.get_blob(self.sd_hash, length=self.descriptor.length), self.descriptor
|
|
|
|
)
|
|
|
|
|
2019-05-06 02:22:10 +02:00
|
|
|
async def download_stream_blob(self, blob_info: 'BlobInfo', connection_id: int = 0) -> 'AbstractBlob':
|
2019-08-02 19:14:41 +02:00
|
|
|
if not filter(lambda b: b.blob_hash == blob_info.blob_hash, self.descriptor.blobs[:-1]):
|
2019-03-31 03:07:43 +02:00
|
|
|
raise ValueError(f"blob {blob_info.blob_hash} is not part of stream with sd hash {self.sd_hash}")
|
2019-05-10 20:50:01 +02:00
|
|
|
blob = await asyncio.wait_for(
|
|
|
|
self.blob_downloader.download_blob(blob_info.blob_hash, blob_info.length, connection_id),
|
|
|
|
self.config.blob_download_timeout * 10, loop=self.loop
|
|
|
|
)
|
2019-03-31 03:07:43 +02:00
|
|
|
return blob
|
|
|
|
|
2019-04-17 19:32:24 +02:00
|
|
|
def decrypt_blob(self, blob_info: 'BlobInfo', blob: 'AbstractBlob') -> bytes:
|
2019-03-31 03:07:43 +02:00
|
|
|
return blob.decrypt(
|
|
|
|
binascii.unhexlify(self.descriptor.key.encode()), binascii.unhexlify(blob_info.iv.encode())
|
|
|
|
)
|
|
|
|
|
2019-05-06 02:22:10 +02:00
|
|
|
async def read_blob(self, blob_info: 'BlobInfo', connection_id: int = 0) -> bytes:
|
2019-03-31 19:42:27 +02:00
|
|
|
start = None
|
|
|
|
if self.time_to_first_bytes is None:
|
|
|
|
start = self.loop.time()
|
2019-05-06 02:22:10 +02:00
|
|
|
blob = await self.download_stream_blob(blob_info, connection_id)
|
2019-04-17 19:32:24 +02:00
|
|
|
decrypted = self.decrypt_blob(blob_info, blob)
|
2019-03-31 19:42:27 +02:00
|
|
|
if start:
|
|
|
|
self.time_to_first_bytes = self.loop.time() - start
|
|
|
|
return decrypted
|
2019-01-22 18:54:17 +01:00
|
|
|
|
2019-02-01 20:04:53 +01:00
|
|
|
def stop(self):
|
|
|
|
if self.accumulate_task:
|
2019-01-30 20:57:09 +01:00
|
|
|
self.accumulate_task.cancel()
|
|
|
|
self.accumulate_task = None
|
|
|
|
if self.fixed_peers_handle:
|
|
|
|
self.fixed_peers_handle.cancel()
|
|
|
|
self.fixed_peers_handle = None
|
2019-03-31 03:07:43 +02:00
|
|
|
self.blob_downloader.close()
|