SystemService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.System;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Net;
  14. using MediaBrowser.Model.Services;
  15. using System.Threading;
  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. private readonly ISecurityManager _security;
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref="SystemService" /> class.
  90. /// </summary>
  91. /// <param name="appHost">The app host.</param>
  92. /// <param name="appPaths">The application paths.</param>
  93. /// <param name="fileSystem">The file system.</param>
  94. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  95. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
  96. {
  97. _appHost = appHost;
  98. _appPaths = appPaths;
  99. _fileSystem = fileSystem;
  100. _network = network;
  101. _security = security;
  102. }
  103. public object Post(PingSystem request)
  104. {
  105. return _appHost.Name;
  106. }
  107. public object Get(GetWakeOnLanInfo request)
  108. {
  109. var result = _appHost.GetWakeOnLanInfo();
  110. return ToOptimizedResult(result);
  111. }
  112. public object Get(GetServerLogs request)
  113. {
  114. IEnumerable<FileSystemMetadata> files;
  115. try
  116. {
  117. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  118. }
  119. catch (IOException ex)
  120. {
  121. Logger.LogError(ex, "Error getting logs");
  122. files = Enumerable.Empty<FileSystemMetadata>();
  123. }
  124. var result = files.Select(i => new LogFile
  125. {
  126. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  127. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  128. Name = i.Name,
  129. Size = i.Length
  130. }).OrderByDescending(i => i.DateModified)
  131. .ThenByDescending(i => i.DateCreated)
  132. .ThenBy(i => i.Name)
  133. .ToArray();
  134. return ToOptimizedResult(result);
  135. }
  136. public Task<object> Get(GetLogFile request)
  137. {
  138. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  139. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  140. // For older files, assume fully static
  141. if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
  142. {
  143. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read);
  144. }
  145. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
  146. }
  147. /// <summary>
  148. /// Gets the specified request.
  149. /// </summary>
  150. /// <param name="request">The request.</param>
  151. /// <returns>System.Object.</returns>
  152. public async Task<object> Get(GetSystemInfo request)
  153. {
  154. var result = await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
  155. return ToOptimizedResult(result);
  156. }
  157. public async Task<object> Get(GetPublicSystemInfo request)
  158. {
  159. var result = await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
  160. return ToOptimizedResult(result);
  161. }
  162. /// <summary>
  163. /// Posts the specified request.
  164. /// </summary>
  165. /// <param name="request">The request.</param>
  166. public void Post(RestartApplication request)
  167. {
  168. _appHost.Restart();
  169. }
  170. /// <summary>
  171. /// Posts the specified request.
  172. /// </summary>
  173. /// <param name="request">The request.</param>
  174. public void Post(ShutdownApplication request)
  175. {
  176. Task.Run(async () =>
  177. {
  178. await Task.Delay(100).ConfigureAwait(false);
  179. await _appHost.Shutdown().ConfigureAwait(false);
  180. });
  181. }
  182. public object Get(GetEndpointInfo request)
  183. {
  184. return ToOptimizedResult(new EndPointInfo
  185. {
  186. IsLocal = Request.IsLocal,
  187. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  188. });
  189. }
  190. }
  191. }