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

Moved UrlAcl registration to adapter

This commit is contained in:
Mark McDowall 2013-09-20 19:50:57 -07:00
parent f826890d2b
commit 32b287387d
2 changed files with 50 additions and 51 deletions

View File

@ -10,10 +10,8 @@ namespace NzbDrone.Host.AccessControl
{
public interface IUrlAclAdapter
{
void RefreshRegistration();
bool IsRegistered();
void ConfigureUrl();
string UrlAcl { get; }
string LocalUrlAcl { get; }
}
public class UrlAclAdapter : IUrlAclAdapter
@ -22,42 +20,43 @@ public class UrlAclAdapter : IUrlAclAdapter
private readonly IProcessProvider _processProvider;
private readonly IConfigFileProvider _configFileProvider;
private readonly IRuntimeInfo _runtimeInfo;
private readonly Logger _logger;
public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider configFileProvider, Logger logger)
public string UrlAcl { get; private set; }
private string _localUrl;
private string _wildcardUrl;
public UrlAclAdapter(IProcessProvider processProvider,
IConfigFileProvider configFileProvider,
IRuntimeInfo runtimeInfo,
Logger logger)
{
_processProvider = processProvider;
_configFileProvider = configFileProvider;
_runtimeInfo = runtimeInfo;
_logger = logger;
_localUrl = String.Format(URL_ACL, "localhost", _configFileProvider.Port);
_wildcardUrl = String.Format(URL_ACL, "*", _configFileProvider.Port);
UrlAcl = _wildcardUrl;
}
public bool IsRegistered()
public void ConfigureUrl()
{
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
if (!_runtimeInfo.IsAdmin && !IsRegistered)
{
return String.Format(URL_ACL, "*", _configFileProvider.Port);
UrlAcl = _localUrl;
}
if (_runtimeInfo.IsAdmin)
{
RefreshRegistration();
}
}
public string LocalUrlAcl
{
get
{
return String.Format(URL_ACL, "localhost", _configFileProvider.Port);
}
}
public void RefreshRegistration()
private void RefreshRegistration()
{
if (OsInfo.Version.Major < 6)
return;
@ -65,6 +64,19 @@ public void RefreshRegistration()
RegisterUrl();
}
private bool IsRegistered
{
get
{
var arguments = String.Format("http show urlacl {0}", _wildcardUrl);
var output = RunNetsh(arguments);
if (output == null || !output.Standard.Any()) return false;
return output.Standard.Any(line => line.Contains(_wildcardUrl));
}
}
private void RegisterUrl()
{
var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl);

View File

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