diff --git a/NzbDrone.Console/ConsoleApp.cs b/NzbDrone.Console/ConsoleApp.cs index 586027a54..726139f8a 100644 --- a/NzbDrone.Console/ConsoleApp.cs +++ b/NzbDrone.Console/ConsoleApp.cs @@ -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(); } diff --git a/NzbDrone.Host/AccessControl/FirewallAdapter.cs b/NzbDrone.Host/AccessControl/FirewallAdapter.cs index 96fb87679..72d194446 100644 --- a/NzbDrone.Host/AccessControl/FirewallAdapter.cs +++ b/NzbDrone.Host/AccessControl/FirewallAdapter.cs @@ -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; } } - - } } diff --git a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs index c7e1480d6..e536fffbd 100644 --- a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs +++ b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs @@ -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); } diff --git a/NzbDrone.Host/ApplicationServer.cs b/NzbDrone.Host/ApplicationServer.cs index b1abc00c5..d6e1e3625 100644 --- a/NzbDrone.Host/ApplicationServer.cs +++ b/NzbDrone.Host/ApplicationServer.cs @@ -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) && diff --git a/NzbDrone.Host/Owin/IHostController.cs b/NzbDrone.Host/Owin/IHostController.cs index 9df77feb4..026bff89e 100644 --- a/NzbDrone.Host/Owin/IHostController.cs +++ b/NzbDrone.Host/Owin/IHostController.cs @@ -4,7 +4,6 @@ { string AppUrl { get; } void StartServer(); - void RestartServer(); void StopServer(); } } \ No newline at end of file diff --git a/NzbDrone.Host/Owin/OwinHostController.cs b/NzbDrone.Host/Owin/OwinHostController.cs index a571d5530..9968b22bb 100644 --- a/NzbDrone.Host/Owin/OwinHostController.cs +++ b/NzbDrone.Host/Owin/OwinHostController.cs @@ -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 _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 owinMiddleWares, Logger logger) + public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable 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; diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index b599ed47b..5d45930cd 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -83,6 +83,10 @@ False ..\packages\Newtonsoft.Json.5.0.6\lib\net40\Newtonsoft.Json.dll + + False + ..\packages\NLog.2.0.1.2\lib\net40\NLog.dll + False ..\packages\Owin.1.0\lib\net40\Owin.dll diff --git a/NzbDrone/WindowsApp.cs b/NzbDrone/WindowsApp.cs index 0ded14d61..e144b82cc 100644 --- a/NzbDrone/WindowsApp.cs +++ b/NzbDrone/WindowsApp.cs @@ -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!"); } diff --git a/NzbDrone/packages.config b/NzbDrone/packages.config index 907d39c5a..19f3a3d8b 100644 --- a/NzbDrone/packages.config +++ b/NzbDrone/packages.config @@ -5,5 +5,6 @@ + \ No newline at end of file