2014-06-11 04:34:28 -04:00
|
|
|
import os
|
2014-07-02 14:51:14 -04:00
|
|
|
import threading
|
|
|
|
import sys
|
2014-03-10 01:18:05 -04:00
|
|
|
import sickbeard
|
2014-06-15 07:08:41 -04:00
|
|
|
|
2014-12-07 16:00:18 -05:00
|
|
|
from sickbeard.webserve import LoginHandler, LogoutHandler, KeyHandler
|
|
|
|
from sickbeard.webapi import ApiHandler
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import logger
|
2014-12-07 16:00:18 -05:00
|
|
|
from sickbeard.helpers import create_https_certificates, generateApiKey
|
2014-12-17 20:05:05 -05:00
|
|
|
from tornado.web import Application, StaticFileHandler, RedirectHandler
|
2014-06-15 03:16:55 -04:00
|
|
|
from tornado.httpserver import HTTPServer
|
2014-06-16 08:19:07 -04:00
|
|
|
from tornado.ioloop import IOLoop
|
2014-12-07 12:16:41 -05:00
|
|
|
from tornado.routes import route
|
2014-07-09 14:41:04 -04:00
|
|
|
|
2014-07-02 14:51:14 -04:00
|
|
|
class SRWebServer(threading.Thread):
|
2014-07-05 20:57:43 -04:00
|
|
|
def __init__(self, options={}, io_loop=None):
|
2014-07-02 14:51:14 -04:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.alive = True
|
|
|
|
self.name = "TORNADO"
|
|
|
|
self.io_loop = io_loop or IOLoop.current()
|
|
|
|
|
|
|
|
self.options = options
|
|
|
|
self.options.setdefault('port', 8081)
|
|
|
|
self.options.setdefault('host', '0.0.0.0')
|
|
|
|
self.options.setdefault('log_dir', None)
|
|
|
|
self.options.setdefault('username', '')
|
|
|
|
self.options.setdefault('password', '')
|
2014-12-18 17:48:55 -05:00
|
|
|
self.options.setdefault('web_root', '/')
|
2014-07-02 14:51:14 -04:00
|
|
|
assert isinstance(self.options['port'], int)
|
|
|
|
assert 'data_root' in self.options
|
|
|
|
|
2014-07-09 14:41:04 -04:00
|
|
|
# video root
|
2014-07-10 20:20:31 -04:00
|
|
|
if sickbeard.ROOT_DIRS:
|
|
|
|
root_dirs = sickbeard.ROOT_DIRS.split('|')
|
|
|
|
self.video_root = root_dirs[int(root_dirs[0]) + 1]
|
|
|
|
else:
|
|
|
|
self.video_root = None
|
2014-07-09 14:41:04 -04:00
|
|
|
|
|
|
|
# web root
|
2014-12-09 22:31:24 -05:00
|
|
|
if self.options['web_root']:
|
|
|
|
sickbeard.WEB_ROOT = self.options['web_root'] = ('/' + self.options['web_root'].lstrip('/').strip('/'))
|
2014-12-07 16:00:18 -05:00
|
|
|
|
|
|
|
# api root
|
|
|
|
if not sickbeard.API_KEY:
|
|
|
|
sickbeard.API_KEY = generateApiKey()
|
2014-12-13 07:14:29 -05:00
|
|
|
self.options['api_root'] = r'%s/api/%s' % (sickbeard.WEB_ROOT, sickbeard.API_KEY)
|
2014-07-09 14:41:04 -04:00
|
|
|
|
2014-07-02 14:51:14 -04:00
|
|
|
# tornado setup
|
|
|
|
self.enable_https = self.options['enable_https']
|
|
|
|
self.https_cert = self.options['https_cert']
|
|
|
|
self.https_key = self.options['https_key']
|
|
|
|
|
|
|
|
if self.enable_https:
|
|
|
|
# If either the HTTPS certificate or key do not exist, make some self-signed ones.
|
2014-07-09 14:41:04 -04:00
|
|
|
if not (self.https_cert and os.path.exists(self.https_cert)) or not (
|
2014-12-08 10:34:14 -05:00
|
|
|
self.https_key and os.path.exists(self.https_key)):
|
2014-07-02 14:51:14 -04:00
|
|
|
if not create_https_certificates(self.https_cert, self.https_key):
|
|
|
|
logger.log(u"Unable to create CERT/KEY files, disabling HTTPS")
|
|
|
|
sickbeard.ENABLE_HTTPS = False
|
2014-07-05 20:57:43 -04:00
|
|
|
self.enable_https = False
|
2014-07-02 14:51:14 -04:00
|
|
|
|
|
|
|
if not (os.path.exists(self.https_cert) and os.path.exists(self.https_key)):
|
|
|
|
logger.log(u"Disabled HTTPS because of missing CERT and KEY files", logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
sickbeard.ENABLE_HTTPS = False
|
2014-07-05 20:57:43 -04:00
|
|
|
self.enable_https = False
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-02 14:51:14 -04:00
|
|
|
# Load the app
|
|
|
|
self.app = Application([],
|
2014-07-09 14:41:04 -04:00
|
|
|
debug=True,
|
|
|
|
autoreload=False,
|
|
|
|
gzip=True,
|
|
|
|
xheaders=sickbeard.HANDLE_REVERSE_PROXY,
|
2014-12-07 12:16:41 -05:00
|
|
|
cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=',
|
2014-12-13 01:19:49 -05:00
|
|
|
login_url='/login/',
|
2014-07-02 14:51:14 -04:00
|
|
|
)
|
|
|
|
|
2014-12-07 12:16:41 -05:00
|
|
|
# Main Handlers
|
2014-12-09 22:31:24 -05:00
|
|
|
self.app.add_handlers('.*$', [
|
2014-12-10 18:52:28 -05:00
|
|
|
# webapi handler
|
2014-12-13 07:14:29 -05:00
|
|
|
(r'%s(/?.*)' % self.options['api_root'], ApiHandler),
|
2014-12-10 18:52:28 -05:00
|
|
|
|
|
|
|
# webapi key retrieval
|
2014-12-13 07:14:29 -05:00
|
|
|
(r'%s/getkey(/?.*)' % self.options['web_root'], KeyHandler),
|
2014-12-10 18:52:28 -05:00
|
|
|
|
|
|
|
# webapi builder redirect
|
2014-12-13 01:19:49 -05:00
|
|
|
(r'%s/api/builder' % self.options['web_root'], RedirectHandler, {"url": self.options['web_root'] + '/apibuilder/'}),
|
2014-12-10 18:52:28 -05:00
|
|
|
|
|
|
|
# webui login/logout handlers
|
2014-12-13 07:14:29 -05:00
|
|
|
(r'%s/login(/?.*)' % self.options['web_root'], LoginHandler),
|
|
|
|
(r'%s/logout(/?.*)' % self.options['web_root'], LogoutHandler),
|
2014-12-10 18:52:28 -05:00
|
|
|
|
2014-12-13 01:19:49 -05:00
|
|
|
# webui redirect
|
|
|
|
(r'/', RedirectHandler, {"url": self.options['web_root'] + '/home/'}),
|
|
|
|
|
2014-12-10 18:52:28 -05:00
|
|
|
# webui handlers
|
2014-12-09 22:31:24 -05:00
|
|
|
] + route.get_routes(self.options['web_root']))
|
2014-07-02 14:51:14 -04:00
|
|
|
|
2014-12-10 18:52:28 -05:00
|
|
|
# Static File Handlers
|
2014-07-02 14:51:14 -04:00
|
|
|
self.app.add_handlers(".*$", [
|
2014-12-10 18:52:28 -05:00
|
|
|
# favicon
|
|
|
|
(r'%s/(favicon\.ico)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": os.path.join(self.options['data_root'], 'images/ico/favicon.ico')}),
|
|
|
|
|
|
|
|
# images
|
|
|
|
(r'%s/images/(.*)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": os.path.join(self.options['data_root'], 'images')}),
|
|
|
|
|
|
|
|
# cached images
|
|
|
|
(r'%s/cache/images/(.*)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": os.path.join(sickbeard.CACHE_DIR, 'images')}),
|
|
|
|
|
|
|
|
# css
|
|
|
|
(r'%s/css/(.*)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": os.path.join(self.options['data_root'], 'css')}),
|
|
|
|
|
|
|
|
# javascript
|
|
|
|
(r'%s/js/(.*)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": os.path.join(self.options['data_root'], 'js')}),
|
|
|
|
|
|
|
|
# videos
|
|
|
|
] + [(r'%s/videos/(.*)' % self.options['web_root'], StaticFileHandler,
|
|
|
|
{"path": self.video_root})])
|
2014-07-09 14:41:04 -04:00
|
|
|
|
2014-07-02 14:51:14 -04:00
|
|
|
def run(self):
|
|
|
|
if self.enable_https:
|
|
|
|
protocol = "https"
|
2014-07-24 02:36:44 -04:00
|
|
|
self.server = HTTPServer(self.app, ssl_options={"certfile": self.https_cert, "keyfile": self.https_key})
|
2014-07-02 14:51:14 -04:00
|
|
|
else:
|
|
|
|
protocol = "http"
|
2014-07-24 02:36:44 -04:00
|
|
|
self.server = HTTPServer(self.app)
|
2014-07-02 14:51:14 -04:00
|
|
|
|
|
|
|
logger.log(u"Starting SickRage on " + protocol + "://" + str(self.options['host']) + ":" + str(
|
|
|
|
self.options['port']) + "/")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.server.listen(self.options['port'], self.options['host'])
|
|
|
|
except:
|
|
|
|
etype, evalue, etb = sys.exc_info()
|
2014-07-09 14:41:04 -04:00
|
|
|
logger.log(
|
|
|
|
"Could not start webserver on %s. Excpeption: %s, Error: %s" % (self.options['port'], etype, evalue),
|
|
|
|
logger.ERROR)
|
2014-07-02 14:51:14 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.io_loop.start()
|
|
|
|
self.io_loop.close(True)
|
2014-07-14 22:00:53 -04:00
|
|
|
except (IOError, ValueError):
|
2014-07-02 14:51:14 -04:00
|
|
|
# Ignore errors like "ValueError: I/O operation on closed kqueue fd". These might be thrown during a reload.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def shutDown(self):
|
|
|
|
self.alive = False
|
2014-11-27 07:37:56 -05:00
|
|
|
self.io_loop.stop()
|