Check if URL is registered when running in non-admin and run accordingly

pull/2/head
Mark McDowall 11 years ago
parent 5343bde070
commit f826890d2b

@ -4,6 +4,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Model; using NzbDrone.Common.Model;
using NzbDrone.Common.Processes;
using NzbDrone.Host; using NzbDrone.Host;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;

@ -6,6 +6,7 @@ using System.Linq;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Model; using NzbDrone.Common.Model;
using NzbDrone.Common.Processes;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Test.Dummy; using NzbDrone.Test.Dummy;

@ -98,6 +98,7 @@
<Compile Include="Messaging\IEvent.cs" /> <Compile Include="Messaging\IEvent.cs" />
<Compile Include="Messaging\IMessage.cs" /> <Compile Include="Messaging\IMessage.cs" />
<Compile Include="PathEqualityComparer.cs" /> <Compile Include="PathEqualityComparer.cs" />
<Compile Include="Processes\ProcessOutput.cs" />
<Compile Include="Serializer\IntConverter.cs" /> <Compile Include="Serializer\IntConverter.cs" />
<Compile Include="Services.cs" /> <Compile Include="Services.cs" />
<Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" /> <Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" />
@ -122,7 +123,7 @@
<Compile Include="DiskProvider.cs" /> <Compile Include="DiskProvider.cs" />
<Compile Include="EnvironmentInfo\AppFolderInfo.cs" /> <Compile Include="EnvironmentInfo\AppFolderInfo.cs" />
<Compile Include="Model\ProcessInfo.cs" /> <Compile Include="Model\ProcessInfo.cs" />
<Compile Include="ProcessProvider.cs" /> <Compile Include="Processes\ProcessProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\SharedAssemblyInfo.cs" /> <Compile Include="Properties\SharedAssemblyInfo.cs" />
<Compile Include="ServiceProvider.cs" /> <Compile Include="ServiceProvider.cs" />

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Common.Processes
{
public class ProcessOutput
{
public List<String> Standard { get; set; }
public List<String> Error { get; set; }
public ProcessOutput()
{
Standard = new List<string>();
Error = new List<string>();
}
}
}

@ -9,7 +9,7 @@ using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Model; using NzbDrone.Common.Model;
namespace NzbDrone.Common namespace NzbDrone.Common.Processes
{ {
public interface IProcessProvider public interface IProcessProvider
{ {
@ -23,6 +23,7 @@ namespace NzbDrone.Common
ProcessPriorityClass GetCurrentProcessPriority(); ProcessPriorityClass GetCurrentProcessPriority();
Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null); Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null);
Process SpawnNewProcess(string path, string args = null); Process SpawnNewProcess(string path, string args = null);
ProcessOutput StartAndCapture(string path, string args = null);
} }
public class ProcessProvider : IProcessProvider public class ProcessProvider : IProcessProvider
@ -88,11 +89,8 @@ namespace NzbDrone.Common
process.Start(); process.Start();
} }
public Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null) public Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null)
{ {
if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)) if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
{ {
args = path + " " + args; args = path + " " + args;
@ -147,7 +145,6 @@ namespace NzbDrone.Common
process.BeginErrorReadLine(); process.BeginErrorReadLine();
process.BeginOutputReadLine(); process.BeginOutputReadLine();
return process; return process;
} }
@ -172,6 +169,16 @@ namespace NzbDrone.Common
return process; return process;
} }
public ProcessOutput StartAndCapture(string path, string args = null)
{
var output = new ProcessOutput();
var process = Start(path, args, s => output.Standard.Add(s), error => output.Error.Add(error));
WaitForExit(process);
return output;
}
public void WaitForExit(Process process) public void WaitForExit(Process process)
{ {
Logger.Trace("Waiting for process {0} to exit.", process.ProcessName); Logger.Trace("Waiting for process {0} to exit.", process.ProcessName);
@ -225,7 +232,6 @@ namespace NzbDrone.Common
return null; return null;
} }
private static string GetExeFileName(Process process) private static string GetExeFileName(Process process)
{ {
if (process.MainModule.FileName != "mono.exe") if (process.MainModule.FileName != "mono.exe")

@ -6,6 +6,7 @@ using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Model; using NzbDrone.Common.Model;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Update; using NzbDrone.Core.Update;
using NzbDrone.Core.Update.Commands; using NzbDrone.Core.Update.Commands;

@ -3,6 +3,7 @@ using System.IO;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Update.Commands; using NzbDrone.Core.Update.Commands;
using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Instrumentation;

@ -1,7 +1,9 @@
using System; using System;
using System.Linq;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
namespace NzbDrone.Host.AccessControl namespace NzbDrone.Host.AccessControl
@ -9,11 +11,15 @@ namespace NzbDrone.Host.AccessControl
public interface IUrlAclAdapter public interface IUrlAclAdapter
{ {
void RefreshRegistration(); void RefreshRegistration();
bool IsRegistered();
string UrlAcl { get; } string UrlAcl { get; }
string LocalUrlAcl { get; }
} }
public class UrlAclAdapter : IUrlAclAdapter public class UrlAclAdapter : IUrlAclAdapter
{ {
private const string URL_ACL = "http://{0}:{1}/";
private readonly IProcessProvider _processProvider; private readonly IProcessProvider _processProvider;
private readonly IConfigFileProvider _configFileProvider; private readonly IConfigFileProvider _configFileProvider;
private readonly Logger _logger; private readonly Logger _logger;
@ -25,11 +31,29 @@ namespace NzbDrone.Host.AccessControl
_logger = logger; _logger = logger;
} }
public bool IsRegistered()
{
var arguments = String.Format("http show urlacl {0}", UrlAcl);
var output = RunNetsh(arguments);
if (output == null || !output.Standard.Any()) return false;
return output.Standard.Any(line => line.Contains(UrlAcl));
}
public string UrlAcl public string UrlAcl
{ {
get get
{ {
return "http://*:" + _configFileProvider.Port + "/"; return String.Format(URL_ACL, "*", _configFileProvider.Port);
}
}
public string LocalUrlAcl
{
get
{
return String.Format(URL_ACL, "localhost", _configFileProvider.Port);
} }
} }
@ -47,17 +71,20 @@ namespace NzbDrone.Host.AccessControl
RunNetsh(arguments); RunNetsh(arguments);
} }
private void RunNetsh(string arguments) private ProcessOutput RunNetsh(string arguments)
{ {
try try
{ {
var process = _processProvider.Start("netsh.exe", arguments); var output = _processProvider.StartAndCapture("netsh.exe", arguments);
process.WaitForExit(5000);
return output;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.WarnException("Error executing netsh with arguments: " + arguments, ex); _logger.WarnException("Error executing netsh with arguments: " + arguments, ex);
} }
return null;
} }
} }
} }

