2013-02-19 04:13:42 +03:00
|
|
|
using System;
|
2013-05-04 23:29:24 +03:00
|
|
|
using System.Collections.Generic;
|
2013-08-07 08:32:22 +03:00
|
|
|
using System.Linq;
|
2013-05-04 23:29:24 +03:00
|
|
|
using Microsoft.Owin.Hosting;
|
2013-02-19 04:13:42 +03:00
|
|
|
using NLog;
|
2013-08-31 01:55:01 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-07-07 20:27:11 +03:00
|
|
|
using NzbDrone.Common.Security;
|
2013-05-23 08:12:01 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-08-31 01:55:01 +03:00
|
|
|
using NzbDrone.Host.AccessControl;
|
2013-08-07 08:32:22 +03:00
|
|
|
using NzbDrone.Host.Owin.MiddleWare;
|
2013-05-04 23:29:24 +03:00
|
|
|
using Owin;
|
2013-02-19 04:13:42 +03:00
|
|
|
|
2013-08-07 08:32:22 +03:00
|
|
|
namespace NzbDrone.Host.Owin
|
2013-02-19 04:13:42 +03:00
|
|
|
{
|
2013-05-04 23:29:24 +03:00
|
|
|
public class OwinHostController : IHostController
|
2013-02-19 04:13:42 +03:00
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2013-05-05 00:09:25 +03:00
|
|
|
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
|
2013-08-31 01:55:01 +03:00
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
|
|
|
private readonly IUrlAclAdapter _urlAclAdapter;
|
|
|
|
private readonly IFirewallAdapter _firewallAdapter;
|
2013-02-19 04:13:42 +03:00
|
|
|
private readonly Logger _logger;
|
2013-05-04 23:29:24 +03:00
|
|
|
private IDisposable _host;
|
2013-02-19 04:13:42 +03:00
|
|
|
|
2013-08-31 01:55:01 +03:00
|
|
|
public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares,
|
|
|
|
IRuntimeInfo runtimeInfo, IUrlAclAdapter urlAclAdapter, IFirewallAdapter firewallAdapter, Logger logger)
|
2013-02-19 04:13:42 +03:00
|
|
|
{
|
|
|
|
_configFileProvider = configFileProvider;
|
2013-05-05 00:09:25 +03:00
|
|
|
_owinMiddleWares = owinMiddleWares;
|
2013-08-31 01:55:01 +03:00
|
|
|
_runtimeInfo = runtimeInfo;
|
|
|
|
_urlAclAdapter = urlAclAdapter;
|
|
|
|
_firewallAdapter = firewallAdapter;
|
2013-02-19 04:13:42 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartServer()
|
|
|
|
{
|
2013-07-07 20:27:11 +03:00
|
|
|
IgnoreCertErrorPolicy.Register();
|
|
|
|
|
2013-08-31 01:55:01 +03:00
|
|
|
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
|
|
|
|
{
|
|
|
|
_urlAclAdapter.RefreshRegistration();
|
|
|
|
_firewallAdapter.MakeAccessible();
|
|
|
|
}
|
2013-07-06 22:10:11 +03:00
|
|
|
|
2013-08-31 01:55:01 +03:00
|
|
|
var options = new StartOptions(_urlAclAdapter.UrlAcl)
|
2013-05-06 00:24:33 +03:00
|
|
|
{
|
2013-07-06 22:10:11 +03:00
|
|
|
ServerFactory = "Microsoft.Owin.Host.HttpListener"
|
2013-05-06 00:24:33 +03:00
|
|
|
};
|
|
|
|
|
2013-08-31 01:55:01 +03:00
|
|
|
_logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl);
|
2013-05-21 06:28:14 +03:00
|
|
|
|
2013-08-29 01:25:12 +03:00
|
|
|
_host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp);
|
2013-05-05 00:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void BuildApp(IAppBuilder appBuilder)
|
|
|
|
{
|
2013-07-06 22:10:11 +03:00
|
|
|
appBuilder.Properties["host.AppName"] = "NzbDrone";
|
|
|
|
|
2013-05-06 00:24:33 +03:00
|
|
|
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
|
2013-05-05 00:09:25 +03:00
|
|
|
{
|
2013-05-06 00:24:33 +03:00
|
|
|
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
|
2013-05-05 00:09:25 +03:00
|
|
|
middleWare.Attach(appBuilder);
|
|
|
|
}
|
2013-05-04 23:29:24 +03:00
|
|
|
}
|
2013-03-26 07:03:16 +03:00
|
|
|
|
2013-02-19 04:13:42 +03:00
|
|
|
public string AppUrl
|
|
|
|
{
|
2013-03-01 03:50:50 +03:00
|
|
|
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
|
2013-02-19 04:13:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void StopServer()
|
|
|
|
{
|
|
|
|
if (_host == null) return;
|
|
|
|
|
|
|
|
_logger.Info("Attempting to stop Nancy host");
|
2013-05-04 23:29:24 +03:00
|
|
|
_host.Dispose();
|
2013-02-19 04:13:42 +03:00
|
|
|
_host = null;
|
|
|
|
_logger.Info("Host has stopped");
|
|
|
|
}
|
2013-05-04 23:29:24 +03:00
|
|
|
|
2013-02-19 04:13:42 +03:00
|
|
|
}
|
|
|
|
}
|