SystemController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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;
  7. using System.Net.Mime;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Api.Attributes;
  11. using Jellyfin.Api.Constants;
  12. using MediaBrowser.Common.Configuration;
  13. using MediaBrowser.Common.Extensions;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Controller;
  16. using MediaBrowser.Controller.Configuration;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Net;
  19. using MediaBrowser.Model.System;
  20. using Microsoft.AspNetCore.Authorization;
  21. using Microsoft.AspNetCore.Http;
  22. using Microsoft.AspNetCore.Mvc;
  23. using Microsoft.Extensions.Logging;
  24. namespace Jellyfin.Api.Controllers
  25. {
  26. /// <summary>
  27. /// The system controller.
  28. /// </summary>
  29. public class SystemController : BaseJellyfinApiController
  30. {
  31. private readonly IServerApplicationHost _appHost;
  32. private readonly IApplicationPaths _appPaths;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly INetworkManager _network;
  35. private readonly ILogger<SystemController> _logger;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="SystemController"/> class.
  38. /// </summary>
  39. /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>
  40. /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
  41. /// <param name="fileSystem">Instance of <see cref="IFileSystem"/> interface.</param>
  42. /// <param name="network">Instance of <see cref="INetworkManager"/> interface.</param>
  43. /// <param name="logger">Instance of <see cref="ILogger{SystemController}"/> interface.</param>
  44. public SystemController(
  45. IServerConfigurationManager serverConfigurationManager,
  46. IServerApplicationHost appHost,
  47. IFileSystem fileSystem,
  48. INetworkManager network,
  49. ILogger<SystemController> logger)
  50. {
  51. _appPaths = serverConfigurationManager.ApplicationPaths;
  52. _appHost = appHost;
  53. _fileSystem = fileSystem;
  54. _network = network;
  55. _logger = logger;
  56. }
  57. /// <summary>
  58. /// Gets information about the server.
  59. /// </summary>
  60. /// <response code="200">Information retrieved.</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. public ActionResult<SystemInfo> GetSystemInfo()
  66. {
  67. return _appHost.GetSystemInfo(Request.HttpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback);
  68. }
  69. /// <summary>
  70. /// Gets public information about the server.
  71. /// </summary>
  72. /// <response code="200">Information retrieved.</response>
  73. /// <returns>A <see cref="PublicSystemInfo"/> with public info about the system.</returns>
  74. [HttpGet("Info/Public")]
  75. [ProducesResponseType(StatusCodes.Status200OK)]
  76. public ActionResult<PublicSystemInfo> GetPublicSystemInfo()
  77. {
  78. return _appHost.GetPublicSystemInfo(Request.HttpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback);
  79. }
  80. /// <summary>
  81. /// Pings the system.
  82. /// </summary>
  83. /// <response code="200">Information retrieved.</response>
  84. /// <returns>The server name.</returns>
  85. [HttpGet("Ping", Name = "GetPingSystem")]
  86. [HttpPost("Ping", Name = "PostPingSystem")]
  87. [ProducesResponseType(StatusCodes.Status200OK)]
  88. public ActionResult<string> PingSystem()
  89. {
  90. return _appHost.Name;
  91. }
  92. /// <summary>
  93. /// Restarts the application.
  94. /// </summary>
  95. /// <response code="204">Server restarted.</response>
  96. /// <returns>No content. Server restarted.</returns>
  97. [HttpPost("Restart")]
  98. [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
  99. [ProducesResponseType(StatusCodes.Status204NoContent)]
  100. public ActionResult RestartApplication()
  101. {
  102. Task.Run(async () =>
  103. {
  104. await Task.Delay(100).ConfigureAwait(false);
  105. _appHost.Restart();
  106. });
  107. return NoContent();
  108. }
  109. /// <summary>
  110. /// Shuts down the application.
  111. /// </summary>
  112. /// <response code="204">Server shut down.</response>
  113. /// <returns>No content. Server shut down.</returns>
  114. [HttpPost("Shutdown")]
  115. [Authorize(Policy = Policies.RequiresElevation)]
  116. [ProducesResponseType(StatusCodes.Status204NoContent)]
  117. public ActionResult ShutdownApplication()
  118. {
  119. Task.Run(async () =>
  120. {
  121. await Task.Delay(100).ConfigureAwait(false);
  122. await _appHost.Shutdown().ConfigureAwait(false);
  123. });
  124. return NoContent();
  125. }
  126. /// <summary>
  127. /// Gets a list of available server log files.
  128. /// </summary>
  129. /// <response code="200">Information retrieved.</response>
  130. /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
  131. [HttpGet("Logs")]
  132. [Authorize(Policy = Policies.RequiresElevation)]
  133. [ProducesResponseType(StatusCodes.Status200OK)]
  134. public ActionResult<LogFile[]> GetServerLogs()
  135. {
  136. IEnumerable<FileSystemMetadata> files;
  137. try
  138. {
  139. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  140. }
  141. catch (IOException ex)
  142. {
  143. _logger.LogError(ex, "Error getting logs");
  144. files = Enumerable.Empty<FileSystemMetadata>();
  145. }
  146. var result = files.Select(i => new LogFile
  147. {
  148. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  149. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  150. Name = i.Name,
  151. Size = i.Length
  152. })
  153. .OrderByDescending(i => i.DateModified)
  154. .ThenByDescending(i => i.DateCreated)
  155. .ThenBy(i => i.Name)
  156. .ToArray();
  157. return result;
  158. }
  159. /// <summary>
  160. /// Gets information about the request endpoint.
  161. /// </summary>
  162. /// <response code="200">Information retrieved.</response>
  163. /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
  164. [HttpGet("Endpoint")]
  165. [Authorize(Policy = Policies.DefaultAuthorization)]
  166. [ProducesResponseType(StatusCodes.Status200OK)]
  167. public ActionResult<EndPointInfo> GetEndpointInfo()
  168. {
  169. return new EndPointInfo
  170. {
  171. IsLocal = HttpContext.IsLocal(),
  172. IsInNetwork = _network.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIp())
  173. };
  174. }
  175. /// <summary>
  176. /// Gets a log file.
  177. /// </summary>
  178. /// <param name="name">The name of the log file to get.</param>
  179. /// <response code="200">Log file retrieved.</response>
  180. /// <returns>The log file.</returns>
  181. [HttpGet("Logs/Log")]
  182. [Authorize(Policy = Policies.RequiresElevation)]
  183. [ProducesResponseType(StatusCodes.Status200OK)]
  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);
  192. return File(stream, "text/plain");
  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(Policy = Policies.DefaultAuthorization)]
  201. [ProducesResponseType(StatusCodes.Status200OK)]
  202. public ActionResult<IEnumerable<WakeOnLanInfo>> GetWakeOnLanInfo()
  203. {
  204. var result = _appHost.GetWakeOnLanInfo();
  205. return Ok(result);
  206. }
  207. }
  208. }