From b13efa08ff6c03f0b1aba8e427adc298ae912f78 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Tue, 23 Jun 2009 18:21:25 +0000 Subject: [PATCH] add ssl support to the webui webserver --- deluge/ui/web/server.py | 19 +++++++++++++++++++ deluge/ui/web/web.py | 10 ++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/deluge/ui/web/server.py b/deluge/ui/web/server.py index 9844966f0..5e49f51ea 100644 --- a/deluge/ui/web/server.py +++ b/deluge/ui/web/server.py @@ -47,6 +47,7 @@ import pkg_resources from twisted.application import service, internet from twisted.internet import reactor, error +from twisted.internet.ssl import SSL from twisted.web import http, resource, server, static from deluge import common, component @@ -378,6 +379,15 @@ class TopLevel(resource.Resource): request.setHeader("content-type", "text/html; charset=utf-8") return template.render(scripts=scripts, stylesheets=self.stylesheets) +class ServerContextFactory: + + def getContext(self): + """Creates an SSL context.""" + ctx = SSL.Context(SSL.SSLv3_METHOD) + ctx.use_privatekey_file(common.get_default_config_dir(os.path.join('ssl', 'web.key'))) + ctx.use_certificate_file(common.get_default_config_dir(os.path.join('ssl', 'web.crt'))) + return ctx + class DelugeWeb(component.Component): def __init__(self): @@ -409,6 +419,7 @@ class DelugeWeb(component.Component): self.top_level = TopLevel() self.site = server.Site(self.top_level) self.port = self.config["port"] + self.https = self.config["https"] self.web_api = WebApi() self.auth = Auth() @@ -439,6 +450,14 @@ class DelugeWeb(component.Component): self.port, self.port) self.plugins.enable_plugins() reactor.run() + + def start_ssl(self): + log.info("%s %s.", _("Starting server in PID"), os.getpid()) + reactor.listenSSL(self.port, self.site, ServerContextFactory()) + log.info("serving on %s:%s view at https://127.0.0.1:%s", "0.0.0.0", + self.port, self.port) + self.plugins.enable_plugins() + reactor.run() def shutdown(self, *args): log.info("Shutting down webserver") diff --git a/deluge/ui/web/web.py b/deluge/ui/web/web.py index 7ea9c180b..526cb21c9 100644 --- a/deluge/ui/web/web.py +++ b/deluge/ui/web/web.py @@ -54,6 +54,8 @@ class Web(_UI): group.add_option("-p", "--port", dest="port", type="int", help="Sets the port to be used for the webserver", action="store", default=None) + group.add_option("--ssl", dest="ssl", action="store_true", + help="Tells the webserver to use ssl", default=False) self.parser.add_option_group(group) @property @@ -67,9 +69,13 @@ class Web(_UI): self.__server = server.DelugeWeb() if self.options.port: - self.server.port = self.options.port + self.server.port = self.options.port - self.server.start() + if self.options.ssl or self.server.https: + self.server.https = self.options.ssl + self.server.start_ssl() + else: + self.server.start() def start(): web = Web()