@ -3,6 +3,7 @@ using System.ServiceProcess;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Host.Owin; using NzbDrone.Host.Owin;

@ -38,19 +38,14 @@ namespace NzbDrone.Host.Owin
public void StartServer() public void StartServer()
{ {
IgnoreCertErrorPolicy.Register(); IgnoreCertErrorPolicy.Register();
var urlAcl = DetermineUrlAcl();
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin) var options = new StartOptions(urlAcl)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
var options = new StartOptions(_urlAclAdapter.UrlAcl)
{ {
ServerFactory = "Microsoft.Owin.Host.HttpListener" ServerFactory = "Microsoft.Owin.Host.HttpListener"
}; };
_logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl); _logger.Info("starting server on {0}", urlAcl);
try try
{ {
@ -100,5 +95,26 @@ namespace NzbDrone.Host.Owin
_logger.Info("Host has stopped"); _logger.Info("Host has stopped");
} }
private string DetermineUrlAcl()
{
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
{
if (_runtimeInfo.IsAdmin)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
else
{
if (!_urlAclAdapter.IsRegistered())
{
return _urlAclAdapter.LocalUrlAcl;
}
}
}
return _urlAclAdapter.UrlAcl;
}
} }
} }

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.Threading; using System.Threading;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Processes;
namespace NzbDrone.Host namespace NzbDrone.Host
{ {

@ -5,6 +5,7 @@ using System.Threading;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using RestSharp; using RestSharp;
namespace NzbDrone.Integration.Test namespace NzbDrone.Integration.Test

@ -3,6 +3,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Model; using NzbDrone.Common.Model;
using NzbDrone.Common.Processes;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Update.UpdateEngine; using NzbDrone.Update.UpdateEngine;

@ -3,6 +3,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Update.UpdateEngine; using NzbDrone.Update.UpdateEngine;
using IServiceProvider = NzbDrone.Common.IServiceProvider; using IServiceProvider = NzbDrone.Common.IServiceProvider;

@ -5,6 +5,7 @@ using NzbDrone.Common;
using NzbDrone.Common.Composition; using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Processes;
using NzbDrone.Common.Security; using NzbDrone.Common.Security;
using NzbDrone.Update.UpdateEngine; using NzbDrone.Update.UpdateEngine;

@ -1,4 +1,5 @@
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Processes;
namespace NzbDrone.Update.UpdateEngine namespace NzbDrone.Update.UpdateEngine
{ {

@ -3,6 +3,7 @@ using System.IO;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using IServiceProvider = NzbDrone.Common.IServiceProvider; using IServiceProvider = NzbDrone.Common.IServiceProvider;
namespace NzbDrone.Update.UpdateEngine namespace NzbDrone.Update.UpdateEngine

@ -1,6 +1,7 @@
using System; using System;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Processes;
using IServiceProvider = NzbDrone.Common.IServiceProvider; using IServiceProvider = NzbDrone.Common.IServiceProvider;
namespace NzbDrone.Update.UpdateEngine namespace NzbDrone.Update.UpdateEngine

@ -3,6 +3,7 @@ using System.ComponentModel;
using System.Windows.Forms; using System.Windows.Forms;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Host.Owin; using NzbDrone.Host.Owin;
namespace NzbDrone.SysTray namespace NzbDrone.SysTray

Loading…
Cancel
Save