SystemController.cs 8.8 KB

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