SystemService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Controller.IO;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Net;
  15. using MediaBrowser.Model.Services;
  16. using System.Threading;
  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. 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. /// <summary>
  70. /// Class SystemInfoService
  71. /// </summary>
  72. public class SystemService : BaseApiService
  73. {
  74. /// <summary>
  75. /// The _app host
  76. /// </summary>
  77. private readonly IServerApplicationHost _appHost;
  78. private readonly IApplicationPaths _appPaths;
  79. private readonly IFileSystem _fileSystem;
  80. private readonly INetworkManager _network;
  81. private readonly ISecurityManager _security;
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="SystemService" /> class.
  84. /// </summary>
  85. /// <param name="appHost">The app host.</param>
  86. /// <param name="appPaths">The application paths.</param>
  87. /// <param name="fileSystem">The file system.</param>
  88. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  89. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
  90. {
  91. _appHost = appHost;
  92. _appPaths = appPaths;
  93. _fileSystem = fileSystem;
  94. _network = network;
  95. _security = security;
  96. }
  97. public object Post(PingSystem request)
  98. {
  99. return _appHost.Name;
  100. }
  101. public object Get(GetServerLogs request)
  102. {
  103. IEnumerable<FileSystemMetadata> files;
  104. try
  105. {
  106. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt" }, true, false);
  107. }
  108. catch (IOException)
  109. {
  110. files = new FileSystemMetadata[] { };
  111. }
  112. var result = files.Select(i => new LogFile
  113. {
  114. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  115. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  116. Name = i.Name,
  117. Size = i.Length
  118. }).OrderByDescending(i => i.DateModified)
  119. .ThenByDescending(i => i.DateCreated)
  120. .ThenBy(i => i.Name)
  121. .ToArray();
  122. return ToOptimizedResult(result);
  123. }
  124. public Task<object> Get(GetLogFile request)
  125. {
  126. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  127. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  128. // For older files, assume fully static
  129. if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
  130. {
  131. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read);
  132. }
  133. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
  134. }
  135. /// <summary>
  136. /// Gets the specified request.
  137. /// </summary>
  138. /// <param name="request">The request.</param>
  139. /// <returns>System.Object.</returns>
  140. public async Task<object> Get(GetSystemInfo request)
  141. {
  142. var result = await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
  143. return ToOptimizedResult(result);
  144. }
  145. public async Task<object> Get(GetPublicSystemInfo request)
  146. {
  147. var result = await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
  148. return ToOptimizedResult(result);
  149. }
  150. /// <summary>
  151. /// Posts the specified request.
  152. /// </summary>
  153. /// <param name="request">The request.</param>
  154. public void Post(RestartApplication request)
  155. {
  156. _appHost.Restart();
  157. }
  158. /// <summary>
  159. /// Posts the specified request.
  160. /// </summary>
  161. /// <param name="request">The request.</param>
  162. public void Post(ShutdownApplication request)
  163. {
  164. Task.Run(async () =>
  165. {
  166. await Task.Delay(100).ConfigureAwait(false);
  167. await _appHost.Shutdown().ConfigureAwait(false);
  168. });
  169. }
  170. public object Get(GetEndpointInfo request)
  171. {
  172. return ToOptimizedResult(new EndPointInfo
  173. {
  174. IsLocal = Request.IsLocal,
  175. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  176. });
  177. }
  178. }
  179. }