fixed jobprovider teardown breaking in mono.

pull/6/head
kay.one 12 years ago
parent cb0c3b85de
commit aeae9d75d1

@ -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 ()) {
{ Logger.Trace ("NzbDrone port is already open, skipping.");
if(IsNzbDronePortOpen()) return;
{ }
Logger.Trace("NzbDrone port is already open, skipping.");
return; //Close any old ports
} port = CloseFirewallPort ();
//Close any old ports //Open the new port
port = CloseFirewallPort(); OpenFirewallPort (_configFileProvider.Port);
}
//Open the new port
OpenFirewallPort(_configFileProvider.Port); //Skip Url Register if not Vista or 7
} if (_environmentProvider.GetOsVersion ().Major < 6)
return;
//Skip Url Register if not Vista or 7
if (_environmentProvider.GetOsVersion().Major < 6) //Unregister Url (if port != 0)
return; if (port != 0)
UnregisterUrl (port);
//Unregister Url (if port != 0)
if (port != 0) //Register Url
UnregisterUrl(port); RegisterUrl (_configFileProvider.Port);
}
//Register Url
RegisterUrl(_configFileProvider.Port); public virtual bool IsCurrentUserAdmin ()
} {
try {
public virtual bool IsCurrentUserAdmin() var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
{ return principal.IsInRole (WindowsBuiltInRole.Administrator);
try } catch (Exception ex) {
{ Logger.WarnException ("Error checking if the current user is an administrator.", ex);
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return false;
return principal.IsInRole(WindowsBuiltInRole.Administrator); }
} }
catch(Exception ex)
{ public virtual bool IsNzbDronePortOpen ()
Logger.WarnException("Error checking if the current user is an administrator.", ex); {
return false; #if __MonoCS__
} #else
}
try {
public virtual bool IsNzbDronePortOpen() var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
{
try
{ var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
return false;
if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
return false; var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; foreach (INetFwOpenPort p in ports) {
if (p.Port == _configFileProvider.Port)
foreach (INetFwOpenPort p in ports) return true;
{ }
if (p.Port == _configFileProvider.Port) } catch (Exception ex) {
return true; Logger.WarnException ("Failed to check for open port in firewall", ex);
} }
} #endif
catch(Exception ex) return false;
{ }
Logger.WarnException("Failed to check for open port in firewall", ex);
} private bool OpenFirewallPort (int portNumber)
return false; {
} #if __MonoCS__
return true;
private bool OpenFirewallPort(int portNumber) #else
{ try {
try var type = Type.GetTypeFromProgID ("HNetCfg.FWOpenPort", false);
{ var port = Activator.CreateInstance (type) as INetFwOpenPort;
var type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort", false);
var port = Activator.CreateInstance(type) as INetFwOpenPort; port.Port = portNumber;
port.Name = "NzbDrone";
port.Port = portNumber; port.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
port.Name = "NzbDrone"; port.Enabled = true;
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 netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; ports.Add (port);
return true;
ports.Add(port); } catch (Exception ex) {
return true; Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
} return false;
catch(Exception ex) }
{ #endif
Logger.WarnException("Failed to open port in firewall for NzbDrone " + portNumber, ex); }
return false;
} private int CloseFirewallPort ()
} {
private int CloseFirewallPort() #if __MonoCS__
{ #else
try
{ try {
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var portNumber = 8989; var portNumber = 8989;
foreach (INetFwOpenPort p in ports) foreach (INetFwOpenPort p in ports) {
{ if (p.Name == "NzbDrone") {
if (p.Name == "NzbDrone") portNumber = p.Port;
{ break;
portNumber = p.Port; }
break; }
}
} if (portNumber != _configFileProvider.Port) {
ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
if (portNumber != _configFileProvider.Port) return portNumber;
{ }
ports.Remove(portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); } catch (Exception ex) {
return portNumber; Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
} }
} #endif
catch(Exception ex) return 0;
{ }
Logger.WarnException("Failed to close port in firewall for NzbDrone", ex);
} private bool IsFirewallEnabled ()
{
return 0; #if __MonoCS__
} return true;
#else
private bool IsFirewallEnabled()
{ try {
try var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
{ var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); return mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); } catch (Exception ex) {
return mgr.LocalPolicy.CurrentProfile.FirewallEnabled; Logger.WarnException ("Failed to check if the firewall is enabled", ex);
} return false;
}
catch(Exception ex) #endif
{ }
Logger.WarnException("Failed to check if the firewall is enabled", ex);
return false; private bool RegisterUrl (int portNumber)
} {
} try {
var startInfo = new ProcessStartInfo ()
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) }
{
Logger.WarnException("Error registering URL", ex);
}
return false; return false;
} }
private bool UnregisterUrl(int portNumber) private bool UnregisterUrl (int portNumber)
{ {
try try {
{ var startInfo = new ProcessStartInfo ()
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) }
{
Logger.WarnException("Error registering URL", ex);
}
return false; return false;
} }
} }
} }

@ -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()

Loading…
Cancel
Save