diff options
| author | Paul <jappe+github@posteo.de> | 2026-10-07 02:57:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-07 02:57:21 +0200 |
| commit | 97bfb7c1777d944859f6c6eeef4894daa18203b8 (patch) | |
| tree | 51b364fc76f7a0f37b1efcd7fe2295080ceb01cb /src/slskd/Core/API | |
| parent | 357effeedf8439459004f113bb532a7011e95fe5 (diff) | |
| parent | 7c302a753b1814345fcb26eb78dec97db6b698df (diff) | |
| download | slskd-97bfb7c1777d944859f6c6eeef4894daa18203b8.tar.gz | |
Merge branch 'master' into fix/upload-retry-button
Diffstat (limited to 'src/slskd/Core/API')
| -rw-r--r-- | src/slskd/Core/API/Controllers/LogsController.cs | 94 |
1 files changed, 92 insertions, 2 deletions
diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index 8055c696..a83e608c 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -32,9 +32,16 @@ namespace slskd.Core.API { + using System; + using System.IO; + using System.Linq; + using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; + using Serilog; + using slskd.Files; /// <summary> /// Logs. @@ -46,15 +53,98 @@ namespace slskd.Core.API [Consumes("application/json")] public class LogsController : ControllerBase { + public LogsController(FileService fileService) + { + Files = fileService; + } + + private FileService Files { get; } + private ILogger Log { get; } = Serilog.Log.ForContext<LogsController>(); + /// <summary> /// Gets the last few application logs. /// </summary> /// <returns></returns> - [HttpGet] - [Authorize(Policy = AuthPolicy.Any)] + [HttpGet("live")] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] public IActionResult Logs() { return Ok(Program.LogBuffer); } + + /// <summary> + /// Lists the log files currently on disk. + /// </summary> + /// <returns></returns> + [HttpGet("files")] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] + public async Task<IActionResult> List() + { + if (!Directory.Exists(Path.GetFullPath(Program.LogDirectory))) + { + return NotFound(); + } + + var directory = await Files.ListDirectoryContentsAsync(Path.GetFullPath(Program.LogDirectory), enumerationOptions: new EnumerationOptions + { + IgnoreInaccessible = true, + AttributesToSkip = FileAttributes.System | FileAttributes.Hidden | FileAttributes.ReparsePoint, + RecurseSubdirectories = false, + }); + + var logs = directory.Files.Where(f => f.Name.EndsWith(".log", StringComparison.OrdinalIgnoreCase)); + + return Ok(logs); + } + + /// <summary> + /// Retrieves the requested log file from disk as plain text. + /// </summary> + /// <param name="filename">The name of the log file.</param> + /// <returns></returns> + [HttpGet("files/{filename}")] + [Produces("text/plain")] + [ProducesResponseType(typeof(string), StatusCodes.Status200OK, contentType: "text/plain")] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] + public IActionResult Get([FromRoute] string filename) + { + if (string.IsNullOrWhiteSpace(filename)) + { + return BadRequest("Filename is required"); + } + + if (filename.ContainsAny('/', '\\')) + { + return BadRequest("Filename must not contain a path"); + } + + var sanitizedFilename = FileSafety.GetFileNameSafely(filename, sanitize: true); + + if (!sanitizedFilename.Equals(filename, StringComparison.OrdinalIgnoreCase)) + { + Log.Warning("Input filename {Filename} sanitized to {Sanitized}", filename, sanitizedFilename); + return BadRequest("Filename contains one or more invalid characters"); + } + + if (!sanitizedFilename.EndsWith(".log", StringComparison.OrdinalIgnoreCase)) + { + return StatusCode(StatusCodes.Status403Forbidden, "Only .log files may be retrieved"); + } + + try + { + var stream = Files.GetFileContents(FileSafety.CombineSafely(Program.LogDirectory, sanitizedFilename)); + return File(stream, "text/plain; charset=utf-8", enableRangeProcessing: true); + } + catch (NotFoundException) + { + return NotFound(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to retrieve log file {Filename}: {Message}", filename, ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); + } + } } } |
