2014-06-11 08:34:28 +00:00
|
|
|
import os
|
2014-06-15 07:16:55 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
import datetime
|
2014-03-10 05:18:05 +00:00
|
|
|
import sickbeard
|
2014-06-11 08:34:28 +00:00
|
|
|
import webserve
|
2014-06-15 07:16:55 +00:00
|
|
|
from sickbeard.exceptions import ex
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.helpers import create_https_certificates
|
2014-06-15 07:16:55 +00:00
|
|
|
from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
class MultiStaticFileHandler(StaticFileHandler):
|
|
|
|
def initialize(self, paths, default_filename=None):
|
|
|
|
self.paths = paths
|
|
|
|
|
|
|
|
def get(self, path, include_body=True):
|
|
|
|
for p in self.paths:
|
|
|
|
try:
|
|
|
|
# Initialize the Static file with a path
|
|
|
|
super(MultiStaticFileHandler, self).initialize(p)
|
|
|
|
# Try to get the file
|
|
|
|
return super(MultiStaticFileHandler, self).get(path)
|
|
|
|
except HTTPError as exc:
|
|
|
|
# File not found, carry on
|
|
|
|
if exc.status_code == 404:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Oops file not found anywhere!
|
|
|
|
raise HTTPError(404)
|
|
|
|
|
2014-06-15 07:16:55 +00:00
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
class webserverInit():
|
2014-06-15 07:16:55 +00:00
|
|
|
def __init__(self, options, cycleTime=datetime.timedelta(seconds=3)):
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
self.amActive = False
|
2014-06-15 07:16:55 +00:00
|
|
|
self.lastRun = datetime.datetime.fromordinal(1)
|
|
|
|
self.cycleTime = cycleTime
|
|
|
|
self.abort = False
|
|
|
|
|
|
|
|
self.server = None
|
|
|
|
self.thread = None
|
|
|
|
|
|
|
|
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', '')
|
|
|
|
self.options.setdefault('web_root', '/')
|
|
|
|
assert isinstance(self.options['port'], int)
|
|
|
|
assert 'data_root' in self.options
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def http_error_401_hander(status, message, traceback, version):
|
|
|
|
""" Custom handler for 401 error """
|
|
|
|
if status != "401 Unauthorized":
|
|
|
|
logger.log(u"Tornado caught an error: %s %s" % (status, message), logger.ERROR)
|
|
|
|
logger.log(traceback, logger.DEBUG)
|
|
|
|
return r'''<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>%s</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<br/>
|
|
|
|
<font color="#0000FF">Error %s: You need to provide a valid username and password.</font>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''' % ('Access denied', status)
|
|
|
|
|
|
|
|
def http_error_404_hander(status, message, traceback, version):
|
|
|
|
""" Custom handler for 404 error, redirect back to main page """
|
|
|
|
return r'''<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>404</title>
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
|
<!--
|
|
|
|
location.href = "%s/home/"
|
|
|
|
//-->
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<br/>
|
|
|
|
</body>
|
|
|
|
</html>
|
2014-06-15 07:16:55 +00:00
|
|
|
''' % self.options['web_root']
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
# tornado setup
|
2014-06-15 07:16:55 +00:00
|
|
|
enable_https = self.options['enable_https']
|
|
|
|
https_cert = self.options['https_cert']
|
|
|
|
https_key = self.options['https_key']
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
if enable_https:
|
|
|
|
# If either the HTTPS certificate or key do not exist, make some self-signed ones.
|
|
|
|
if not (https_cert and os.path.exists(https_cert)) or not (https_key and os.path.exists(https_key)):
|
|
|
|
if not create_https_certificates(https_cert, https_key):
|
|
|
|
logger.log(u"Unable to create CERT/KEY files, disabling HTTPS")
|
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
|
|
|
if not (os.path.exists(https_cert) and os.path.exists(https_key)):
|
|
|
|
logger.log(u"Disabled HTTPS because of missing CERT and KEY files", logger.WARNING)
|
2014-03-10 05:18:05 +00:00
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
# Load the app
|
|
|
|
app = Application([],
|
|
|
|
log_function=lambda x: None,
|
|
|
|
debug=False,
|
|
|
|
gzip=True,
|
|
|
|
cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=',
|
|
|
|
login_url='/login'
|
|
|
|
)
|
|
|
|
|
|
|
|
# Index Handler
|
|
|
|
app.add_handlers(".*$", [
|
2014-06-15 07:16:55 +00:00
|
|
|
(r"/", RedirectHandler, {'url': '/home/'}),
|
2014-06-11 08:34:28 +00:00
|
|
|
(r'/login', webserve.LoginHandler),
|
2014-06-15 07:16:55 +00:00
|
|
|
(r'%s(.*)(/?)' % self.options['web_root'], webserve.IndexHandler)
|
2014-06-11 08:34:28 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# Static Path Handler
|
|
|
|
app.add_handlers(".*$", [
|
2014-06-15 07:16:55 +00:00
|
|
|
('%s/%s/(.*)([^/]*)' % (self.options['web_root'], 'images'), MultiStaticFileHandler,
|
|
|
|
{'paths': [os.path.join(self.options['data_root'], 'images'),
|
2014-06-11 08:34:28 +00:00
|
|
|
os.path.join(sickbeard.CACHE_DIR, 'images'),
|
|
|
|
os.path.join(sickbeard.CACHE_DIR, 'images', 'thumbnails')]}),
|
2014-06-15 07:16:55 +00:00
|
|
|
('%s/%s/(.*)([^/]*)' % (self.options['web_root'], 'css'), MultiStaticFileHandler,
|
|
|
|
{'paths': [os.path.join(self.options['data_root'], 'css')]}),
|
|
|
|
('%s/%s/(.*)([^/]*)' % (self.options['web_root'], 'js'), MultiStaticFileHandler,
|
|
|
|
{'paths': [os.path.join(self.options['data_root'], 'js')]})
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
if enable_https:
|
|
|
|
protocol = "https"
|
2014-06-15 07:16:55 +00:00
|
|
|
self.server = HTTPServer(app, no_keep_alive=True,
|
|
|
|
ssl_options={"certfile": https_cert, "keyfile": https_key})
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-06-11 08:34:28 +00:00
|
|
|
protocol = "http"
|
2014-06-15 07:16:55 +00:00
|
|
|
self.server = HTTPServer(app, no_keep_alive=True)
|
|
|
|
|
|
|
|
logger.log(u"Starting SickRage on " + protocol + "://" + str(self.options['host']) + ":" + str(
|
|
|
|
self.options['port']) + "/")
|
|
|
|
|
|
|
|
self.server.listen(self.options['port'], self.options['host'])
|
|
|
|
|
|
|
|
if self.thread == None or not self.thread.isAlive():
|
|
|
|
self.thread = threading.Thread(None, self.monitor, 'TORNADO')
|
|
|
|
|
|
|
|
def monitor(self):
|
|
|
|
|
|
|
|
while True:
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-15 07:16:55 +00:00
|
|
|
currentTime = datetime.datetime.now()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-15 07:16:55 +00:00
|
|
|
if currentTime - self.lastRun > self.cycleTime:
|
|
|
|
self.lastRun = currentTime
|
|
|
|
try:
|
|
|
|
logger.log(u"Starting tornado", logger.DEBUG)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-15 07:16:55 +00:00
|
|
|
IOLoop.instance().start()
|
|
|
|
except Exception, e:
|
|
|
|
logger.log(u"Exception generated in tornado: " + ex(e), logger.ERROR)
|
|
|
|
logger.log(repr(traceback.format_exc()), logger.DEBUG)
|
|
|
|
|
|
|
|
if self.abort:
|
|
|
|
self.abort = False
|
|
|
|
self.thread = None
|
|
|
|
return
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
|
|
|
|
logger.logging.info('Shutting down tornado')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
try:
|
2014-06-15 07:16:55 +00:00
|
|
|
self.abort = True
|
|
|
|
self.server.stop()
|
|
|
|
|
|
|
|
deadline = time.time() + 10
|
|
|
|
|
|
|
|
io_loop = IOLoop.instance()
|
|
|
|
def stop_loop():
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
if now < deadline:
|
|
|
|
if io_loop._callbacks:
|
|
|
|
io_loop.add_timeout(now + 1, stop_loop)
|
|
|
|
return
|
|
|
|
stop_loop()
|
|
|
|
self.thread.join(10)
|
2014-06-11 08:34:28 +00:00
|
|
|
except:
|
2014-06-15 07:16:55 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
logger.logging.info('Tornado is now shutdown')
|