2
0

SystemService.cs 7.3 KB

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