SystemService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. namespace MediaBrowser.Api.System
  16. {
  17. /// <summary>
  18. /// Class GetSystemInfo
  19. /// </summary>
  20. [Route("/System/Info", "GET", Summary = "Gets information about the server")]
  21. [Authenticated(EscapeParentalControl = true)]
  22. public class GetSystemInfo : IReturn<SystemInfo>
  23. {
  24. }
  25. [Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
  26. public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
  27. {
  28. }
  29. /// <summary>
  30. /// Class RestartApplication
  31. /// </summary>
  32. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  33. [Authenticated(Roles = "Admin")]
  34. public class RestartApplication
  35. {
  36. }
  37. /// <summary>
  38. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  39. /// </summary>
  40. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  41. public class ShutdownApplication
  42. {
  43. // TODO: This is not currently authenticated due to uninstaller
  44. // Improve later
  45. }
  46. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  47. [Authenticated(Roles = "Admin")]
  48. public class GetServerLogs : IReturn<List<LogFile>>
  49. {
  50. }
  51. [Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
  52. [Authenticated]
  53. public class GetEndpointInfo : IReturn<EndpointInfo>
  54. {
  55. public string Endpoint { get; set; }
  56. }
  57. [Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
  58. [Authenticated(Roles = "Admin")]
  59. public class GetLogFile
  60. {
  61. [ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  62. public string Name { get; set; }
  63. }
  64. [Route("/System/SupporterInfo", "GET")]
  65. [Authenticated]
  66. public class GetSupporterInfo : IReturn<SupporterInfo>
  67. {
  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 async Task<object> Get(GetSupporterInfo request)
  98. {
  99. var result = await _security.GetSupporterInfo().ConfigureAwait(false);
  100. return ToOptimizedResult(result);
  101. }
  102. public object Get(GetServerLogs request)
  103. {
  104. List<FileInfo> files;
  105. try
  106. {
  107. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  108. .Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
  109. .ToList();
  110. }
  111. catch (DirectoryNotFoundException)
  112. {
  113. files = new List<FileInfo>();
  114. }
  115. var result = files.Select(i => new LogFile
  116. {
  117. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  118. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  119. Name = i.Name,
  120. Size = i.Length
  121. }).OrderByDescending(i => i.DateModified)
  122. .ThenByDescending(i => i.DateCreated)
  123. .ThenBy(i => i.Name)
  124. .ToList();
  125. return ToOptimizedResult(result);
  126. }
  127. public object Get(GetLogFile request)
  128. {
  129. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  130. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  131. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
  132. }
  133. /// <summary>
  134. /// Gets the specified request.
  135. /// </summary>
  136. /// <param name="request">The request.</param>
  137. /// <returns>System.Object.</returns>
  138. public object Get(GetSystemInfo request)
  139. {
  140. var result = _appHost.GetSystemInfo();
  141. return ToOptimizedResult(result);
  142. }
  143. public object Get(GetPublicSystemInfo request)
  144. {
  145. var result = _appHost.GetSystemInfo();
  146. var publicInfo = new PublicSystemInfo
  147. {
  148. Id = result.Id,
  149. ServerName = result.ServerName,
  150. Version = result.Version,
  151. LocalAddress = result.LocalAddress,
  152. WanAddress = result.WanAddress,
  153. OperatingSystem = result.OperatingSystem
  154. };
  155. return ToOptimizedResult(publicInfo);
  156. }
  157. /// <summary>
  158. /// Posts the specified request.
  159. /// </summary>
  160. /// <param name="request">The request.</param>
  161. public void Post(RestartApplication request)
  162. {
  163. Task.Run(async () =>
  164. {
  165. await Task.Delay(100).ConfigureAwait(false);
  166. await _appHost.Restart().ConfigureAwait(false);
  167. });
  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. public class EndpointInfo
  191. {
  192. public bool IsLocal { get; set; }
  193. public bool IsInNetwork { get; set; }
  194. }
  195. }