SystemService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. [Route("/System/SupporterInfo", "GET")]
  66. [Authenticated]
  67. public class GetSupporterInfo : IReturn<SupporterInfo>
  68. {
  69. }
  70. /// <summary>
  71. /// Class SystemInfoService
  72. /// </summary>
  73. public class SystemService : BaseApiService
  74. {
  75. /// <summary>
  76. /// The _app host
  77. /// </summary>
  78. private readonly IServerApplicationHost _appHost;
  79. private readonly IApplicationPaths _appPaths;
  80. private readonly IFileSystem _fileSystem;
  81. private readonly INetworkManager _network;
  82. private readonly ISecurityManager _security;
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="SystemService" /> class.
  85. /// </summary>
  86. /// <param name="appHost">The app host.</param>
  87. /// <param name="appPaths">The application paths.</param>
  88. /// <param name="fileSystem">The file system.</param>
  89. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  90. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
  91. {
  92. _appHost = appHost;
  93. _appPaths = appPaths;
  94. _fileSystem = fileSystem;
  95. _network = network;
  96. _security = security;
  97. }
  98. public async Task<object> Get(GetSupporterInfo request)
  99. {
  100. var result = await _security.GetSupporterInfo().ConfigureAwait(false);
  101. return ToOptimizedResult(result);
  102. }
  103. public object Get(GetServerLogs request)
  104. {
  105. List<FileSystemMetadata> files;
  106. try
  107. {
  108. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  109. .Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
  110. .ToList();
  111. }
  112. catch (DirectoryNotFoundException)
  113. {
  114. files = new List<FileSystemMetadata>();
  115. }
  116. var result = files.Select(i => new LogFile
  117. {
  118. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  119. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  120. Name = i.Name,
  121. Size = i.Length
  122. }).OrderByDescending(i => i.DateModified)
  123. .ThenByDescending(i => i.DateCreated)
  124. .ThenBy(i => i.Name)
  125. .ToList();
  126. return ToOptimizedResult(result);
  127. }
  128. public object Get(GetLogFile request)
  129. {
  130. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  131. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  132. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.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 object Get(GetSystemInfo request)
  140. {
  141. var result = _appHost.GetSystemInfo();
  142. return ToOptimizedResult(result);
  143. }
  144. public object Get(GetPublicSystemInfo request)
  145. {
  146. var result = _appHost.GetSystemInfo();
  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. public class EndpointInfo
  192. {
  193. public bool IsLocal { get; set; }
  194. public bool IsInNetwork { get; set; }
  195. }
  196. }