From f41983a7b6df243656402a6952d6b8411c1d6996 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 29 Jan 2019 18:59:07 -0500 Subject: [PATCH] update and clean up lbrynetservice.py (#367) --- buildozer.spec.sample | 2 +- buildozer.spec.travis | 2 +- buildozer.spec.vagrant | 2 +- src/main/python/lbrynetservice.py | 169 +++++++----------------------- 4 files changed, 38 insertions(+), 137 deletions(-) diff --git a/buildozer.spec.sample b/buildozer.spec.sample index 5983a545..32f3d0e9 100644 --- a/buildozer.spec.sample +++ b/buildozer.spec.sample @@ -36,7 +36,7 @@ version.filename = %(source.dir)s/main.py # (list) Application requirements # comma seperated e.g. requirements = sqlite3,kivy -requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.1#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve +requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.4#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes diff --git a/buildozer.spec.travis b/buildozer.spec.travis index 5983a545..32f3d0e9 100644 --- a/buildozer.spec.travis +++ b/buildozer.spec.travis @@ -36,7 +36,7 @@ version.filename = %(source.dir)s/main.py # (list) Application requirements # comma seperated e.g. requirements = sqlite3,kivy -requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.1#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve +requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.4#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes diff --git a/buildozer.spec.vagrant b/buildozer.spec.vagrant index 5983a545..32f3d0e9 100644 --- a/buildozer.spec.vagrant +++ b/buildozer.spec.vagrant @@ -36,7 +36,7 @@ version.filename = %(source.dir)s/main.py # (list) Application requirements # comma seperated e.g. requirements = sqlite3,kivy -requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.1#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve +requirements = python3crystax, openssl, sqlite3, hostpython3crystax, android, distro, pyjnius, certifi==2018.4.16, constantly, incremental, miniupnpc==1.9, gmpy, appdirs==1.4.3, argparse==1.2.1, docopt, base58==1.0.0, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2, pyyaml, qrcode==5.2.2, requests, seccure==0.3.1.3, attrs==18.1.0, pyasn1, pyasn1-modules, service_identity==16.0.0, six==1.9.0, txJSON-RPC, zope.interface==4.3.3, protobuf==3.6.1, keyring==10.4.0, txupnp, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbry.git@v0.30.4#egg=lbrynet, git+https://github.com/lbryio/aioupnp.git#egg=aioupnp, asn1crypto, treq==17.8.0, funcsigs, mock, pbr, pyopenssl, twisted, idna, Automat, hyperlink, PyHamcrest, netifaces, cryptography, aiohttp==3.4.4, multidict==4.5.2, idna_ssl==1.1.0, typing_extensions==3.6.5, yarl, chardet==3.0.4, async_timeout==3.0.1, aiorpcX==0.9.0, git+https://github.com/lbryio/torba#egg=torba, coincurve # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes diff --git a/src/main/python/lbrynetservice.py b/src/main/python/lbrynetservice.py index a6df8863..fe409659 100644 --- a/src/main/python/lbrynetservice.py +++ b/src/main/python/lbrynetservice.py @@ -1,165 +1,66 @@ -import sys -from twisted.internet import asyncioreactor -if 'twisted.internet.reactor' not in sys.modules: - asyncioreactor.install() -else: - from twisted.internet import reactor - if not isinstance(reactor, asyncioreactor.AsyncioSelectorReactor) and getattr(sys, 'frozen', False): - # pyinstaller hooks install the default reactor before - # any of our code runs, see kivy for similar problem: - # https://github.com/kivy/kivy/issues/4182 - del sys.modules['twisted.internet.reactor'] - asyncioreactor.install() - from twisted.internet import reactor - -import keyring.backend -from lbrynet import build_type +import keyring import platform -import ssl from jnius import autoclass +from keyring.backend import KeyringBackend +from lbrynet import build_type +from lbrynet.extras.cli import conf, log_support, check_connection, Daemon, reactor +from lbrynet.extras.daemon.Components import DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT +from lbrynet.extras.daemon.Components import REFLECTOR_COMPONENT -# Fixes / patches / overrides -# platform.platform() in libc_ver: IOError: [Errno 21] Is a directory -lbrynet_utils = autoclass('io.lbry.browser.Utils') + +lbrynet_android_utils = autoclass('io.lbry.browser.Utils') service = autoclass('io.lbry.browser.LbrynetService').serviceInstance -platform.platform = lambda: 'Android %s (API %s)' % (lbrynet_utils.getAndroidRelease(), lbrynet_utils.getAndroidSdk()) -build_type.BUILD = 'dev' if lbrynet_utils.isDebug() else 'release' - -import lbrynet.androidhelpers -lbrynet.androidhelpers.paths.android_files_dir = lambda: lbrynet_utils.getFilesDir(service.getApplicationContext()) -lbrynet.androidhelpers.paths.android_internal_storage_dir = lambda: lbrynet_utils.getInternalStorageDir(service.getApplicationContext()) -lbrynet.androidhelpers.paths.android_external_storage_dir = lambda: lbrynet_utils.getExternalStorageDir(service.getApplicationContext()) -lbrynet.androidhelpers.paths.android_app_internal_storage_dir = lambda: lbrynet_utils.getAppInternalStorageDir(service.getApplicationContext()) -lbrynet.androidhelpers.paths.android_app_external_storage_dir = lambda: lbrynet_utils.getAppExternalStorageDir(service.getApplicationContext()) - -# RPC authentication secret -# Retrieve the Anroid keystore -ks = lbrynet_utils.initKeyStore(service.getApplicationContext()); - -''' -import lbrynet.daemon.auth -from lbrynet.daemon.auth.util import APIKey, API_KEY_NAME - -def load_api_keys(path): - key_name = API_KEY_NAME - context = service.getApplicationContext(); - secret = lbrynet_utils.loadApiSecret(context, ks) - # TODO: For testing. Normally, this should not be displayed. - log.info('Loaded API Secret: %s', secret); - return { key_name: APIKey(secret, key_name, None) } - -def save_api_keys(keys, path): - key_name = API_KEY_NAME - if key_name in keys: - secret = keys[key_name].secret - # TODO: For testing. Normally, this should not be displayed. - log.info('Saving API Secret: %s', secret) - context = service.getApplicationContext() - lbrynet_utils.saveApiSecret(secret, context, ks) - -def initialize_api_key_file(key_path): - context = service.getApplicationContext() - secret = lbrynet_utils.loadApiSecret(context, ks) - if secret is None: - keys = {} - new_api_key = APIKey.new(name=API_KEY_NAME) - keys.update({new_api_key.name: new_api_key}) - save_api_keys(keys, key_path) - -lbrynet.daemon.auth.util.load_api_keys = load_api_keys -lbrynet.daemon.auth.util.save_api_keys = save_api_keys -lbrynet.daemon.auth.util.initialize_api_key_file = initialize_api_key_file -''' +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' # Keyring backend -class LbryAndroidKeyring(keyring.backend.KeyringBackend): +class LbryAndroidKeyring(KeyringBackend): priority = 1 + def __init__(self): + self._keystore = lbrynet_android_utils.initKeyStore(service.getApplicationContext()) + def set_password(self, servicename, username, password): context = service.getApplicationContext() - lbrynet_utils.setPassword(servicename, username, password, context, ks) + lbrynet_android_utils.setPassword(servicename, username, password, context, self._keystore) def get_password(self, servicename, username): context = service.getApplicationContext() - return lbrynet_utils.getPassword(servicename, username, context, ks) + return lbrynet_android_utils.getPassword(servicename, username, context, self._keystore) def delete_password(self, servicename, username): context = service.getApplicationContext() - lbrynet_utils.deletePassword(servicename, username, context, ks) + lbrynet_android_utils.deletePassword(servicename, username, context, self._keystore) -# set the keyring backend -keyring.set_keyring(LbryAndroidKeyring()) - -import logging.handlers -from twisted.internet import reactor - -from lbrynet import utils, conf, log_support -from lbrynet.extras import system_info -from lbrynet.extras.daemon.Components import \ - DHT_COMPONENT, \ - HASH_ANNOUNCER_COMPONENT, \ - PEER_PROTOCOL_SERVER_COMPONENT, \ - REFLECTOR_COMPONENT -from lbrynet.extras.daemon import analytics -from lbrynet.extras.daemon.Daemon import Daemon - -# https certificate verification -# TODO: this is bad. Need to find a way to properly verify https requests -def https_context(): - #urllib2 - try: - _create_unverified_https_context = ssl._create_unverified_context - except AttributeError: - # Legacy Python that doesn't verify HTTPS certificates by default - pass - else: - # Handle target environment that doesn't support HTTPS verification - ssl._create_default_https_context = _create_unverified_https_context - - ''' - # requests - from functools import partial - class partialmethod(partial): - def __get__(self, instance, owner): - if instance is None: - return self - - return partial(self.func, instance, *(self.args or ()), **(self.keywords or {})) - - default_request = requests.Session.request - requests.Session.request = partialmethod(default_request, verify=False) - ''' - -# LBRY Daemon -log = logging.getLogger(__name__) - -def test_internet_connection(): - return utils.check_connection() def start(): - # lbry daemon - https_context() - conf.initialize_settings() + keyring.set_keyring(LbryAndroidKeyring()) - lbrynet_log = conf.settings.get_log_filename() - log_support.configure_logging(lbrynet_log, True, []) - - # TODO: specify components, initialise auth + private_storage_dir = lbrynet_android_utils.getAppInternalStorageDir(service.getApplicationContext()) + conf.initialize_settings( + data_dir=f'{private_storage_dir}/lbrynet', + wallet_dir=f'{private_storage_dir}/lbryum', + download_dir=f'{lbrynet_android_utils.getAppExternalStorageDir(service.getApplicationContext())}/Download' + ) conf.settings.update({ - 'components_to_skip': [DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT], - 'use_upnp': False + 'components_to_skip': [ + DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, + REFLECTOR_COMPONENT + ], + 'use_upnp': False, + # 'use_https': True, # TODO: does this work on android? + # 'use_auth_http': True }) - log.info('Final Settings: %s', conf.settings.get_current_settings_dict()) - log.info("Starting lbrynet-daemon") + log_support.configure_logging(conf.settings.get_log_filename(), True, []) + log_support.configure_loggly_handler() - if test_internet_connection(): + if check_connection(): daemon = Daemon() daemon.start_listening() reactor.run() else: - log.info("Not connected to the Internet. Unable to start.") - + print("Not connected to internet, unable to start") if __name__ == '__main__': start()