From 4876d4a5f024e2223312e95999f9be12bd8511fc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 20 Jan 2021 22:26:31 -0600 Subject: wip --- src/slskd/Options.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/slskd/Program.cs | 15 ++++++++------- src/slskd/Roles.cs | 9 +++++++++ src/slskd/Startup.cs | 34 +++++++++++++++++++++++++++++---- src/slskd/config.yml | 25 ++++++++++++++++++++++++ src/slskd/slskd.csproj | 10 ++++++++++ 6 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 src/slskd/Options.cs create mode 100644 src/slskd/Roles.cs create mode 100644 src/slskd/config.yml diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs new file mode 100644 index 00000000..f02f4d49 --- /dev/null +++ b/src/slskd/Options.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; + +namespace slskd +{ + public class Options + { + public class slskd + { + public Soulseek Soulseek { get; set; } + public IEnumerable Users { get; set; } + public IEnumerable ApiKeys { get; set; } + } + + public class Soulseek + { + public SoulseekCredentials Credentials { get; set; } + public SoulseekDistributedNetwork DistributedNetwork { get; set; } + + [Range(1024, 65535)] + public int ListenPort { get; set; } + + public class SoulseekCredentials + { + public string Username { get; set; } + public string Password { get; set; } + } + + public class SoulseekDistributedNetwork + { + public bool Enable { get; set; } + public int ChildLimit { get; set; } + } + } + + public class User + { + public string Username { get; set; } + public string PasswordHash { get; set; } + public Roles Role { get; set; } + } + + public class ApiKey + { + public string Key { get; set; } + public Roles Role { get; set; } + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index dc3b7c05..1b08b918 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -13,6 +13,7 @@ using System.Reflection; using Utility.CommandLine; using Utility.EnvironmentVariables; + using Utility.Extensions.Configuration.Yaml; public class Program { @@ -93,7 +94,7 @@ { if (!DisableLogo) { - PrintBanner(Version); + PrintLogo(Version); } PrintHelp(); @@ -155,7 +156,7 @@ if (!DisableLogo) { - PrintBanner(Version); + PrintLogo(Version); } var logger = Log.ForContext(); @@ -209,12 +210,12 @@ .ConfigureAppConfiguration((hostingContext, config) => { config.AddEnvironmentVariables(prefix: "SLSK_"); - config.AddJsonFile("config.json", optional: true, reloadOnChange: false); + config.AddYamlFile("config.yml", optional: true, reloadOnChange: true); }) .UseSerilog() .UseStartup(); - public static void PrintBanner(string version) + private static void PrintLogo(string version) { var padding = 56 - version.Length; var paddingLeft = padding / 2; @@ -237,13 +238,13 @@ Console.WriteLine(banner); } - public static void PrintHelp() + private static void PrintHelp() { PrintArguments(); PrintEnvironmentVariables(); } - public static void PrintArguments() + private static void PrintArguments() { static string GetLongName(ArgumentInfo info) => info.Property.PropertyType == typeof(bool) ? info.LongName : $"{info.LongName} <{info.Property.PropertyType.Name.ToLowerInvariant()}>"; @@ -267,7 +268,7 @@ } } - public static void PrintEnvironmentVariables() + private static void PrintEnvironmentVariables() { static string GetEnvironmentVariableName(ArgumentInfo info, bool includeType = false) => info.Property.CustomAttributes diff --git a/src/slskd/Roles.cs b/src/slskd/Roles.cs new file mode 100644 index 00000000..5ddee6d8 --- /dev/null +++ b/src/slskd/Roles.cs @@ -0,0 +1,9 @@ +namespace slskd +{ + public enum Roles + { + ReadOnly = 0, + User = 1, + Administrator = 2 + } +} diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 37a3c7cd..9dd9f52d 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -32,6 +32,7 @@ using Microsoft.Extensions.Logging; using Prometheus; using Prometheus.SystemMetrics; + using Microsoft.Extensions.Options; public class Startup { @@ -101,6 +102,20 @@ public void ConfigureServices(IServiceCollection services) { + services.AddOptions() + .Bind(Configuration.GetSection("soulseek")); + + services.PostConfigure>(slskd => + { + Console.WriteLine($"config updated"); + Console.WriteLine(JsonSerializer.Serialize(slskd)); + }); + + services.AddOptions>().Bind(Configuration.GetSection("sparse")); + services.PostConfigure>(s => Console.WriteLine(JsonSerializer.Serialize(s))); + + services.AddOptions>().Bind(Configuration.GetSection("users")); + services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); if (!Program.DisableAuthentication) @@ -184,6 +199,9 @@ ITransferTracker tracker, IBrowseTracker browseTracker, IConversationTracker conversationTracker, + IOptionsMonitor optionsAccessor, + IOptionsMonitor> users, + IOptionsMonitor> sparse, IRoomTracker roomTracker) { if (!env.IsDevelopment()) @@ -191,6 +209,14 @@ app.UseHsts(); } + optionsAccessor.OnChange(options => + { + Console.WriteLine(JsonSerializer.Serialize(optionsAccessor)); + }); + + sparse.OnChange(v => Console.WriteLine(JsonSerializer.Serialize(v))); + users.OnChange(users => Console.WriteLine(JsonSerializer.Serialize(users))); + app.UseCors("AllowAll"); app.UsePathBase(BasePath); @@ -395,10 +421,10 @@ } }; - Task.Run(async () => - { - await Client.ConnectAsync(Username, Password); - }).GetAwaiter().GetResult(); + //Task.Run(async () => + //{ + // await Client.ConnectAsync(Username, Password); + //}).GetAwaiter().GetResult(); logger.LogInformation("Connected and logged in as {Username}", Username); } diff --git a/src/slskd/config.yml b/src/slskd/config.yml new file mode 100644 index 00000000..266ab9da --- /dev/null +++ b/src/slskd/config.yml @@ -0,0 +1,25 @@ +soulseek: + credentials: + username: foo + password: bar + listenPort: 500 + distributedNetwork: + enable: true + childLimit: 100 +users: +- username: admin + passwordHash: abc + role: Administrators +- username: user + passwordHash: xyz + role: User +apiKeys: +- key: '1234567890' + role: ReadOnly +sparse: + - ~ + - 2nd entry + - + - 4th entry + - Null + - tests \ No newline at end of file diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 7377ea60..4e1d9994 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -22,6 +22,14 @@ + + + + + + + + @@ -43,6 +51,8 @@ + + -- cgit v1.2.3