2013-05-23 05:12:01 +00:00
|
|
|
using System;
|
2013-05-20 02:35:48 +00:00
|
|
|
using NetFwTypeLib;
|
2013-08-07 05:32:22 +00:00
|
|
|
using NLog;
|
2013-08-30 22:55:01 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-23 05:12:01 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-20 02:35:48 +00:00
|
|
|
|
2013-08-10 20:30:27 +00:00
|
|
|
namespace NzbDrone.Host.AccessControl
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
|
|
|
public interface IFirewallAdapter
|
|
|
|
{
|
|
|
|
void MakeAccessible();
|
|
|
|
}
|
|
|
|
|
|
|
|
public class FirewallAdapter : IFirewallAdapter
|
|
|
|
{
|
2013-12-09 04:59:04 +00:00
|
|
|
private const NET_FW_PROFILE_TYPE_ FIREWALL_PROFILE = NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD;
|
|
|
|
|
2013-05-20 02:35:48 +00:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
private readonly Logger _logger;
|
2013-12-09 04:59:04 +00:00
|
|
|
|
2013-05-20 02:35:48 +00:00
|
|
|
public FirewallAdapter(IConfigFileProvider configFileProvider, Logger logger)
|
|
|
|
{
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MakeAccessible()
|
|
|
|
{
|
|
|
|
if (IsFirewallEnabled())
|
|
|
|
{
|
2013-09-26 21:57:36 +00:00
|
|
|
if (!IsNzbDronePortOpen(_configFileProvider.Port))
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
2013-09-26 21:57:36 +00:00
|
|
|
_logger.Trace("Opening Port for NzbDrone: {0}", _configFileProvider.Port);
|
|
|
|
OpenFirewallPort(_configFileProvider.Port);
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:57:36 +00:00
|
|
|
if (_configFileProvider.EnableSsl && !IsNzbDronePortOpen(_configFileProvider.SslPort))
|
|
|
|
{
|
|
|
|
_logger.Trace("Opening SSL Port for NzbDrone: {0}", _configFileProvider.SslPort);
|
|
|
|
OpenFirewallPort(_configFileProvider.SslPort);
|
|
|
|
}
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:57:36 +00:00
|
|
|
private bool IsNzbDronePortOpen(int port)
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
|
|
|
|
|
|
|
|
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
|
2013-12-09 04:59:04 +00:00
|
|
|
var ports = mgr.LocalPolicy.GetProfileByType(FIREWALL_PROFILE).GloballyOpenPorts;
|
2013-05-20 02:35:48 +00:00
|
|
|
|
|
|
|
foreach (INetFwOpenPort p in ports)
|
|
|
|
{
|
2013-09-26 21:57:36 +00:00
|
|
|
if (p.Port == port)
|
2013-05-20 02:35:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Failed to check for open port in firewall", ex);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OpenFirewallPort(int portNumber)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort", false);
|
2013-08-10 20:30:27 +00:00
|
|
|
var port = (INetFwOpenPort)Activator.CreateInstance(type);
|
2013-05-20 02:35:48 +00:00
|
|
|
|
|
|
|
port.Port = portNumber;
|
|
|
|
port.Name = "NzbDrone";
|
|
|
|
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);
|
|
|
|
|
2013-12-09 04:59:04 +00:00
|
|
|
//Open the port for the standard profile, should help when the user has multiple network adapters
|
|
|
|
mgr.LocalPolicy.GetProfileByType(FIREWALL_PROFILE).GloballyOpenPorts.Add(port);
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Failed to open port in firewall for NzbDrone " + portNumber, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsFirewallEnabled()
|
|
|
|
{
|
2014-03-09 05:28:40 +00:00
|
|
|
if (OsInfo.IsMono) return false;
|
2013-08-30 22:55:01 +00:00
|
|
|
|
2013-05-20 02:35:48 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
|
|
|
|
var mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
|
2013-12-09 04:59:04 +00:00
|
|
|
return mgr.LocalPolicy.GetProfileByType(FIREWALL_PROFILE).FirewallEnabled;
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Failed to check if the firewall is enabled", ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|