//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
using Microsoft.Extensions.Logging;
namespace slskd
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Prometheus.DotNetRuntime;
using Prometheus.SystemMetrics;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Grafana.Loki;
using slskd.Authentication;
using slskd.Configuration;
using slskd.Core.API;
using slskd.Cryptography;
using slskd.Integrations.FTP;
using slskd.Integrations.Pushbullet;
using slskd.Messaging;
using slskd.Relay;
using slskd.Search;
using slskd.Search.API;
using slskd.Shares;
using slskd.Transfers;
using slskd.Transfers.Downloads;
using slskd.Transfers.Uploads;
using slskd.Users;
using slskd.Validation;
using Soulseek;
using Utility.CommandLine;
using Utility.EnvironmentVariables;
using IOFile = System.IO.File;
///
/// Bootstraps configuration and handles primitive command-line instructions.
///
public static class Program
{
///
/// The name of the application.
///
public static readonly string AppName = "slskd";
///
/// The name of the local share host.
///
public static readonly string LocalHostName = "local";
///
/// The url to the issues/support site.
///
public static readonly string IssuesUrl = "https://github.com/slskd/slskd/issues";
///
/// The global prefix for environment variables.
///
public static readonly string EnvironmentVariablePrefix = $"{AppName.ToUpperInvariant()}_";
///
/// The default XML documentation filename.
///
public static readonly string XmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml");
///
/// The default application data directory.
///
public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName);
///
/// Gets the unique Id of this application invocation.
///
public static readonly Guid InvocationId = Guid.NewGuid();
///
/// Gets the Id of the current application process.
///
public static readonly int ProcessId = Environment.ProcessId;
///
/// Inaccurate when running locally.
///
private static readonly Version AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.Equals(new Version(1, 0, 0, 0)) ? new Version(0, 0, 0, 0) : Assembly.GetExecutingAssembly().GetName().Version;
///
/// Inaccurate when running locally.
///
private static readonly string InformationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion == "1.0.0" ? "0.0.0" : Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion;
///
/// Occurs when a new log event is emitted.
///
public static event EventHandler LogEmitted;
///
/// Gets the semantic application version.
///
public static string SemanticVersion { get; } = InformationalVersion.Split('+').First();
///
/// Gets the full application version, including both assembly and informational versions.
///
public static string FullVersion { get; } = $"{SemanticVersion} ({InformationalVersion})";
///
/// Gets a value indicating whether the current version is a Canary build.
///
public static bool IsCanary { get; } = AssemblyVersion.Revision == 65534;
///
/// Gets a value indicating whether the current version is a Development build.
///
public static bool IsDevelopment { get; } = new Version(0, 0, 0, 0) == AssemblyVersion;
///
/// Gets the path where application data is saved.
///
[Argument('a', "app-dir", "path where application data is saved")]
[EnvironmentVariable("APP_DIR")]
public static string AppDirectory { get; private set; } = null;
///
/// Gets the fully qualified path to the application configuration file.
///
[Argument('c', "config", "path to configuration file")]
[EnvironmentVariable("CONFIG")]
public static string ConfigurationFile { get; private set; } = null;
///
/// Gets the path where persistent data is saved.
///
public static string DataDirectory { get; private set; } = null;
///
/// Gets the default fully qualified path to the configuration file.
///
public static string DefaultConfigurationFile { get; private set; }
///
/// Gets the default downloads directory.
///
public static string DefaultDownloadsDirectory { get; private set; }
///
/// Gets the default incomplete download directory.
///
public static string DefaultIncompleteDirectory { get; private set; }
///
/// Gets a buffer containing the last few log events.
///
public static ConcurrentFixedSizeQueue LogBuffer { get; } = new ConcurrentFixedSizeQueue(size: 100);
///
/// Gets the master cancellation token source for the program.
///
///
/// The token from this source should be used (or linked) to any long-running asynchronous task, so that when the application
/// begins to shut down these tasks also shut down in a timely manner. Actions that control the lifecycle of the program
/// (POSIX signals, a restart from the API, etc) should cancel this source.
///
public static CancellationTokenSource MasterCancellationTokenSource { get; } = new CancellationTokenSource();
private static IConfigurationRoot Configuration { get; set; }
private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup();
private static ILogger Log { get; set; } = new ConsoleWriteLineLogger();
[Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")]
private static bool GenerateCertificate { get; set; }
[Argument('k', "generate-secret", "generate random secret of the specified length")]
private static int GenerateSecret { get; set; }
[Argument('n', "no-logo", "suppress logo on startup")]
private static bool NoLogo { get; set; }
[Argument('e', "envars", "display environment variables")]
private static bool ShowEnvironmentVariables { get; set; }
[Argument('h', "help", "display command line usage")]
private static bool ShowHelp { get; set; }
[Argument('v', "version", "display version information")]
private static bool ShowVersion { get; set; }
///
/// Entrypoint.
///
/// Command line arguments.
public static void Main(string[] args)
{
// populate the properties above so that we can override the default config file if needed, and to
// check if the application is being run in command mode (run task and quit).
EnvironmentVariables.Populate(prefix: EnvironmentVariablePrefix);
Arguments.Populate(clearExistingValues: false);
// if a user has used one of the arguments above, perform the requested task, then quit
if (ShowVersion)
{
Log.Information(FullVersion);
return;
}
if (ShowHelp || ShowEnvironmentVariables)
{
if (!NoLogo)
{
PrintLogo(FullVersion);
}
if (ShowHelp)
{
PrintCommandLineArguments(typeof(Options));
}
if (ShowEnvironmentVariables)
{
PrintEnvironmentVariables(typeof(Options), EnvironmentVariablePrefix);
}
return;
}
var reqInitialOwnership = false;
using (var mutex = new Mutex(reqInitialOwnership, AppName))
{
var hasHandle = false;
try
{
try
{
if (!mutex.WaitOne(0, false))
{
Console.WriteLine("Another instance of this program is running");
Environment.Exit(0);
}
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
}
finally
{
if (hasHandle)
{
mutex.ReleaseMutex();
}
}
}
if (GenerateCertificate)
{
GenerateX509Certificate(password: Cryptography.Random.GetBytes(16).ToBase62(), filename: $"{AppName}.pfx");
return;
}
if (GenerateSecret > 0)
{
Log.Information(Cryptography.Random.GetBytes(GenerateSecret).ToBase62());
return;
}
// the application isn't being run in command mode. derive the application directory value
// and defaults that are dependent upon it
AppDirectory ??= DefaultAppDirectory;
DataDirectory = Path.Combine(AppDirectory, "data");
DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml");
DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads");
DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete");
// the location of the configuration file might have been overriden by command line or envar.
// if not, set it to the default.
ConfigurationFile ??= DefaultConfigurationFile;
// verify(create if needed) default application directories. if the downloads or complete
// directories are overridden in config, those will be validated after the config is loaded.
try
{
VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true);
VerifyDirectory(DataDirectory, createIfMissing: true, verifyWriteable: true);
VerifyDirectory(DefaultDownloadsDirectory, createIfMissing: true, verifyWriteable: true);
VerifyDirectory(DefaultIncompleteDirectory, createIfMissing: true, verifyWriteable: true);
}
catch (Exception ex)
{
Log.Information($"Filesystem exception: {ex.Message}");
return;
}
// load and validate the configuration
try
{
Configuration = new ConfigurationBuilder()
.AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile)
.Build();
Configuration.GetSection(AppName)
.Bind(OptionsAtStartup, (o) => { o.BindNonPublicProperties = true; });
if (OptionsAtStartup.Debug)
{
Log.Information($"Configuration:\n{Configuration.GetDebugView()}");
}
if (!OptionsAtStartup.TryValidate(out var result))
{
Log.Information(result.GetResultView());
return;
}
}
catch (Exception ex)
{
Log.Information($"Invalid configuration: {(!OptionsAtStartup.Debug ? ex : ex.Message)}");
return;
}
ConfigureGlobalLogger();
Log = Serilog.Log.ForContext(typeof(Program));
if (!OptionsAtStartup.Flags.NoLogo)
{
PrintLogo(FullVersion);
}
Log.Information("Version: {Version}", FullVersion);
if (IsDevelopment)
{
Log.Warning("This is a Development build; YMMV");
}
if (IsCanary)
{
Log.Warning("This is a canary build");
Log.Warning("Canary builds are considered UNSTABLE and may be completely BROKEN");
Log.Warning($"Please report any issues here: {IssuesUrl}");
}
Log.Information("System: .NET {DotNet}, {OS}, {BitNess} bit, {ProcessorCount} processors", Environment.Version, Environment.OSVersion, Environment.Is64BitOperatingSystem ? 64 : 32, Environment.ProcessorCount);
Log.Information("Process ID: {ProcessId} ({BitNess} bit)", ProcessId, Environment.Is64BitProcess ? 64 : 32);
Log.Information("Invocation ID: {InvocationId}", InvocationId);
Log.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName);
// SQLite must have specific capabilities to function properly. this shouldn't be a concern for shrinkwrapped
// binaries or in Docker, but if someone builds from source weird things can happen.
InitSQLiteOrFailFast();
Log.Information("Using application directory {AppDirectory}", AppDirectory);
Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile);
Log.Information("Storing application data in {DataDirectory}", DataDirectory);
RecreateConfigurationFileIfMissing(ConfigurationFile);
if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki))
{
Log.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", OptionsAtStartup.Logger.Loki);
}
// bootstrap the ASP.NET application
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile);
builder.Host
.UseSerilog();
builder.WebHost
.UseUrls()
.UseKestrel(options =>
{
Log.Information($"Listening for HTTP requests at http://{IPAddress.Any}:{OptionsAtStartup.Web.Port}/");
options.Listen(IPAddress.Any, OptionsAtStartup.Web.Port);
if (!OptionsAtStartup.Web.Https.Disabled)
{
Log.Information($"Listening for HTTPS requests at https://{IPAddress.Any}:{OptionsAtStartup.Web.Https.Port}/");
options.Listen(IPAddress.Any, OptionsAtStartup.Web.Https.Port, listenOptions =>
{
var cert = OptionsAtStartup.Web.Https.Certificate;
if (!string.IsNullOrEmpty(cert.Pfx))
{
Log.Information($"Using certificate from {cert.Pfx}");
listenOptions.UseHttps(cert.Pfx, cert.Password);
}
else
{
Log.Information($"Using randomly generated self-signed certificate");
listenOptions.UseHttps(X509.Generate(subject: AppName));
}
});
}
});
builder.Services
.ConfigureAspDotNetServices()
.ConfigureDependencyInjectionContainer();
var app = builder.Build();
app.ConfigureAspDotNetPipeline();
if (OptionsAtStartup.Flags.NoStart)
{
Log.Information("Qutting because 'no-start' option is enabled");
return;
}
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Serilog.Log.CloseAndFlush();
}
}
private static IServiceCollection ConfigureDependencyInjectionContainer(this IServiceCollection services)
{
// add the instance of OptionsAtStartup to DI as they were at startup. use when Options might change, but
// the values at startup are to be used (generally anything marked RequiresRestart).
services.AddSingleton(OptionsAtStartup);
// add IOptionsMonitor and IOptionsSnapshot to DI.
// use when the current Options are to be used (generally anything not marked RequiresRestart)
// the monitor should be used for services with Singleton lifetime, snapshots for everything else
services.AddOptions()
.Bind(Configuration.GetSection(AppName), o => { o.BindNonPublicProperties = true; })
.Validate(options =>
{
if (!options.TryValidate(out var result))
{
Log.Warning("Options (re)configuration rejected.");
Log.Warning(result.GetResultView());
return false;
}
return true;
});
// add IManagedState, IStateMutator, IStateMonitor, and IStateSnapshot state to DI.
// the mutator should be used any time application state needs to be mutated (as the name implies)
// as with options, the monitor should be used for services with Singleton lifetime, snapshots for everything else
// IManagedState should be used where state is being mutated and accessed in the same context
services.AddManagedState();
// add IHttpClientFactory
// use through 'using var http = HttpClientFactory.CreateClient()' wherever HTTP calls will be made
// this is important to prevent memory leaks
services.AddHttpClient();
// add a partially configured instance of SoulseekClient. the Application instance will
// complete configuration at startup.
services.AddSingleton(_ =>
new SoulseekClient(options: new SoulseekClientOptions(
maximumConcurrentUploads: OptionsAtStartup.Global.Upload.Slots,
maximumConcurrentDownloads: OptionsAtStartup.Global.Download.Slots,
minimumDiagnosticLevel: OptionsAtStartup.Soulseek.DiagnosticLevel)));
// add the core application service to DI as well as a hosted service so that other services can
// access instance methods
services.AddSingleton();
services.AddHostedService(p => p.GetRequiredService());
services.AddSingleton();
services.AddSingleton();
if (OptionsAtStartup.Flags.Volatile)
{
services.AddDbContext($"Data Source=file:search?mode=memory;Cache=shared;Pooling=True;");
services.AddDbContext($"Data Source=file:transfers?mode=memory;Cache=shared;Pooling=True;");
}
else
{
services.AddDbContext($"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;");
services.AddDbContext($"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;");
}
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(_ => new RoomTracker(messageLimit: 250));
services.AddSingleton();
services.AddTransient();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
return services;
}
private static void InitSQLiteOrFailFast()
{
// initialize
// avoids: System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().
SQLitePCL.Batteries.Init();
// check the threading mode set at compile time. if it is 0 it is unsafe to use in a multithreaded application, which slskd is.
// https://www.sqlite.org/compile.html#threadsafe
var threadSafe = SQLitePCL.raw.sqlite3_threadsafe();
if (threadSafe == 0)
{
throw new InvalidOperationException($"SQLite binary was not compiled with THREADSAFE={threadSafe}, which is not compatible with this application. Please create a GitHub issue to report this and include details about your environment.");
}
Log.Debug("SQLite was compiled with THREADSAFE={Mode}", threadSafe);
if (SQLitePCL.raw.sqlite3_config(SQLitePCL.raw.SQLITE_CONFIG_SERIALIZED) != SQLitePCL.raw.SQLITE_OK)
{
throw new InvalidOperationException($"SQLite threading mode could not be set to . Please create a GitHub issue to report this and include details about your environment.");
}
Log.Debug("SQLite threading mode set to {Mode} ({Number})", "SERIALIZED", SQLitePCL.raw.SQLITE_CONFIG_SERIALIZED);
}
private static IServiceCollection ConfigureAspDotNetServices(this IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll", builder => builder
.SetIsOriginAllowed((host) => true)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithExposedHeaders("X-URL-Base")));
services.AddSystemMetrics();
using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting();
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, "misc", ".DataProtection-Keys")));
var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key));
services.AddSingleton(jwtSigningKey);
services.AddSingleton();
if (!OptionsAtStartup.Web.Authentication.Disabled)
{
services.AddAuthorization(options =>
{
options.AddPolicy(AuthPolicy.JwtOnly, policy =>
{
policy.RequireRole(Role.Administrator.ToString());
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
options.AddPolicy(AuthPolicy.Any, policy =>
{
policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme);
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
});
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.FromMinutes(5),
RequireSignedTokens = true,
RequireExpirationTime = true,
ValidateLifetime = true,
ValidIssuer = AppName,
ValidateIssuer = true,
ValidateAudience = false,
IssuerSigningKey = jwtSigningKey,
ValidateIssuerSigningKey = true,
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
// signalr authentication is stupid
if (context.Request.Path.StartsWithSegments("/hub"))
{
// assign the request token from the access_token query parameter if one is present
// this typically means that the calling signalr client is running in a browser. this takes
// precedent over the Authorization header value (if one is present)
// https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0
if (context.Request.Query.TryGetValue("access_token", out var accessToken))
{
context.Token = accessToken;
}
else if (context.Request.Headers.ContainsKey("Authorization")
&& context.Request.Headers.TryGetValue("Authorization", out var authorization)
&& authorization.ToString().StartsWith("Bearer ", StringComparison.InvariantCultureIgnoreCase))
{
// extract the bearer token. this value might be an API key, a JWT, or some garbage value
var token = authorization.ToString().Split(' ').LastOrDefault();
try
{
// check to see if the provided value is a valid API key
var service = services.BuildServiceProvider().GetRequiredService();
var (name, role) = service.AuthenticateWithApiKey(token, callerIpAddress: context.HttpContext.Connection.RemoteIpAddress);
// the API key is valid. create a new, short lived jwt for the key name and role
context.Token = service.GenerateJwt(name, role, ttl: 1000).Serialize();
}
catch
{
// the token either isn't a valid API key. use the provided value and let the
// rest of the auth middleware figure out whether it is valid
context.Token = token;
}
}
}
return Task.CompletedTask;
},
};
})
.AddScheme(ApiKeyAuthentication.AuthenticationScheme, (_) => { });
}
else
{
Log.Warning("Authentication of web requests is DISABLED");
services.AddAuthorization(options =>
{
options.AddPolicy(AuthPolicy.Any, policy =>
{
policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
options.AddPolicy(AuthPolicy.JwtOnly, policy =>
{
policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
});
services.AddAuthentication(PassthroughAuthentication.AuthenticationScheme)
.AddScheme(PassthroughAuthentication.AuthenticationScheme, options =>
{
options.Username = "Anonymous";
options.Role = Role.Administrator;
});
}
services.AddRouting(options => options.LowercaseUrls = true);
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new IPAddressConverter());
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
services
.AddSignalR(options =>
{
// https://github.com/SignalR/SignalR/issues/1149#issuecomment-973887222
options.MaximumParallelInvocationsPerClient = 2;
})
.AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.Converters.Add(new IPAddressConverter());
options.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
services.AddHealthChecks();
services.AddApiVersioning(options => options.ReportApiVersions = true);
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
if (OptionsAtStartup.Feature.Swagger)
{
services.AddSwaggerGen(options =>
{
options.DescribeAllParametersInCamelCase();
options.SwaggerDoc(
"v0",
new OpenApiInfo
{
Title = AppName,
Version = "v0",
});
if (System.IO.File.Exists(XmlDocumentationFile))
{
options.IncludeXmlComments(XmlDocumentationFile);
}
else
{
Log.Warning($"Unable to find XML documentation in {XmlDocumentationFile}, Swagger will not include metadata");
}
});
}
return services;
}
private static WebApplication ConfigureAspDotNetPipeline(this WebApplication app)
{
app.UseExceptionHandler(a => a.Run(async context =>
{
await context.Response.WriteAsJsonAsync(context.Features.Get().Error.Message);
}));
app.UseCors("AllowAll");
if (OptionsAtStartup.Web.Https.Force)
{
app.UseHttpsRedirection();
app.UseHsts();
Log.Information($"Forcing HTTP requests to HTTPS");
}
// allow users to specify a custom path base, for use behind a reverse proxy
var urlBase = OptionsAtStartup.Web.UrlBase;
urlBase = urlBase.StartsWith("/") ? urlBase : "/" + urlBase;
// use urlBase. this effectively just removes urlBase from the path.
// inject urlBase into any html files we serve, and rewrite links to ./static or /static to
// prepend the url base.
app.UsePathBase(urlBase);
app.UseHTMLRewrite("((\\.)?\\/static)", $"{(urlBase == "/" ? string.Empty : urlBase)}/static");
app.UseHTMLInjection($"", excludedRoutes: new[] { "/api", "/swagger" });
Log.Information("Using base url {UrlBase}", urlBase);
// serve static content from the configured path
FileServerOptions fileServerOptions = default;
var contentPath = Path.Combine(AppContext.BaseDirectory, OptionsAtStartup.Web.ContentPath);
fileServerOptions = new FileServerOptions
{
FileProvider = new PhysicalFileProvider(contentPath),
RequestPath = string.Empty,
EnableDirectoryBrowsing = false,
EnableDefaultFiles = true,
};
app.UseFileServer(fileServerOptions);
Log.Information("Serving static content from {ContentPath}", contentPath);
if (OptionsAtStartup.Web.Logging)
{
app.UseSerilogRequestLogging();
}
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub("/hub/application");
endpoints.MapHub("/hub/logs");
endpoints.MapHub("/hub/search");
endpoints.MapHub("/hub/relay");
endpoints.MapControllers();
endpoints.MapHealthChecks("/health");
if (OptionsAtStartup.Metrics.Enabled)
{
var options = OptionsAtStartup.Metrics;
var url = options.Url.StartsWith('/') ? options.Url : "/" + options.Url;
Log.Information("Publishing Prometheus metrics to {URL}", url);
if (options.Authentication.Disabled)
{
Log.Warning("Authentication for the metrics endpoint is DISABLED");
}
endpoints.MapGet(url, async context =>
{
if (!options.Authentication.Disabled)
{
var auth = context.Request.Headers["Authorization"].FirstOrDefault();
var providedCreds = auth?.Split(' ').Last();
var validCreds = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{options.Authentication.Username}:{options.Authentication.Password}"));
if (string.IsNullOrEmpty(auth) ||
!auth.StartsWith("Basic", StringComparison.InvariantCultureIgnoreCase) ||
!string.Equals(providedCreds, validCreds, StringComparison.InvariantCultureIgnoreCase))
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
}
var response = await Metrics.BuildAsync();
await context.Response.WriteAsync(response);
});
}
});
// if this is an /api route and no API controller was matched, give up and return a 404.
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/api"))
{
context.Response.StatusCode = 404;
return;
}
await next();
});
if (OptionsAtStartup.Feature.Swagger)
{
app.UseSwagger();
app.UseSwaggerUI(options => app.Services.GetRequiredService().ApiVersionDescriptions.ToList()
.ForEach(description => options.SwaggerEndpoint($"{(urlBase == "/" ? string.Empty : urlBase)}/swagger/{description.GroupName}/swagger.json", description.GroupName)));
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 (Path.GetExtension(context.Request.Path.ToString()) == string.Empty)
{
context.Request.Path = "/";
}
await next();
});
// either serve the index, or 404
app.UseFileServer(fileServerOptions);
return app;
}
private static void ConfigureGlobalLogger()
{
Serilog.Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information())
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal)
.MinimumLevel.Override("slskd.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Warning)
.MinimumLevel.Override("slskd.Authentication.ApiKeyAuthenticationHandler", LogEventLevel.Warning)
.Enrich.WithProperty("InstanceName", OptionsAtStartup.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
.WriteTo.Console(
outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Async(config =>
config.File(
Path.Combine(AppDirectory, "logs", $"{AppName}-.log"),
outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki),
config => config.GrafanaLoki(
OptionsAtStartup.Logger.Loki ?? string.Empty,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
.WriteTo.Sink(new DelegatingSink(logEvent =>
{
string message = default;
try
{
message = logEvent.RenderMessage();
if (logEvent.Exception != null)
{
message = $"{message}: {logEvent.Exception}";
}
var record = new LogRecord()
{
Timestamp = logEvent.Timestamp.LocalDateTime,
Context = logEvent.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'),
SubContext = logEvent.Properties.ContainsKey("SubContext") ? logEvent.Properties["SubContext"].ToString().TrimStart('"').TrimEnd('"') : null,
Level = logEvent.Level.ToString(),
Message = message.TrimStart('"').TrimEnd('"'),
};
LogBuffer.Enqueue(record);
LogEmitted?.Invoke(null, record);
}
catch (Exception ex)
{
Log.Error("Misconfigured delegating logger: {Exception}. Message: {Message}", ex.Message, message);
}
}))
.CreateLogger();
}
private static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile)
{
configurationFile = Path.GetFullPath(configurationFile);
var multiValuedArguments = typeof(Options)
.GetPropertiesRecursively()
.Where(p => p.PropertyType.IsArray)
.SelectMany(p =>
p.CustomAttributes
.Where(a => a.AttributeType == typeof(ArgumentAttribute))
.Select(a => new[] { a.ConstructorArguments[0].Value, a.ConstructorArguments[1].Value })
.SelectMany(v => v))
.Select(v => v.ToString())
.Where(v => v != "\u0000")
.ToArray();
return builder
.AddDefaultValues(
targetType: typeof(Options))
.AddEnvironmentVariables(
targetType: typeof(Options),
prefix: environmentVariablePrefix)
.AddYamlFile(
path: Path.GetFileName(configurationFile),
targetType: typeof(Options),
optional: true,
reloadOnChange: true,
provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory
.AddCommandLine(
targetType: typeof(Options),
multiValuedArguments,
commandLine: Environment.CommandLine);
}
private static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString)
where T : DbContext
{
Log.Debug("Initializing database context {Name}", typeof(T).Name);
try
{
services.AddDbContextFactory(options =>
{
options.UseSqlite(connectionString);
if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL)
{
options.LogTo(Log.Debug, LogLevel.Information);
}
});
using var ctx = services
.BuildServiceProvider()
.GetRequiredService>()
.CreateDbContext();
return services;
}
catch (Exception ex)
{
Log.Error(ex, $"Failed to initialize database context {typeof(T).Name}: ${ex.Message}");
throw;
}
}
private static void RecreateConfigurationFileIfMissing(string configurationFile)
{
if (!IOFile.Exists(configurationFile))
{
try
{
Log.Warning("Configuration file {ConfigurationFile} does not exist; creating from example", configurationFile);
var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml");
var destination = configurationFile;
IOFile.Copy(source, destination);
}
catch (Exception ex)
{
Log.Error("Failed to create configuration file {ConfigurationFile}: {Message}", configurationFile, ex.Message);
}
}
}
private static void GenerateX509Certificate(string password, string filename)
{
filename = Path.Combine(AppContext.BaseDirectory, filename);
var cert = X509.Generate(subject: AppName, password, X509KeyStorageFlags.Exportable);
IOFile.WriteAllBytes(filename, cert.Export(X509ContentType.Pkcs12, password));
Log.Information($"Certificate exported to {filename}");
Log.Information($"Password: {password}");
}
private static void PrintCommandLineArguments(Type targetType)
{
static string GetLongName(string longName, Type type)
=> type == typeof(bool) ? longName : $"{longName} <{type.ToColloquialString().ToLowerInvariant()}>";
var lines = new List<(string Item, string Description)>();
void Map(Type type)
{
try
{
var defaults = Activator.CreateInstance(type);
var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in props)
{
var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute));
var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute));
var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute));
if (attribute != default)
{
var shortName = (char)attribute.ConstructorArguments[0].Value;
var longName = (string)attribute.ConstructorArguments[1].Value;
var description = descriptionAttribute?.ConstructorArguments[0].Value;
var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})";
var item = $"{(shortName == default ? " " : $"{shortName}|")}--{GetLongName(longName, property.PropertyType)}";
var desc = $"{description}{(property.PropertyType == typeof(bool) ? string.Empty : suffix)}";
lines.Add(new(item, desc));
}
else
{
Map(property.PropertyType);
}
}
}
catch
{
return;
}
}
Map(targetType);
var longestItem = lines.Max(l => l.Item.Length);
Log.Information("\nusage: slskd [arguments]\n");
Log.Information("arguments:\n");
foreach (var line in lines)
{
Log.Information($" {line.Item.PadRight(longestItem)} {line.Description}");
}
}
private static void PrintEnvironmentVariables(Type targetType, string prefix)
{
static string GetName(string name, Type type) => $"{name} <{type.ToColloquialString().ToLowerInvariant()}>";
var lines = new List<(string Item, string Description)>();
void Map(Type type)
{
try
{
var defaults = Activator.CreateInstance(type);
var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in props)
{
var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute));
var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute));
var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute));
if (attribute != default)
{
var name = (string)attribute.ConstructorArguments[0].Value;
var description = descriptionAttribute?.ConstructorArguments[0].Value;
var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})";
var item = $"{prefix}{GetName(name, property.PropertyType)}";
var desc = $"{description}{(type == typeof(bool) ? string.Empty : suffix)}";
lines.Add(new(item, desc));
}
else
{
Map(property.PropertyType);
}
}
}
catch
{
return;
}
}
Map(targetType);
var longestItem = lines.Max(l => l.Item.Length);
Log.Information("\nenvironment variables (arguments and config file have precedence):\n");
foreach (var line in lines)
{
Log.Information($" {line.Item.PadRight(longestItem)} {line.Description}");
}
}
private static void PrintLogo(string version)
{
var padding = 56 - version.Length;
var paddingLeft = padding / 2;
var paddingRight = paddingLeft + (padding % 2);
var centeredVersion = new string(' ', paddingLeft) + version + new string(' ', paddingRight);
var logos = new[]
{
$@"
▄▄▄▄ ▄▄▄▄ ▄▄▄▄
▄▄▄▄▄▄▄ █ █ ▄▄▄▄▄▄▄ █ █▄▄▄ ▄▄▄█ █
█__ --█ █ █ █__ --█ █ ◄█ █ - █
█▄▄▄▄▄█ █▄▄█ █▄▄▄▄▄█ █▄▄█▄▄█ █▄▄▄▄▄█",
@$"
▄▄▄▄ ▄▄▄▄ ▄▄▄▄
▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
█__ --█ █__ --█ ◄█ - █
█▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█",
};
var logo = logos[new System.Random().Next(0, logos.Length)];
var banner = @$"
{logo}
╒════════════════════════════════════════════════════════╕
│ GNU AFFERO GENERAL PUBLIC LICENSE │
│ https://slskd.org │
│ │
│{centeredVersion}│";
if (IsDevelopment)
{
banner += "\n│■■■■■■■■■■■■■■■■■■■■► DEVELOPMENT ◄■■■■■■■■■■■■■■■■■■■■■│";
}
if (IsCanary)
{
banner += "\n│■■■■■■■■■■■■■■■■■■■■■■■► CANARY ◄■■■■■■■■■■■■■■■■■■■■■■■│";
}
banner += "\n└────────────────────────────────────────────────────────┘";
try
{
Console.WriteLine(banner);
}
catch
{
// noop. console may not be available in all cases.
}
}
private static void VerifyDirectory(string directory, bool createIfMissing = true, bool verifyWriteable = true)
{
if (!System.IO.Directory.Exists(directory))
{
if (createIfMissing)
{
try
{
System.IO.Directory.CreateDirectory(directory);
}
catch (Exception ex)
{
throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex);
}
}
else
{
throw new IOException($"Directory {directory} does not exist");
}
}
if (verifyWriteable)
{
try
{
var file = Guid.NewGuid().ToString();
var probe = Path.Combine(directory, file);
IOFile.WriteAllText(probe, string.Empty);
IOFile.Delete(probe);
}
catch (Exception ex)
{
throw new IOException($"Directory {directory} is not writeable: {ex.Message}", ex);
}
}
}
}
}