From dd3e043d2ff855ab08106b3de5b432e80944703a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 21:10:49 -0500 Subject: don't verify app path --- src/slskd/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f7d3fd66..2264a59f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -55,7 +55,7 @@ namespace slskd /// /// The default application data directory. /// - public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppName); + public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName); /// /// The default configuration filename. -- cgit v1.2.3 From 9ab4e8ba6f80ffbfe38a0eea7013db4ac2ee459a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 21:11:21 -0500 Subject: move option dump for debugging --- src/slskd/Program.cs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 2264a59f..de8f27ba 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -207,6 +207,12 @@ namespace slskd Console.WriteLine($"Invalid HTTPs certificate: {certResult}"); return; } + + if (Options.Debug) + { + Console.WriteLine("Configuration:"); + Console.WriteLine(Configuration.GetDebugView()); + } } catch (Exception ex) { -- cgit v1.2.3 From 1dc218c100b454209a2fc000bf1cb869e326d1ee Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 21:11:49 -0500 Subject: bail if app directory doesn't exist --- src/slskd/Program.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index de8f27ba..4f44a82e 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -232,15 +232,11 @@ namespace slskd catch (Exception ex) { Console.WriteLine($"App directory {Options.Directories.App} does not exist, and could not be created: {ex.Message}"); + return; } try { - if (!Directory.Exists(Options.Directories.App)) - { - Directory.CreateDirectory(Options.Directories.App); - } - var probe = Path.Combine(Options.Directories.App, "probe"); File.WriteAllText(Path.Combine(Options.Directories.App, "probe"), string.Empty); File.Delete(probe); -- cgit v1.2.3 From 7afd2709e12477aa631879b66398368166802d36 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 21:12:04 -0500 Subject: simplify logger setup --- src/slskd/Program.cs | 76 +++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4f44a82e..d0e73c4e 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -252,59 +252,27 @@ namespace slskd PrintLogo(Version); } - if (Options.Debug) - { - Console.WriteLine("Configuration:"); - Console.WriteLine(Configuration.GetDebugView()); - - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Debug() - .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) - .MinimumLevel.Override("slskd.API.Authentication.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: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") - .WriteTo.Async(config => - config.File( - Path.Combine(AppContext.BaseDirectory, "logs", $"{AppName}-.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.API.Authentication.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( - Path.Combine(AppContext.BaseDirectory, "logs", $"{AppName}-.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(); - } + Log.Logger = (Options.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("slskd.API.Authentication.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: (Options.Debug ? "[{SourceContext}] [{SoulseekContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") + .WriteTo.Async(config => + config.File( + Path.Combine(AppContext.BaseDirectory, "logs", $"{AppName}-.log"), + outputTemplate: (Options.Debug ? "[{SourceContext}] " : string.Empty) + "[{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(typeof(Program)); -- cgit v1.2.3 From 675fbc147bdb17a51218392696a0fd7065e7ab17 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 22:14:45 -0500 Subject: add X509CertificateAttribute --- .../Common/Validation/X509CertificateAttribute.cs | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/slskd/Common/Validation/X509CertificateAttribute.cs (limited to 'src') diff --git a/src/slskd/Common/Validation/X509CertificateAttribute.cs b/src/slskd/Common/Validation/X509CertificateAttribute.cs new file mode 100644 index 00000000..db122930 --- /dev/null +++ b/src/slskd/Common/Validation/X509CertificateAttribute.cs @@ -0,0 +1,26 @@ +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using slskd.Common.Cryptography; + using static slskd.Options.WebOptions.HttpsOptions; + + public class X509CertificateAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value != null) + { + var cert = (CertificateOptions)value; + + System.Console.WriteLine($"pass: {cert.Password} {string.IsNullOrEmpty(cert.Password)}"); + + if (!string.IsNullOrEmpty(cert.Pfx) && !X509.TryValidate(cert.Pfx, cert.Password, out var certResult)) + { + return new ValidationResult($"Invalid HTTPs certificate: {certResult}"); + } + } + + return ValidationResult.Success; + } + } +} \ No newline at end of file -- cgit v1.2.3 From a6aabcf7fbd2241de75e7db7a4caa6d0b61d4678 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 22:24:47 -0500 Subject: validate X509 certificate --- src/slskd/Options.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index efd689d5..bef9b962 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -521,6 +521,7 @@ namespace slskd /// /// Certificate options. /// + [X509Certificate] public class CertificateOptions { /// -- cgit v1.2.3 From 95df56cab13a1b30800c7514dcd07c55ebd8f587 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 19 May 2021 22:25:27 -0500 Subject: simplify options validation, verify directories --- src/slskd/Program.cs | 69 +++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d0e73c4e..8869f441 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -183,7 +183,7 @@ namespace slskd return; } - // the application isn't bein run in command mode. load all configuration values and proceed + // the application isn't being run in command mode. load all configuration values and proceed // with bootstrapping. try { @@ -199,20 +199,6 @@ namespace slskd 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; - } - - if (Options.Debug) - { - Console.WriteLine("Configuration:"); - Console.WriteLine(Configuration.GetDebugView()); - } } catch (Exception ex) { @@ -220,30 +206,21 @@ namespace slskd return; } - // ensure the application directory exists and is writeable. the most comprehensive way to test this is to try - // to write a file to it. - try + if (Options.Debug) { - if (!Directory.Exists(Options.Directories.App)) - { - Directory.CreateDirectory(Options.Directories.App); - } - } - catch (Exception ex) - { - Console.WriteLine($"App directory {Options.Directories.App} does not exist, and could not be created: {ex.Message}"); - return; + Console.WriteLine($"Configuration:\n{Configuration.GetDebugView()}"); } try { - var probe = Path.Combine(Options.Directories.App, "probe"); - File.WriteAllText(Path.Combine(Options.Directories.App, "probe"), string.Empty); - File.Delete(probe); + VerifyOrCreateDirectory(Options.Directories.App); + VerifyOrCreateDirectory(Options.Directories.Incomplete); + VerifyOrCreateDirectory(Options.Directories.Downloads); + VerifyOrCreateDirectory(Options.Directories.Shared, verifyWriteable: false); } catch (Exception ex) { - Console.WriteLine($"App directory {Options.Directories.App} is not writeable: {ex.Message}"); + Console.WriteLine($"Filesystem exception: {ex.Message}"); return; } @@ -492,5 +469,35 @@ namespace slskd Console.WriteLine(banner); } + + private static void VerifyOrCreateDirectory(string directory, bool verifyWriteable = true) + { + try + { + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + } + catch (Exception ex) + { + throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex); + } + + if (verifyWriteable) + { + try + { + var file = Guid.NewGuid().ToString(); + var probe = Path.Combine(directory, file); + File.WriteAllText(probe, string.Empty); + File.Delete(probe); + } + catch (Exception ex) + { + throw new IOException($"Directory {directory} is not writeable: {ex.Message}", ex); + } + } + } } } \ No newline at end of file -- cgit v1.2.3 From cb11b625b7ad9fa9d1d2221cdda0b97476c2fd9c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 20 May 2021 07:36:36 -0500 Subject: remove log --- .../Common/Validation/X509CertificateAttribute.cs | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Validation/X509CertificateAttribute.cs b/src/slskd/Common/Validation/X509CertificateAttribute.cs index db122930..1f0849f9 100644 --- a/src/slskd/Common/Validation/X509CertificateAttribute.cs +++ b/src/slskd/Common/Validation/X509CertificateAttribute.cs @@ -1,9 +1,29 @@ -namespace slskd.Validation +// +// 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.Validation { using System.ComponentModel.DataAnnotations; using slskd.Common.Cryptography; using static slskd.Options.WebOptions.HttpsOptions; + /// + /// Validates X509 certificate parameters specified through . + /// public class X509CertificateAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) @@ -12,8 +32,6 @@ { var cert = (CertificateOptions)value; - System.Console.WriteLine($"pass: {cert.Password} {string.IsNullOrEmpty(cert.Password)}"); - if (!string.IsNullOrEmpty(cert.Pfx) && !X509.TryValidate(cert.Pfx, cert.Password, out var certResult)) { return new ValidationResult($"Invalid HTTPs certificate: {certResult}"); -- cgit v1.2.3 From 73a54cb7d47ba76274aac8908126cccb9fa3cb6b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 20 May 2021 07:50:47 -0500 Subject: randomize logos --- src/slskd/Program.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 8869f441..85a56e0f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -224,11 +224,6 @@ namespace slskd return; } - if (!Options.NoLogo) - { - PrintLogo(Version); - } - Log.Logger = (Options.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("slskd.API.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Information) @@ -253,6 +248,11 @@ namespace slskd var logger = Log.ForContext(typeof(Program)); + if (!Options.NoLogo) + { + PrintLogo(Version); + } + if (ConfigurationFile != DefaultConfigurationFile && !File.Exists(ConfigurationFile)) { logger.Warning($"Specified configuration file '{ConfigurationFile}' could not be found and was not loaded."); @@ -455,11 +455,24 @@ namespace slskd var centeredVersion = new string(' ', paddingLeft) + version + new string(' ', paddingRight); - var banner = @$" + var logos = new[] + { + $@" ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ ▄▄▄▄▄▄▄ █ █ ▄▄▄▄▄▄▄ █ █▄▄▄ ▄▄▄█ █ █__ --█ █ █ █__ --█ █ ◄█ █ - █ - █▄▄▄▄▄█ █▄▄█ █▄▄▄▄▄█ █▄▄█▄▄█ █▄▄▄▄▄█ + █▄▄▄▄▄█ █▄▄█ █▄▄▄▄▄█ █▄▄█▄▄█ █▄▄▄▄▄█", + @$" + ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ + ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ + █__ --█ █__ --█ ◄█ - █ + █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█", + }; + + var logo = logos[new Random().Next(0, logos.Length)]; + + var banner = @$" +{logo} ╒════════════════════════════════════════════════════════╕ │ GNU AFFERO GENERAL PUBLIC LICENSE │ │ https://slskd.org │ -- cgit v1.2.3