|
|
|
@ -3,7 +3,10 @@ using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using NLog;
|
|
|
|
|
#if __MonoCS__
|
|
|
|
|
#else
|
|
|
|
|
using NetFwTypeLib;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
|
{
|
|
|
|
@ -29,18 +32,15 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
public virtual void MakeAccessible ()
|
|
|
|
|
{
|
|
|
|
|
if (!IsCurrentUserAdmin())
|
|
|
|
|
{
|
|
|
|
|
if (!IsCurrentUserAdmin ()) {
|
|
|
|
|
Logger.Trace ("User is not an admin, skipping.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
|
|
if (IsFirewallEnabled())
|
|
|
|
|
{
|
|
|
|
|
if(IsNzbDronePortOpen())
|
|
|
|
|
{
|
|
|
|
|
if (IsFirewallEnabled ()) {
|
|
|
|
|
if (IsNzbDronePortOpen ()) {
|
|
|
|
|
Logger.Trace ("NzbDrone port is already open, skipping.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -66,13 +66,10 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
public virtual bool IsCurrentUserAdmin ()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
|
|
|
|
|
return principal.IsInRole (WindowsBuiltInRole.Administrator);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Error checking if the current user is an administrator.", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -80,9 +77,13 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
public virtual bool IsNzbDronePortOpen ()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#if __MonoCS__
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
|
|
|
|
|
|
|
|
|
|
if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
|
|
|
|
@ -90,23 +91,23 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
|
|
|
|
|
|
|
|
|
|
foreach (INetFwOpenPort p in ports)
|
|
|
|
|
{
|
|
|
|
|
foreach (INetFwOpenPort p in ports) {
|
|
|
|
|
if (p.Port == _configFileProvider.Port)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Failed to check for open port in firewall", ex);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool OpenFirewallPort (int portNumber)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#if __MonoCS__
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
|
|
|
|
try {
|
|
|
|
|
var type = Type.GetTypeFromProgID ("HNetCfg.FWOpenPort", false);
|
|
|
|
|
var port = Activator.CreateInstance (type) as INetFwOpenPort;
|
|
|
|
|
|
|
|
|
@ -121,67 +122,64 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
ports.Add (port);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int CloseFirewallPort ()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if __MonoCS__
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
{
|
|
|
|
|
foreach (INetFwOpenPort p in ports) {
|
|
|
|
|
if (p.Name == "NzbDrone") {
|
|
|
|
|
portNumber = p.Port;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (portNumber != _configFileProvider.Port)
|
|
|
|
|
{
|
|
|
|
|
if (portNumber != _configFileProvider.Port) {
|
|
|
|
|
ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
|
|
|
|
|
return portNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsFirewallEnabled ()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#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)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Failed to check if the firewall is enabled", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool RegisterUrl (int portNumber)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
var startInfo = new ProcessStartInfo ()
|
|
|
|
|
{
|
|
|
|
|
FileName = "netsh.exe",
|
|
|
|
@ -191,10 +189,7 @@ namespace NzbDrone.Common
|
|
|
|
|
var process = _processProvider.Start (startInfo);
|
|
|
|
|
process.WaitForExit (5000);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Error registering URL", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -203,8 +198,7 @@ namespace NzbDrone.Common
|
|
|
|
|
|
|
|
|
|
private bool UnregisterUrl (int portNumber)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
var startInfo = new ProcessStartInfo ()
|
|
|
|
|
{
|
|
|
|
|
FileName = "netsh.exe",
|
|
|
|
@ -214,10 +208,7 @@ namespace NzbDrone.Common
|
|
|
|
|
var process = _processProvider.Start (startInfo);
|
|
|
|
|
process.WaitForExit (5000);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.WarnException ("Error registering URL", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|