2015-08-20 17:27:15 +02:00
|
|
|
import logging
|
|
|
|
from lbrynet.core.Session import LBRYSession
|
|
|
|
import os.path
|
|
|
|
import argparse
|
2015-12-04 02:56:56 +01:00
|
|
|
import requests
|
|
|
|
import locale
|
2015-12-20 09:29:13 +01:00
|
|
|
import sys
|
2015-08-20 17:27:15 +02:00
|
|
|
from yapsy.PluginManager import PluginManager
|
2015-10-19 21:05:17 +02:00
|
|
|
from twisted.internet import defer, threads, stdio, task, error
|
2015-10-15 20:12:22 +02:00
|
|
|
from lbrynet.lbrynet_console.ConsoleControl import ConsoleControl
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.lbrynet_console.LBRYSettings import LBRYSettings
|
|
|
|
from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager
|
|
|
|
from lbrynet.conf import MIN_BLOB_DATA_PAYMENT_RATE # , MIN_BLOB_INFO_PAYMENT_RATE
|
|
|
|
from lbrynet.core.utils import generate_id
|
|
|
|
from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier
|
|
|
|
from lbrynet.core.PaymentRateManager import PaymentRateManager
|
|
|
|
from lbrynet.core.server.BlobAvailabilityHandler import BlobAvailabilityHandlerFactory
|
|
|
|
from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory
|
|
|
|
from lbrynet.core.server.ServerProtocol import ServerProtocolFactory
|
|
|
|
from lbrynet.core.PTCWallet import PTCWallet
|
2015-08-27 21:41:17 +02:00
|
|
|
from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifier
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileOpenerFactory
|
|
|
|
from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType
|
|
|
|
from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ApplicationStatusFactory, GetWalletBalancesFactory, ShutDownFactory
|
2016-02-23 21:13:34 +01:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ImmediateAnnounceAllBlobsFactory
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import LBRYFileStatusFactory, DeleteLBRYFileChooserFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ToggleLBRYFileRunningChooserFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ModifyApplicationDefaultsFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import CreateLBRYFileFactory, PublishStreamDescriptorChooserFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ShowPublishedSDHashesChooserFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import CreatePlainStreamDescriptorChooserFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ShowLBRYFileStreamHashChooserFactory, AddStreamFromHashFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import AddStreamFromSDFactory, AddStreamFromLBRYcrdNameFactory
|
2015-09-17 16:59:57 +02:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ClaimNameFactory, GetNewWalletAddressFactory
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ShowServerStatusFactory, ModifyServerSettingsFactory
|
2015-10-23 20:51:26 +02:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import ModifyLBRYFileOptionsChooserFactory, StatusFactory
|
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import PeerStatsAndSettingsChooserFactory, PublishFactory
|
2015-12-04 02:56:56 +01:00
|
|
|
from lbrynet.lbrynet_console.ControlHandlers import BlockchainStatusFactory
|
2016-02-19 06:44:08 +01:00
|
|
|
from lbrynet.core.LBRYcrdWallet import LBRYcrdWallet, LBRYumWallet
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2015-10-22 08:23:12 +02:00
|
|
|
alert = logging.getLogger("lbryalert." + __name__)
|
2015-09-08 21:42:56 +02:00
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class LBRYConsole():
|
|
|
|
"""A class which can upload and download file streams to and from the network"""
|
2016-02-23 21:13:34 +01:00
|
|
|
def __init__(self, peer_port, dht_node_port, known_dht_nodes, fake_wallet,
|
2015-10-08 17:01:50 +02:00
|
|
|
lbrycrd_conf, lbrycrd_dir, use_upnp, data_dir, created_data_dir,
|
2016-02-23 21:13:34 +01:00
|
|
|
lbrycrdd_path):
|
2015-08-20 17:27:15 +02:00
|
|
|
"""
|
|
|
|
@param peer_port: the network port on which to listen for peers
|
|
|
|
|
|
|
|
@param dht_node_port: the network port on which to listen for dht node requests
|
|
|
|
|
|
|
|
@param known_dht_nodes: a list of (ip_address, dht_port) which will be used to join the DHT network
|
|
|
|
"""
|
|
|
|
self.peer_port = peer_port
|
|
|
|
self.dht_node_port = dht_node_port
|
|
|
|
self.known_dht_nodes = known_dht_nodes
|
2016-02-23 21:13:34 +01:00
|
|
|
self.fake_wallet = fake_wallet
|
2015-08-24 19:19:03 +02:00
|
|
|
self.lbrycrd_conf = lbrycrd_conf
|
2015-10-08 17:01:50 +02:00
|
|
|
self.lbrycrd_dir = lbrycrd_dir
|
|
|
|
if not self.lbrycrd_dir:
|
2015-12-20 09:29:13 +01:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
self.lbrycrd_dir = os.path.join(os.path.expanduser("~"), "Library/Application Support/lbrycrd")
|
|
|
|
else:
|
|
|
|
self.lbrycrd_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd")
|
2015-10-08 17:01:50 +02:00
|
|
|
if not self.lbrycrd_conf:
|
2015-10-19 04:36:09 +02:00
|
|
|
self.lbrycrd_conf = os.path.join(self.lbrycrd_dir, "lbrycrd.conf")
|
2015-10-08 17:01:50 +02:00
|
|
|
self.lbrycrdd_path = lbrycrdd_path
|
2015-08-20 17:27:15 +02:00
|
|
|
self.use_upnp = use_upnp
|
|
|
|
self.lbry_server_port = None
|
|
|
|
self.session = None
|
|
|
|
self.lbry_file_metadata_manager = None
|
|
|
|
self.lbry_file_manager = None
|
2015-09-18 05:10:14 +02:00
|
|
|
self.db_dir = data_dir
|
2015-09-04 22:22:02 +02:00
|
|
|
self.current_db_revision = 1
|
2015-09-18 05:10:14 +02:00
|
|
|
self.blobfile_dir = os.path.join(self.db_dir, "blobfiles")
|
|
|
|
self.created_data_dir = created_data_dir
|
2015-08-20 17:27:15 +02:00
|
|
|
self.plugin_manager = PluginManager()
|
|
|
|
self.plugin_manager.setPluginPlaces([
|
2015-09-18 05:10:14 +02:00
|
|
|
os.path.join(self.db_dir, "plugins"),
|
2015-08-20 17:27:15 +02:00
|
|
|
os.path.join(os.path.dirname(__file__), "plugins"),
|
|
|
|
])
|
2015-10-12 19:52:47 +02:00
|
|
|
self.command_handlers = []
|
2015-08-20 17:27:15 +02:00
|
|
|
self.query_handlers = {}
|
|
|
|
|
2015-09-18 05:10:14 +02:00
|
|
|
self.settings = LBRYSettings(self.db_dir)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.blob_request_payment_rate_manager = None
|
|
|
|
self.lbryid = None
|
|
|
|
self.sd_identifier = StreamDescriptorIdentifier()
|
2015-09-04 22:22:02 +02:00
|
|
|
self.plugin_objects = []
|
|
|
|
self.db_migration_revisions = None
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Initialize the session and restore everything to its saved state"""
|
2015-10-22 08:23:12 +02:00
|
|
|
d = self._setup_controller()
|
|
|
|
d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory))
|
2015-09-04 22:22:02 +02:00
|
|
|
d.addCallback(lambda _: self._check_db_migration())
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._get_settings())
|
|
|
|
d.addCallback(lambda _: self._get_session())
|
2015-08-27 21:41:17 +02:00
|
|
|
d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier))
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._setup_lbry_file_manager())
|
|
|
|
d.addCallback(lambda _: self._setup_lbry_file_opener())
|
|
|
|
d.addCallback(lambda _: self._setup_control_handlers())
|
|
|
|
d.addCallback(lambda _: self._setup_query_handlers())
|
|
|
|
d.addCallback(lambda _: self._load_plugins())
|
|
|
|
d.addCallback(lambda _: self._setup_server())
|
|
|
|
d.addCallback(lambda _: self._start_controller())
|
2015-09-01 04:05:50 +02:00
|
|
|
d.addErrback(self._show_start_error)
|
2015-08-20 17:27:15 +02:00
|
|
|
return d
|
|
|
|
|
2015-09-01 04:05:50 +02:00
|
|
|
def _show_start_error(self, error):
|
2016-01-16 07:16:37 +01:00
|
|
|
print error.getTraceback()
|
2015-10-19 05:56:13 +02:00
|
|
|
log.error("An error occurred during start up: %s", error.getTraceback())
|
|
|
|
return error
|
|
|
|
|
|
|
|
def _show_shutdown_error(self, error):
|
|
|
|
print error.getErrorMessage()
|
|
|
|
log.error("An error occurred during shutdown: %s", error.getTraceback())
|
2015-09-01 04:05:50 +02:00
|
|
|
return error
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def shut_down(self):
|
|
|
|
"""Stop the session, all currently running streams, and stop the server"""
|
2015-09-15 06:29:18 +02:00
|
|
|
d = self._shut_down()
|
2015-09-04 22:22:02 +02:00
|
|
|
if self.session is not None:
|
2015-09-15 06:29:18 +02:00
|
|
|
d.addCallback(lambda _: self.session.shut_down())
|
2015-10-19 05:56:13 +02:00
|
|
|
d.addErrback(self._show_shutdown_error)
|
2015-08-20 17:27:15 +02:00
|
|
|
return d
|
|
|
|
|
|
|
|
def add_control_handlers(self, control_handlers):
|
|
|
|
for control_handler in control_handlers:
|
2015-10-15 20:12:22 +02:00
|
|
|
self.command_handlers.append(control_handler)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def add_query_handlers(self, query_handlers):
|
|
|
|
|
|
|
|
def _set_query_handlers(statuses):
|
|
|
|
from future_builtins import zip
|
|
|
|
for handler, (success, status) in zip(query_handlers, statuses):
|
|
|
|
if success is True:
|
|
|
|
self.query_handlers[handler] = status
|
|
|
|
|
|
|
|
ds = []
|
|
|
|
for handler in query_handlers:
|
|
|
|
ds.append(self.settings.get_query_handler_status(handler.get_primary_query_identifier()))
|
|
|
|
dl = defer.DeferredList(ds)
|
|
|
|
dl.addCallback(_set_query_handlers)
|
|
|
|
return dl
|
|
|
|
|
2015-09-18 05:10:14 +02:00
|
|
|
def _setup_data_directory(self):
|
2015-10-22 08:23:12 +02:00
|
|
|
alert.info("Loading databases...")
|
2015-09-18 05:10:14 +02:00
|
|
|
if self.created_data_dir:
|
|
|
|
db_revision = open(os.path.join(self.db_dir, "db_revision"), mode='w')
|
2015-09-04 22:22:02 +02:00
|
|
|
db_revision.write(str(self.current_db_revision))
|
|
|
|
db_revision.close()
|
2015-09-18 05:10:14 +02:00
|
|
|
log.debug("Created the db revision file: %s", str(os.path.join(self.db_dir, "db_revision")))
|
2015-09-18 05:47:36 +02:00
|
|
|
if not os.path.exists(self.blobfile_dir):
|
|
|
|
os.mkdir(self.blobfile_dir)
|
|
|
|
log.debug("Created the blobfile directory: %s", str(self.blobfile_dir))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-04 22:22:02 +02:00
|
|
|
def _check_db_migration(self):
|
|
|
|
old_revision = 0
|
2015-09-18 05:10:14 +02:00
|
|
|
db_revision_file = os.path.join(self.db_dir, "db_revision")
|
2015-09-04 22:22:02 +02:00
|
|
|
if os.path.exists(db_revision_file):
|
|
|
|
old_revision = int(open(db_revision_file).read().strip())
|
|
|
|
if old_revision < self.current_db_revision:
|
|
|
|
from lbrynet.db_migrator import dbmigrator
|
|
|
|
print "Upgrading your databases..."
|
2015-09-18 05:10:14 +02:00
|
|
|
d = threads.deferToThread(dbmigrator.migrate_db, self.db_dir, old_revision, self.current_db_revision)
|
2015-09-04 22:22:02 +02:00
|
|
|
|
|
|
|
def print_success(old_dirs):
|
|
|
|
success_string = "Finished upgrading the databases. It is now safe to delete the"
|
|
|
|
success_string += " following directories, if you feel like it. It won't make any"
|
|
|
|
success_string += " difference.\nAnyway here they are: "
|
|
|
|
for i, old_dir in enumerate(old_dirs):
|
|
|
|
success_string += old_dir
|
|
|
|
if i + 1 < len(old_dir):
|
|
|
|
success_string += ", "
|
|
|
|
print success_string
|
|
|
|
|
|
|
|
d.addCallback(print_success)
|
|
|
|
return d
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def _get_settings(self):
|
|
|
|
d = self.settings.start()
|
|
|
|
d.addCallback(lambda _: self.settings.get_lbryid())
|
|
|
|
d.addCallback(self.set_lbryid)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def set_lbryid(self, lbryid):
|
|
|
|
if lbryid is None:
|
|
|
|
return self._make_lbryid()
|
|
|
|
else:
|
|
|
|
self.lbryid = lbryid
|
|
|
|
|
|
|
|
def _make_lbryid(self):
|
|
|
|
self.lbryid = generate_id()
|
|
|
|
d = self.settings.save_lbryid(self.lbryid)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _get_session(self):
|
2015-08-24 19:19:03 +02:00
|
|
|
def get_default_data_rate():
|
|
|
|
d = self.settings.get_default_data_payment_rate()
|
|
|
|
d.addCallback(lambda rate: {"default_data_payment_rate": rate if rate is not None else MIN_BLOB_DATA_PAYMENT_RATE})
|
|
|
|
return d
|
|
|
|
|
|
|
|
def get_wallet():
|
2016-02-23 21:13:34 +01:00
|
|
|
if self.fake_wallet:
|
|
|
|
d = defer.succeed(PTCWallet(self.db_dir))
|
|
|
|
elif self.lbrycrdd_path is not None:
|
2015-10-29 01:59:07 +01:00
|
|
|
d = defer.succeed(LBRYcrdWallet(self.db_dir, wallet_dir=self.lbrycrd_dir,
|
2015-12-04 02:56:56 +01:00
|
|
|
wallet_conf=self.lbrycrd_conf,
|
2016-02-23 21:13:34 +01:00
|
|
|
lbrycrdd_path=self.lbrycrdd_path))
|
2016-02-19 06:44:08 +01:00
|
|
|
else:
|
2016-02-23 21:13:34 +01:00
|
|
|
d = defer.succeed(LBRYumWallet(self.db_dir))
|
2015-08-24 19:19:03 +02:00
|
|
|
d.addCallback(lambda wallet: {"wallet": wallet})
|
|
|
|
return d
|
|
|
|
|
|
|
|
d1 = get_default_data_rate()
|
|
|
|
d2 = get_wallet()
|
|
|
|
|
|
|
|
def combine_results(results):
|
|
|
|
r = {}
|
|
|
|
for success, result in results:
|
|
|
|
if success is True:
|
|
|
|
r.update(result)
|
|
|
|
return r
|
|
|
|
|
|
|
|
def create_session(results):
|
|
|
|
|
2015-10-22 08:23:12 +02:00
|
|
|
alert.info("Databases loaded.")
|
|
|
|
|
2015-09-18 05:10:14 +02:00
|
|
|
self.session = LBRYSession(results['default_data_payment_rate'], db_dir=self.db_dir, lbryid=self.lbryid,
|
|
|
|
blob_dir=self.blobfile_dir, dht_node_port=self.dht_node_port,
|
2015-08-20 17:27:15 +02:00
|
|
|
known_dht_nodes=self.known_dht_nodes, peer_port=self.peer_port,
|
2015-08-24 19:19:03 +02:00
|
|
|
use_upnp=self.use_upnp, wallet=results['wallet'])
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-08-24 19:19:03 +02:00
|
|
|
dl = defer.DeferredList([d1, d2], fireOnOneErrback=True)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-08-24 19:19:03 +02:00
|
|
|
dl.addCallback(combine_results)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-08-24 19:19:03 +02:00
|
|
|
dl.addCallback(create_session)
|
|
|
|
|
|
|
|
dl.addCallback(lambda _: self.session.setup())
|
|
|
|
|
2015-12-04 02:56:56 +01:00
|
|
|
dl.addCallback(lambda _: self.check_first_run())
|
|
|
|
|
|
|
|
dl.addCallback(self._show_first_run_result)
|
|
|
|
|
2015-08-24 19:19:03 +02:00
|
|
|
return dl
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-12-04 02:56:56 +01:00
|
|
|
def check_first_run(self):
|
2016-03-20 02:24:44 +01:00
|
|
|
d = self.session.wallet.is_first_run()
|
2015-12-04 02:56:56 +01:00
|
|
|
d.addCallback(lambda is_first_run: self._do_first_run() if is_first_run else 0.0)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _do_first_run(self):
|
|
|
|
d = self.session.wallet.get_new_address()
|
|
|
|
|
|
|
|
def send_request(url, data):
|
|
|
|
r = requests.post(url, json=data)
|
|
|
|
if r.status_code == 200:
|
|
|
|
return r.json()['credits_sent']
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
def log_error(err):
|
|
|
|
log.warning("unable to request free credits. %s", err.getErrorMessage())
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
def request_credits(address):
|
|
|
|
url = "http://credreq.lbry.io/requestcredits"
|
|
|
|
data = {"address": address}
|
|
|
|
d = threads.deferToThread(send_request, url, data)
|
|
|
|
d.addErrback(log_error)
|
|
|
|
return d
|
|
|
|
|
|
|
|
d.addCallback(request_credits)
|
|
|
|
return d
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _show_first_run_result(credits_received):
|
|
|
|
if credits_received != 0.0:
|
|
|
|
points_string = locale.format_string("%.2f LBC", (round(credits_received, 2),),
|
|
|
|
grouping=True)
|
2015-12-15 04:35:16 +01:00
|
|
|
alert.info("\n\nThank you for testing the alpha version of LBRY!\n\n"
|
|
|
|
"You have been given %s for free because we love you.\n"
|
|
|
|
"Please give them a few minutes to show up while you\n"
|
|
|
|
"catch up with our blockchain.\n", points_string)
|
2015-12-04 02:56:56 +01:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def _setup_lbry_file_manager(self):
|
2015-09-18 05:10:14 +02:00
|
|
|
self.lbry_file_metadata_manager = DBLBRYFileMetadataManager(self.db_dir)
|
2015-08-20 17:27:15 +02:00
|
|
|
d = self.lbry_file_metadata_manager.setup()
|
|
|
|
|
|
|
|
def set_lbry_file_manager():
|
|
|
|
self.lbry_file_manager = LBRYFileManager(self.session, self.lbry_file_metadata_manager, self.sd_identifier)
|
|
|
|
return self.lbry_file_manager.setup()
|
|
|
|
|
|
|
|
d.addCallback(lambda _: set_lbry_file_manager())
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _setup_lbry_file_opener(self):
|
|
|
|
stream_info_manager = TempLBRYFileMetadataManager()
|
|
|
|
downloader_factory = LBRYFileOpenerFactory(self.session.peer_finder, self.session.rate_limiter,
|
|
|
|
self.session.blob_manager, stream_info_manager,
|
|
|
|
self.session.wallet)
|
|
|
|
self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory)
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def _setup_control_handlers(self):
|
|
|
|
handlers = [
|
2015-10-15 20:12:22 +02:00
|
|
|
ApplicationStatusFactory(self.session.rate_limiter, self.session.dht_node),
|
|
|
|
GetWalletBalancesFactory(self.session.wallet),
|
|
|
|
ModifyApplicationDefaultsFactory(self),
|
|
|
|
ShutDownFactory(self),
|
|
|
|
PeerStatsAndSettingsChooserFactory(self.session.peer_manager),
|
|
|
|
LBRYFileStatusFactory(self.lbry_file_manager),
|
2015-12-15 20:42:29 +01:00
|
|
|
AddStreamFromSDFactory(self.sd_identifier, self.session.base_payment_rate_manager,
|
|
|
|
self.session.wallet),
|
2015-10-15 20:12:22 +02:00
|
|
|
DeleteLBRYFileChooserFactory(self.lbry_file_metadata_manager, self.session.blob_manager,
|
2015-10-23 20:51:26 +02:00
|
|
|
self.lbry_file_manager),
|
2015-10-15 20:12:22 +02:00
|
|
|
ToggleLBRYFileRunningChooserFactory(self.lbry_file_manager),
|
|
|
|
CreateLBRYFileFactory(self.session, self.lbry_file_manager),
|
|
|
|
PublishStreamDescriptorChooserFactory(self.lbry_file_metadata_manager,
|
2015-10-23 02:36:03 +02:00
|
|
|
self.session.blob_manager),
|
2015-10-15 20:12:22 +02:00
|
|
|
ShowPublishedSDHashesChooserFactory(self.lbry_file_metadata_manager,
|
|
|
|
self.lbry_file_manager),
|
|
|
|
CreatePlainStreamDescriptorChooserFactory(self.lbry_file_manager),
|
|
|
|
ShowLBRYFileStreamHashChooserFactory(self.lbry_file_manager),
|
|
|
|
ModifyLBRYFileOptionsChooserFactory(self.lbry_file_manager),
|
2015-12-15 20:42:29 +01:00
|
|
|
AddStreamFromHashFactory(self.sd_identifier, self.session, self.session.wallet),
|
2015-10-23 20:51:26 +02:00
|
|
|
StatusFactory(self, self.session.rate_limiter, self.lbry_file_manager,
|
2016-02-23 21:13:34 +01:00
|
|
|
self.session.blob_manager, self.session.wallet if not self.fake_wallet else None),
|
2015-11-10 17:33:35 +01:00
|
|
|
ImmediateAnnounceAllBlobsFactory(self.session.blob_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
]
|
|
|
|
self.add_control_handlers(handlers)
|
2016-02-23 21:13:34 +01:00
|
|
|
if not self.fake_wallet:
|
2015-08-20 17:27:15 +02:00
|
|
|
lbrycrd_handlers = [
|
2015-10-15 20:12:22 +02:00
|
|
|
AddStreamFromLBRYcrdNameFactory(self.sd_identifier, self.session,
|
2015-10-28 06:38:01 +01:00
|
|
|
self.session.wallet),
|
2015-12-04 02:56:56 +01:00
|
|
|
ClaimNameFactory(self.session.wallet, self.lbry_file_manager,
|
2015-10-23 02:36:03 +02:00
|
|
|
self.session.blob_manager),
|
2015-10-15 20:12:22 +02:00
|
|
|
GetNewWalletAddressFactory(self.session.wallet),
|
2015-12-04 02:56:56 +01:00
|
|
|
PublishFactory(self.session, self.lbry_file_manager, self.session.wallet),
|
|
|
|
BlockchainStatusFactory(self.session.wallet)
|
2015-08-20 17:27:15 +02:00
|
|
|
]
|
|
|
|
self.add_control_handlers(lbrycrd_handlers)
|
|
|
|
if self.peer_port is not None:
|
|
|
|
server_handlers = [
|
2015-10-15 20:12:22 +02:00
|
|
|
ShowServerStatusFactory(self),
|
|
|
|
ModifyServerSettingsFactory(self),
|
2015-08-20 17:27:15 +02:00
|
|
|
]
|
|
|
|
self.add_control_handlers(server_handlers)
|
|
|
|
|
|
|
|
def _setup_query_handlers(self):
|
|
|
|
handlers = [
|
|
|
|
#CryptBlobInfoQueryHandlerFactory(self.lbry_file_metadata_manager, self.session.wallet,
|
|
|
|
# self._server_payment_rate_manager),
|
|
|
|
BlobAvailabilityHandlerFactory(self.session.blob_manager),
|
|
|
|
#BlobRequestHandlerFactory(self.session.blob_manager, self.session.wallet,
|
|
|
|
# self._server_payment_rate_manager),
|
|
|
|
self.session.wallet.get_wallet_info_query_handler_factory(),
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_blob_request_handler_factory(rate):
|
|
|
|
self.blob_request_payment_rate_manager = PaymentRateManager(
|
|
|
|
self.session.base_payment_rate_manager, rate
|
|
|
|
)
|
|
|
|
handlers.append(BlobRequestHandlerFactory(self.session.blob_manager, self.session.wallet,
|
|
|
|
self.blob_request_payment_rate_manager))
|
|
|
|
|
|
|
|
d1 = self.settings.get_server_data_payment_rate()
|
|
|
|
d1.addCallback(get_blob_request_handler_factory)
|
|
|
|
|
|
|
|
dl = defer.DeferredList([d1])
|
|
|
|
dl.addCallback(lambda _: self.add_query_handlers(handlers))
|
|
|
|
return dl
|
|
|
|
|
|
|
|
def _load_plugins(self):
|
|
|
|
d = threads.deferToThread(self.plugin_manager.collectPlugins)
|
|
|
|
|
|
|
|
def setup_plugins():
|
|
|
|
ds = []
|
|
|
|
for plugin in self.plugin_manager.getAllPlugins():
|
2015-09-04 22:22:02 +02:00
|
|
|
self.plugin_objects.append(plugin.plugin_object)
|
2015-08-20 17:27:15 +02:00
|
|
|
ds.append(plugin.plugin_object.setup(self))
|
|
|
|
return defer.DeferredList(ds)
|
|
|
|
|
|
|
|
d.addCallback(lambda _: setup_plugins())
|
|
|
|
return d
|
|
|
|
|
2015-09-04 22:22:02 +02:00
|
|
|
def _stop_plugins(self):
|
|
|
|
ds = []
|
|
|
|
for plugin_object in self.plugin_objects:
|
|
|
|
ds.append(defer.maybeDeferred(plugin_object.stop))
|
|
|
|
return defer.DeferredList(ds)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def _setup_server(self):
|
|
|
|
|
|
|
|
def restore_running_status(running):
|
|
|
|
if running is True:
|
|
|
|
return self.start_server()
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
dl = self.settings.get_server_running_status()
|
|
|
|
dl.addCallback(restore_running_status)
|
|
|
|
return dl
|
|
|
|
|
|
|
|
def start_server(self):
|
|
|
|
|
|
|
|
if self.peer_port is not None:
|
|
|
|
|
|
|
|
server_factory = ServerProtocolFactory(self.session.rate_limiter,
|
|
|
|
self.query_handlers,
|
|
|
|
self.session.peer_manager)
|
|
|
|
from twisted.internet import reactor
|
2015-10-19 21:05:17 +02:00
|
|
|
try:
|
|
|
|
self.lbry_server_port = reactor.listenTCP(self.peer_port, server_factory)
|
|
|
|
except error.CannotListenError as e:
|
|
|
|
import traceback
|
|
|
|
log.error("Couldn't bind to port %d. %s", self.peer_port, traceback.format_exc())
|
|
|
|
raise ValueError("%s lbrynet may already be running on your computer.", str(e))
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def stop_server(self):
|
|
|
|
if self.lbry_server_port is not None:
|
|
|
|
self.lbry_server_port, p = None, self.lbry_server_port
|
|
|
|
return defer.maybeDeferred(p.stopListening)
|
|
|
|
else:
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
2015-10-22 08:23:12 +02:00
|
|
|
def _setup_controller(self):
|
|
|
|
self.controller = ConsoleControl()
|
|
|
|
stdio.StandardIO(self.controller)
|
|
|
|
logger = logging.getLogger()
|
|
|
|
formatter = logging.Formatter("%(message)s")
|
|
|
|
alert_handler = logging.StreamHandler(self.controller)
|
|
|
|
alert_handler.setFormatter(formatter)
|
|
|
|
alert_handler.addFilter(logging.Filter("lbryalert"))
|
|
|
|
alert_handler.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(alert_handler)
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed(True)
|
|
|
|
|
2015-10-22 08:23:12 +02:00
|
|
|
def _start_controller(self):
|
|
|
|
return self.controller.start(self.command_handlers)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def _shut_down(self):
|
|
|
|
self.plugin_manager = None
|
2015-09-01 04:05:50 +02:00
|
|
|
ds = []
|
|
|
|
if self.lbry_file_metadata_manager is not None:
|
|
|
|
d = self.lbry_file_metadata_manager.stop()
|
|
|
|
d.addCallback(lambda _: self.lbry_file_manager.stop())
|
|
|
|
ds.append(d)
|
|
|
|
ds.append(self.stop_server())
|
2015-09-04 22:22:02 +02:00
|
|
|
ds.append(self._stop_plugins())
|
2015-09-01 04:05:50 +02:00
|
|
|
dl = defer.DeferredList(ds)
|
2015-08-20 17:27:15 +02:00
|
|
|
return dl
|
|
|
|
|
|
|
|
|
|
|
|
def launch_lbry_console():
|
|
|
|
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Launch a lbrynet console")
|
|
|
|
parser.add_argument("--no_listen_peer",
|
|
|
|
help="Don't listen for incoming data connections.",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("--peer_port",
|
|
|
|
help="The port on which the console will listen for incoming data connections.",
|
|
|
|
type=int, default=3333)
|
|
|
|
parser.add_argument("--no_listen_dht",
|
|
|
|
help="Don't listen for incoming DHT connections.",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("--dht_node_port",
|
|
|
|
help="The port on which the console will listen for DHT connections.",
|
|
|
|
type=int, default=4444)
|
2016-02-23 21:13:34 +01:00
|
|
|
parser.add_argument("--fake_wallet",
|
|
|
|
help="Testing purposes only. Use a non-blockchain wallet.",
|
|
|
|
action="store_true")
|
2015-08-20 17:27:15 +02:00
|
|
|
parser.add_argument("--no_dht_bootstrap",
|
|
|
|
help="Don't try to connect to the DHT",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("--dht_bootstrap_host",
|
|
|
|
help="The hostname of a known DHT node, to be used to bootstrap into the DHT. "
|
|
|
|
"Must be used with --dht_bootstrap_port",
|
|
|
|
type=str, default='104.236.42.182')
|
|
|
|
parser.add_argument("--dht_bootstrap_port",
|
|
|
|
help="The port of a known DHT node, to be used to bootstrap into the DHT. Must "
|
|
|
|
"be used with --dht_bootstrap_host",
|
|
|
|
type=int, default=4000)
|
2015-10-08 17:01:50 +02:00
|
|
|
parser.add_argument("--disable_upnp",
|
|
|
|
help="Don't try to use UPnP to enable incoming connections through the firewall",
|
2015-08-20 17:27:15 +02:00
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("--data_dir",
|
2015-09-18 05:10:14 +02:00
|
|
|
help=("The full path to the directory in which lbrynet data and metadata will be stored. "
|
2016-02-16 19:39:08 +01:00
|
|
|
"Default: ~/.lbrynet on linux, ~/Library/Application Support/lbrynet on OS X"),
|
2015-08-20 17:27:15 +02:00
|
|
|
type=str)
|
2015-10-08 17:01:50 +02:00
|
|
|
parser.add_argument("--lbrycrdd_path",
|
2016-02-23 21:13:34 +01:00
|
|
|
help="The path to lbrycrdd, which will be launched if it isn't running. If"
|
|
|
|
"this option is chosen, lbrycrdd will be used as the interface to the"
|
|
|
|
"blockchain. By default, a lightweight interface is used.")
|
|
|
|
parser.add_argument("--lbrycrd_wallet_dir",
|
|
|
|
help="The directory in which lbrycrd data will stored. Used if lbrycrdd is "
|
|
|
|
"launched by this application.")
|
|
|
|
parser.add_argument("--lbrycrd_wallet_conf",
|
|
|
|
help="The configuration file for the LBRYcrd wallet. Default: ~/.lbrycrd/lbrycrd.conf",
|
|
|
|
type=str)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.no_dht_bootstrap:
|
|
|
|
bootstrap_nodes = []
|
|
|
|
else:
|
|
|
|
bootstrap_nodes = [(args.dht_bootstrap_host, args.dht_bootstrap_port)]
|
|
|
|
|
|
|
|
if args.no_listen_peer:
|
|
|
|
peer_port = None
|
|
|
|
else:
|
|
|
|
peer_port = args.peer_port
|
|
|
|
|
|
|
|
if args.no_listen_dht:
|
|
|
|
dht_node_port = None
|
|
|
|
else:
|
|
|
|
dht_node_port = args.dht_node_port
|
|
|
|
|
2015-09-18 05:10:14 +02:00
|
|
|
created_data_dir = False
|
2015-08-20 17:27:15 +02:00
|
|
|
if not args.data_dir:
|
2016-02-16 19:39:08 +01:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
data_dir = os.path.join(os.path.expanduser("~"), "Library/Application Support/lbrynet")
|
|
|
|
else:
|
|
|
|
data_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
|
2015-08-20 17:27:15 +02:00
|
|
|
else:
|
|
|
|
data_dir = args.data_dir
|
|
|
|
if not os.path.exists(data_dir):
|
|
|
|
os.mkdir(data_dir)
|
2015-09-18 05:10:14 +02:00
|
|
|
created_data_dir = True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-08-24 19:19:03 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
log_format = "(%(asctime)s)[%(filename)s:%(lineno)s] %(funcName)s(): %(message)s"
|
2015-09-08 21:42:56 +02:00
|
|
|
formatter = logging.Formatter(log_format)
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.DEBUG)
|
2015-09-18 05:10:14 +02:00
|
|
|
file_handler = logging.FileHandler(os.path.join(data_dir, "console.log"))
|
2015-09-08 21:42:56 +02:00
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
file_handler.addFilter(logging.Filter("lbrynet"))
|
|
|
|
logger.addHandler(file_handler)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-02-23 21:13:34 +01:00
|
|
|
console = LBRYConsole(peer_port, dht_node_port, bootstrap_nodes, fake_wallet=args.fake_wallet,
|
2015-10-22 08:23:12 +02:00
|
|
|
lbrycrd_conf=args.lbrycrd_wallet_conf, lbrycrd_dir=args.lbrycrd_wallet_dir,
|
2015-10-08 17:01:50 +02:00
|
|
|
use_upnp=not args.disable_upnp, data_dir=data_dir,
|
2016-02-23 21:13:34 +01:00
|
|
|
created_data_dir=created_data_dir, lbrycrdd_path=args.lbrycrdd_path)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
d = task.deferLater(reactor, 0, console.start)
|
2015-09-01 04:05:50 +02:00
|
|
|
|
|
|
|
d.addErrback(lambda _: reactor.stop())
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
reactor.addSystemEventTrigger('before', 'shutdown', console.shut_down)
|
2015-10-27 17:07:09 +01:00
|
|
|
reactor.run()
|
2015-11-10 17:33:35 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
launch_lbry_console()
|