SystemController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. {
  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. /// <summary>
  35. /// Initializes a new instance of the <see cref="SystemController"/> class.
  36. /// </summary>
  37. /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> 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="network">Instance of <see cref="INetworkManager"/> interface.</param>
  41. /// <param name="logger">Instance of <see cref="ILogger{SystemController}"/> interface.</param>
  42. public SystemController(
  43. IServerConfigurationManager serverConfigurationManager,
  44. IServerApplicationHost appHost,
  45. IFileSystem fileSystem,
  46. INetworkManager network,
  47. ILogger<SystemController> logger)
  48. {
  49. _appPaths = serverConfigurationManager.ApplicationPaths;
  50. _appHost = appHost;
  51. _fileSystem = fileSystem;
  52. _network = network;
  53. _logger = logger;
  54. }
  55. /// <summary>
  56. /// Gets information about the server.
  57. /// </summary>
  58. /// <response code="200">Information retrieved.</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. 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. /// <returns>No content. Server restarted.</returns>
  95. [HttpPost("Restart")]
  96. [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
  97. [ProducesResponseType(StatusCodes.Status204NoContent)]
  98. public ActionResult RestartApplication()
  99. {
  100. Task.Run(async () =>
  101. {
  102. await Task.Delay(100).ConfigureAwait(false);
  103. _appHost.Restart();
  104. });
  105. return NoContent();
  106. }
  107. /// <summary>
  108. /// Shuts down the application.
  109. /// </summary>
  110. /// <response code="204">Server shut down.</response>
  111. /// <returns>No content. Server shut down.</returns>
  112. [HttpPost("Shutdown")]
  113. [Authorize(Policy = Policies.RequiresElevation)]
  114. [ProducesResponseType(StatusCodes.Status204NoContent)]
  115. public ActionResult ShutdownApplication()
  116. {
  117. Task.Run(async () =>
  118. {
  119. await Task.Delay(100).ConfigureAwait(false);
  120. await _appHost.Shutdown().ConfigureAwait(false);
  121. });
  122. return NoContent();
  123. }
  124. /// <summary>
  125. /// Gets a list of available server log files.
  126. /// </summary>
  127. /// <response code="200">Information retrieved.</response>
  128. /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
  129. [HttpGet("Logs")]
  130. [Authorize(Policy = Policies.RequiresElevation)]
  131. [ProducesResponseType(StatusCodes.Status200OK)]
  132. public ActionResult<LogFile[]> GetServerLogs()
  133. {
  134. IEnumerable<FileSystemMetadata> files;
  135. try
  136. {
  137. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  138. }
  139. catch (IOException ex)
  140. {
  141. _logger.LogError(ex, "Error getting logs");
  142. files = Enumerable.Empty<FileSystemMetadata>();
  143. }
  144. var result = files.Select(i => new LogFile
  145. {
  146. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  147. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  148. Name = i.Name,
  149. Size = i.Length
  150. })
  151. .OrderByDescending(i => i.DateModified)
  152. .ThenByDescending(i => i.DateCreated)
  153. .ThenBy(i => i.Name)
  154. .ToArray();
  155. return result;
  156. }
  157. /// <summary>
  158. /// Gets information about the request endpoint.
  159. /// </summary>
  160. /// <response code="200">Information retrieved.</response>
  161. /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
  162. [HttpGet("Endpoint")]
  163. [Authorize(Policy = Policies.DefaultAuthorization)]
  164. [ProducesResponseType(StatusCodes.Status200OK)]
  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. /// <returns>The log file.</returns>
  179. [HttpGet("Logs/Log")]
  180. [Authorize(Policy = Policies.RequiresElevation)]
  181. [ProducesResponseType(StatusCodes.Status200OK)]
  182. [ProducesFile(MediaTypeNames.Text.Plain)]
  183. public ActionResult GetLogFile([FromQuery, Required] string name)
  184. {
  185. var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
  186. .First(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
  187. // For older files, assume fully static
  188. var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
  189. FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, fileShare, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  190. return File(stream, "text/plain; charset=utf-8");
  191. }
  192. /// <summary>
  193. /// Gets wake on lan information.
  194. /// </summary>
  195. /// <response code="200">Information retrieved.</response>
  196. /// <returns>An <see cref="IEnumerable{WakeOnLanInfo}"/> with the WakeOnLan infos.</returns>
  197. [HttpGet("WakeOnLanInfo")]
  198. [Authorize(Policy = Policies.DefaultAuthorization)]
  199. [Obsolete("This endpoint is obsolete.")]
  200. [ProducesResponseType(StatusCodes.Status200OK)]
  201. public ActionResult<IEnumerable<WakeOnLanInfo>> GetWakeOnLanInfo()
  202. {
  203. var result = _network.GetMacAddresses()
  204. .Select(i => new WakeOnLanInfo(i))
  205. .ToList();
  206. return Ok(result);
  207. }
  208. }
  209. }