From 0b194f1922f1eedf0e4cf7d9dce59b47e7f5389a Mon Sep 17 00:00:00 2001 From: Graham Sutherland Date: Fri, 26 Jan 2024 22:28:14 +0000 Subject: Update IsBlacklisted to use the new GetBlacklistedCIDRs function. This ensures that blacklisted CIDRs from both the config file and the blacklist file are considered. --- src/slskd/Users/UserService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 5b15b671..324e3743 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -273,7 +273,7 @@ namespace slskd.Users return true; } - if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) + if (ipAddress is not null && blacklist.GetBlacklistedCIDRs().Any(range => range.Contains(ipAddress))) { return true; } @@ -410,4 +410,4 @@ namespace slskd.Users updateValueFactory: (key, user) => user with { Username = username, Status = status }); } } -} \ No newline at end of file +} -- cgit v1.2.3 From 522974041e4c07050d8cb13b8ca3e65f8dd8060a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Mar 2024 18:25:10 -0500 Subject: add todo --- src/slskd/Users/UserService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 324e3743..7a7de43a 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -273,7 +273,8 @@ namespace slskd.Users return true; } - if (ipAddress is not null && blacklist.GetBlacklistedCIDRs().Any(range => range.Contains(ipAddress))) + // todo: substitute this with a call to the managed blacklist + if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) { return true; } -- cgit v1.2.3 From 4e2386deebe1bc5ede1d51aa29c126fc0f694c90 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:14:36 -0500 Subject: add docs to events --- src/slskd/Users/UserService.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 7a7de43a..757d9333 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -91,8 +91,16 @@ namespace slskd.Users _ = GetStatisticsAsync(userStatus.Username); }; - Client.Connected += (_, _) => Reset(); + // it's important for us to force a reconfig at login to discard any users that were previously tracked + // specific scenario being; up and running for some time, offline for some time (days?), reconnect, are users still online? have stats changed? + // to avoid needing to go through and exhaustively check each user in the tracking dictionary, just delete it and let it be built back up + // any user we are downloading from will be tracked again when pending downloads are re-requested Client.LoggedIn += (_, _) => Configure(OptionsMonitor.CurrentValue, force: true); + + // working hand-in-hand with the forced reconfig on login, reset clears everything upon connect; clearing the way + // for the reconfig at login to rebuild it. i think. yeah, that sounds right. we don't ever want Configure() to reset anything + // so the connect is distinctly different from reconfig at login. + Client.Connected += (_, _) => Reset(); Client.PrivilegedUserListReceived += (_, list) => Client_PrivilegedUserListReceived(list); Configure(OptionsMonitor.CurrentValue); -- cgit v1.2.3 From 0691977ea44fdccf46d752560b669cddf72e080b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:15:09 -0500 Subject: add a check for Blacklist --- src/slskd/Users/UserService.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 757d9333..2f718c7c 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -281,12 +281,20 @@ namespace slskd.Users return true; } - // todo: substitute this with a call to the managed blacklist + // check the user-curated list of blacklisted CIDRs that exists along with the list of + // blacklisted usernames. these CIDRs should be one-offs and would not be expected to appear in a + // blacklist supplied by a third party (but might?) if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) { return true; } + // check the managed blacklist loaded from a third party blacklist file + if (ipAddress is not null && Blacklist.Contains(ipAddress)) + { + return true; + } + return false; } -- cgit v1.2.3 From 0cf8603c69176f194bb61f764e2f66a3c40e4e0f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:15:50 -0500 Subject: invert reconfig expression --- src/slskd/Users/UserService.cs | 58 ++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 2f718c7c..4b702933 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -364,45 +364,43 @@ namespace slskd.Users { var optionsHash = Compute.Sha1Hash(options.Groups.UserDefined.ToJson()); - if (optionsHash == LastOptionsHash && !force) + if (optionsHash != LastOptionsHash || force) { - return; - } - - // get a list of tracked names that haven't been explicitly added to any group, including those that were previlously - // configured but have now been removed - var usernamesBeforeUpdate = UserDictionary.Keys.ToList(); - var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); - var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); - - // clear the configured group for anyone that was removed from config, or that was added transiently - foreach (var username in usernamesRemoved) - { - UserDictionary.AddOrUpdate( - key: username, - addValue: new User() { Username = username }, - updateValueFactory: (key, user) => user with { Username = username, Group = null }); - } - - // sort by priority, descending. this will cause the highest priority group for the user to be persisted when the - // operation is complete. - foreach (var group in options.Groups.UserDefined.OrderByDescending(kvp => kvp.Value.Upload.Priority)) - { - foreach (var username in group.Value.Members) + // get a list of tracked names that haven't been explicitly added to any group, including those that were previlously + // configured but have now been removed + var usernamesBeforeUpdate = UserDictionary.Keys.ToList(); + var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); + var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); + + // clear the configured group for anyone that was removed from config, or that was added transiently + foreach (var username in usernamesRemoved) { UserDictionary.AddOrUpdate( key: username, - addValue: new User() { Username = username, Group = group.Key }, - updateValueFactory: (key, user) => user with { Username = username, Group = group.Key }); + addValue: new User() { Username = username }, + updateValueFactory: (key, user) => user with { Username = username, Group = null }); + } - if (Client.State.HasFlag(SoulseekClientStates.Connected) && Client.State.HasFlag(SoulseekClientStates.LoggedIn)) + // sort by priority, descending. this will cause the highest priority group for the user to be persisted when the + // operation is complete. + foreach (var group in options.Groups.UserDefined.OrderByDescending(kvp => kvp.Value.Upload.Priority)) + { + foreach (var username in group.Value.Members) { - _ = WatchAsync(username); + UserDictionary.AddOrUpdate( + key: username, + addValue: new User() { Username = username, Group = group.Key }, + updateValueFactory: (key, user) => user with { Username = username, Group = group.Key }); + + if (Client.State.HasFlag(SoulseekClientStates.Connected) && Client.State.HasFlag(SoulseekClientStates.LoggedIn)) + { + _ = WatchAsync(username); + } } } - } - LastOptionsHash = optionsHash; + LastOptionsHash = optionsHash; + } } private void Reset() -- cgit v1.2.3 From 23f82bd69dfb80a517cf5d324f5e972456c20606 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:28:27 -0500 Subject: blacklist reconfig logic --- src/slskd/Users/UserService.cs | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 4b702933..61ad66a6 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -118,8 +118,10 @@ namespace slskd.Users private ISoulseekClient Client { get; } private string LastOptionsHash { get; set; } + private string LastBlacklistOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } + private Blacklist Blacklist { get; } = new Blacklist(); /// /// Gets or sets the internal cache of User data. @@ -401,6 +403,47 @@ namespace slskd.Users LastOptionsHash = optionsHash; } + + var blacklistOptionsHash = Compute.Sha1Hash(options.Blacklist.ToJson()); + + // there's no forced re-config of the blacklist; either the config changed or it didn't. + // if the underlying file changed, users can toggle the blacklist off and on or restart + // the application. these sorts of blacklists should be relatively static (i think) + if (blacklistOptionsHash != LastBlacklistOptionsHash) + { + Log.Debug("Blacklist options changed: {JSON}", options.Blacklist.ToJson()); + + if (!options.Blacklist.Enabled) + { + Log.Debug("Blacklist disabled; clearing contents"); + Blacklist.Clear(); + Log.Information("Blacklist disabled"); + } + else + { + // the option validation logic should have ensured the file format could be auto-detected and that the file + // contained no formatting errors. this should only fail on transient I/O errors. + _ = Task.Run(() => Blacklist.Load(options.Blacklist.File, BlacklistFormat.AutoDetect)) + .ContinueWith(task => + { + // if the task faulted, the load of the file failed, and the user isn't getting the + // intended protection from the blacklist. in this case we can: + // 1) log an error and continue, potentially against the desires of the user + // 2) kill the application and force the user to deal with the cause + // a user taking advantage of the blacklist feature would absolutely want to know if it wasn't working, + // so we're taking door #2. + if (task.IsFaulted) + { + Log.Fatal(task.Exception, "Fatal error loading blacklist from file {File}: {Message}", options.Blacklist.File, task.Exception?.Message); + Program.Exit(1); + } + else + { + Log.Information("Blacklist updated with {Count} CIDRs from file {File}", Blacklist.Count, options.Blacklist.File); + } + }); + } + } } private void Reset() -- cgit v1.2.3 From 5013879d30e0091decbadfc38360117873238518 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 27 Apr 2024 19:06:21 -0500 Subject: capture blacklist option hash --- src/slskd/Users/UserService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/slskd/Users/UserService.cs') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 61ad66a6..e278ccc7 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -443,6 +443,8 @@ namespace slskd.Users } }); } + + LastBlacklistOptionsHash = blacklistOptionsHash; } } -- cgit v1.2.3