Host process cleanup.

pull/29/merge
Keivan Beigi 11 years ago
parent dd835d5503
commit 7a19b6a2b3

@ -1,5 +1,6 @@
using System;
using System.Threading;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Host;
@ -7,6 +8,8 @@ namespace NzbDrone.Console
{
public static class ConsoleApp
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
try
@ -18,7 +21,7 @@ namespace NzbDrone.Console
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
Logger.FatalException(e.Message, e);
System.Console.ReadLine();
}

@ -1,6 +1,7 @@
using System;
using NetFwTypeLib;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Host.AccessControl
@ -31,9 +32,6 @@ namespace NzbDrone.Host.AccessControl
return;
}
CloseFirewallPort();
//Open the new port
OpenFirewallPort(_configFileProvider.Port);
}
}
@ -91,38 +89,10 @@ namespace NzbDrone.Host.AccessControl
}
}
private void CloseFirewallPort()
{
try
{
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var portNumber = 8989;
foreach (INetFwOpenPort p in ports)
{
if (p.Name == "NzbDrone")
{
portNumber = p.Port;
break;
}
}
if (portNumber != _configFileProvider.Port)
{
ports.Remove(portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
}
}
catch (Exception ex)
{
_logger.WarnException("Failed to close port in firewall for NzbDrone", ex);
}
}
private bool IsFirewallEnabled()
{
if (OsInfo.IsLinux) return false;
try
{
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
@ -135,7 +105,5 @@ namespace NzbDrone.Host.AccessControl
return false;
}
}
}
}

@ -9,6 +9,7 @@ namespace NzbDrone.Host.AccessControl
public interface IUrlAclAdapter
{
void RefreshRegistration();
string UrlAcl { get; }
}
public class UrlAclAdapter : IUrlAclAdapter
@ -24,18 +25,25 @@ namespace NzbDrone.Host.AccessControl
_logger = logger;
}
public string UrlAcl
{
get
{
return "http://*:" + _configFileProvider.Port + "/";
}
}
public void RefreshRegistration()
{
if (OsInfo.Version.Major < 6)
return;
RegisterUrl(_configFileProvider.Port);
RegisterUrl();
}
private void RegisterUrl(int portNumber)
private void RegisterUrl()
{
var arguments = String.Format("http add urlacl http://*:{0}/ sddl=D:(A;;GX;;;S-1-1-0)", portNumber);
var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl);
RunNetsh(arguments);
}

@ -4,7 +4,6 @@ using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
using NzbDrone.Host.AccessControl;
using NzbDrone.Host.Owin;
namespace NzbDrone.Host
@ -23,13 +22,10 @@ namespace NzbDrone.Host
private readonly IProcessProvider _processProvider;
private readonly PriorityMonitor _priorityMonitor;
private readonly IStartupArguments _startupArguments;
private readonly IFirewallAdapter _firewallAdapter;
private readonly IUrlAclAdapter _urlAclAdapter;
private readonly Logger _logger;
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider, IHostController hostController, IRuntimeInfo runtimeInfo,
IProcessProvider processProvider, PriorityMonitor priorityMonitor, IStartupArguments startupArguments,
IFirewallAdapter firewallAdapter, IUrlAclAdapter urlAclAdapter, Logger logger)
IProcessProvider processProvider, PriorityMonitor priorityMonitor, IStartupArguments startupArguments, Logger logger)
{
_configFileProvider = configFileProvider;
_hostController = hostController;
@ -37,8 +33,6 @@ namespace NzbDrone.Host
_processProvider = processProvider;
_priorityMonitor = priorityMonitor;
_startupArguments = startupArguments;
_firewallAdapter = firewallAdapter;
_urlAclAdapter = urlAclAdapter;
_logger = logger;
}
@ -49,11 +43,6 @@ namespace NzbDrone.Host
public void Start()
{
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
_hostController.StartServer();
if (!_startupArguments.Flags.Contains(StartupArguments.NO_BROWSER) &&

@ -4,7 +4,6 @@
{
string AppUrl { get; }
void StartServer();
void RestartServer();
void StopServer();
}
}

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin.Hosting;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Security;
using NzbDrone.Core.Configuration;
using NzbDrone.Host.AccessControl;
using NzbDrone.Host.Owin.MiddleWare;
using Owin;
@ -14,13 +16,20 @@ namespace NzbDrone.Host.Owin
{
private readonly IConfigFileProvider _configFileProvider;
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
private readonly IRuntimeInfo _runtimeInfo;
private readonly IUrlAclAdapter _urlAclAdapter;
private readonly IFirewallAdapter _firewallAdapter;
private readonly Logger _logger;
private IDisposable _host;
public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares, Logger logger)
public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares,
IRuntimeInfo runtimeInfo, IUrlAclAdapter urlAclAdapter, IFirewallAdapter firewallAdapter, Logger logger)
{
_configFileProvider = configFileProvider;
_owinMiddleWares = owinMiddleWares;
_runtimeInfo = runtimeInfo;
_urlAclAdapter = urlAclAdapter;
_firewallAdapter = firewallAdapter;
_logger = logger;
}
@ -28,14 +37,18 @@ namespace NzbDrone.Host.Owin
{
IgnoreCertErrorPolicy.Register();
var url = "http://*:" + _configFileProvider.Port;
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
var options = new StartOptions(url)
var options = new StartOptions(_urlAclAdapter.UrlAcl)
{
ServerFactory = "Microsoft.Owin.Host.HttpListener"
};
_logger.Info("starting server on {0}", url);
_logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl);
_host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp);
}
@ -56,14 +69,6 @@ namespace NzbDrone.Host.Owin
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
}
public void RestartServer()
{
_logger.Warn("Attempting to restart server.");
StopServer();
StartServer();
}
public void StopServer()
{
if (_host == null) return;

@ -83,6 +83,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>

@ -1,5 +1,6 @@
using System;
using System.Windows.Forms;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Host;
using NzbDrone.SysTray;
@ -8,6 +9,8 @@ namespace NzbDrone
{
public static class WindowsApp
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
try
@ -21,6 +24,7 @@ namespace NzbDrone
}
catch (Exception e)
{
Logger.FatalException(e.Message, e);
var message = string.Format("{0}: {1}", e.GetType().Name, e.Message);
MessageBox.Show(text: message, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error, caption: "Epic Fail!");
}

@ -5,5 +5,6 @@
<package id="Microsoft.Owin" version="1.1.0-beta2" targetFramework="net40" />
<package id="Microsoft.Owin.Hosting" version="1.1.0-beta2" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="Owin" version="1.0" targetFramework="net40" />
</packages>
Loading…
Cancel
Save