2
0

SystemService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 ServiceStack;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using CommonIO;
  14. using MediaBrowser.Model.Net;
  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. [Route("/System/Ping", "POST")]
  30. public class PingSystem : IReturnVoid
  31. {
  32. }
  33. /// <summary>
  34. /// Class RestartApplication
  35. /// </summary>
  36. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  37. [Authenticated(Roles = "Admin")]
  38. public class RestartApplication
  39. {
  40. }
  41. /// <summary>
  42. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  43. /// </summary>
  44. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  45. public class ShutdownApplication
  46. {
  47. // TODO: This is not currently authenticated due to uninstaller
  48. // Improve later
  49. }
  50. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  51. [Authenticated(Roles = "Admin")]
  52. public class GetServerLogs : IReturn<List<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. List<FileSystemMetadata> files;
  103. try
  104. {
  105. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  106. .Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
  107. .ToList();
  108. }
  109. catch (DirectoryNotFoundException)
  110. {
  111. files = new List<FileSystemMetadata>();
  112. }
  113. var result = files.Select(i => new LogFile
  114. {
  115. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  116. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  117. Name = i.Name,
  118. Size = i.Length
  119. }).OrderByDescending(i => i.DateModified)
  120. .ThenByDescending(i => i.DateCreated)
  121. .ThenBy(i => i.Name)
  122. .ToList();
  123. return ToOptimizedResult(result);
  124. }
  125. public object Get(GetLogFile request)
  126. {
  127. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  128. .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
  129. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
  130. }
  131. /// <summary>
  132. /// Gets the specified request.
  133. /// </summary>
  134. /// <param name="request">The request.</param>
  135. /// <returns>System.Object.</returns>
  136. public object Get(GetSystemInfo request)
  137. {
  138. var result = _appHost.GetSystemInfo();
  139. return ToOptimizedResult(result);
  140. }
  141. public object Get(GetPublicSystemInfo request)
  142. {
  143. var result = _appHost.GetSystemInfo();
  144. var publicInfo = new PublicSystemInfo
  145. {
  146. Id = result.Id,
  147. ServerName = result.ServerName,
  148. Version = result.Version,
  149. LocalAddress = result.LocalAddress,
  150. WanAddress = result.WanAddress,
  151. OperatingSystem = result.OperatingSystem
  152. };
  153. return ToOptimizedResult(publicInfo);
  154. }
  155. /// <summary>
  156. /// Posts the specified request.
  157. /// </summary>
  158. /// <param name="request">The request.</param>
  159. public void Post(RestartApplication request)
  160. {
  161. Task.Run(async () =>
  162. {
  163. await Task.Delay(100).ConfigureAwait(false);
  164. await _appHost.Restart().ConfigureAwait(false);
  165. });
  166. }
  167. /// <summary>
  168. /// Posts the specified request.
  169. /// </summary>
  170. /// <param name="request">The request.</param>
  171. public void Post(ShutdownApplication request)
  172. {
  173. Task.Run(async () =>
  174. {
  175. await Task.Delay(100).ConfigureAwait(false);
  176. await _appHost.Shutdown().ConfigureAwait(false);
  177. });
  178. }
  179. public object Get(GetEndpointInfo request)
  180. {
  181. return ToOptimizedResult(new EndPointInfo
  182. {
  183. IsLocal = Request.IsLocal,
  184. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  185. });
  186. }
  187. }
  188. }