fixed jobprovider teardown breaking in mono.

This commit is contained in:
kay.one 2013-02-17 08:09:49 -08:00
parent cb0c3b85de
commit aeae9d75d1
2 changed files with 171 additions and 176 deletions

View File

@ -3,225 +3,216 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Security.Principal; using System.Security.Principal;
using NLog; using NLog;
#if __MonoCS__
#else
using NetFwTypeLib; using NetFwTypeLib;
#endif
namespace NzbDrone.Common namespace NzbDrone.Common
{ {
public class SecurityProvider public class SecurityProvider
{ {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger ();
private readonly ConfigFileProvider _configFileProvider; private readonly ConfigFileProvider _configFileProvider;
private readonly EnvironmentProvider _environmentProvider; private readonly EnvironmentProvider _environmentProvider;
private readonly ProcessProvider _processProvider; private readonly ProcessProvider _processProvider;
public SecurityProvider(ConfigFileProvider configFileProvider, EnvironmentProvider environmentProvider, public SecurityProvider (ConfigFileProvider configFileProvider, EnvironmentProvider environmentProvider,
ProcessProvider processProvider) ProcessProvider processProvider)
{ {
_configFileProvider = configFileProvider; _configFileProvider = configFileProvider;
_environmentProvider = environmentProvider; _environmentProvider = environmentProvider;
_processProvider = processProvider; _processProvider = processProvider;
} }
public SecurityProvider() public SecurityProvider ()
{ {
} }
public virtual void MakeAccessible() public virtual void MakeAccessible ()
{ {
if (!IsCurrentUserAdmin()) if (!IsCurrentUserAdmin ()) {
{ Logger.Trace ("User is not an admin, skipping.");
Logger.Trace("User is not an admin, skipping."); return;
return; }
}
int port = 0; int port = 0;
if (IsFirewallEnabled()) if (IsFirewallEnabled ()) {
{ if (IsNzbDronePortOpen ()) {
if(IsNzbDronePortOpen()) Logger.Trace ("NzbDrone port is already open, skipping.");
{ return;
Logger.Trace("NzbDrone port is already open, skipping."); }
return;
}
//Close any old ports //Close any old ports
port = CloseFirewallPort(); port = CloseFirewallPort ();
//Open the new port //Open the new port
OpenFirewallPort(_configFileProvider.Port); OpenFirewallPort (_configFileProvider.Port);
} }
//Skip Url Register if not Vista or 7 //Skip Url Register if not Vista or 7
if (_environmentProvider.GetOsVersion().Major < 6) if (_environmentProvider.GetOsVersion ().Major < 6)
return; return;
//Unregister Url (if port != 0) //Unregister Url (if port != 0)
if (port != 0) if (port != 0)
UnregisterUrl(port); UnregisterUrl (port);
//Register Url //Register Url
RegisterUrl(_configFileProvider.Port); RegisterUrl (_configFileProvider.Port);
} }
public virtual bool IsCurrentUserAdmin() public virtual bool IsCurrentUserAdmin ()
{ {
try try {
{ var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return principal.IsInRole (WindowsBuiltInRole.Administrator);
return principal.IsInRole(WindowsBuiltInRole.Administrator); } catch (Exception ex) {
} Logger.WarnException ("Error checking if the current user is an administrator.", ex);
catch(Exception ex) return false;
{ }
Logger.WarnException("Error checking if the current user is an administrator.", ex); }
return false;
}
}
public virtual bool IsNzbDronePortOpen() public virtual bool IsNzbDronePortOpen ()
{ {
try #if __MonoCS__
{ #else
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled) try {
return false; var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
foreach (INetFwOpenPort p in ports) var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
{
if (p.Port == _configFileProvider.Port)
return true;
}
}
catch(Exception ex)
{
Logger.WarnException("Failed to check for open port in firewall", ex);
}
return false;
}
private bool OpenFirewallPort(int portNumber) if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
{ return false;
try
{
var type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort", false);
var port = Activator.CreateInstance(type) as INetFwOpenPort;
port.Port = portNumber; var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
port.Name = "NzbDrone";
port.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
port.Enabled = true;
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); foreach (INetFwOpenPort p in ports) {
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); if (p.Port == _configFileProvider.Port)
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; return true;
}
} catch (Exception ex) {
Logger.WarnException ("Failed to check for open port in firewall", ex);
}
#endif
return false;
}
ports.Add(port); private bool OpenFirewallPort (int portNumber)
return true; {
} #if __MonoCS__
catch(Exception ex) return true;
{ #else
Logger.WarnException("Failed to open port in firewall for NzbDrone " + portNumber, ex); try {
return false; var type = Type.GetTypeFromProgID ("HNetCfg.FWOpenPort", false);
} var port = Activator.CreateInstance (type) as INetFwOpenPort;
}
private int CloseFirewallPort() port.Port = portNumber;
{ port.Name = "NzbDrone";
try port.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
{ port.Enabled = true;
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var portNumber = 8989; var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
foreach (INetFwOpenPort p in ports) ports.Add (port);
{ return true;
if (p.Name == "NzbDrone") } catch (Exception ex) {
{ Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
portNumber = p.Port; return false;
break; }
} #endif
} }
if (portNumber != _configFileProvider.Port) private int CloseFirewallPort ()
{ {
ports.Remove(portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
return portNumber;
}
}
catch(Exception ex)
{
Logger.WarnException("Failed to close port in firewall for NzbDrone", ex);
}
return 0; #if __MonoCS__
} #else
private bool IsFirewallEnabled() try {
{ var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
try var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
{ var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
return mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
}
catch(Exception ex) var portNumber = 8989;
{
Logger.WarnException("Failed to check if the firewall is enabled", ex);
return false;
}
}
private bool RegisterUrl(int portNumber) foreach (INetFwOpenPort p in ports) {
{ if (p.Name == "NzbDrone") {
try portNumber = p.Port;
{ break;
var startInfo = new ProcessStartInfo() }
}
if (portNumber != _configFileProvider.Port) {
ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
return portNumber;
}
} catch (Exception ex) {
Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
}
#endif
return 0;
}
private bool IsFirewallEnabled ()
{
#if __MonoCS__
return true;
#else
try {
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
return mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
} catch (Exception ex) {
Logger.WarnException ("Failed to check if the firewall is enabled", ex);
return false;
}
#endif
}
private bool RegisterUrl (int portNumber)
{
try {
var startInfo = new ProcessStartInfo ()
{ {
FileName = "netsh.exe", FileName = "netsh.exe",
Arguments = string.Format("http add urlacl http://*:{0}/ user=EVERYONE", portNumber) Arguments = string.Format("http add urlacl http://*:{0}/ user=EVERYONE", portNumber)
}; };
var process = _processProvider.Start(startInfo); var process = _processProvider.Start (startInfo);
process.WaitForExit(5000); process.WaitForExit (5000);
return true; return true;
} } catch (Exception ex) {
Logger.WarnException ("Error registering URL", ex);
}
catch(Exception ex) return false;
{ }
Logger.WarnException("Error registering URL", ex);
}
return false; private bool UnregisterUrl (int portNumber)
} {
try {
private bool UnregisterUrl(int portNumber) var startInfo = new ProcessStartInfo ()
{
try
{
var startInfo = new ProcessStartInfo()
{ {
FileName = "netsh.exe", FileName = "netsh.exe",
Arguments = string.Format("http delete urlacl http://*:{0}/", portNumber) Arguments = string.Format("http delete urlacl http://*:{0}/", portNumber)
}; };
var process = _processProvider.Start(startInfo); var process = _processProvider.Start (startInfo);
process.WaitForExit(5000); process.WaitForExit (5000);
return true; return true;
} } catch (Exception ex) {
Logger.WarnException ("Error registering URL", ex);
}
catch (Exception ex) return false;
{ }
Logger.WarnException("Error registering URL", ex); }
}
return false;
}
}
} }

View File

@ -8,6 +8,7 @@ using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NCrunch.Framework; using NCrunch.Framework;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Jobs; using NzbDrone.Core.Jobs;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
@ -40,7 +41,10 @@ namespace NzbDrone.Core.Test.ProviderTests.JobProviderTests
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{ {
Mocker.Resolve<JobProvider>().Queue.Should().BeEmpty(); if(!EnvironmentProvider.IsMono)
{
Mocker.Resolve<JobProvider>().Queue.Should().BeEmpty();
}
} }
private void ResetLastExecution() private void ResetLastExecution()