SystemService.cs 7.1 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.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. [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" }, true, false);
  118. }
  119. catch (IOException)
  120. {
  121. files = new FileSystemMetadata[] { };
  122. }
  123. var result = files.Select(i => new LogFile
  124. {
  125. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  126. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  127. Name = i.Name,
  128. Size = i.Length
  129. }).OrderByDescending(i => i.DateModified)
  130. .ThenByDescending(i => i.DateCreated)
  131. .ThenBy(i => i.Name)
  132. .ToArray();
  133. return ToOptimizedResult(result);
  134. }
  135. public Task<object> Get(GetLogFile request)
  136. {
  137. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  138. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  139. // For older files, assume fully static
  140. if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
  141. {
  142. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read);
  143. }
  144. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
  145. }
  146. /// <summary>
  147. /// Gets the specified request.
  148. /// </summary>
  149. /// <param name="request">The request.</param>
  150. /// <returns>System.Object.</returns>
  151. public async Task<object> Get(GetSystemInfo request)
  152. {
  153. var result = await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
  154. return ToOptimizedResult(result);
  155. }
  156. public async Task<object> Get(GetPublicSystemInfo request)
  157. {
  158. var result = await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
  159. return ToOptimizedResult(result);
  160. }
  161. /// <summary>
  162. /// Posts the specified request.
  163. /// </summary>
  164. /// <param name="request">The request.</param>
  165. public void Post(RestartApplication request)
  166. {
  167. _appHost.Restart();
  168. }
  169. /// <summary>
  170. /// Posts the specified request.
  171. /// </summary>
  172. /// <param name="request">The request.</param>
  173. public void Post(ShutdownApplication request)
  174. {
  175. Task.Run(async () =>
  176. {
  177. await Task.Delay(100).ConfigureAwait(false);
  178. await _appHost.Shutdown().ConfigureAwait(false);
  179. });
  180. }
  181. public object Get(GetEndpointInfo request)
  182. {
  183. return ToOptimizedResult(new EndPointInfo
  184. {
  185. IsLocal = Request.IsLocal,
  186. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  187. });
  188. }
  189. }
  190. }