From d2dd0c85b8966bf20b8bfbf896c460db794c461c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Dec 2024 15:57:02 -0600 Subject: add headless option --- config/slskd.example.yml | 1 + src/slskd/Core/Options.cs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index c29f8ebb..dd164084 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -1,4 +1,5 @@ # debug: false +# headless: false # remote_configuration: false # remote_file_management: false # instance_name: default diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 32188aef..ff9cc407 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -151,6 +151,15 @@ namespace slskd [RequiresRestart] public bool Debug { get; init; } = Debugger.IsAttached; + /// + /// Gets a value indicating whether the application should run in headless (no web UI) mode. + /// + [Argument('H', "headless")] + [EnvironmentVariable("HEADLESS")] + [Description("run in headless (no web UI) mode")] + [RequiresRestart] + public bool Headless { get; init; } = false; + /// /// Gets a value indicating whether remote configuration of options is allowed. /// -- cgit v1.2.3 From 7b59b4ad31050c49b244315b4917e7a7c1b75bf5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Dec 2024 15:57:29 -0600 Subject: don't serve static files when running in headless mode --- src/slskd/Program.cs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f2c61b14..49b01516 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -909,8 +909,15 @@ namespace slskd EnableDefaultFiles = true, }; - app.UseFileServer(fileServerOptions); - Log.Information("Serving static content from {ContentPath}", contentPath); + if (!OptionsAtStartup.Headless) + { + app.UseFileServer(fileServerOptions); + Log.Information("Serving static content from {ContentPath}", contentPath); + } + else + { + Log.Warning("Running in headless mode; web UI is DISABLED"); + } if (OptionsAtStartup.Web.Logging) { @@ -998,21 +1005,28 @@ namespace slskd Log.Information("Publishing Swagger documentation to {URL}", "/swagger"); } - // if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused. - // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next - // middleware - app.Use(async (context, next) => + /* + if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused. + if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next + middleware. + + if we're running in headless mode, do nothing and let ASP.NET return a 404 + */ + if (!OptionsAtStartup.Headless) { - if (Path.GetExtension(context.Request.Path.ToString()) == string.Empty) + app.Use(async (context, next) => { - context.Request.Path = "/"; - } + if (Path.GetExtension(context.Request.Path.ToString()) == string.Empty) + { + context.Request.Path = "/"; + } - await next(); - }); + await next(); + }); - // either serve the index, or 404 - app.UseFileServer(fileServerOptions); + // either serve the index, or 404 + app.UseFileServer(fileServerOptions); + } return app; } -- cgit v1.2.3 From 30a3e14536685cea1cfafed29aba0a217ce86f03 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Dec 2024 15:57:54 -0600 Subject: forbid login when running headless --- src/slskd/Core/API/Controllers/SessionController.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/slskd/Core/API/Controllers/SessionController.cs b/src/slskd/Core/API/Controllers/SessionController.cs index ffd1bdb6..861fd468 100644 --- a/src/slskd/Core/API/Controllers/SessionController.cs +++ b/src/slskd/Core/API/Controllers/SessionController.cs @@ -22,6 +22,7 @@ namespace slskd.Core.API using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Serilog; using slskd.Authentication; /// @@ -47,6 +48,7 @@ namespace slskd.Core.API private IOptionsSnapshot OptionsSnapshot { get; set; } private OptionsAtStartup OptionsAtStartup { get; set; } private ISecurityService Security { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Checks whether the provided authentication is valid. @@ -96,6 +98,12 @@ namespace slskd.Core.API [ProducesResponseType(typeof(string), 500)] public IActionResult Login([FromBody] LoginRequest login) { + if (OptionsAtStartup.Headless) + { + Log.Warning("Login from {User} rejected; web UI is DISABLED when running in headless mode", login.Username); + return Forbid(); + } + if (login == default) { return BadRequest(); -- cgit v1.2.3 From 727ea41ebca3b3e7d032d6c54451b4af4ab78a82 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Dec 2024 17:04:17 -0600 Subject: docs for headless mode --- docs/config.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/config.md b/docs/config.md index e83fc1a9..4d8764a5 100644 --- a/docs/config.md +++ b/docs/config.md @@ -73,6 +73,20 @@ If an attacker were to gain access to the application and retrieve the YAML file remote_configuration: false ``` +# Headless Mode + +Users that want extra security and don't intend to use the application's web UI can disable it. This setting is ideal in +situations where slskd will be controlled by another application via the API (like when run as a Relay agent). + +| Command-Line | Environment Variable | Description | +| ------------------------ | -----------------------------| ------------------------------------------------------------- | +| `--headless` | `SLSKD_HEADLESS` | Determines whether the application should run in headless (no web UI) mode | + +#### **YAML** +```yaml +headless: false +``` + # Permissions On [Unix-like](https://en.wikipedia.org/wiki/Unix-like) operating systems, slskd creates downloaded files with permissions dictated by the [umask](https://en.wikipedia.org/wiki/Umask) of the process, usually 022, which translates to a file mode of 644; read/write for the owner and read for the group and all others. -- cgit v1.2.3