diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-12-18 17:24:16 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-18 17:24:16 -0600 |
| commit | b18f7b00bc1d438bfa78ff16cb1badf6fe255e97 (patch) | |
| tree | e278cb305877999c42c134565c195d6990b5f8a4 /src/slskd | |
| parent | cfcdeff46861cdd11a5cbafe533ce08bda584566 (diff) | |
| parent | 727ea41ebca3b3e7d032d6c54451b4af4ab78a82 (diff) | |
| download | slskd-b18f7b00bc1d438bfa78ff16cb1badf6fe255e97.tar.gz | |
Merge pull request #1262 from slskd/api-only
Add 'headless' mode
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Core/API/Controllers/SessionController.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 40 |
3 files changed, 44 insertions, 13 deletions
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; /// <summary> @@ -47,6 +48,7 @@ namespace slskd.Core.API private IOptionsSnapshot<Options> OptionsSnapshot { get; set; } private OptionsAtStartup OptionsAtStartup { get; set; } private ISecurityService Security { get; } + private ILogger Log { get; } = Serilog.Log.ForContext<SessionController>(); /// <summary> /// 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(); 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 @@ -152,6 +152,15 @@ namespace slskd public bool Debug { get; init; } = Debugger.IsAttached; /// <summary> + /// Gets a value indicating whether the application should run in headless (no web UI) mode. + /// </summary> + [Argument('H', "headless")] + [EnvironmentVariable("HEADLESS")] + [Description("run in headless (no web UI) mode")] + [RequiresRestart] + public bool Headless { get; init; } = false; + + /// <summary> /// Gets a value indicating whether remote configuration of options is allowed. /// </summary> [Argument(default, "remote-configuration")] 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; } |
