|
|
@ -1,40 +1,30 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
using Microsoft.Owin.Hosting;
|
|
|
|
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Common.Security;
|
|
|
|
using NzbDrone.Common.Security;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
using NzbDrone.Host.AccessControl;
|
|
|
|
using NzbDrone.Host.AccessControl;
|
|
|
|
using NzbDrone.Host.Owin.MiddleWare;
|
|
|
|
|
|
|
|
using Owin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Host.Owin
|
|
|
|
namespace NzbDrone.Host.Owin
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class OwinHostController : IHostController
|
|
|
|
public class OwinHostController : IHostController
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
private readonly IOwinAppFactory _owinAppFactory;
|
|
|
|
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
|
|
|
|
|
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
|
|
|
private readonly IUrlAclAdapter _urlAclAdapter;
|
|
|
|
private readonly IUrlAclAdapter _urlAclAdapter;
|
|
|
|
private readonly IFirewallAdapter _firewallAdapter;
|
|
|
|
private readonly IFirewallAdapter _firewallAdapter;
|
|
|
|
private readonly ISslAdapter _sslAdapter;
|
|
|
|
private readonly ISslAdapter _sslAdapter;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private IDisposable _host;
|
|
|
|
private IDisposable _owinApp;
|
|
|
|
|
|
|
|
|
|
|
|
public OwinHostController(IConfigFileProvider configFileProvider,
|
|
|
|
public OwinHostController(
|
|
|
|
IEnumerable<IOwinMiddleWare> owinMiddleWares,
|
|
|
|
IOwinAppFactory owinAppFactory,
|
|
|
|
IRuntimeInfo runtimeInfo,
|
|
|
|
IRuntimeInfo runtimeInfo,
|
|
|
|
IUrlAclAdapter urlAclAdapter,
|
|
|
|
IUrlAclAdapter urlAclAdapter,
|
|
|
|
IFirewallAdapter firewallAdapter,
|
|
|
|
IFirewallAdapter firewallAdapter,
|
|
|
|
ISslAdapter sslAdapter,
|
|
|
|
ISslAdapter sslAdapter,
|
|
|
|
Logger logger)
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
_owinAppFactory = owinAppFactory;
|
|
|
|
_owinMiddleWares = owinMiddleWares;
|
|
|
|
|
|
|
|
_runtimeInfo = runtimeInfo;
|
|
|
|
_runtimeInfo = runtimeInfo;
|
|
|
|
_urlAclAdapter = urlAclAdapter;
|
|
|
|
_urlAclAdapter = urlAclAdapter;
|
|
|
|
_firewallAdapter = firewallAdapter;
|
|
|
|
_firewallAdapter = firewallAdapter;
|
|
|
@ -57,60 +47,27 @@ namespace NzbDrone.Host.Owin
|
|
|
|
|
|
|
|
|
|
|
|
_urlAclAdapter.ConfigureUrl();
|
|
|
|
_urlAclAdapter.ConfigureUrl();
|
|
|
|
|
|
|
|
|
|
|
|
var options = new StartOptions()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ServerFactory = "Microsoft.Owin.Host.HttpListener"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_urlAclAdapter.Urls.ForEach(options.Urls.Add);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_logger.Info("Listening on the following URLs:");
|
|
|
|
_logger.Info("Listening on the following URLs:");
|
|
|
|
foreach (var url in options.Urls)
|
|
|
|
foreach (var url in _urlAclAdapter.Urls)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Info(" {0}", url);
|
|
|
|
_logger.Info(" {0}", url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
_owinApp = _owinAppFactory.CreateApp(_urlAclAdapter.Urls);
|
|
|
|
{
|
|
|
|
|
|
|
|
_host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (TargetInvocationException ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ex.InnerException == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ex.InnerException is HttpListenerException)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new PortInUseException("Port {0} is already in use, please ensure NzbDrone is not already running.",
|
|
|
|
|
|
|
|
ex,
|
|
|
|
|
|
|
|
_configFileProvider.Port);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw ex.InnerException;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void BuildApp(IAppBuilder appBuilder)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
appBuilder.Properties["host.AppName"] = "NzbDrone";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
|
|
|
|
|
|
|
|
middleWare.Attach(appBuilder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void StopServer()
|
|
|
|
public void StopServer()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_host == null) return;
|
|
|
|
if (_owinApp == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
_logger.Info("Attempting to stop Nancy host");
|
|
|
|
_logger.Info("Attempting to stop OWIN host");
|
|
|
|
_host.Dispose();
|
|
|
|
_owinApp.Dispose();
|
|
|
|
_host = null;
|
|
|
|
_owinApp = null;
|
|
|
|
_logger.Info("Host has stopped");
|
|
|
|
_logger.Info("Host has stopped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|