2017-02-10 16:56:22 +01:00
|
|
|
from twisted.internet import reactor, defer
|
2018-11-09 20:02:03 +01:00
|
|
|
from lbrynet.extras.reflector.client.client import EncryptedFileReflectorClientFactory
|
|
|
|
from lbrynet.extras.reflector.client.blob import BlobReflectorClientFactory
|
2016-10-28 21:48:51 +02:00
|
|
|
|
|
|
|
|
2017-08-04 04:06:12 +02:00
|
|
|
def _is_ip(host):
|
|
|
|
try:
|
|
|
|
if len(host.split(".")) == 4 and all([0 <= int(x) <= 255 for x in host.split(".")]):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def resolve(host):
|
|
|
|
if _is_ip(host):
|
|
|
|
ip = host
|
|
|
|
else:
|
|
|
|
ip = yield reactor.resolve(host)
|
|
|
|
defer.returnValue(ip)
|
|
|
|
|
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
@defer.inlineCallbacks
|
2018-03-29 15:45:36 +02:00
|
|
|
def _reflect_stream(blob_manager, stream_hash, sd_hash, reflector_server):
|
2016-10-28 21:48:51 +02:00
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
2018-11-09 20:02:03 +01:00
|
|
|
factory = EncryptedFileReflectorClientFactory(blob_manager, stream_hash, sd_hash)
|
2017-08-04 04:06:12 +02:00
|
|
|
ip = yield resolve(reflector_address)
|
2017-02-10 16:56:22 +01:00
|
|
|
yield reactor.connectTCP(ip, reflector_port, factory)
|
2017-08-04 20:48:35 +02:00
|
|
|
result = yield factory.finished_deferred
|
|
|
|
defer.returnValue(result)
|
2016-10-28 21:48:51 +02:00
|
|
|
|
|
|
|
|
2018-03-19 18:36:53 +01:00
|
|
|
def _reflect_file(lbry_file, reflector_server):
|
2018-03-29 15:45:36 +02:00
|
|
|
return _reflect_stream(lbry_file.blob_manager, lbry_file.stream_hash, lbry_file.sd_hash, reflector_server)
|
2018-03-19 18:36:53 +01:00
|
|
|
|
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _reflect_blobs(blob_manager, blob_hashes, reflector_server):
|
2016-10-28 21:48:51 +02:00
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
2018-11-09 20:02:03 +01:00
|
|
|
factory = BlobReflectorClientFactory(blob_manager, blob_hashes)
|
2017-08-04 04:06:12 +02:00
|
|
|
ip = yield resolve(reflector_address)
|
2017-02-10 16:56:22 +01:00
|
|
|
yield reactor.connectTCP(ip, reflector_port, factory)
|
2017-08-04 20:48:35 +02:00
|
|
|
result = yield factory.finished_deferred
|
2019-01-11 00:40:20 +01:00
|
|
|
return result
|
2017-02-10 16:56:22 +01:00
|
|
|
|
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
def reflect_file(lbry_file, reflector_server):
|
|
|
|
if len(reflector_server.split(":")) == 2:
|
|
|
|
host, port = tuple(reflector_server.split(":"))
|
|
|
|
reflector_server = host, int(port)
|
2018-03-19 18:36:53 +01:00
|
|
|
else:
|
2019-01-21 21:55:50 +01:00
|
|
|
reflector_server = reflector_server, 5566
|
2018-03-19 18:36:53 +01:00
|
|
|
return _reflect_file(lbry_file, reflector_server)
|
|
|
|
|
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
def reflect_blob_hashes(blob_hashes, blob_manager, reflector_server):
|
|
|
|
if len(reflector_server.split(":")) == 2:
|
|
|
|
host, port = tuple(reflector_server.split(":"))
|
|
|
|
reflector_server = host, int(port)
|
2017-08-04 20:48:35 +02:00
|
|
|
else:
|
2019-01-21 21:55:50 +01:00
|
|
|
reflector_server = reflector_server, 5566
|
2017-02-10 16:56:22 +01:00
|
|
|
return _reflect_blobs(blob_manager, blob_hashes, reflector_server)
|