SystemService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Common.Security;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.System;
  9. using ServiceStack;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using CommonIO;
  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)]
  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. /// <summary>
  31. /// Class RestartApplication
  32. /// </summary>
  33. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  34. [Authenticated(Roles = "Admin")]
  35. public class RestartApplication
  36. {
  37. }
  38. /// <summary>
  39. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  40. /// </summary>
  41. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  42. public class ShutdownApplication
  43. {
  44. // TODO: This is not currently authenticated due to uninstaller
  45. // Improve later
  46. }
  47. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  48. [Authenticated(Roles = "Admin")]
  49. public class GetServerLogs : IReturn<List<LogFile>>
  50. {
  51. }
  52. [Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
  53. [Authenticated]
  54. public class GetEndpointInfo : IReturn<EndpointInfo>
  55. {
  56. public string Endpoint { get; set; }
  57. }
  58. [Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
  59. [Authenticated(Roles = "Admin")]
  60. public class GetLogFile
  61. {
  62. [ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  63. public string Name { get; set; }
  64. }
  65. /// <summary>
  66. /// Class SystemInfoService
  67. /// </summary>
  68. public class SystemService : BaseApiService
  69. {
  70. /// <summary>
  71. /// The _app host
  72. /// </summary>
  73. private readonly IServerApplicationHost _appHost;
  74. private readonly IApplicationPaths _appPaths;
  75. private readonly IFileSystem _fileSystem;
  76. private readonly INetworkManager _network;
  77. private readonly ISecurityManager _security;
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="SystemService" /> class.
  80. /// </summary>
  81. /// <param name="appHost">The app host.</param>
  82. /// <param name="appPaths">The application paths.</param>
  83. /// <param name="fileSystem">The file system.</param>
  84. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  85. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
  86. {
  87. _appHost = appHost;
  88. _appPaths = appPaths;
  89. _fileSystem = fileSystem;
  90. _network = network;
  91. _security = security;
  92. }
  93. public object Get(GetServerLogs request)
  94. {
  95. List<FileSystemMetadata> files;
  96. try
  97. {
  98. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  99. .Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
  100. .ToList();
  101. }
  102. catch (DirectoryNotFoundException)
  103. {
  104. files = new List<FileSystemMetadata>();
  105. }
  106. var result = files.Select(i => new LogFile
  107. {
  108. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  109. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  110. Name = i.Name,
  111. Size = i.Length
  112. }).OrderByDescending(i => i.DateModified)
  113. .ThenByDescending(i => i.DateCreated)
  114. .ThenBy(i => i.Name)
  115. .ToList();
  116. return ToOptimizedResult(result);
  117. }
  118. public object Get(GetLogFile request)
  119. {
  120. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  121. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  122. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
  123. }
  124. /// <summary>
  125. /// Gets the specified request.
  126. /// </summary>
  127. /// <param name="request">The request.</param>
  128. /// <returns>System.Object.</returns>
  129. public object Get(GetSystemInfo request)
  130. {
  131. var result = _appHost.GetSystemInfo();
  132. return ToOptimizedResult(result);
  133. }
  134. public object Get(GetPublicSystemInfo request)
  135. {
  136. var result = _appHost.GetSystemInfo();
  137. var publicInfo = new PublicSystemInfo
  138. {
  139. Id = result.Id,
  140. ServerName = result.ServerName,
  141. Version = result.Version,
  142. LocalAddress = result.LocalAddress,
  143. WanAddress = result.WanAddress,
  144. OperatingSystem = result.OperatingSystem
  145. };
  146. return ToOptimizedResult(publicInfo);
  147. }
  148. /// <summary>
  149. /// Posts the specified request.
  150. /// </summary>
  151. /// <param name="request">The request.</param>
  152. public void Post(RestartApplication request)
  153. {
  154. Task.Run(async () =>
  155. {
  156. await Task.Delay(100).ConfigureAwait(false);
  157. await _appHost.Restart().ConfigureAwait(false);
  158. });
  159. }
  160. /// <summary>
  161. /// Posts the specified request.
  162. /// </summary>
  163. /// <param name="request">The request.</param>
  164. public void Post(ShutdownApplication request)
  165. {
  166. Task.Run(async () =>
  167. {
  168. await Task.Delay(100).ConfigureAwait(false);
  169. await _appHost.Shutdown().ConfigureAwait(false);
  170. });
  171. }
  172. public object Get(GetEndpointInfo request)
  173. {
  174. return ToOptimizedResult(new EndpointInfo
  175. {
  176. IsLocal = Request.IsLocal,
  177. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  178. });
  179. }
  180. }
  181. public class EndpointInfo
  182. {
  183. public bool IsLocal { get; set; }
  184. public bool IsInNetwork { get; set; }
  185. }
  186. }