1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Run Nancy SelfHost locally only

Will run locally when URL hasn't been registered and not running in administrator context.
This commit is contained in:
Mark McDowall 2013-03-25 21:03:16 -07:00
parent 6ec700c632
commit 29ec800996
2 changed files with 107 additions and 68 deletions

View File

@ -18,6 +18,7 @@ public interface IHostController
public class HostController : IHostController
{
private readonly ConfigFileProvider _configFileProvider;
private readonly SecurityProvider _securityProvider;
private readonly INancyBootstrapper _bootstrapper;
private readonly Logger _logger;
private NancyHost _host;
@ -25,18 +26,22 @@ public class HostController : IHostController
public bool ServerStarted { get; private set; }
public HostController(ConfigFileProvider configFileProvider, INancyBootstrapper bootstrapper, Logger logger)
public HostController(ConfigFileProvider configFileProvider, SecurityProvider securityProvider, INancyBootstrapper bootstrapper, Logger logger)
{
_configFileProvider = configFileProvider;
_securityProvider = securityProvider;
_bootstrapper = bootstrapper;
_logger = logger;
}
public void StartServer()
{
//Todo: We need this to be able run when the user isn't an admin
//Todo: And when the URL hasn't been registered in URL ACL: netsh http add urlacl url=http://+:8989/ user=everyone
if (_securityProvider.IsNzbDroneUrlRegistered())
_host = new NancyHost(new Uri(AppUrl), _bootstrapper);
else
_host = new NancyHost(new Uri(AppUrl), _bootstrapper, new HostConfiguration { RewriteLocalhost = false });
_host.Start();
}
@ -45,7 +50,6 @@ public string AppUrl
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
}
public void RestartServer()
{

View File

@ -10,38 +10,45 @@
namespace NzbDrone.Common
{
public class SecurityProvider
public interface ISecurityProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger ();
void MakeAccessible();
bool IsCurrentUserAdmin();
bool IsNzbDronePortOpen();
bool IsNzbDroneUrlRegistered();
}
public class SecurityProvider : ISecurityProvider
{
private readonly ConfigFileProvider _configFileProvider;
private readonly EnvironmentProvider _environmentProvider;
private readonly ProcessProvider _processProvider;
private readonly Logger _logger;
public SecurityProvider(ConfigFileProvider configFileProvider, EnvironmentProvider environmentProvider,
ProcessProvider processProvider)
ProcessProvider processProvider, Logger logger)
{
_configFileProvider = configFileProvider;
_environmentProvider = environmentProvider;
_processProvider = processProvider;
_logger = logger;
}
public SecurityProvider ()
public void MakeAccessible()
{
}
public virtual void MakeAccessible ()
if (!IsCurrentUserAdmin ())
{
if (!IsCurrentUserAdmin ()) {
Logger.Trace ("User is not an admin, skipping.");
_logger.Trace ("User is not an admin, skipping.");
return;
}
int port = 0;
if (IsFirewallEnabled ()) {
if (IsNzbDronePortOpen ()) {
Logger.Trace ("NzbDrone port is already open, skipping.");
if (IsFirewallEnabled ())
{
if (IsNzbDronePortOpen ())
{
_logger.Trace ("NzbDrone port is already open, skipping.");
return;
}
@ -64,18 +71,18 @@ public virtual void MakeAccessible ()
RegisterUrl(_configFileProvider.Port);
}
public virtual bool IsCurrentUserAdmin ()
public bool IsCurrentUserAdmin()
{
try {
var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
return principal.IsInRole (WindowsBuiltInRole.Administrator);
} catch (Exception ex) {
Logger.WarnException ("Error checking if the current user is an administrator.", ex);
_logger.WarnException ("Error checking if the current user is an administrator.", ex);
return false;
}
}
public virtual bool IsNzbDronePortOpen ()
public bool IsNzbDronePortOpen()
{
#if __MonoCS__
#else
@ -83,7 +90,6 @@ public virtual bool IsNzbDronePortOpen ()
try {
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
@ -95,14 +101,20 @@ public virtual bool IsNzbDronePortOpen ()
if (p.Port == _configFileProvider.Port)
return true;
}
} catch (Exception ex) {
Logger.WarnException ("Failed to check for open port in firewall", ex);
}
catch (Exception ex) {
_logger.WarnException ("Failed to check for open port in firewall", ex);
}
#endif
return false;
}
private bool OpenFirewallPort (int portNumber)
public bool IsNzbDroneUrlRegistered()
{
return CheckIfUrlIsRegisteredUrl(_configFileProvider.Port);
}
private void OpenFirewallPort(int portNumber)
{
#if __MonoCS__
return true;
@ -121,10 +133,9 @@ private bool OpenFirewallPort (int portNumber)
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
ports.Add (port);
return true;
} catch (Exception ex) {
Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
return false;
}
catch (Exception ex) {
_logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
}
#endif
}
@ -149,12 +160,15 @@ private int CloseFirewallPort ()
}
}
if (portNumber != _configFileProvider.Port) {
if (portNumber != _configFileProvider.Port)
{
ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
return portNumber;
}
} catch (Exception ex) {
Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
}
catch (Exception ex)
{
_logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
}
#endif
return 0;
@ -170,49 +184,70 @@ private bool IsFirewallEnabled ()
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
return mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
} catch (Exception ex) {
Logger.WarnException ("Failed to check if the firewall is enabled", ex);
}
catch (Exception ex)
{
_logger.WarnException ("Failed to check if the firewall is enabled", ex);
return false;
}
#endif
}
private bool RegisterUrl (int portNumber)
private void RegisterUrl(int portNumber)
{
var arguments = String.Format("http add urlacl http://+:{0}/ user=EVERYONE", portNumber);
RunNetsh(arguments);
}
private void UnregisterUrl(int portNumber)
{
var arguments = String.Format("http delete urlacl http://+:{0}/", portNumber);
RunNetsh(arguments);
}
private bool CheckIfUrlIsRegisteredUrl(int portNumber)
{
var url = String.Format("http://+:{0}/", portNumber);
var arguments = String.Format("http show urlacl url=\"{0}\"", url);
var output = RunNetsh(arguments);
if(String.IsNullOrWhiteSpace(output))
{
_logger.Error("netsh output is invalid for arguments: {0}", arguments);
}
if(!output.Contains(url))
{
_logger.Trace("Url has not already been registered");
return false;
}
_logger.Trace("Url has already been registered!");
return true;
}
private string RunNetsh(string arguments)
{
try
{
try {
var startInfo = new ProcessStartInfo()
{
RedirectStandardOutput = true,
UseShellExecute = false,
FileName = "netsh.exe",
Arguments = string.Format("http add urlacl http://*:{0}/ user=EVERYONE", portNumber)
Arguments = arguments
};
var process = _processProvider.Start(startInfo);
process.WaitForExit(5000);
return true;
} catch (Exception ex) {
Logger.WarnException ("Error registering URL", ex);
return process.StandardOutput.ReadToEnd();
}
return false;
}
private bool UnregisterUrl (int portNumber)
catch (Exception ex)
{
try {
var startInfo = new ProcessStartInfo ()
{
FileName = "netsh.exe",
Arguments = string.Format("http delete urlacl http://*:{0}/", portNumber)
};
var process = _processProvider.Start (startInfo);
process.WaitForExit (5000);
return true;
} catch (Exception ex) {
Logger.WarnException ("Error registering URL", ex);
_logger.WarnException("Error executing netsh with arguments: " + arguments, ex);
}
return false;
return null;
}
}
}