SystemController.cs 8.3 KB

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