SystemService.cs 7.0 KB

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