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