1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
namespace slskd
{
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Prometheus.DotNetRuntime;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Grafana.Loki;
using slskd.Common.Cryptography;
using slskd.Configuration;
using slskd.Validation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
public static class ProgramExtensions
{
public static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile)
{
configurationFile = Path.GetFullPath(configurationFile);
return builder
.AddDefaultValues(
map: Options.Map.Select(o => o.ToDefaultValue()))
.AddEnvironmentVariables(
prefix: environmentVariablePrefix,
map: Options.Map.Select(o => o.ToEnvironmentVariable()))
.AddYamlFile(
path: Path.GetFileName(configurationFile),
optional: true,
reloadOnChange: false,
provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None))
.AddCommandLine(
commandLine: Environment.CommandLine,
map: Options.Map.Select(o => o.ToCommandLineArgument()));
}
}
public class Program
{
public static readonly string AppName = "slskd";
public static readonly string DefaultConfigurationFile = $"{AppName}.yml";
public static readonly string EnvironmentVariablePrefix = $"{AppName.ToUpperInvariant()}_";
public static Guid InvocationId { get; } = Guid.NewGuid();
public static int ProcessId { get; } = Environment.ProcessId;
public static Version AssemblyVersion { get; } = Assembly.GetExecutingAssembly().GetName().Version;
public static string InformationalVersion { get; } = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
public static string Version { get; } = $"{AssemblyVersion} ({InformationalVersion})";
[CommandLineArgument('n', "no-logo", "suppress logo on startup")]
private static bool NoLogo { get; set; }
[CommandLineArgument('e', "envars", "display environment variables")]
private static bool ShowEnvironmentVariables { get; set; }
[CommandLineArgument('h', "help", "display command line usage")]
private static bool ShowHelp { get; set; }
[CommandLineArgument('v', "version", "display version information")]
private static bool ShowVersion { get; set; }
[CommandLineArgument('g', "generate-cert", "generate X509 certificate and password for HTTPs")]
private static bool GenerateCertificate { get; set; }
[EnvironmentVariable("CONFIG")]
[CommandLineArgument('c', "config", "path to configuration file")]
private static string ConfigurationFile { get; set; } = DefaultConfigurationFile;
private static Options Options { get; } = new Options();
private static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args)
{
EnvironmentVariables.Populate(prefix: EnvironmentVariablePrefix);
CommandLineArguments.Populate(clearExistingValues: false);
if (ShowVersion)
{
Console.WriteLine(Version);
return;
}
if (ShowHelp || ShowEnvironmentVariables)
{
if (!NoLogo) PrintLogo(Version);
if (ShowHelp) PrintCommandLineArguments(Options.Map);
if (ShowEnvironmentVariables) PrintEnvironmentVariables(Options.Map, EnvironmentVariablePrefix);
return;
}
if (GenerateCertificate)
{
GenerateX509Certificate(password: Guid.NewGuid().ToString(), filename: $"{AppName}.pfx");
return;
}
try
{
Configuration = new ConfigurationBuilder()
.AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile)
.Build();
Configuration.GetSection("slskd")
.Bind(Options, (o) => { o.BindNonPublicProperties = true; });
if (!Options.TryValidate(out var result))
{
Console.WriteLine(result.GetResultView("Invalid configuration:"));
return;
}
var cert = Options.Web.Https.Certificate;
if (!string.IsNullOrEmpty(cert.Pfx) && !X509.TryValidate(cert.Pfx, cert.Password, out var certResult))
{
Console.WriteLine($"Invalid HTTPs certificate: {certResult}");
return;
}
}
catch (Exception ex)
{
Console.WriteLine($"Invalid configuration: {(Options.Debug ? ex : ex.Message)}");
return;
}
if (!Options.NoLogo)
{
PrintLogo(Version);
}
if (Options.Debug)
{
Console.WriteLine("Configuration:");
Console.WriteLine(Configuration.GetDebugView());
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.WithProperty("Version", Version)
.Enrich.WithProperty("InstanceName", Options.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
.WriteTo.Console(
outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Async(config =>
config.File(
"logs/slskd-.log",
outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
e => !string.IsNullOrEmpty(Options.Logger.Loki),
config => config.GrafanaLoki(
Options.Logger.Loki ?? string.Empty,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
.CreateLogger();
}
else
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("slskd.Security.PassthroughAuthenticationHandler", LogEventLevel.Information)
.Enrich.WithProperty("Version", Version)
.Enrich.WithProperty("InstanceName", Options.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Async(config =>
config.File(
"logs/slskd-.log",
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
e => !string.IsNullOrEmpty(Options.Logger.Loki),
config => config.GrafanaLoki(
Options.Logger.Loki ?? string.Empty,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
.CreateLogger();
}
var logger = Log.ForContext<Program>();
if (ConfigurationFile != DefaultConfigurationFile && !File.Exists(ConfigurationFile))
{
logger.Warning($"Specified configuration file '{ConfigurationFile}' could not be found and was not loaded.");
}
logger.Information("Version: {Version}", Version);
logger.Information("Instance Name: {InstanceName}", Options.InstanceName);
logger.Information("Invocation ID: {InvocationId}", InvocationId);
logger.Information("Process ID: {ProcessId}", ProcessId);
if (!string.IsNullOrEmpty(Options.Logger.Loki))
{
logger.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", Options.Logger.Loki);
}
try
{
if (Options.Feature.Prometheus)
{
using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting();
}
WebHost.CreateDefaultBuilder(args)
.SuppressStatusMessages(true)
.ConfigureAppConfiguration((hostingContext, builder) =>
{
builder.Sources.Clear();
builder.AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile);
})
.UseSerilog()
.UseUrls()
.UseKestrel(options =>
{
logger.Information($"Listening for HTTP requests at http://{IPAddress.Any}:{Options.Web.Port}/");
options.Listen(IPAddress.Any, Options.Web.Port);
logger.Information($"Listening for HTTPS requests at https://{IPAddress.Any}:{Options.Web.Https.Port}/");
options.Listen(IPAddress.Any, Options.Web.Https.Port, listenOptions =>
{
var cert = Options.Web.Https.Certificate;
if (!string.IsNullOrEmpty(cert.Pfx))
{
logger.Information($"Using certificate from {cert.Pfx}");
listenOptions.UseHttps(cert.Pfx, cert.Password);
}
else
{
logger.Information($"Using randomly generated self-signed certificate");
listenOptions.UseHttps(X509.Generate(subject: AppName));
}
});
})
.UseStartup<Startup>()
.Build()
.Run();
}
catch (Exception ex)
{
logger.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
public static void GenerateX509Certificate(string password, string filename)
{
Console.WriteLine("Generating X509 certificate...");
filename = Path.Combine(AppContext.BaseDirectory, filename);
var cert = X509.Generate(subject: AppName, password, X509KeyStorageFlags.Exportable);
File.WriteAllBytes(filename, cert.Export(X509ContentType.Pkcs12, password));
Console.WriteLine($"Password: {password}");
Console.WriteLine($"Certificate exported to {filename}");
}
public static void PrintCommandLineArguments(IEnumerable<Option> map)
{
static string GetLongName(string longName, Type type)
=> type == typeof(bool) ? longName : $"{longName} <{type.Name.ToLowerInvariant()}>";
var longestName = map.Where(a => !string.IsNullOrEmpty(a.LongName)).Select(a => GetLongName(a.LongName, a.Type)).Max(n => n.Length);
Console.WriteLine("\nusage: slskd [arguments]\n");
Console.WriteLine("arguments:\n");
foreach (Option item in map)
{
var (shortName, longName, _, key, type, defaultValue, description) = item;
if (shortName == default && string.IsNullOrEmpty(longName))
{
continue;
}
var suffix = type == typeof(bool) ? string.Empty : $" (default: {defaultValue ?? "<null>"})";
var result = $" {shortName}{(shortName == default ? " " : "|")}--{GetLongName(longName, type).PadRight(longestName + 3)}{description}{suffix}";
Console.WriteLine(result);
}
}
public static void PrintEnvironmentVariables(IEnumerable<Option> map, string prefix)
{
static string GetName(string name, Type type) => $"{name} <{type.Name.ToLowerInvariant()}>";
var longestName = map.Select(a => GetName(a.EnvironmentVariable, a.Type)).Max(n => n.Length);
Console.WriteLine("\nenvironment variables (arguments and config file have precedence):\n");
foreach (Option item in map)
{
var (_, _, environmentVariable, key, type, defaultValue, description) = item;
if (string.IsNullOrEmpty(environmentVariable))
{
continue;
}
var suffix = type == typeof(bool) ? string.Empty : $" (default: {defaultValue ?? "<null>"})";
var result = $" {prefix}{GetName(environmentVariable, type).PadRight(longestName + 3)}{description}{suffix}";
Console.WriteLine(result);
}
}
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 banner = @$"
▄▄▄▄ ▄▄▄▄ ▄▄▄▄
▄▄▄▄▄▄▄ █ █ ▄▄▄▄▄▄▄ █ █▄▄▄ ▄▄▄█ █
█__ --█ █ █ █__ --█ █ ◄█ █ - █
█▄▄▄▄▄█ █▄▄█ █▄▄▄▄▄█ █▄▄█▄▄█ █▄▄▄▄▄█
╒════════════════════════════════════════════════════════╕
│ GNU AFFERO GENERAL PUBLIC LICENSE │
│ https://slskd.org │
│ │
│{centeredVersion}│
└────────────────────────────────────────────────────────┘";
Console.WriteLine(banner);
}
}
}
|