1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

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

This commit is contained in:
Mark McDowall 2013-09-20 16:56:17 -07:00
parent 5343bde070
commit f826890d2b
19 changed files with 100 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,9 @@
using System;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Host.AccessControl
@ -9,11 +11,15 @@ namespace NzbDrone.Host.AccessControl
public interface IUrlAclAdapter
{
void RefreshRegistration();
bool IsRegistered();
string UrlAcl { get; }
string LocalUrlAcl { get; }
}
public class UrlAclAdapter : IUrlAclAdapter
{
private const string URL_ACL = "http://{0}:{1}/";
private readonly IProcessProvider _processProvider;
private readonly IConfigFileProvider _configFileProvider;
private readonly Logger _logger;
@ -25,11 +31,29 @@ public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider confi
_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
{
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 @@ private void RegisterUrl()
RunNetsh(arguments);
}
private void RunNetsh(string arguments)
private ProcessOutput RunNetsh(string arguments)
{
try
{
var process = _processProvider.Start("netsh.exe", arguments);
process.WaitForExit(5000);
var output = _processProvider.StartAndCapture("netsh.exe", arguments);
return output;
}
catch (Exception ex)
{
_logger.WarnException("Error executing netsh with arguments: " + arguments, ex);
}
return null;
}
}
}

View File

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

View File

@ -38,19 +38,14 @@ public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IO
public void StartServer()
{
IgnoreCertErrorPolicy.Register();
var urlAcl = DetermineUrlAcl();
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
var options = new StartOptions(_urlAclAdapter.UrlAcl)
var options = new StartOptions(urlAcl)
{
ServerFactory = "Microsoft.Owin.Host.HttpListener"
};
_logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl);
_logger.Info("starting server on {0}", urlAcl);
try
{
@ -100,5 +95,26 @@ public void StopServer()
_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;
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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