2013-02-19 01:13:42 +00:00
|
|
|
|
using System;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-07 05:32:22 +00:00
|
|
|
|
using System.Linq;
|
2013-09-11 15:35:58 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Reflection;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
using Microsoft.Owin.Hosting;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
using NLog;
|
2013-08-30 22:55:01 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-07-07 17:27:11 +00:00
|
|
|
|
using NzbDrone.Common.Security;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-08-30 22:55:01 +00:00
|
|
|
|
using NzbDrone.Host.AccessControl;
|
2013-08-07 05:32:22 +00:00
|
|
|
|
using NzbDrone.Host.Owin.MiddleWare;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
using Owin;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
|
2013-08-07 05:32:22 +00:00
|
|
|
|
namespace NzbDrone.Host.Owin
|
2013-02-19 01:13:42 +00:00
|
|
|
|
{
|
2013-05-04 20:29:24 +00:00
|
|
|
|
public class OwinHostController : IHostController
|
2013-02-19 01:13:42 +00:00
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2013-05-04 21:09:25 +00:00
|
|
|
|
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
|
2013-08-30 22:55:01 +00:00
|
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
|
|
|
|
private readonly IUrlAclAdapter _urlAclAdapter;
|
|
|
|
|
private readonly IFirewallAdapter _firewallAdapter;
|
2013-09-22 19:57:03 +00:00
|
|
|
|
private readonly ISslAdapter _sslAdapter;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
private IDisposable _host;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
|
2013-09-22 19:57:03 +00:00
|
|
|
|
public OwinHostController(IConfigFileProvider configFileProvider,
|
|
|
|
|
IEnumerable<IOwinMiddleWare> owinMiddleWares,
|
|
|
|
|
IRuntimeInfo runtimeInfo,
|
|
|
|
|
IUrlAclAdapter urlAclAdapter,
|
|
|
|
|
IFirewallAdapter firewallAdapter,
|
|
|
|
|
ISslAdapter sslAdapter,
|
|
|
|
|
Logger logger)
|
2013-02-19 01:13:42 +00:00
|
|
|
|
{
|
|
|
|
|
_configFileProvider = configFileProvider;
|
2013-05-04 21:09:25 +00:00
|
|
|
|
_owinMiddleWares = owinMiddleWares;
|
2013-08-30 22:55:01 +00:00
|
|
|
|
_runtimeInfo = runtimeInfo;
|
|
|
|
|
_urlAclAdapter = urlAclAdapter;
|
|
|
|
|
_firewallAdapter = firewallAdapter;
|
2013-09-22 19:57:03 +00:00
|
|
|
|
_sslAdapter = sslAdapter;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartServer()
|
|
|
|
|
{
|
2013-07-07 17:27:11 +00:00
|
|
|
|
IgnoreCertErrorPolicy.Register();
|
|
|
|
|
|
2013-09-21 02:50:57 +00:00
|
|
|
|
if (OsInfo.IsWindows)
|
|
|
|
|
{
|
|
|
|
|
if (_runtimeInfo.IsAdmin)
|
|
|
|
|
{
|
|
|
|
|
_firewallAdapter.MakeAccessible();
|
2013-09-22 19:57:03 +00:00
|
|
|
|
_sslAdapter.Register();
|
2013-09-21 02:50:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_urlAclAdapter.ConfigureUrl();
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 19:57:03 +00:00
|
|
|
|
var options = new StartOptions(_urlAclAdapter.Url)
|
2013-05-05 21:24:33 +00:00
|
|
|
|
{
|
2013-07-06 19:10:11 +00:00
|
|
|
|
ServerFactory = "Microsoft.Owin.Host.HttpListener"
|
2013-05-05 21:24:33 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-09-22 19:57:03 +00:00
|
|
|
|
if (_configFileProvider.EnableSsl)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("SSL enabled, listening on: {0}", _urlAclAdapter.HttpsUrl);
|
|
|
|
|
options.Urls.Add(_urlAclAdapter.HttpsUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Info("starting server on {0}", _urlAclAdapter.Url);
|
2013-05-21 03:28:14 +00:00
|
|
|
|
|
2013-09-11 15:35:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp);
|
|
|
|
|
}
|
|
|
|
|
catch (TargetInvocationException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.InnerException == null)
|
|
|
|
|
{
|
2013-09-11 21:38:35 +00:00
|
|
|
|
throw;
|
2013-09-11 15:35:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 21:38:35 +00:00
|
|
|
|
if (ex.InnerException is HttpListenerException)
|
2013-09-11 15:35:58 +00:00
|
|
|
|
{
|
2013-09-11 21:38:35 +00:00
|
|
|
|
throw new PortInUseException("Port {0} is already in use, please ensure NzbDrone is not already running.",
|
|
|
|
|
ex,
|
|
|
|
|
_configFileProvider.Port);
|
2013-09-11 15:35:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 21:38:35 +00:00
|
|
|
|
throw ex.InnerException;
|
2013-09-11 15:35:58 +00:00
|
|
|
|
}
|
2013-05-04 21:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BuildApp(IAppBuilder appBuilder)
|
|
|
|
|
{
|
2013-07-06 19:10:11 +00:00
|
|
|
|
appBuilder.Properties["host.AppName"] = "NzbDrone";
|
|
|
|
|
|
2013-05-05 21:24:33 +00:00
|
|
|
|
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
|
2013-05-04 21:09:25 +00:00
|
|
|
|
{
|
2013-05-05 21:24:33 +00:00
|
|
|
|
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
|
2013-05-04 21:09:25 +00:00
|
|
|
|
middleWare.Attach(appBuilder);
|
|
|
|
|
}
|
2013-05-04 20:29:24 +00:00
|
|
|
|
}
|
2013-03-26 04:03:16 +00:00
|
|
|
|
|
2013-02-19 01:13:42 +00:00
|
|
|
|
public string AppUrl
|
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
|
2013-02-19 01:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopServer()
|
|
|
|
|
{
|
|
|
|
|
if (_host == null) return;
|
|
|
|
|
|
|
|
|
|
_logger.Info("Attempting to stop Nancy host");
|
2013-05-04 20:29:24 +00:00
|
|
|
|
_host.Dispose();
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_host = null;
|
|
|
|
|
_logger.Info("Host has stopped");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|