//
// 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/.
//
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.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
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;
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.Search;
using slskd.Search.API;
using slskd.Shares;
using slskd.Transfers;
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 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 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);
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('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;
}
if (GenerateCertificate)
{
GenerateX509Certificate(password: Cryptography.Random.GetBytes(16).ToBase62String(), filename: $"{AppName}.pfx");
return;
}
// the application isn't being run in command mode. derive the application directory value
// and defaults that are dependent upon it
AppDirectory ??= DefaultAppDirectory;
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(Path.Combine(AppDirectory, "data"), 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.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("Invocation ID: {InvocationId}", InvocationId);
Log.Information("Process ID: {ProcessId}", ProcessId);
Log.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName);
Log.Information("Using application directory {AppDirectory}", AppDirectory);
Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile);
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
{
if (OptionsAtStartup.Feature.Prometheus)
{
using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting();
}
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.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.AddDbContext("search.db");
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(_ => new RoomTracker(messageLimit: 250));
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
return services;
}
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")));
var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key));
services.AddSingleton(jwtSigningKey);
if (!OptionsAtStartup.Web.Authentication.Disabled)
{
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 =>
{
// assign the request token from the access_token query parameter
// but only if the destination is a SignalR hub
// https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0
if (context.HttpContext.Request.Path.StartsWithSegments("/hub"))
{
context.Token = context.Request.Query["access_token"];
}
return Task.CompletedTask;
},
};
});
}
else
{
Log.Warning("Authentication of web requests is DISABLED");
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().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");
}
});
}
if (OptionsAtStartup.Feature.Prometheus)
{
services.AddSystemMetrics();
}
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;
app.UsePathBase(urlBase);
app.Use((context, next) =>
{
context.Response.OnStarting(() =>
{
// the UI needs to be aware of what the URL base is,
// so that react-router can use explicit paths.
context.Response.Headers.Add("X-URL-Base", urlBase);
return Task.CompletedTask;
});
return next();
});
Log.Information("Using base url {UrlBase}", urlBase);
// remove any errant double forward slashes which may have been introduced by manipulating the path base
app.Use(async (context, next) =>
{
var path = context.Request.Path.ToString();
if (path.StartsWith("//"))
{
context.Request.Path = new string(path.Skip(1).ToArray());
}
await next();
});
// serve static content from the configured path
FileServerOptions fileServerOptions = default;
var contentPath = Path.GetFullPath(OptionsAtStartup.Web.ContentPath);
if (!System.IO.Directory.Exists(contentPath))
{
Log.Warning($"Static content disabled; cannot find content path '{contentPath}'");
}
else
{
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.Feature.Prometheus)
{
app.UseHttpMetrics();
Log.Information("Publishing Prometheus metrics to /metrics");
}
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.MapControllers();
endpoints.MapHealthChecks("/health");
if (OptionsAtStartup.Feature.Prometheus)
{
endpoints.MapMetrics("/metrics");
}
});
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 /swagger");
}
// if we made it this far and the route still wasn't matched, return the index unless it's an api route. this is
// required so that SPA routing (React Router, etc) can work properly
app.Use(async (context, next) =>
{
// exclude API routes which are not matched or return a 404
if (!context.Request.Path.StartsWithSegments("/api"))
{
context.Request.Path = "/";
}
await next();
});
// finally, hit the fileserver again. if the path was modified to return the index above, the index document will be
// returned. otherwise it will throw a final 404 back to the client.
if (System.IO.Directory.Exists(contentPath))
{
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)
.Enrich.WithProperty("InstanceName", OptionsAtStartup.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
.WriteTo.Console(
outputTemplate: (OptionsAtStartup.Debug ? "[{SubContext}] " : 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 ? "[{SubContext}] " : 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 =>
{
try
{
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 = logEvent.RenderMessage().TrimStart('"').TrimEnd('"'),
};
LogBuffer.Enqueue(record);
LogEmitted?.Invoke(null, record);
}
catch (Exception ex)
{
Log.Information($"Misconfigured delegating logger: {ex.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 filename)
where T : DbContext
{
Log.Debug($"Initializing database context {typeof(T).Name}");
try
{
services.AddDbContextFactory(options =>
{
options.UseSqlite($"Data Source={Path.Combine(AppDirectory, "data", filename)}");
});
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)
{
Log.Information("Generating X509 certificate...");
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($"Password: {password}");
Log.Information($"Certificate exported to {filename}");
}
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└────────────────────────────────────────────────────────┘";
Console.WriteLine(banner);
}
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);
}
}
}
}
}