2016-08-04 05:03:14 +02:00
|
|
|
"""
|
|
|
|
The reflector protocol (all dicts encoded in json):
|
|
|
|
|
|
|
|
Client Handshake (sent once per connection, at the start of the connection):
|
|
|
|
|
|
|
|
{
|
|
|
|
'version': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Server Handshake (sent once per connection, after receiving the client handshake):
|
|
|
|
|
|
|
|
{
|
|
|
|
'version': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Client Info Request:
|
|
|
|
|
|
|
|
{
|
|
|
|
'blob_hash': "<blob_hash>",
|
|
|
|
'blob_size': <blob_size>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Server Info Response (sent in response to Client Info Request):
|
|
|
|
|
|
|
|
{
|
2016-08-08 04:33:40 +02:00
|
|
|
'send_blob': True|False
|
2016-08-04 05:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
If response is 'YES', client may send a Client Blob Request or a Client Info Request.
|
|
|
|
If response is 'NO', client may only send a Client Info Request
|
|
|
|
|
|
|
|
|
|
|
|
Client Blob Request:
|
|
|
|
|
|
|
|
{} # Yes, this is an empty dictionary, in case something needs to go here in the future
|
|
|
|
<raw blob_data> # this blob data must match the info sent in the most recent Client Info Request
|
|
|
|
|
|
|
|
|
|
|
|
Server Blob Response (sent in response to Client Blob Request):
|
|
|
|
{
|
2016-08-08 04:33:40 +02:00
|
|
|
'received_blob': True
|
2016-08-04 05:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Client may now send another Client Info Request
|
|
|
|
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import logging
|
2016-11-10 23:16:35 +01:00
|
|
|
|
2016-08-08 04:33:40 +02:00
|
|
|
from twisted.protocols.basic import FileSender
|
2016-08-04 05:03:14 +02:00
|
|
|
from twisted.internet.protocol import Protocol, ClientFactory
|
2016-08-09 06:59:50 +02:00
|
|
|
from twisted.internet import defer, error
|
2016-08-04 05:03:14 +02:00
|
|
|
|
2016-11-10 23:18:15 +01:00
|
|
|
from lbrynet.reflector.common import IncompleteResponse
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
|
2016-11-10 23:18:15 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileReflectorClient(Protocol):
|
2016-08-08 04:33:40 +02:00
|
|
|
# Protocol stuff
|
2016-08-04 05:03:14 +02:00
|
|
|
def connectionMade(self):
|
2016-08-08 04:33:40 +02:00
|
|
|
self.blob_manager = self.factory.blob_manager
|
2016-08-04 05:03:14 +02:00
|
|
|
self.response_buff = ''
|
|
|
|
self.outgoing_buff = ''
|
|
|
|
self.blob_hashes_to_send = []
|
|
|
|
self.next_blob_to_send = None
|
2016-08-08 04:33:40 +02:00
|
|
|
self.blob_read_handle = None
|
2016-08-04 05:03:14 +02:00
|
|
|
self.received_handshake_response = False
|
2016-08-08 04:33:40 +02:00
|
|
|
self.protocol_version = None
|
|
|
|
self.file_sender = None
|
2016-08-09 06:59:50 +02:00
|
|
|
self.producer = None
|
|
|
|
self.streaming = False
|
2016-08-04 05:03:14 +02:00
|
|
|
d = self.get_blobs_to_send(self.factory.stream_info_manager, self.factory.stream_hash)
|
|
|
|
d.addCallback(lambda _: self.send_handshake())
|
2016-11-10 23:16:35 +01:00
|
|
|
d.addErrback(
|
|
|
|
lambda err: log.warning("An error occurred immediately: %s", err.getTraceback()))
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
def dataReceived(self, data):
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('Recieved %s', data)
|
2016-08-04 05:03:14 +02:00
|
|
|
self.response_buff += data
|
|
|
|
try:
|
|
|
|
msg = self.parse_response(self.response_buff)
|
2016-11-10 23:18:15 +01:00
|
|
|
except IncompleteResponse:
|
2016-08-04 05:03:14 +02:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.response_buff = ''
|
|
|
|
d = self.handle_response(msg)
|
2016-08-09 06:59:50 +02:00
|
|
|
d.addCallback(lambda _: self.send_next_request())
|
|
|
|
d.addErrback(self.response_failure_handler)
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
2016-08-09 06:59:50 +02:00
|
|
|
if reason.check(error.ConnectionDone):
|
2017-01-25 19:03:48 +01:00
|
|
|
log.info('Finished sending data via reflector')
|
2016-08-09 06:59:50 +02:00
|
|
|
self.factory.finished_deferred.callback(True)
|
|
|
|
else:
|
2016-11-01 18:05:19 +01:00
|
|
|
log.debug('Reflector finished: %s', reason)
|
2016-08-09 06:59:50 +02:00
|
|
|
self.factory.finished_deferred.callback(reason)
|
2016-08-04 05:03:14 +02:00
|
|
|
|
2016-08-08 04:33:40 +02:00
|
|
|
# IConsumer stuff
|
|
|
|
|
|
|
|
def registerProducer(self, producer, streaming):
|
2016-08-09 06:59:50 +02:00
|
|
|
self.producer = producer
|
|
|
|
self.streaming = streaming
|
|
|
|
if self.streaming is False:
|
|
|
|
from twisted.internet import reactor
|
|
|
|
reactor.callLater(0, self.producer.resumeProducing)
|
2016-08-08 04:33:40 +02:00
|
|
|
|
|
|
|
def unregisterProducer(self):
|
2016-08-09 06:59:50 +02:00
|
|
|
self.producer = None
|
2016-08-08 04:33:40 +02:00
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self.transport.write(data)
|
2016-08-09 06:59:50 +02:00
|
|
|
if self.producer is not None and self.streaming is False:
|
|
|
|
from twisted.internet import reactor
|
|
|
|
reactor.callLater(0, self.producer.resumeProducing)
|
2016-08-08 04:33:40 +02:00
|
|
|
|
2016-08-04 05:03:14 +02:00
|
|
|
def get_blobs_to_send(self, stream_info_manager, stream_hash):
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('Getting blobs from stream hash: %s', stream_hash)
|
2016-08-04 05:03:14 +02:00
|
|
|
d = stream_info_manager.get_blobs_for_stream(stream_hash)
|
|
|
|
|
|
|
|
def set_blobs(blob_hashes):
|
|
|
|
for blob_hash, position, iv, length in blob_hashes:
|
2016-08-09 06:59:50 +02:00
|
|
|
if blob_hash is not None:
|
|
|
|
self.blob_hashes_to_send.append(blob_hash)
|
2017-01-25 19:03:48 +01:00
|
|
|
log.debug("Preparing to send %i blobs", len(self.blob_hashes_to_send))
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
d.addCallback(set_blobs)
|
2016-08-04 05:11:29 +02:00
|
|
|
|
2016-08-09 06:59:50 +02:00
|
|
|
d.addCallback(lambda _: stream_info_manager.get_sd_blob_hashes_for_stream(stream_hash))
|
2016-08-04 05:11:29 +02:00
|
|
|
|
|
|
|
def set_sd_blobs(sd_blob_hashes):
|
2016-08-09 06:59:50 +02:00
|
|
|
for sd_blob_hash in sd_blob_hashes:
|
2016-08-04 05:11:29 +02:00
|
|
|
self.blob_hashes_to_send.append(sd_blob_hash)
|
2017-01-25 19:03:48 +01:00
|
|
|
log.debug("Preparing to send %i sd blobs", len(sd_blob_hashes))
|
2016-08-09 06:59:50 +02:00
|
|
|
|
2016-08-04 05:11:29 +02:00
|
|
|
d.addCallback(set_sd_blobs)
|
2016-08-04 05:03:14 +02:00
|
|
|
return d
|
|
|
|
|
2016-08-09 06:59:50 +02:00
|
|
|
def send_handshake(self):
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('Sending handshake')
|
2016-08-09 06:59:50 +02:00
|
|
|
self.write(json.dumps({'version': 0}))
|
|
|
|
|
2016-08-04 05:03:14 +02:00
|
|
|
def parse_response(self, buff):
|
|
|
|
try:
|
|
|
|
return json.loads(buff)
|
|
|
|
except ValueError:
|
2016-11-10 23:18:15 +01:00
|
|
|
raise IncompleteResponse()
|
2016-08-04 05:03:14 +02:00
|
|
|
|
2016-08-09 06:59:50 +02:00
|
|
|
def response_failure_handler(self, err):
|
|
|
|
log.warning("An error occurred handling the response: %s", err.getTraceback())
|
|
|
|
|
2016-08-04 05:03:14 +02:00
|
|
|
def handle_response(self, response_dict):
|
|
|
|
if self.received_handshake_response is False:
|
2016-08-08 04:33:40 +02:00
|
|
|
return self.handle_handshake_response(response_dict)
|
2016-08-04 05:03:14 +02:00
|
|
|
else:
|
2016-08-08 04:33:40 +02:00
|
|
|
return self.handle_normal_response(response_dict)
|
|
|
|
|
|
|
|
def set_not_uploading(self):
|
2016-08-09 06:59:50 +02:00
|
|
|
if self.next_blob_to_send is not None:
|
|
|
|
self.next_blob_to_send.close_read_handle(self.read_handle)
|
2016-08-08 04:33:40 +02:00
|
|
|
self.read_handle = None
|
2016-08-09 06:59:50 +02:00
|
|
|
self.next_blob_to_send = None
|
2016-08-08 04:33:40 +02:00
|
|
|
self.file_sender = None
|
2016-08-09 06:59:50 +02:00
|
|
|
return defer.succeed(None)
|
2016-08-08 04:33:40 +02:00
|
|
|
|
|
|
|
def start_transfer(self):
|
2016-08-09 06:59:50 +02:00
|
|
|
self.write(json.dumps({}))
|
2016-11-30 21:20:45 +01:00
|
|
|
assert self.read_handle is not None, \
|
|
|
|
"self.read_handle was None when trying to start the transfer"
|
2016-08-08 04:33:40 +02:00
|
|
|
d = self.file_sender.beginFileTransfer(self.read_handle, self)
|
|
|
|
return d
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
def handle_handshake_response(self, response_dict):
|
2016-08-08 04:33:40 +02:00
|
|
|
if 'version' not in response_dict:
|
|
|
|
raise ValueError("Need protocol version number!")
|
|
|
|
self.protocol_version = int(response_dict['version'])
|
|
|
|
if self.protocol_version != 0:
|
|
|
|
raise ValueError("I can't handle protocol version {}!".format(self.protocol_version))
|
|
|
|
self.received_handshake_response = True
|
2016-08-09 06:59:50 +02:00
|
|
|
return defer.succeed(True)
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
def handle_normal_response(self, response_dict):
|
2016-08-09 06:59:50 +02:00
|
|
|
if self.file_sender is None: # Expecting Server Info Response
|
2016-08-08 04:33:40 +02:00
|
|
|
if 'send_blob' not in response_dict:
|
|
|
|
raise ValueError("I don't know whether to send the blob or not!")
|
|
|
|
if response_dict['send_blob'] is True:
|
|
|
|
self.file_sender = FileSender()
|
2016-08-09 06:59:50 +02:00
|
|
|
return defer.succeed(True)
|
2016-08-08 04:33:40 +02:00
|
|
|
else:
|
2017-01-25 19:03:48 +01:00
|
|
|
log.debug("Reflector already has %s", str(self.next_blob_to_send.blob_hash)[:16])
|
2016-08-08 04:33:40 +02:00
|
|
|
return self.set_not_uploading()
|
|
|
|
else: # Expecting Server Blob Response
|
|
|
|
if 'received_blob' not in response_dict:
|
|
|
|
raise ValueError("I don't know if the blob made it to the intended destination!")
|
|
|
|
else:
|
2017-01-25 19:03:48 +01:00
|
|
|
log.info("Reflector received %s", str(self.next_blob_to_send.blob_hash)[:16])
|
2016-08-08 04:33:40 +02:00
|
|
|
return self.set_not_uploading()
|
|
|
|
|
|
|
|
def open_blob_for_reading(self, blob):
|
|
|
|
if blob.is_validated():
|
|
|
|
read_handle = blob.open_for_reading()
|
|
|
|
if read_handle is not None:
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('Getting ready to send %s', blob.blob_hash)
|
2016-08-08 04:33:40 +02:00
|
|
|
self.next_blob_to_send = blob
|
|
|
|
self.read_handle = read_handle
|
|
|
|
return None
|
2017-01-25 19:03:48 +01:00
|
|
|
else:
|
|
|
|
log.warning("Can't reflect blob %s", str(blob.blob_hash)[:16])
|
|
|
|
return defer.fail(ValueError(
|
|
|
|
"Couldn't open that blob for some reason. blob_hash: {}".format(blob.blob_hash)))
|
2016-08-08 04:33:40 +02:00
|
|
|
|
|
|
|
def send_blob_info(self):
|
|
|
|
assert self.next_blob_to_send is not None, "need to have a next blob to send at this point"
|
2017-01-25 19:03:48 +01:00
|
|
|
log.debug("Send blob info for %s", self.next_blob_to_send.blob_hash)
|
2016-08-08 04:33:40 +02:00
|
|
|
self.write(json.dumps({
|
|
|
|
'blob_hash': self.next_blob_to_send.blob_hash,
|
2016-08-09 06:59:50 +02:00
|
|
|
'blob_size': self.next_blob_to_send.length
|
2016-08-08 04:33:40 +02:00
|
|
|
}))
|
2016-08-04 05:03:14 +02:00
|
|
|
|
2017-01-25 19:03:48 +01:00
|
|
|
def skip_missing_blob(self, err, blob_hash):
|
|
|
|
err.trap(ValueError)
|
|
|
|
return self.send_next_request()
|
|
|
|
|
2016-08-04 05:03:14 +02:00
|
|
|
def send_next_request(self):
|
2016-08-08 04:33:40 +02:00
|
|
|
if self.file_sender is not None:
|
2016-08-04 05:03:14 +02:00
|
|
|
# send the blob
|
2017-01-25 19:03:48 +01:00
|
|
|
log.debug('Sending %s to reflector', str(self.next_blob_to_send.blob_hash)[:16])
|
2016-08-08 04:33:40 +02:00
|
|
|
return self.start_transfer()
|
|
|
|
elif self.blob_hashes_to_send:
|
|
|
|
# open the next blob to send
|
|
|
|
blob_hash = self.blob_hashes_to_send[0]
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('No current blob, sending the next one: %s', blob_hash)
|
2016-08-08 04:33:40 +02:00
|
|
|
self.blob_hashes_to_send = self.blob_hashes_to_send[1:]
|
|
|
|
d = self.blob_manager.get_blob(blob_hash, True)
|
|
|
|
d.addCallback(self.open_blob_for_reading)
|
2016-08-04 05:03:14 +02:00
|
|
|
# send the server the next blob hash + length
|
2017-01-25 19:10:38 +01:00
|
|
|
d.addCallbacks(lambda _: self.send_blob_info(),
|
|
|
|
lambda err: self.skip_missing_blob(err, blob_hash))
|
2016-08-08 04:33:40 +02:00
|
|
|
return d
|
2016-08-04 05:03:14 +02:00
|
|
|
else:
|
|
|
|
# close connection
|
2016-08-11 02:04:03 +02:00
|
|
|
log.debug('No more blob hashes, closing connection')
|
2016-08-11 02:36:52 +02:00
|
|
|
self.transport.loseConnection()
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class EncryptedFileReflectorClientFactory(ClientFactory):
|
|
|
|
protocol = EncryptedFileReflectorClient
|
2016-08-04 05:03:14 +02:00
|
|
|
|
2016-08-08 04:33:40 +02:00
|
|
|
def __init__(self, blob_manager, stream_info_manager, stream_hash):
|
|
|
|
self.blob_manager = blob_manager
|
2016-08-04 05:03:14 +02:00
|
|
|
self.stream_info_manager = stream_info_manager
|
|
|
|
self.stream_hash = stream_hash
|
|
|
|
self.p = None
|
2016-08-09 06:59:50 +02:00
|
|
|
self.finished_deferred = defer.Deferred()
|
2016-08-04 05:03:14 +02:00
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
p = self.protocol()
|
|
|
|
p.factory = self
|
|
|
|
self.p = p
|
2016-08-11 02:04:03 +02:00
|
|
|
return p
|
|
|
|
|
2016-08-27 01:58:53 +02:00
|
|
|
def startFactory(self):
|
|
|
|
log.debug('Starting reflector factory')
|
|
|
|
ClientFactory.startFactory(self)
|
|
|
|
|
|
|
|
def startedConnecting(self, connector):
|
|
|
|
log.debug('Started connecting')
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
"""If we get disconnected, reconnect to server."""
|
|
|
|
log.debug("connection lost: %s", reason)
|
|
|
|
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
|
|
log.debug("connection failed: %s", reason)
|