2016-04-21 04:02:52 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2017-08-02 21:50:17 +02:00
|
|
|
from twisted.web import server, guard, resource
|
2016-12-06 22:08:15 +01:00
|
|
|
from twisted.internet import defer, reactor, error
|
|
|
|
from twisted.cred import portal
|
2016-10-23 07:17:24 +02:00
|
|
|
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2017-06-26 03:04:33 +02:00
|
|
|
from lbrynet.daemon.Daemon import Daemon
|
|
|
|
from lbrynet.daemon.auth.auth import PasswordChecker, HttpPasswordRealm
|
|
|
|
from lbrynet.daemon.auth.util import initialize_api_key_file
|
|
|
|
from lbrynet.daemon.DaemonRequest import DaemonRequest
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2016-05-01 11:17:59 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2016-07-25 20:04:30 +02:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
|
2017-08-02 21:50:17 +02:00
|
|
|
class IndexResource(resource.Resource):
|
|
|
|
def getChild(self, name, request):
|
|
|
|
request.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
|
|
|
|
request.setHeader('expires', '0')
|
|
|
|
return self if name == '' else resource.Resource.getChild(self, name, request)
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:35 +02:00
|
|
|
class DaemonServer(object):
|
2016-11-10 21:49:51 +01:00
|
|
|
def __init__(self, analytics_manager=None):
|
2017-08-02 21:50:17 +02:00
|
|
|
self._daemon = None
|
2016-11-10 21:49:51 +01:00
|
|
|
self.root = None
|
2017-03-22 16:16:13 +01:00
|
|
|
self.server_port = None
|
2016-11-10 21:49:51 +01:00
|
|
|
self.analytics_manager = analytics_manager
|
|
|
|
|
2016-12-06 22:08:15 +01:00
|
|
|
def _setup_server(self, use_auth):
|
2017-08-02 21:50:17 +02:00
|
|
|
self.root = IndexResource()
|
|
|
|
self._daemon = Daemon(self.analytics_manager)
|
|
|
|
self.root.putChild("", self._daemon)
|
|
|
|
# TODO: DEPRECATED, remove this and just serve the API at the root
|
|
|
|
self.root.putChild(conf.settings['API_ADDRESS'], self._daemon)
|
|
|
|
|
|
|
|
lbrynet_server = get_site_base(use_auth, self.root)
|
2016-12-06 22:08:15 +01:00
|
|
|
lbrynet_server.requestFactory = DaemonRequest
|
|
|
|
|
|
|
|
try:
|
2017-03-22 16:16:13 +01:00
|
|
|
self.server_port = reactor.listenTCP(
|
2017-01-17 04:23:20 +01:00
|
|
|
conf.settings['api_port'], lbrynet_server, interface=conf.settings['api_host'])
|
2016-12-06 22:08:15 +01:00
|
|
|
except error.CannotListenError:
|
|
|
|
log.info('Daemon already running, exiting app')
|
2017-03-22 16:16:13 +01:00
|
|
|
raise
|
2016-12-06 22:08:15 +01:00
|
|
|
|
2016-04-21 04:02:52 +02:00
|
|
|
return defer.succeed(True)
|
|
|
|
|
2016-12-28 18:20:04 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-06-22 00:17:54 +02:00
|
|
|
def start(self, use_auth):
|
2016-12-28 18:20:04 +01:00
|
|
|
yield self._setup_server(use_auth)
|
2017-08-02 21:50:17 +02:00
|
|
|
yield self._daemon.setup()
|
2016-12-06 22:08:15 +01:00
|
|
|
|
2017-03-22 16:16:13 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def stop(self):
|
|
|
|
if self._api is not None:
|
|
|
|
yield self._api._shutdown()
|
|
|
|
if self.server_port is not None:
|
|
|
|
yield self.server_port.stopListening()
|
2016-12-06 22:08:15 +01:00
|
|
|
|
2017-04-30 21:50:33 +02:00
|
|
|
|
2016-12-06 22:08:15 +01:00
|
|
|
def get_site_base(use_auth, root):
|
|
|
|
if use_auth:
|
|
|
|
log.info("Using authenticated API")
|
2017-08-02 21:50:17 +02:00
|
|
|
root = create_auth_session(root)
|
2016-12-06 22:08:15 +01:00
|
|
|
else:
|
|
|
|
log.info("Using non-authenticated API")
|
2017-08-02 21:50:17 +02:00
|
|
|
return server.Site(root)
|
2016-12-06 22:08:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_auth_session(root):
|
2017-01-17 04:23:20 +01:00
|
|
|
pw_path = os.path.join(conf.settings['data_dir'], ".api_keys")
|
2016-12-06 22:08:15 +01:00
|
|
|
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, ])
|
2016-12-14 00:16:12 +01:00
|
|
|
return _lbrynet_server
|