SystemService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. 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. public class PingSystem : IReturnVoid
  32. {
  33. }
  34. /// <summary>
  35. /// Class RestartApplication
  36. /// </summary>
  37. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  38. [Authenticated(Roles = "Admin", AllowLocal = true)]
  39. public class RestartApplication
  40. {
  41. }
  42. /// <summary>
  43. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  44. /// </summary>
  45. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  46. [Authenticated(Roles = "Admin", AllowLocal = true)]
  47. public class ShutdownApplication
  48. {
  49. }
  50. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  51. [Authenticated(Roles = "Admin")]
  52. public class GetServerLogs : IReturn<LogFile[]>
  53. {
  54. }
  55. [Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
  56. [Authenticated]
  57. public class GetEndpointInfo : IReturn<EndPointInfo>
  58. {
  59. public string Endpoint { get; set; }
  60. }
  61. [Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
  62. [Authenticated(Roles = "Admin")]
  63. public class GetLogFile
  64. {
  65. [ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  66. public string Name { get; set; }
  67. }
  68. /// <summary>
  69. /// Class SystemInfoService
  70. /// </summary>
  71. public class SystemService : BaseApiService
  72. {
  73. /// <summary>
  74. /// The _app host
  75. /// </summary>
  76. private readonly IServerApplicationHost _appHost;
  77. private readonly IApplicationPaths _appPaths;
  78. private readonly IFileSystem _fileSystem;
  79. private readonly INetworkManager _network;
  80. private readonly ISecurityManager _security;
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="SystemService" /> class.
  83. /// </summary>
  84. /// <param name="appHost">The app host.</param>
  85. /// <param name="appPaths">The application paths.</param>
  86. /// <param name="fileSystem">The file system.</param>
  87. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  88. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
  89. {
  90. _appHost = appHost;
  91. _appPaths = appPaths;
  92. _fileSystem = fileSystem;
  93. _network = network;
  94. _security = security;
  95. }
  96. public object Post(PingSystem request)
  97. {
  98. return _appHost.Name;
  99. }
  100. public object Get(GetServerLogs request)
  101. {
  102. IEnumerable<FileSystemMetadata> files;
  103. try
  104. {
  105. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt" }, true, false);
  106. }
  107. catch (IOException)
  108. {
  109. files = new FileSystemMetadata[] { };
  110. }
  111. var result = files.Select(i => new LogFile
  112. {
  113. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  114. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  115. Name = i.Name,
  116. Size = i.Length
  117. }).OrderByDescending(i => i.DateModified)
  118. .ThenByDescending(i => i.DateCreated)
  119. .ThenBy(i => i.Name)
  120. .ToArray();
  121. return ToOptimizedResult(result);
  122. }
  123. public Task<object> Get(GetLogFile request)
  124. {
  125. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  126. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  127. // For older files, assume fully static
  128. if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
  129. {
  130. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read);
  131. }
  132. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
  133. }
  134. /// <summary>
  135. /// Gets the specified request.
  136. /// </summary>
  137. /// <param name="request">The request.</param>
  138. /// <returns>System.Object.</returns>
  139. public async Task<object> Get(GetSystemInfo request)
  140. {
  141. var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
  142. return ToOptimizedResult(result);
  143. }
  144. public async Task<object> Get(GetPublicSystemInfo request)
  145. {
  146. var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
  147. var publicInfo = new PublicSystemInfo
  148. {
  149. Id = result.Id,
  150. ServerName = result.ServerName,
  151. Version = result.Version,
  152. LocalAddress = result.LocalAddress,
  153. WanAddress = result.WanAddress,
  154. OperatingSystem = result.OperatingSystem
  155. };
  156. return ToOptimizedResult(publicInfo);
  157. }
  158. /// <summary>
  159. /// Posts the specified request.
  160. /// </summary>
  161. /// <param name="request">The request.</param>
  162. public void Post(RestartApplication request)
  163. {
  164. Task.Run(async () =>
  165. {
  166. await Task.Delay(100).ConfigureAwait(false);
  167. await _appHost.Restart().ConfigureAwait(false);
  168. });
  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. }