SystemService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Net;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Net;
  14. using MediaBrowser.Model.Services;
  15. using MediaBrowser.Model.System;
  16. using Microsoft.Extensions.Logging;
  17. namespace MediaBrowser.Api.System
  18. {
  19. /// <summary>
  20. /// Class GetSystemInfo
  21. /// </summary>
  22. [Route("/System/Info", "GET", Summary = "Gets information about the server")]
  23. [Authenticated(EscapeParentalControl = true, AllowBeforeStartupWizard = true)]
  24. public class GetSystemInfo : IReturn<SystemInfo>
  25. {
  26. }
  27. [Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
  28. public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
  29. {
  30. }
  31. [Route("/System/Ping", "POST")]
  32. [Route("/System/Ping", "GET")]
  33. public class PingSystem : IReturnVoid
  34. {
  35. }
  36. /// <summary>
  37. /// Class RestartApplication
  38. /// </summary>
  39. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  40. [Authenticated(Roles = "Admin", AllowLocal = true)]
  41. public class RestartApplication
  42. {
  43. }
  44. /// <summary>
  45. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  46. /// </summary>
  47. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  48. [Authenticated(Roles = "Admin", AllowLocal = true)]
  49. public class ShutdownApplication
  50. {
  51. }
  52. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  53. [Authenticated(Roles = "Admin")]
  54. public class GetServerLogs : IReturn<LogFile[]>
  55. {
  56. }
  57. [Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
  58. [Authenticated]
  59. public class GetEndpointInfo : IReturn<EndPointInfo>
  60. {
  61. public string Endpoint { get; set; }
  62. }
  63. [Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
  64. [Authenticated(Roles = "Admin")]
  65. public class GetLogFile
  66. {
  67. [ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  68. public string Name { get; set; }
  69. }
  70. [Route("/System/WakeOnLanInfo", "GET", Summary = "Gets wake on lan information")]
  71. [Authenticated]
  72. public class GetWakeOnLanInfo : IReturn<WakeOnLanInfo[]>
  73. {
  74. }
  75. /// <summary>
  76. /// Class SystemInfoService
  77. /// </summary>
  78. public class SystemService : BaseApiService
  79. {
  80. /// <summary>
  81. /// The _app host
  82. /// </summary>
  83. private readonly IServerApplicationHost _appHost;
  84. private readonly IApplicationPaths _appPaths;
  85. private readonly IFileSystem _fileSystem;
  86. private readonly INetworkManager _network;
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="SystemService" /> class.
  89. /// </summary>
  90. /// <param name="appHost">The app host.</param>
  91. /// <param name="fileSystem">The file system.</param>
  92. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  93. public SystemService(
  94. ILogger<SystemService> logger,
  95. IServerConfigurationManager serverConfigurationManager,
  96. IHttpResultFactory httpResultFactory,
  97. IServerApplicationHost appHost,
  98. IFileSystem fileSystem,
  99. INetworkManager network)
  100. : base(logger, serverConfigurationManager, httpResultFactory)
  101. {
  102. _appPaths = serverConfigurationManager.ApplicationPaths;
  103. _appHost = appHost;
  104. _fileSystem = fileSystem;
  105. _network = network;
  106. }
  107. public object Post(PingSystem request)
  108. {
  109. return _appHost.Name;
  110. }
  111. public object Get(GetWakeOnLanInfo request)
  112. {
  113. var result = _appHost.GetWakeOnLanInfo();
  114. return ToOptimizedResult(result);
  115. }
  116. public object Get(GetServerLogs request)
  117. {
  118. IEnumerable<FileSystemMetadata> files;
  119. try
  120. {
  121. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  122. }
  123. catch (IOException ex)
  124. {
  125. Logger.LogError(ex, "Error getting logs");
  126. files = Enumerable.Empty<FileSystemMetadata>();
  127. }
  128. var result = files.Select(i => new LogFile
  129. {
  130. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  131. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  132. Name = i.Name,
  133. Size = i.Length
  134. }).OrderByDescending(i => i.DateModified)
  135. .ThenByDescending(i => i.DateCreated)
  136. .ThenBy(i => i.Name)
  137. .ToArray();
  138. return ToOptimizedResult(result);
  139. }
  140. public Task<object> Get(GetLogFile request)
  141. {
  142. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  143. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  144. // For older files, assume fully static
  145. var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
  146. return ResultFactory.GetStaticFileResult(Request, file.FullName, fileShare);
  147. }
  148. /// <summary>
  149. /// Gets the specified request.
  150. /// </summary>
  151. /// <param name="request">The request.</param>
  152. /// <returns>System.Object.</returns>
  153. public async Task<object> Get(GetSystemInfo request)
  154. {
  155. var result = await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
  156. return ToOptimizedResult(result);
  157. }
  158. public async Task<object> Get(GetPublicSystemInfo request)
  159. {
  160. var result = await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
  161. return ToOptimizedResult(result);
  162. }
  163. /// <summary>
  164. /// Posts the specified request.
  165. /// </summary>
  166. /// <param name="request">The request.</param>
  167. public void Post(RestartApplication request)
  168. {
  169. _appHost.Restart();
  170. }
  171. /// <summary>
  172. /// Posts the specified request.
  173. /// </summary>
  174. /// <param name="request">The request.</param>
  175. public void Post(ShutdownApplication request)
  176. {
  177. Task.Run(async () =>
  178. {
  179. await Task.Delay(100).ConfigureAwait(false);
  180. await _appHost.Shutdown().ConfigureAwait(false);
  181. });
  182. }
  183. public object Get(GetEndpointInfo request)
  184. {
  185. return ToOptimizedResult(new EndPointInfo
  186. {
  187. IsLocal = Request.IsLocal,
  188. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  189. });
  190. }
  191. }
  192. }