SystemController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Jellyfin.Api.Constants;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Net;
  15. using MediaBrowser.Model.System;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. using Microsoft.Extensions.Logging;
  20. namespace Jellyfin.Api.Controllers
  21. {
  22. /// <summary>
  23. /// The system controller.
  24. /// </summary>
  25. public class SystemController : BaseJellyfinApiController
  26. {
  27. private readonly IServerApplicationHost _appHost;
  28. private readonly IApplicationPaths _appPaths;
  29. private readonly IFileSystem _fileSystem;
  30. private readonly INetworkManager _network;
  31. private readonly ILogger<SystemController> _logger;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="SystemController"/> class.
  34. /// </summary>
  35. /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>
  36. /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
  37. /// <param name="fileSystem">Instance of <see cref="IFileSystem"/> interface.</param>
  38. /// <param name="network">Instance of <see cref="INetworkManager"/> interface.</param>
  39. /// <param name="logger">Instance of <see cref="ILogger{SystemController}"/> interface.</param>
  40. public SystemController(
  41. IServerConfigurationManager serverConfigurationManager,
  42. IServerApplicationHost appHost,
  43. IFileSystem fileSystem,
  44. INetworkManager network,
  45. ILogger<SystemController> logger)
  46. {
  47. _appPaths = serverConfigurationManager.ApplicationPaths;
  48. _appHost = appHost;
  49. _fileSystem = fileSystem;
  50. _network = network;
  51. _logger = logger;
  52. }
  53. /// <summary>
  54. /// Gets information about the server.
  55. /// </summary>
  56. /// <response code="200">Information retrieved.</response>
  57. /// <returns>A <see cref="SystemInfo"/> with info about the system.</returns>
  58. [HttpGet("Info")]
  59. [Authorize(Policy = Policies.IgnoreParentalControlOrFirstTimeSetup)]
  60. [ProducesResponseType(StatusCodes.Status200OK)]
  61. public async Task<ActionResult<SystemInfo>> GetSystemInfo()
  62. {
  63. return await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
  64. }
  65. /// <summary>
  66. /// Gets public information about the server.
  67. /// </summary>
  68. /// <response code="200">Information retrieved.</response>
  69. /// <returns>A <see cref="PublicSystemInfo"/> with public info about the system.</returns>
  70. [HttpGet("Info/Public")]
  71. [ProducesResponseType(StatusCodes.Status200OK)]
  72. public async Task<ActionResult<PublicSystemInfo>> GetPublicSystemInfo()
  73. {
  74. return await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
  75. }
  76. /// <summary>
  77. /// Pings the system.
  78. /// </summary>
  79. /// <response code="200">Information retrieved.</response>
  80. /// <returns>The server name.</returns>
  81. [HttpGet("Ping", Name = "GetPingSystem")]
  82. [HttpPost("Ping", Name = "PostPingSystem")]
  83. [ProducesResponseType(StatusCodes.Status200OK)]
  84. public ActionResult<string> PingSystem()
  85. {
  86. return _appHost.Name;
  87. }
  88. /// <summary>
  89. /// Restarts the application.
  90. /// </summary>
  91. /// <response code="204">Server restarted.</response>
  92. /// <returns>No content. Server restarted.</returns>
  93. [HttpPost("Restart")]
  94. [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
  95. [ProducesResponseType(StatusCodes.Status204NoContent)]
  96. public ActionResult RestartApplication()
  97. {
  98. Task.Run(async () =>
  99. {
  100. await Task.Delay(100).ConfigureAwait(false);
  101. _appHost.Restart();
  102. });
  103. return NoContent();
  104. }
  105. /// <summary>
  106. /// Shuts down the application.
  107. /// </summary>
  108. /// <response code="204">Server shut down.</response>
  109. /// <returns>No content. Server shut down.</returns>
  110. [HttpPost("Shutdown")]
  111. [Authorize(Policy = Policies.RequiresElevation)]
  112. [ProducesResponseType(StatusCodes.Status204NoContent)]
  113. public ActionResult ShutdownApplication()
  114. {
  115. Task.Run(async () =>
  116. {
  117. await Task.Delay(100).ConfigureAwait(false);
  118. await _appHost.Shutdown().ConfigureAwait(false);
  119. });
  120. return NoContent();
  121. }
  122. /// <summary>
  123. /// Gets a list of available server log files.
  124. /// </summary>
  125. /// <response code="200">Information retrieved.</response>
  126. /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
  127. [HttpGet("Logs")]
  128. [Authorize(Policy = Policies.RequiresElevation)]
  129. [ProducesResponseType(StatusCodes.Status200OK)]
  130. public ActionResult<LogFile[]> GetServerLogs()
  131. {
  132. IEnumerable<FileSystemMetadata> files;
  133. try
  134. {
  135. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  136. }
  137. catch (IOException ex)
  138. {
  139. _logger.LogError(ex, "Error getting logs");
  140. files = Enumerable.Empty<FileSystemMetadata>();
  141. }
  142. var result = files.Select(i => new LogFile
  143. {
  144. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  145. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  146. Name = i.Name,
  147. Size = i.Length
  148. })
  149. .OrderByDescending(i => i.DateModified)
  150. .ThenByDescending(i => i.DateCreated)
  151. .ThenBy(i => i.Name)
  152. .ToArray();
  153. return result;
  154. }
  155. /// <summary>
  156. /// Gets information about the request endpoint.
  157. /// </summary>
  158. /// <response code="200">Information retrieved.</response>
  159. /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
  160. [HttpGet("Endpoint")]
  161. [Authorize(Policy = Policies.DefaultAuthorization)]
  162. [ProducesResponseType(StatusCodes.Status200OK)]
  163. public ActionResult<EndPointInfo> GetEndpointInfo()
  164. {
  165. return new EndPointInfo
  166. {
  167. IsLocal = Request.HttpContext.Connection.LocalIpAddress.Equals(Request.HttpContext.Connection.RemoteIpAddress),
  168. IsInNetwork = _network.IsInLocalNetwork(Request.HttpContext.Connection.RemoteIpAddress.ToString())
  169. };
  170. }
  171. /// <summary>
  172. /// Gets a log file.
  173. /// </summary>
  174. /// <param name="name">The name of the log file to get.</param>
  175. /// <response code="200">Log file retrieved.</response>
  176. /// <returns>The log file.</returns>
  177. [HttpGet("Logs/Log")]
  178. [Authorize(Policy = Policies.RequiresElevation)]
  179. [ProducesResponseType(StatusCodes.Status200OK)]
  180. public ActionResult GetLogFile([FromQuery, Required] string? name)
  181. {
  182. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  183. .First(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
  184. // For older files, assume fully static
  185. var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
  186. FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, fileShare);
  187. return File(stream, "text/plain");
  188. }
  189. /// <summary>
  190. /// Gets wake on lan information.
  191. /// </summary>
  192. /// <response code="200">Information retrieved.</response>
  193. /// <returns>An <see cref="IEnumerable{WakeOnLanInfo}"/> with the WakeOnLan infos.</returns>
  194. [HttpGet("WakeOnLanInfo")]
  195. [Authorize(Policy = Policies.DefaultAuthorization)]
  196. [ProducesResponseType(StatusCodes.Status200OK)]
  197. public ActionResult<IEnumerable<WakeOnLanInfo>> GetWakeOnLanInfo()
  198. {
  199. var result = _appHost.GetWakeOnLanInfo();
  200. return Ok(result);
  201. }
  202. }
  203. }