From 2300d56f688c2515def000fbd2330ad9e0116d33 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 6 Jan 2015 22:36:42 -0500 Subject: [PATCH] Ssl in mediabrowser against new listener. --- .../IServerApplicationHost.cs | 22 +++++++++++++++++ MediaBrowser.Controller/Net/IHttpServer.cs | 4 +++- MediaBrowser.Controller/Net/IServerManager.cs | 4 +++- .../Configuration/ServerConfiguration.cs | 17 ++++++++++++- MediaBrowser.Model/System/SystemInfo.cs | 18 ++++++++++++++ .../HttpServer/HttpListenerHost.cs | 9 +++++-- .../SocketSharp/WebSocketSharpListener.cs | 7 ++++-- .../Localization/Server/server.json | 8 +++++++ .../ServerManager/ServerManager.cs | 8 +++---- .../ApplicationHost.cs | 24 ++++++++++++++++++- 10 files changed, 109 insertions(+), 12 deletions(-) diff --git a/MediaBrowser.Controller/IServerApplicationHost.cs b/MediaBrowser.Controller/IServerApplicationHost.cs index 76eb9fceb0..6bee5e58a3 100644 --- a/MediaBrowser.Controller/IServerApplicationHost.cs +++ b/MediaBrowser.Controller/IServerApplicationHost.cs @@ -36,6 +36,28 @@ namespace MediaBrowser.Controller /// The HTTP server port. int HttpServerPort { get; } + /// + /// Gets the HTTPS server port. + /// + /// The HTTPS server port. + int HttpsServerPort { get; } + + /// + /// Gets the value indiciating if an https port should be hosted. + /// + /// + /// The value indiciating if an https port should be hosted. + /// + bool UseHttps { get; } + + /// + /// Gets the value pointing to the file system where the ssl certiifcate is located. + /// + /// + /// The value pointing to the file system where the ssl certiifcate is located. + /// + string CertificatePath { get; } + /// /// Gets a value indicating whether this instance has update available. /// diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs index 5b179d479a..d56bee009e 100644 --- a/MediaBrowser.Controller/Net/IHttpServer.cs +++ b/MediaBrowser.Controller/Net/IHttpServer.cs @@ -19,7 +19,9 @@ namespace MediaBrowser.Controller.Net /// Starts the specified server name. /// /// The URL prefixes. - void StartServer(IEnumerable urlPrefixes); + /// If an https prefix is specified, + /// the ssl certificate localtion on the file system. + void StartServer(IEnumerable urlPrefixes, string certificatePath); /// /// Gets the local end points. diff --git a/MediaBrowser.Controller/Net/IServerManager.cs b/MediaBrowser.Controller/Net/IServerManager.cs index dff0863478..d90a0f8ed6 100644 --- a/MediaBrowser.Controller/Net/IServerManager.cs +++ b/MediaBrowser.Controller/Net/IServerManager.cs @@ -15,7 +15,9 @@ namespace MediaBrowser.Controller.Net /// Starts this instance. /// /// The URL prefixes. - void Start(IEnumerable urlPrefixes); + /// If an https prefix is specified, + /// the ssl certificate localtion on the file system. + void Start(IEnumerable urlPrefixes, string certificatePath); /// /// Sends a message to all clients currently connected via a web socket diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index c4a9c5eeaf..755fe8aa83 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Model.Dto; +using System.Xml.Schema; +using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; namespace MediaBrowser.Model.Configuration @@ -32,6 +33,17 @@ namespace MediaBrowser.Model.Configuration /// The HTTPS server port number. public int HttpsPortNumber { get; set; } + /// Gets or sets the value pointing to the file system where the ssl certiifcate is located. + /// + /// The value pointing to the file system where the ssl certiifcate is located. + public bool UseHttps { get; set; } + + /// + /// Gets or sets the value pointing to the file system where the ssl certiifcate is located.. + /// + /// The value pointing to the file system where the ssl certiifcate is located.. + public string CertificatePath { get; set; } + /// /// Gets or sets a value indicating whether [enable internet providers]. /// @@ -187,6 +199,7 @@ namespace MediaBrowser.Model.Configuration public string[] InsecureApps8 { get; set; } public bool SaveMetadataHidden { get; set; } + public bool EnableWin8HttpListener { get; set; } public NameValuePair[] ContentTypes { get; set; } @@ -204,6 +217,8 @@ namespace MediaBrowser.Model.Configuration PublicPort = 8096; HttpServerPortNumber = 8096; HttpsPortNumber = 8920; + UseHttps = false; + CertificatePath = null; EnableDashboardResponseCaching = true; EnableAutomaticRestart = true; diff --git a/MediaBrowser.Model/System/SystemInfo.cs b/MediaBrowser.Model/System/SystemInfo.cs index f9cacea122..9d4cfd6dbd 100644 --- a/MediaBrowser.Model/System/SystemInfo.cs +++ b/MediaBrowser.Model/System/SystemInfo.cs @@ -122,6 +122,24 @@ namespace MediaBrowser.Model.System /// The HTTP server port number. public int HttpServerPortNumber { get; set; } + /// + /// Gets or sets the value pointing to the file system where the ssl certiifcate is located. + /// + /// The value pointing to the file system where the ssl certiifcate is located. + public bool UseHttps { get; set; } + + /// + /// Gets or sets the value pointing to the file system where the ssl certiifcate is located.. + /// + /// The value pointing to the file system where the ssl certiifcate is located.. + public string CertificatePath { get; set; } + + /// + /// Gets or sets the HTTPS server port number. + /// + /// The HTTPS server port number. + public int HttpsPortNumber { get; set; } + /// /// Gets or sets a value indicating whether this instance has update available. /// diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index c3228db920..0c0922800a 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -44,6 +44,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer private readonly bool _supportsNativeWebSocket; + private string _certificatePath; + /// /// Gets the local end points. /// @@ -217,10 +219,12 @@ namespace MediaBrowser.Server.Implementations.HttpServer { if (_supportsNativeWebSocket && NativeWebSocket.IsSupported) { + // Certificate location is ignored here. You need to use netsh + // to assign the certificate to the proper port. return new HttpListenerServer(_logger, OnRequestReceived); } - return new WebSocketSharpListener(_logger, OnRequestReceived); + return new WebSocketSharpListener(_logger, OnRequestReceived, _certificatePath); } private void WebSocketHandler(WebSocketConnectEventArgs args) @@ -425,8 +429,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer GC.SuppressFinalize(this); } - public void StartServer(IEnumerable urlPrefixes) + public void StartServer(IEnumerable urlPrefixes, string certificatePath) { + _certificatePath = certificatePath; UrlPrefixes = urlPrefixes.ToList(); Start(UrlPrefixes.First()); } diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 04db0d8a58..1cf523ad29 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -18,11 +18,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp private readonly ILogger _logger; private readonly Action _endpointListener; + private readonly string _certificatePath ; - public WebSocketSharpListener(ILogger logger, Action endpointListener) + public WebSocketSharpListener(ILogger logger, Action endpointListener, + string certificatePath) { _logger = logger; _endpointListener = endpointListener; + _certificatePath = certificatePath; } public Action ErrorHandler { get; set; } @@ -34,7 +37,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp public void Start(IEnumerable urlPrefixes) { if (_listener == null) - _listener = new HttpListener(new PatternsLogger(_logger), null); + _listener = new HttpListener(new PatternsLogger(_logger), _certificatePath); foreach (var prefix in urlPrefixes) { diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index 35d58837cb..253d9a00db 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -508,6 +508,14 @@ "LabelLocalHttpServerPortNumberHelp": "The tcp port number that Media Browser's http server should bind to.", "LabelPublicPort": "Public port number:", "LabelPublicPortHelp": "The public port number that should be mapped to the local port.", + + "LabelUseHttps": "Enable SSL", + "LabelUseHttpsHelp": "Check to enable SSL hosting.", + "LabelHttpsPort": "Local http port:", + "LabelHttpsPortHelp": "The tcp port number that Media Browser's https server should bind to.", + "LabelCertificatePath": "SSL Certificate path:", + "LabelCertificatePathHelp": "The path on the filesystem to the ssl certificate pfx file.", + "LabelWebSocketPortNumber": "Web socket port number:", "LabelEnableAutomaticPortMap": "Enable automatic port mapping", "LabelEnableAutomaticPortMapHelp": "Attempt to automatically map the public port to the local port via UPnP. This may not work with some router models.", diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index 7a23d8e082..ef2fef7466 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -99,22 +99,22 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// Starts this instance. /// - public void Start(IEnumerable urlPrefixes) + public void Start(IEnumerable urlPrefixes, string certificatePath) { - ReloadHttpServer(urlPrefixes); + ReloadHttpServer(urlPrefixes, certificatePath); } /// /// Restarts the Http Server, or starts it if not currently running /// - private void ReloadHttpServer(IEnumerable urlPrefixes) + private void ReloadHttpServer(IEnumerable urlPrefixes, string certificatePath) { _logger.Info("Loading Http Server"); try { HttpServer = _applicationHost.Resolve(); - HttpServer.StartServer(urlPrefixes); + HttpServer.StartServer(urlPrefixes, certificatePath); } catch (SocketException ex) { diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index f7ff5eef15..29c5304382 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -133,6 +133,11 @@ namespace MediaBrowser.Server.Startup.Common "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/" }; + if (ServerConfigurationManager.Configuration.UseHttps) + { + list.Add("https://+:" + ServerConfigurationManager.Configuration.HttpsPortNumber + "/" + WebApplicationName + "/"); + } + return list; } } @@ -805,7 +810,7 @@ namespace MediaBrowser.Server.Startup.Common { try { - ServerManager.Start(HttpServerUrlPrefixes); + ServerManager.Start(HttpServerUrlPrefixes, CertificatePath); } catch (Exception ex) { @@ -972,6 +977,8 @@ namespace MediaBrowser.Server.Startup.Common CachePath = ApplicationPaths.CachePath, MacAddress = GetMacAddress(), HttpServerPortNumber = HttpServerPort, + UseHttps = UseHttps, + CertificatePath = CertificatePath, OperatingSystem = OperatingSystemDisplayName, CanSelfRestart = CanSelfRestart, CanSelfUpdate = CanSelfUpdate, @@ -1046,6 +1053,21 @@ namespace MediaBrowser.Server.Startup.Common get { return ServerConfigurationManager.Configuration.HttpServerPortNumber; } } + public bool UseHttps + { + get { return this.ServerConfigurationManager.Configuration.UseHttps; } + } + + public string CertificatePath + { + get { return this.ServerConfigurationManager.Configuration.CertificatePath; } + } + + public int HttpsServerPort + { + get { return ServerConfigurationManager.Configuration.HttpsPortNumber; } + } + /// /// Gets the mac address. ///