From 8fe5eb514a3ed994ad1e22d98002b14b10fd4590 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Tue, 25 Oct 2016 18:36:40 -0500 Subject: [PATCH] make app startup consistent across versions --- lbrynet/lbrynet_daemon/DaemonControl.py | 72 ++++++++++++------- packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py | 8 +-- .../windows/lbry-win32-app/LBRYWin32App.py | 12 +--- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index 474110ab5..8f132c8ba 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -5,9 +5,8 @@ import webbrowser import sys from twisted.web import server, guard -from twisted.internet import defer, reactor +from twisted.internet import defer, reactor, error from twisted.cred import portal - from jsonrpc.proxy import JSONRPCProxy from lbrynet.lbrynet_daemon.auth.auth import PasswordChecker, HttpPasswordRealm @@ -131,29 +130,7 @@ def start(): print "To quit press ctrl-c or call 'stop' via the API" if test_internet_connection(): - lbry = DaemonServer() - - d = lbry.start() - if args.launchui: - d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) - d.addErrback(log_and_kill) - - if settings.use_auth_http: - log.info("Using authenticated API") - pw_path = os.path.join(settings.data_dir, ".api_keys") - initialize_api_key_file(pw_path) - checker = PasswordChecker.load_file(pw_path) - realm = HttpPasswordRealm(lbry.root) - portal_to_realm = portal.Portal(realm, [checker, ]) - factory = guard.BasicCredentialFactory('Login to lbrynet api') - _lbrynet_server = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ]) - else: - log.info("Using non-authenticated API") - _lbrynet_server = server.Site(lbry.root) - - lbrynet_server = server.Site(_lbrynet_server) - lbrynet_server.requestFactory = DaemonRequest - reactor.listenTCP(settings.api_port, lbrynet_server, interface=settings.API_INTERFACE) + start_server_and_listen(args.launchui, args.useauth) reactor.run() if not args.logtoconsole and not args.quiet: @@ -170,5 +147,50 @@ def log_and_kill(failure): reactor.stop() +def start_server_and_listen(launchui, use_auth, **kwargs): + """The primary entry point for launching the daemon. + + Args: + launchui: set to true to open a browser window + use_auth: set to true to enable http authentication + kwargs: passed along to `DaemonServer().start()` + """ + lbry = DaemonServer() + + d = lbry.start(**kwargs) + if launchui: + d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) + d.addErrback(log_and_kill) + + site_base = get_site_base(use_auth, lbry.root) + lbrynet_server = server.Site(site_base) + lbrynet_server.requestFactory = DaemonRequest + try: + reactor.listenTCP(settings.API_PORT, lbrynet_server, interface=settings.API_INTERFACE) + except error.CannotListenError: + log.info('Daemon already running, exiting app') + sys.exit(1) + + +def get_site_base(use_auth, root): + if use_auth: + log.info("Using authenticated API") + return create_auth_session(root) + else: + log.info("Using non-authenticated API") + return server.Site(root) + + +def create_auth_session(root): + pw_path = os.path.join(settings.data_dir, ".api_keys") + initialize_api_key_file(pw_path) + checker = PasswordChecker.load_file(pw_path) + realm = HttpPasswordRealm(root) + portal_to_realm = portal.Portal(realm, [checker, ]) + factory = guard.BasicCredentialFactory('Login to lbrynet api') + _lbrynet_server = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ]) + return _lbrynet_server + + if __name__ == "__main__": start() diff --git a/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py b/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py index ab43236f3..5646d67dd 100644 --- a/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py +++ b/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py @@ -55,7 +55,6 @@ class LBRYDaemonApp(AppKit.NSApplication): self.statusitem.setMenu_(self.menubarMenu) self.statusitem.setToolTip_(settings.APP_NAME) - if test_internet_connection(): if platform.mac_ver()[0] >= "10.10": LBRYNotify("Starting LBRY") @@ -64,12 +63,7 @@ class LBRYDaemonApp(AppKit.NSApplication): LBRYNotify("LBRY needs an internet connection to start, try again when one is available") sys.exit(0) - lbry = DaemonServer() - d = lbry.start(use_authentication=False) - d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) - lbrynet_server = server.Site(lbry.root) - lbrynet_server.requestFactory = DaemonRequest - reactor.listenTCP(settings.api_port, lbrynet_server, interface=settings.API_INTERFACE) + DaemonControl.start_server_and_listen(launchui=True, use_auth=False) def openui_(self, sender): webbrowser.open(settings.UI_ADDRESS) diff --git a/packaging/windows/lbry-win32-app/LBRYWin32App.py b/packaging/windows/lbry-win32-app/LBRYWin32App.py index d3591dcab..f9e14069f 100644 --- a/packaging/windows/lbry-win32-app/LBRYWin32App.py +++ b/packaging/windows/lbry-win32-app/LBRYWin32App.py @@ -19,6 +19,7 @@ except ImportError: import win32gui from lbrynet.core import utils +from lbrynet.lbrynet_daemon import DaemonControl from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest from lbrynet.conf import settings @@ -278,16 +279,7 @@ def main(lbry_name=None): systray_thread.daemon = True systray_thread.start() - lbry = DaemonServer() - d = lbry.start(use_authentication=False) - d.addCallback(lambda _: LBRYURIHandler.open_address(lbry_name)) - lbrynet_server = server.Site(lbry.root) - lbrynet_server.requestFactory = DaemonRequest - try: - reactor.listenTCP(settings.api_port, lbrynet_server, interface=settings.API_INTERFACE) - except error.CannotListenError: - log.info('Daemon already running, exiting app') - sys.exit(1) + DaemonControl.start_server_and_listen(launchui=True, use_auth=False) reactor.run() if __name__ == '__main__':