SystemController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.Configuration;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Controller.Configuration;
  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 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. /// <response code="403">User does not have permission to retrieve information.</response>
  58. /// <returns>A <see cref="SystemInfo"/> with info about the system.</returns>
  59. [HttpGet("Info")]
  60. [Authorize(Policy = Policies.FirstTimeSetupOrIgnoreParentalControl)]
  61. [ProducesResponseType(StatusCodes.Status200OK)]
  62. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  63. public ActionResult<SystemInfo> GetSystemInfo()
  64. {
  65. return _appHost.GetSystemInfo(Request);
  66. }
  67. /// <summary>
  68. /// Gets public information about the server.
  69. /// </summary>
  70. /// <response code="200">Information retrieved.</response>
  71. /// <returns>A <see cref="PublicSystemInfo"/> with public info about the system.</returns>
  72. [HttpGet("Info/Public")]
  73. [ProducesResponseType(StatusCodes.Status200OK)]
  74. public ActionResult<PublicSystemInfo> GetPublicSystemInfo()
  75. {
  76. return _appHost.GetPublicSystemInfo(Request);
  77. }
  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. {
  88. return _appHost.Name;
  89. }
  90. /// <summary>
  91. /// Restarts the application.
  92. /// </summary>
  93. /// <response code="204">Server restarted.</response>
  94. /// <response code="403">User does not have permission to restart server.</response>
  95. /// <returns>No content. Server restarted.</returns>
  96. [HttpPost("Restart")]
  97. [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
  98. [ProducesResponseType(StatusCodes.Status204NoContent)]
  99. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  100. public ActionResult RestartApplication()
  101. {
  102. _appHost.Restart();
  103. return NoContent();
  104. }
  105. /// <summary>
  106. /// Shuts down the application.
  107. /// </summary>
  108. /// <response code="204">Server shut down.</response>
  109. /// <response code="403">User does not have permission to shutdown server.</response>
  110. /// <returns>No content. Server shut down.</returns>
  111. [HttpPost("Shutdown")]
  112. [Authorize(Policy = Policies.RequiresElevation)]
  113. [ProducesResponseType(StatusCodes.Status204NoContent)]
  114. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  115. public ActionResult ShutdownApplication()
  116. {
  117. _appHost.Shutdown();
  118. return NoContent();
  119. }
  120. /// <summary>
  121. /// Gets a list of available server log files.
  122. /// </summary>
  123. /// <response code="200">Information retrieved.</response>
  124. /// <response code="403">User does not have permission to get server logs.</response>
  125. /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
  126. [HttpGet("Logs")]
  127. [Authorize(Policy = Policies.RequiresElevation)]
  128. [ProducesResponseType(StatusCodes.Status200OK)]
  129. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  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. /// <response code="403">User does not have permission to get endpoint information.</response>
  160. /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
  161. [HttpGet("Endpoint")]
  162. [Authorize]
  163. [ProducesResponseType(StatusCodes.Status200OK)]
  164. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  165. public ActionResult<EndPointInfo> GetEndpointInfo()
  166. {
  167. return new EndPointInfo
  168. {
  169. IsLocal = HttpContext.IsLocal(),
  170. IsInNetwork = _network.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIP())
  171. };
  172. }
  173. /// <summary>
  174. /// Gets a log file.
  175. /// </summary>
  176. /// <param name="name">The name of the log file to get.</param>
  177. /// <response code="200">Log file retrieved.</response>
  178. /// <response code="403">User does not have permission to get log files.</response>
  179. /// <returns>The log file.</returns>
  180. [HttpGet("Logs/Log")]
  181. [Authorize(Policy = Policies.RequiresElevation)]
  182. [ProducesResponseType(StatusCodes.Status200OK)]
  183. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  184. [ProducesFile(MediaTypeNames.Text.Plain)]
  185. public ActionResult GetLogFile([FromQuery, Required] string name)
  186. {
  187. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  188. .First(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
  189. // For older files, assume fully static
  190. var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
  191. FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, fileShare, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  192. return File(stream, "text/plain; charset=utf-8");
  193. }
  194. /// <summary>
  195. /// Gets wake on lan information.
  196. /// </summary>
  197. /// <response code="200">Information retrieved.</response>
  198. /// <returns>An <see cref="IEnumerable{WakeOnLanInfo}"/> with the WakeOnLan infos.</returns>
  199. [HttpGet("WakeOnLanInfo")]
  200. [Authorize]
  201. [Obsolete("This endpoint is obsolete.")]
  202. [ProducesResponseType(StatusCodes.Status200OK)]
  203. public ActionResult<IEnumerable<WakeOnLanInfo>> GetWakeOnLanInfo()
  204. {
  205. var result = _network.GetMacAddresses()
  206. .Select(i => new WakeOnLanInfo(i));
  207. return Ok(result);
  208. }
  209. }