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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
// <copyright file="Options.cs" company="slskd Team">
// 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/.
// </copyright>
namespace slskd
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using slskd.Validation;
using Soulseek.Diagnostics;
using Utility.CommandLine;
using Utility.EnvironmentVariables;
/// <summary>
/// Application options.
/// </summary>
public class Options
{
/// <summary>
/// Gets a value indicating whether to display the application version.
/// </summary>
[Argument('v', "version")]
[Description("display version information")]
public bool ShowVersion { get; private set; } = false;
/// <summary>
/// Gets a value indicating whether to display a list of command line arguments.
/// </summary>
[Argument('h', "help")]
[Description("display command line usage")]
public bool ShowHelp { get; private set; } = false;
/// <summary>
/// Gets a value indicating whether to display a list of configuration environment variables.
/// </summary>
[Argument('e', "envars")]
[Description("display environment variables")]
public bool ShowEnvironmentVariables { get; private set; } = false;
/// <summary>
/// Gets a value indicating whether to generate an X509 certificate and password.
/// </summary>
[Argument('g', "generate-cert")]
[Description("generate X509 certificate and password for HTTPs")]
public bool GenerateCertificate { get; private set; } = false;
/// <summary>
/// Gets the path to the application configuration file.
/// </summary>
[Argument('c', "config")]
[EnvironmentVariable("CONFIG")]
[Description("path to configuration file")]
public string ConfigurationFile { get; private set; } = Program.DefaultConfigurationFile;
/// <summary>
/// Gets a value indicating whether the application should run in debug mode.
/// </summary>
[Argument('d', "debug")]
[EnvironmentVariable("DEBUG")]
[Description("run in debug mode")]
public bool Debug { get; private set; } = Debugger.IsAttached;
/// <summary>
/// Gets a value indicating whether the logo should be suppressed on startup.
/// </summary>
[Argument('n', "no-logo")]
[EnvironmentVariable("NO_LOGO")]
[Description("suppress logo on startup")]
public bool NoLogo { get; private set; } = false;
/// <summary>
/// Gets the unique name for this instance.
/// </summary>
[Argument('i', "instance-name")]
[EnvironmentVariable("INSTANCE_NAME")]
[Description("optional; a unique name for this instance")]
public string InstanceName { get; private set; } = "default";
/// <summary>
/// Gets directory options.
/// </summary>
[Validate]
public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions();
/// <summary>
/// Gets options for the web UI.
/// </summary>
[Validate]
public WebOptions Web { get; private set; } = new WebOptions();
/// <summary>
/// Gets logger options.
/// </summary>
[Validate]
public LoggerOptions Logger { get; private set; } = new LoggerOptions();
/// <summary>
/// Gets feature options.
/// </summary>
[Validate]
public FeatureOptions Feature { get; private set; } = new FeatureOptions();
/// <summary>
/// Gets options for the Soulseek client.
/// </summary>
[Validate]
public SoulseekOptions Soulseek { get; private set; } = new SoulseekOptions();
/// <summary>
/// Directory options.
/// </summary>
public class DirectoriesOptions
{
/// <summary>
/// Gets the path where application data is saved.
/// </summary>
[Argument(default, "app")]
[EnvironmentVariable("APP_DIR")]
[Description("path where application data is saved")]
[DirectoryExists]
public string App { get; private set; } = Program.DefaultAppDirectory;
/// <summary>
/// Gets the path where incomplete downloads are saved.
/// </summary>
[Argument(default, "incomplete")]
[EnvironmentVariable("INCOMPLETE_DIR")]
[Description("path where incomplete downloads are saved")]
[DirectoryExists]
public string Incomplete { get; private set; } = Program.DefaultIncompleteDirectory;
/// <summary>
/// Gets the path where downloaded files are saved.
/// </summary>
[Argument('o', "downloads")]
[EnvironmentVariable("DOWNLOADS_DIR")]
[Description("path where downloaded files are saved")]
[DirectoryExists]
public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory;
/// <summary>
/// Gets the path to shared files.
/// </summary>
[Argument('s', "shared")]
[EnvironmentVariable("SHARED_DIR")]
[Description("path to shared files")]
[DirectoryExists]
public string Shared { get; private set; } = Program.DefaultSharedDirectory;
}
/// <summary>
/// Feature options.
/// </summary>
public class FeatureOptions
{
/// <summary>
/// Gets a value indicating whether prometheus metrics should be collected and published.
/// </summary>
[Argument(default, "prometheus")]
[EnvironmentVariable("PROMETHEUS")]
[Description("enable collection and publishing of prometheus metrics")]
public bool Prometheus { get; private set; } = false;
/// <summary>
/// Gets a value indicating whether swagger documentation and UI should be enabled.
/// </summary>
[Argument(default, "swagger")]
[EnvironmentVariable("SWAGGER")]
[Description("enable swagger documentation and UI")]
public bool Swagger { get; private set; } = false;
}
/// <summary>
/// Logger options.
/// </summary>
public class LoggerOptions
{
/// <summary>
/// Gets the URL to a Grafana Loki instance to which to log.
/// </summary>
[Argument(default, "loki")]
[EnvironmentVariable("LOKI")]
[Description("optional; url to a Grafana Loki instance to which to log")]
public string Loki { get; private set; } = null;
}
/// <summary>
/// Soulseek client options.
/// </summary>
public class SoulseekOptions
{
/// <summary>
/// Gets the password for the Soulseek network.
/// </summary>
[Argument(default, "slsk-password")]
[EnvironmentVariable("SLSK_PASSWORD")]
[Description("password for the Soulseek network")]
[Required]
public string Password { get; private set; } = null;
/// <summary>
/// Gets the username for the Soulseek network.
/// </summary>
[Argument(default, "slsk-username")]
[EnvironmentVariable("SLSK_USERNAME")]
[Description("username for the Soulseek network")]
[Required]
public string Username { get; private set; } = null;
/// <summary>
/// Gets the port on which to listen for incoming connections.
/// </summary>
[Argument(default, "slsk-listen-port")]
[EnvironmentVariable("SLSK_LISTEN_PORT")]
[Description("port on which to listen for incoming connections")]
[Range(1024, 65535)]
public int ListenPort { get; private set; } = 50000;
/// <summary>
/// Gets the minimum diagnostic level.
/// </summary>
[Argument(default, "slsk-diag-level")]
[EnvironmentVariable("SLSK_DIAG_LEVEL")]
[Description("minimum diagnostic level (None, Warning, Info, Debug)")]
public DiagnosticLevel DiagnosticLevel { get; private set; } = DiagnosticLevel.Info;
/// <summary>
/// Gets options for the distributed network.
/// </summary>
[Validate]
public DistributedNetworkOptions DistributedNetwork { get; private set; } = new DistributedNetworkOptions();
/// <summary>
/// Gets connection options.
/// </summary>
[Validate]
public ConnectionOptions Connection { get; private set; } = new ConnectionOptions();
/// <summary>
/// Connection options.
/// </summary>
public class ConnectionOptions
{
/// <summary>
/// Gets connection timeout options.
/// </summary>
[Validate]
public TimeoutOptions Timeout { get; private set; } = new TimeoutOptions();
/// <summary>
/// Gets connection buffer options.
/// </summary>
[Validate]
public BufferOptions Buffer { get; private set; } = new BufferOptions();
/// <summary>
/// Gets connection proxy options.
/// </summary>
[Validate]
public ProxyOptions Proxy { get; private set; } = new ProxyOptions();
/// <summary>
/// Connection buffer options.
/// </summary>
public class BufferOptions
{
/// <summary>
/// Gets the connection read buffer size.
/// </summary>
[Argument(default, "slsk-read-buffer")]
[EnvironmentVariable("SLSK_READ_BUFFER")]
[Description("read buffer size for connections")]
[Range(1024, int.MaxValue)]
public int Read { get; private set; } = 16384;
/// <summary>
/// Gets the connection write buffer size.
/// </summary>
[Argument(default, "slsk-write-buffer")]
[EnvironmentVariable("SLSK_WRITE_BUFFER")]
[Description("write buffer size for connections")]
[Range(1024, int.MaxValue)]
public int Write { get; private set; } = 16384;
}
/// <summary>
/// Connection timeout options.
/// </summary>
public class TimeoutOptions
{
/// <summary>
/// Gets the connection timeout value, in milliseconds.
/// </summary>
[Argument(default, "slsk-connection-timeout")]
[EnvironmentVariable("SLSK_CONNECTION_TIMEOUT")]
[Description("connection timeout, in milliseconds")]
[Range(1000, int.MaxValue)]
public int Connect { get; private set; } = 10000;
/// <summary>
/// Gets the connection inactivity timeout, in milliseconds.
/// </summary>
[Argument(default, "slsk-inactivity-timeout")]
[EnvironmentVariable("SLSK_INACTIVITY_TIMEOUT")]
[Description("connection inactivity timeout, in milliseconds")]
[Range(1000, int.MaxValue)]
public int Inactivity { get; private set; } = 15000;
}
/// <summary>
/// Connection proxy options.
/// </summary>
public class ProxyOptions
{
/// <summary>
/// Gets the proxy address.
/// </summary>
[Argument(default, "slsk-proxy-address")]
[EnvironmentVariable("SLSK_PROXY_ADDRESS")]
[Description("connection proxy address")]
[StringLength(255, MinimumLength = 1)]
public string Address { get; private set; }
/// <summary>
/// Gets the proxy port.
/// </summary>
[Argument(default, "slsk-proxy-port")]
[EnvironmentVariable("SLSK_PROXY_PORT")]
[Description("connection proxy port")]
[Range(1, 65535)]
public int? Port { get; private set; }
/// <summary>
/// Gets the proxy username, if applicable.
/// </summary>
[Argument(default, "slsk-proxy-username")]
[EnvironmentVariable("SLSK_PROXY_USERNAME")]
[Description("connection proxy username")]
[StringLength(255, MinimumLength = 1)]
public string Username { get; private set; }
/// <summary>
/// Gets the proxy password, if applicable.
/// </summary>
[Argument(default, "slsk-proxy-password")]
[EnvironmentVariable("SLSK_PROXY_PASSWORD")]
[Description("connection proxy password")]
[StringLength(255, MinimumLength = 1)]
public string Password { get; private set; }
}
}
/// <summary>
/// Distributed network options.
/// </summary>
public class DistributedNetworkOptions
{
/// <summary>
/// Gets a value indicating whether the distributed network should be disabled.
/// </summary>
[Argument(default, "slsk-no-dnet")]
[EnvironmentVariable("SLSK_NO_DNET")]
[Description("disable the distributed network")]
public bool Disabled { get; private set; } = false;
/// <summary>
/// Gets the distributed child connection limit.
/// </summary>
[Argument(default, "slsk-dnet-children")]
[EnvironmentVariable("SLSK_DNET_CHILDREN")]
[Description("max number of distributed children")]
[Range(1, int.MaxValue)]
public int ChildLimit { get; private set; } = 25;
}
}
/// <summary>
/// Web server options.
/// </summary>
public class WebOptions
{
/// <summary>
/// Gets the HTTP listen port.
/// </summary>
[Argument('l', "http-port")]
[EnvironmentVariable("HTTP_PORT")]
[Description("HTTP listen port for web UI")]
[Range(1, 65535)]
public int Port { get; private set; } = 5000;
/// <summary>
/// Gets HTTPS options.
/// </summary>
[Validate]
public HttpsOptions Https { get; private set; } = new HttpsOptions();
/// <summary>
/// Gets the base url for web requests.
/// </summary>
[Argument(default, "url-base")]
[EnvironmentVariable("URL_BASE")]
[Description("base url for web requests")]
public string UrlBase { get; private set; } = "/";
/// <summary>
/// Gets the path to static web content.
/// </summary>
[Argument(default, "content-path")]
[EnvironmentVariable("CONTENT_PATH")]
[Description("path to static web content")]
[StringLength(255, MinimumLength = 1)]
public string ContentPath { get; private set; } = "wwwroot";
/// <summary>
/// Gets authentication options.
/// </summary>
[Validate]
public AuthenticationOptions Authentication { get; private set; } = new AuthenticationOptions();
/// <summary>
/// Authentication options.
/// </summary>
public class AuthenticationOptions
{
/// <summary>
/// Gets a value indicating whether authentication should be disabled.
/// </summary>
[Argument('x', "no-auth")]
[EnvironmentVariable("NO_AUTH")]
[Description("disable authentication for web requests")]
public bool Disable { get; private set; } = false;
/// <summary>
/// Gets the username for the web UI.
/// </summary>
[Argument('u', "username")]
[EnvironmentVariable("USERNAME")]
[Description("username for web UI")]
[StringLength(255, MinimumLength = 1)]
public string Username { get; private set; } = Program.AppName;
/// <summary>
/// Gets the password for the web UI.
/// </summary>
[Argument('p', "password")]
[EnvironmentVariable("PASSWORD")]
[Description("password for web UI")]
[StringLength(255, MinimumLength = 1)]
public string Password { get; private set; } = Program.AppName;
/// <summary>
/// Gets JWT options.
/// </summary>
[Validate]
public JwtOptions Jwt { get; private set; } = new JwtOptions();
/// <summary>
/// JWT options.
/// </summary>
public class JwtOptions
{
/// <summary>
/// Gets the key with which to sign JWTs.
/// </summary>
[Argument(default, "jwt-key")]
[EnvironmentVariable("JWT_KEY")]
[Description("JWT signing key")]
[StringLength(255, MinimumLength = 1)]
public string Key { get; private set; } = Guid.NewGuid().ToString();
/// <summary>
/// Gets the TTL for JWTs.
/// </summary>
[Argument(default, "jwt-ttl")]
[EnvironmentVariable("JWT_TTL")]
[Description("TTL for JWTs")]
[Range(3600, int.MaxValue)]
public int Ttl { get; private set; } = 604800000;
}
}
/// <summary>
/// HTTPS options.
/// </summary>
public class HttpsOptions
{
/// <summary>
/// Gets the HTTPS listen port.
/// </summary>
[Argument('L', "https-port")]
[EnvironmentVariable("HTTPS_PORT")]
[Description("HTTPS listen port for web UI")]
[Range(1, 65535)]
public int Port { get; private set; } = 5001;
/// <summary>
/// Gets a value indicating whether HTTP requests should be redirected to HTTPS.
/// </summary>
[Argument('f', "force-https")]
[EnvironmentVariable("HTTPS_FORCE")]
[Description("redirect HTTP to HTTPS")]
public bool Force { get; private set; } = false;
/// <summary>
/// Gets certificate options.
/// </summary>
[Validate]
public CertificateOptions Certificate { get; private set; } = new CertificateOptions();
/// <summary>
/// Certificate options.
/// </summary>
[X509Certificate]
public class CertificateOptions
{
/// <summary>
/// Gets the path to the the X509 certificate .pfx file.
/// </summary>
[Argument(default, "https-cert-pfx")]
[EnvironmentVariable("HTTPS_CERT_PFX")]
[Description("path to X509 certificate .pfx")]
[FileExists]
public string Pfx { get; private set; }
/// <summary>
/// Gets the password for the X509 certificate.
/// </summary>
[Argument(default, "https-cert-password")]
[EnvironmentVariable("HTTPS_CERT_PASSWORD")]
[Description("X509 certificate password")]
public string Password { get; private set; }
}
}
}
}
}
|