2019-02-18 19:19:18 +01:00
|
|
|
import asyncio
|
2019-01-30 00:59:07 +01:00
|
|
|
import keyring
|
2019-01-31 00:55:37 +01:00
|
|
|
import logging
|
2019-02-18 19:19:18 +01:00
|
|
|
import pathlib
|
2017-08-13 03:24:00 +02:00
|
|
|
import platform
|
2019-02-18 19:19:18 +01:00
|
|
|
import sys
|
2018-10-28 19:12:40 +01:00
|
|
|
from jnius import autoclass
|
2019-01-30 00:59:07 +01:00
|
|
|
from keyring.backend import KeyringBackend
|
2019-02-18 19:19:18 +01:00
|
|
|
from lbrynet import __version__ as lbrynet_version, build_type
|
|
|
|
from lbrynet.conf import Config
|
|
|
|
from lbrynet.extras.daemon.loggly_handler import get_loggly_handler
|
2019-01-30 00:59:07 +01:00
|
|
|
from lbrynet.extras.daemon.Components import DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT
|
2019-02-18 19:19:18 +01:00
|
|
|
from lbrynet.extras.daemon.Daemon import Daemon
|
|
|
|
from lbrynet.extras.daemon.loggly_handler import get_loggly_handler
|
|
|
|
from lbrynet.utils import check_connection
|
2017-08-22 23:03:05 +02:00
|
|
|
|
2019-02-18 19:19:18 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(logging.DEBUG)
|
2017-08-25 05:59:35 +02:00
|
|
|
|
2019-01-30 00:59:07 +01:00
|
|
|
lbrynet_android_utils = autoclass('io.lbry.browser.Utils')
|
|
|
|
service = autoclass('io.lbry.browser.LbrynetService').serviceInstance
|
|
|
|
platform.platform = lambda: 'Android %s (API %s)' % (lbrynet_android_utils.getAndroidRelease(), lbrynet_android_utils.getAndroidSdk())
|
|
|
|
build_type.BUILD = 'dev' if lbrynet_android_utils.isDebug() else 'release'
|
2017-08-25 05:59:35 +02:00
|
|
|
|
2017-12-20 17:10:30 +01:00
|
|
|
# Keyring backend
|
2019-01-30 00:59:07 +01:00
|
|
|
class LbryAndroidKeyring(KeyringBackend):
|
2017-12-20 17:10:30 +01:00
|
|
|
priority = 1
|
|
|
|
|
2019-01-30 00:59:07 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._keystore = lbrynet_android_utils.initKeyStore(service.getApplicationContext())
|
|
|
|
|
2017-12-20 17:10:30 +01:00
|
|
|
def set_password(self, servicename, username, password):
|
|
|
|
context = service.getApplicationContext()
|
2019-01-30 00:59:07 +01:00
|
|
|
lbrynet_android_utils.setPassword(servicename, username, password, context, self._keystore)
|
2017-12-20 17:10:30 +01:00
|
|
|
|
|
|
|
def get_password(self, servicename, username):
|
|
|
|
context = service.getApplicationContext()
|
2019-01-30 00:59:07 +01:00
|
|
|
return lbrynet_android_utils.getPassword(servicename, username, context, self._keystore)
|
2017-12-20 17:10:30 +01:00
|
|
|
|
|
|
|
def delete_password(self, servicename, username):
|
|
|
|
context = service.getApplicationContext()
|
2019-01-30 00:59:07 +01:00
|
|
|
lbrynet_android_utils.deletePassword(servicename, username, context, self._keystore)
|
2017-12-20 17:10:30 +01:00
|
|
|
|
2019-02-18 19:19:18 +01:00
|
|
|
def ensure_directory_exists(path: str):
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
def configure_logging(conf):
|
|
|
|
default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
|
|
|
|
|
|
|
|
file_handler = logging.handlers.RotatingFileHandler(
|
|
|
|
conf.log_file_path, maxBytes=2097152, backupCount=5
|
|
|
|
)
|
|
|
|
file_handler.setFormatter(default_formatter)
|
|
|
|
logging.getLogger('lbrynet').addHandler(file_handler)
|
|
|
|
logging.getLogger('torba').addHandler(file_handler)
|
|
|
|
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
handler.setFormatter(default_formatter)
|
|
|
|
|
|
|
|
log.addHandler(handler)
|
|
|
|
logging.getLogger('lbrynet').addHandler(handler)
|
|
|
|
logging.getLogger('torba').addHandler(handler)
|
|
|
|
|
|
|
|
logging.getLogger('aioupnp').setLevel(logging.WARNING)
|
|
|
|
logging.getLogger('aiohttp').setLevel(logging.CRITICAL)
|
2019-02-22 04:28:11 +01:00
|
|
|
logging.getLogger('lbrynet').setLevel(logging.DEBUG if lbrynet_android_utils.isDebug() else logging.INFO)
|
2019-02-18 19:19:18 +01:00
|
|
|
logging.getLogger('torba').setLevel(logging.INFO)
|
|
|
|
|
|
|
|
loggly_handler = get_loggly_handler()
|
|
|
|
loggly_handler.setLevel(logging.ERROR)
|
|
|
|
logging.getLogger('lbrynet').addHandler(loggly_handler)
|
2017-08-13 03:24:00 +02:00
|
|
|
|
|
|
|
def start():
|
2019-01-30 00:59:07 +01:00
|
|
|
keyring.set_keyring(LbryAndroidKeyring())
|
|
|
|
private_storage_dir = lbrynet_android_utils.getAppInternalStorageDir(service.getApplicationContext())
|
2019-02-18 19:19:18 +01:00
|
|
|
conf = Config(
|
2019-01-30 00:59:07 +01:00
|
|
|
data_dir=f'{private_storage_dir}/lbrynet',
|
|
|
|
wallet_dir=f'{private_storage_dir}/lbryum',
|
2019-02-18 19:19:18 +01:00
|
|
|
download_dir=f'{lbrynet_android_utils.getInternalStorageDir(service.getApplicationContext())}/Download',
|
|
|
|
components_to_skip=[DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT],
|
|
|
|
use_upnp=False
|
2019-01-30 00:59:07 +01:00
|
|
|
)
|
2019-02-18 19:19:18 +01:00
|
|
|
|
|
|
|
for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir):
|
|
|
|
ensure_directory_exists(directory)
|
|
|
|
|
|
|
|
configure_logging(conf)
|
|
|
|
|
|
|
|
log.info('Starting lbry sdk {}'.format(lbrynet_version));
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
2019-02-22 04:28:11 +01:00
|
|
|
loop.set_debug(lbrynet_android_utils.isDebug())
|
|
|
|
|
|
|
|
daemon = Daemon(conf)
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(daemon.start())
|
|
|
|
loop.run_until_complete(daemon.stop_event.wait())
|
|
|
|
except (GracefulExit):
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
loop.run_until_complete(daemon.stop())
|
|
|
|
if hasattr(loop, 'shutdown_asyncgens'):
|
|
|
|
loop.run_until_complete(loop.shutdown_asyncgens())
|
2017-08-22 23:03:05 +02:00
|
|
|
|
2017-08-13 03:24:00 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
start()
|