SystemController.cs 9.2 KB

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