SystemController.cs 8.6 KB

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