move listen to DaemonServer startup

This commit is contained in:
Jack 2016-12-06 16:08:15 -05:00
parent 0af445cbbc
commit f0ed4e5513
2 changed files with 43 additions and 46 deletions

View file

@ -1,22 +1,15 @@
import argparse import argparse
import logging.handlers import logging.handlers
import os
import webbrowser 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 jsonrpc.proxy import JSONRPCProxy
from lbrynet import analytics from lbrynet import analytics
from lbrynet.lbrynet_daemon.auth.auth import PasswordChecker, HttpPasswordRealm
from lbrynet.lbrynet_daemon.auth.util import initialize_api_key_file
from lbrynet import conf from lbrynet import conf
from lbrynet.core import log_support from lbrynet.core import log_support
from lbrynet.core import utils from lbrynet.core import utils
from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
from lbrynet.conf import settings from lbrynet.conf import settings
@ -140,44 +133,12 @@ def start_server_and_listen(launchui, use_auth, analytics_manager):
kwargs: passed along to `DaemonServer().start()` kwargs: passed along to `DaemonServer().start()`
""" """
daemon_server = DaemonServer(analytics_manager) daemon_server = DaemonServer(analytics_manager)
d = daemon_server.start() d = daemon_server.start(use_auth)
d.addCallback(lambda _: listen(daemon_server, use_auth))
if launchui: if launchui:
d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS))
d.addCallback(lambda _: analytics_manager.send_server_startup_success()) d.addCallback(lambda _: analytics_manager.send_server_startup_success())
d.addErrback(log_and_kill, analytics_manager) d.addErrback(log_and_kill, analytics_manager)
def listen(daemon_server, use_auth):
site_base = get_site_base(use_auth, daemon_server.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__": if __name__ == "__main__":
start() start()

View file

@ -1,11 +1,17 @@
import logging import logging
import os import os
import sys
from twisted.internet import defer from twisted.web import server, guard
from twisted.internet import defer, reactor, error
from twisted.cred import portal
from lbrynet.conf import settings
from lbrynet.lbrynet_daemon.Daemon import Daemon from lbrynet.lbrynet_daemon.Daemon import Daemon
from lbrynet.lbrynet_daemon.Resources import LBRYindex, HostedEncryptedFile, EncryptedFileUpload from lbrynet.lbrynet_daemon.Resources import LBRYindex, HostedEncryptedFile, EncryptedFileUpload
from lbrynet.conf import settings from lbrynet.lbrynet_daemon.auth.auth import PasswordChecker, HttpPasswordRealm
from lbrynet.lbrynet_daemon.auth.util import initialize_api_key_file
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -16,16 +22,46 @@ class DaemonServer(object):
self.root = None self.root = None
self.analytics_manager = analytics_manager self.analytics_manager = analytics_manager
def _setup_server(self): def _setup_server(self, use_auth):
ui_path = os.path.join(settings.ensure_data_dir(), "lbry-ui", "active") ui_path = os.path.join(settings.ensure_data_dir(), "lbry-ui", "active")
self.root = LBRYindex(ui_path) self.root = LBRYindex(ui_path)
self._api = Daemon(self.root, self.analytics_manager) self._api = Daemon(self.root, self.analytics_manager)
self.root.putChild("view", HostedEncryptedFile(self._api)) self.root.putChild("view", HostedEncryptedFile(self._api))
self.root.putChild("upload", EncryptedFileUpload(self._api)) self.root.putChild("upload", EncryptedFileUpload(self._api))
self.root.putChild(settings.API_ADDRESS, self._api) self.root.putChild(settings.API_ADDRESS, self._api)
lbrynet_server = server.Site(get_site_base(use_auth, self.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)
return defer.succeed(True) return defer.succeed(True)
def start(self): def start(self, use_auth):
d = self._setup_server() d = self._setup_server(use_auth)
d.addCallback(lambda _: self._api.setup()) d.addCallback(lambda _: self._api.setup())
return d return d
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