2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
2013-06-28 00:04:52 +00:00
|
|
|
using System.Diagnostics;
|
2013-07-26 06:11:55 +00:00
|
|
|
using System.IO;
|
2014-02-10 09:49:06 +00:00
|
|
|
using System.Reflection;
|
2013-06-28 00:04:52 +00:00
|
|
|
using System.Security.Principal;
|
2013-11-26 06:53:36 +00:00
|
|
|
using System.ServiceProcess;
|
2013-06-28 00:04:52 +00:00
|
|
|
using NLog;
|
2017-09-04 02:20:56 +00:00
|
|
|
using NzbDrone.Common.Processes;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Common.EnvironmentInfo
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
public class RuntimeInfo : IRuntimeInfo
|
2013-06-28 00:04:52 +00:00
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
2017-09-04 02:20:56 +00:00
|
|
|
private readonly DateTime _startTime = DateTime.UtcNow;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
public RuntimeInfo(IServiceProvider serviceProvider, Logger logger)
|
2013-06-28 00:04:52 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
|
|
|
|
IsWindowsService = !IsUserInteractive &&
|
2014-01-13 01:14:57 +00:00
|
|
|
OsInfo.IsWindows &&
|
|
|
|
serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) &&
|
|
|
|
serviceProvider.GetStatus(ServiceProvider.NZBDRONE_SERVICE_NAME) == ServiceControllerStatus.StartPending;
|
2014-02-10 09:49:06 +00:00
|
|
|
|
2016-09-20 19:30:46 +00:00
|
|
|
//Guarded to avoid issues when running in a non-managed process
|
2014-02-10 19:40:13 +00:00
|
|
|
var entry = Assembly.GetEntryAssembly();
|
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
{
|
|
|
|
ExecutingApplication = entry.Location;
|
2016-09-20 19:30:46 +00:00
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
static RuntimeInfo()
|
2013-06-28 00:04:52 +00:00
|
|
|
{
|
2014-01-13 01:14:57 +00:00
|
|
|
IsProduction = InternalIsProduction();
|
2013-06-28 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
public DateTime StartTime
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _startTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public static bool IsUserInteractive => Environment.UserInteractive;
|
2013-07-26 06:11:55 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
bool IRuntimeInfo.IsUserInteractive => IsUserInteractive;
|
2014-07-30 06:23:43 +00:00
|
|
|
|
2013-06-28 00:04:52 +00:00
|
|
|
public bool IsAdmin
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
|
|
|
|
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Warn(ex, "Error checking if the current user is an administrator.");
|
2013-06-28 00:04:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
public bool IsWindowsService { get; private set; }
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
public bool IsExiting { get; set; }
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
public bool IsTray
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (OsInfo.IsWindows)
|
|
|
|
{
|
|
|
|
return IsUserInteractive && Process.GetCurrentProcess().ProcessName.Equals(ProcessProvider.NZB_DRONE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public RuntimeMode Mode
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (IsWindowsService)
|
|
|
|
{
|
|
|
|
return RuntimeMode.Service;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTray)
|
|
|
|
{
|
|
|
|
return RuntimeMode.Tray;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RuntimeMode.Console;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 05:35:50 +00:00
|
|
|
public bool RestartPending { get; set; }
|
2017-01-04 02:36:47 +00:00
|
|
|
public string ExecutingApplication { get; }
|
2013-06-28 00:04:52 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
public static bool IsProduction { get; }
|
2013-07-26 06:11:55 +00:00
|
|
|
|
|
|
|
private static bool InternalIsProduction()
|
2013-06-28 00:04:52 +00:00
|
|
|
{
|
2013-07-26 06:11:55 +00:00
|
|
|
if (BuildInfo.IsDebug || Debugger.IsAttached) return false;
|
2017-01-04 02:36:47 +00:00
|
|
|
|
|
|
|
//Official builds will never have such a high revision
|
|
|
|
if (BuildInfo.Version.Revision > 10000) return false;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
2014-08-01 22:10:31 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var lowerProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
|
|
|
|
|
|
|
|
if (lowerProcessName.Contains("vshost")) return false;
|
|
|
|
if (lowerProcessName.Contains("nunit")) return false;
|
|
|
|
if (lowerProcessName.Contains("jetbrain")) return false;
|
|
|
|
if (lowerProcessName.Contains("resharper")) return false;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2016-09-20 19:30:46 +00:00
|
|
|
|
2014-08-01 22:10:31 +00:00
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var currentAssemblyLocation = typeof(RuntimeInfo).Assembly.Location;
|
|
|
|
if (currentAssemblyLocation.ToLower().Contains("_output")) return false;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2016-09-20 19:30:46 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
}
|
2015-07-22 02:43:06 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
var lowerCurrentDir = Directory.GetCurrentDirectory().ToLower();
|
2013-09-15 00:23:58 +00:00
|
|
|
if (lowerCurrentDir.Contains("teamcity")) return false;
|
2017-01-04 20:07:02 +00:00
|
|
|
if (lowerCurrentDir.Contains("buildagent")) return false;
|
2015-07-22 02:43:06 +00:00
|
|
|
if (lowerCurrentDir.Contains("_output")) return false;
|
2013-07-26 06:11:55 +00:00
|
|
|
|
|
|
|
return true;
|
2013-06-28 00:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 19:30:46 +00:00
|
|
|
}
|