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;
|
2013-06-28 00:04:52 +00:00
|
|
|
using System.Security.Principal;
|
2021-11-22 21:14:29 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Hosting.WindowsServices;
|
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
|
|
|
|
2021-11-22 21:14:29 +00:00
|
|
|
public RuntimeInfo(IHostLifetime hostLifetime, Logger logger)
|
2013-06-28 00:04:52 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2013-11-26 06:53:36 +00:00
|
|
|
|
2021-11-22 21:14:29 +00:00
|
|
|
IsWindowsService = hostLifetime is WindowsServiceLifetime;
|
2014-02-10 09:49:06 +00:00
|
|
|
|
2021-12-24 17:40:37 +00:00
|
|
|
// net6.0 will return Lidarr.dll for entry assembly, we need the actual
|
2021-02-04 21:22:34 +00:00
|
|
|
// executable name (Lidarr on linux). On mono this will return the location of
|
|
|
|
// the mono executable itself, which is not what we want.
|
|
|
|
var entry = Process.GetCurrentProcess().MainModule;
|
2014-02-10 19:40:13 +00:00
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
{
|
2021-02-04 21:22:34 +00:00
|
|
|
ExecutingApplication = entry.FileName;
|
|
|
|
IsWindowsTray = OsInfo.IsWindows && entry.ModuleName == $"{ProcessProvider.LIDARR_PROCESS_NAME}.exe";
|
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
|
|
|
{
|
2019-08-24 19:43:45 +00:00
|
|
|
var officialBuild = InternalIsOfficialBuild();
|
|
|
|
|
|
|
|
// An build running inside of the testing environment. (Analytics disabled)
|
|
|
|
IsTesting = InternalIsTesting();
|
|
|
|
|
|
|
|
// An official build running outside of the testing environment. (Analytics configurable)
|
|
|
|
IsProduction = !IsTesting && officialBuild;
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-08-24 19:43:45 +00:00
|
|
|
// An unofficial build running outside of the testing environment. (Analytics enabled)
|
|
|
|
IsDevelopment = !IsTesting && !officialBuild && !InternalIsDebug();
|
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
|
|
|
|
{
|
2019-10-28 21:30:08 +00:00
|
|
|
if (OsInfo.IsNotWindows)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-28 00:04:52 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-09-27 02:06:05 +00:00
|
|
|
return IsUserInteractive && Process.GetCurrentProcess().ProcessName.Equals(ProcessProvider.LIDARR_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-08-24 19:43:45 +00:00
|
|
|
public static bool IsTesting { get; }
|
2017-01-04 02:36:47 +00:00
|
|
|
public static bool IsProduction { get; }
|
2019-08-24 19:43:45 +00:00
|
|
|
public static bool IsDevelopment { get; }
|
2013-07-26 06:11:55 +00:00
|
|
|
|
2019-08-24 19:43:45 +00:00
|
|
|
private static bool InternalIsTesting()
|
|
|
|
{
|
2014-08-01 22:10:31 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var lowerProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
if (lowerProcessName.Contains("vshost"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerProcessName.Contains("nunit"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerProcessName.Contains("jetbrain"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerProcessName.Contains("resharper"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-01 22:10:31 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var currentAssemblyLocation = typeof(RuntimeInfo).Assembly.Location;
|
2020-01-03 12:49:24 +00:00
|
|
|
if (currentAssemblyLocation.ToLower().Contains("_output"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentAssemblyLocation.ToLower().Contains("_tests"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-04 02:36:47 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
2015-07-22 02:43:06 +00:00
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
var lowerCurrentDir = Directory.GetCurrentDirectory().ToLower();
|
2020-01-03 12:49:24 +00:00
|
|
|
if (lowerCurrentDir.Contains("vsts"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerCurrentDir.Contains("buildagent"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerCurrentDir.Contains("_output"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowerCurrentDir.Contains("_tests"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-24 19:43:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool InternalIsDebug()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (BuildInfo.IsDebug || Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-24 19:43:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool InternalIsOfficialBuild()
|
|
|
|
{
|
|
|
|
//Official builds will never have such a high revision
|
2020-01-03 12:49:24 +00:00
|
|
|
if (BuildInfo.Version.Major >= 10 || BuildInfo.Version.Revision > 10000)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-26 06:11:55 +00:00
|
|
|
|
|
|
|
return true;
|
2013-06-28 00:04:52 +00:00
|
|
|
}
|
2017-10-25 02:31:37 +00:00
|
|
|
|
|
|
|
public bool IsWindowsTray { get; private set; }
|
2013-06-28 00:04:52 +00:00
|
|
|
}
|
2016-09-20 19:30:46 +00:00
|
|
|
}
|