2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-06 00:24:43 -04:00
|
|
|
import cherrypy
|
2014-03-10 01:18:05 -04:00
|
|
|
import cherrypy.lib.auth_basic
|
|
|
|
import os.path
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.webserve import WebInterface
|
|
|
|
|
|
|
|
from sickbeard.helpers import create_https_certificates
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
def initWebServer(options={}):
|
|
|
|
options.setdefault('port', 8081)
|
|
|
|
options.setdefault('host', '0.0.0.0')
|
|
|
|
options.setdefault('log_dir', None)
|
|
|
|
options.setdefault('username', '')
|
|
|
|
options.setdefault('password', '')
|
|
|
|
options.setdefault('web_root', '/')
|
|
|
|
assert isinstance(options['port'], int)
|
|
|
|
assert 'data_root' in options
|
|
|
|
|
|
|
|
def http_error_401_hander(status, message, traceback, version):
|
|
|
|
""" Custom handler for 401 error """
|
|
|
|
if status != "401 Unauthorized":
|
|
|
|
logger.log(u"CherryPy caught an error: %s %s" % (status, message), logger.ERROR)
|
|
|
|
logger.log(traceback, logger.DEBUG)
|
|
|
|
return r'''<!DOCTYPE html>
|
2014-03-10 01:18:05 -04:00
|
|
|
<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)
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
def http_error_404_hander(status, message, traceback, version):
|
|
|
|
""" Custom handler for 404 error, redirect back to main page """
|
|
|
|
return r'''<!DOCTYPE html>
|
2014-03-10 01:18:05 -04:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>404</title>
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
|
<!--
|
|
|
|
location.href = "%s/home/"
|
|
|
|
//-->
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<br/>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''' % options['web_root']
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
# cherrypy setup
|
|
|
|
enable_https = options['enable_https']
|
|
|
|
https_cert = options['https_cert']
|
|
|
|
https_key = options['https_key']
|
|
|
|
|
|
|
|
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")
|
2014-03-10 01:18:05 -04:00
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
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)
|
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
|
|
|
mime_gzip = ('text/html',
|
|
|
|
'text/plain',
|
|
|
|
'text/css',
|
|
|
|
'text/javascript',
|
|
|
|
'application/javascript',
|
|
|
|
'text/x-javascript',
|
|
|
|
'application/x-javascript',
|
|
|
|
'text/x-json',
|
|
|
|
'application/json'
|
|
|
|
)
|
|
|
|
|
|
|
|
options_dict = {
|
|
|
|
'server.socket_port': options['port'],
|
|
|
|
'server.socket_host': options['host'],
|
|
|
|
'log.screen': False,
|
|
|
|
'engine.autoreload.on': False,
|
|
|
|
'engine.autoreload.frequency': 100,
|
|
|
|
'engine.reexec_retry': 100,
|
|
|
|
'tools.gzip.on': True,
|
|
|
|
'tools.gzip.mime_types': mime_gzip,
|
|
|
|
'error_page.401': http_error_401_hander,
|
|
|
|
'error_page.404': http_error_404_hander,
|
|
|
|
}
|
|
|
|
|
|
|
|
if enable_https:
|
|
|
|
options_dict['server.ssl_certificate'] = https_cert
|
|
|
|
options_dict['server.ssl_private_key'] = https_key
|
2014-06-06 00:24:43 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
protocol = "https"
|
|
|
|
else:
|
|
|
|
protocol = "http"
|
|
|
|
|
2014-05-23 08:37:22 -04:00
|
|
|
logger.log(u"Starting SickRage on " + protocol + "://" + str(options['host']) + ":" + str(options['port']) + "/")
|
2014-03-25 01:57:24 -04:00
|
|
|
cherrypy.config.update(options_dict)
|
|
|
|
|
|
|
|
# setup cherrypy logging
|
|
|
|
if options['log_dir'] and os.path.isdir(options['log_dir']):
|
|
|
|
cherrypy.config.update({'log.access_file': os.path.join(options['log_dir'], "cherrypy.log")})
|
|
|
|
logger.log('Using %s for cherrypy log' % cherrypy.config['log.access_file'])
|
|
|
|
|
|
|
|
conf = {
|
|
|
|
'/': {
|
|
|
|
'tools.staticdir.root': options['data_root'],
|
|
|
|
'tools.encode.on': True,
|
|
|
|
'tools.encode.encoding': 'utf-8',
|
2014-05-04 14:47:09 -04:00
|
|
|
'tools.handle_reverse_proxy.on': True,
|
2014-03-25 01:57:24 -04:00
|
|
|
},
|
|
|
|
'/images': {
|
|
|
|
'tools.staticdir.on': True,
|
|
|
|
'tools.staticdir.dir': 'images'
|
|
|
|
},
|
|
|
|
'/js': {
|
|
|
|
'tools.staticdir.on': True,
|
|
|
|
'tools.staticdir.dir': 'js'
|
|
|
|
},
|
|
|
|
'/css': {
|
|
|
|
'tools.staticdir.on': True,
|
|
|
|
'tools.staticdir.dir': 'css'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app = cherrypy.tree.mount(WebInterface(), options['web_root'], conf)
|
|
|
|
|
|
|
|
# auth
|
|
|
|
if options['username'] != "" and options['password'] != "":
|
|
|
|
if sickbeard.CALENDAR_UNPROTECTED:
|
|
|
|
checkpassword = cherrypy.lib.auth_basic.checkpassword_dict({options['username']: options['password']})
|
|
|
|
app.merge({
|
|
|
|
'/': {
|
|
|
|
'tools.auth_basic.on': True,
|
2014-05-23 08:37:22 -04:00
|
|
|
'tools.auth_basic.realm': 'SickRage',
|
2014-03-25 01:57:24 -04:00
|
|
|
'tools.auth_basic.checkpassword': checkpassword
|
|
|
|
},
|
|
|
|
'/api': {
|
|
|
|
'tools.auth_basic.on': False
|
|
|
|
},
|
|
|
|
'/calendar': {
|
|
|
|
'tools.auth_basic.on': False
|
|
|
|
},
|
|
|
|
'/api/builder': {
|
|
|
|
'tools.auth_basic.on': True,
|
2014-05-23 08:37:22 -04:00
|
|
|
'tools.auth_basic.realm': 'SickRage',
|
2014-03-25 01:57:24 -04:00
|
|
|
'tools.auth_basic.checkpassword': checkpassword
|
|
|
|
}
|
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
checkpassword = cherrypy.lib.auth_basic.checkpassword_dict({options['username']: options['password']})
|
|
|
|
app.merge({
|
|
|
|
'/': {
|
|
|
|
'tools.auth_basic.on': True,
|
2014-05-23 08:37:22 -04:00
|
|
|
'tools.auth_basic.realm': 'SickRage',
|
2014-03-25 01:57:24 -04:00
|
|
|
'tools.auth_basic.checkpassword': checkpassword
|
|
|
|
},
|
|
|
|
'/api': {
|
|
|
|
'tools.auth_basic.on': False
|
|
|
|
},
|
|
|
|
'/api/builder': {
|
|
|
|
'tools.auth_basic.on': True,
|
2014-05-23 08:37:22 -04:00
|
|
|
'tools.auth_basic.realm': 'SickRage',
|
2014-03-25 01:57:24 -04:00
|
|
|
'tools.auth_basic.checkpassword': checkpassword
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-06-06 00:24:43 -04:00
|
|
|
|
|
|
|
cherrypy.server.thread_pool = 30
|
2014-03-25 01:57:24 -04:00
|
|
|
cherrypy.server.start()
|
|
|
|
cherrypy.server.wait()
|
2014-03-10 01:18:05 -04:00
|
|
|
|