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') 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 6abd8142a4b526b562bf1fde2c9fde2164a820cb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Mar 2024 18:23:32 -0500 Subject: stub Blacklist --- src/slskd/Users/Blacklist.cs | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/slskd/Users/Blacklist.cs (limited to 'src/slskd/Users') diff --git a/src/slskd/Users/Blacklist.cs b/src/slskd/Users/Blacklist.cs new file mode 100644 index 00000000..de8b567b --- /dev/null +++ b/src/slskd/Users/Blacklist.cs @@ -0,0 +1,73 @@ +// +// 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.Users; + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net; + +/// +/// A managed blacklist for CIDRs. +/// +public class Blacklist +{ + private Dictionary Cache { get; } + + public void Load(string filename) + { + // todo: + // _stream_ contents of file into a ConcurrentDictionary> + // load that dictionary into the Cache + // enjoy + } + + public bool Contains(IPAddress ip) + { + int first = ip.GetAddressBytes()[0]; + + if (!Cache.TryGetValue(first, out (uint, uint)[] cidrs)) + { + return false; + } + + var ipAsUint32 = ToUint32(ip); + + foreach (var cidr in cidrs) + { + if (ipAsUint32 >= cidr.Item1 && ipAsUint32 <= cidr.Item2) + { + return true; + } + } + + return false; + } + + private uint ToUint32(IPAddress ip) + { + byte[] bytes = ip.GetAddressBytes(); + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(bytes); + } + + return BitConverter.ToUInt32(bytes, 0); + } +} \ No newline at end of file -- cgit v1.2.3 From d275223aa4afc588bd0731363c58fa3583b8ff36 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Mar 2024 18:24:44 -0500 Subject: move blacklist to core --- src/slskd/Core/Blacklist.cs | 73 ++++++++++++++++++++++++++++++++++++++++++++ src/slskd/Users/Blacklist.cs | 73 -------------------------------------------- 2 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 src/slskd/Core/Blacklist.cs delete mode 100644 src/slskd/Users/Blacklist.cs (limited to 'src/slskd/Users') diff --git a/src/slskd/Core/Blacklist.cs b/src/slskd/Core/Blacklist.cs new file mode 100644 index 00000000..86232d73 --- /dev/null +++ b/src/slskd/Core/Blacklist.cs @@ -0,0 +1,73 @@ +// +// 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; + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net; + +/// +/// A managed blacklist for CIDRs. +/// +public class Blacklist +{ + private Dictionary Cache { get; } + + public void Load(string filename) + { + // todo: + // _stream_ contents of file into a ConcurrentDictionary> + // load that dictionary into the Cache + // enjoy + } + + public bool Contains(IPAddress ip) + { + int first = ip.GetAddressBytes()[0]; + + if (!Cache.TryGetValue(first, out (uint, uint)[] cidrs)) + { + return false; + } + + var ipAsUint32 = ToUint32(ip); + + foreach (var cidr in cidrs) + { + if (ipAsUint32 >= cidr.Item1 && ipAsUint32 <= cidr.Item2) + { + return true; + } + } + + return false; + } + + private uint ToUint32(IPAddress ip) + { + byte[] bytes = ip.GetAddressBytes(); + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(bytes); + } + + return BitConverter.ToUInt32(bytes, 0); + } +} \ No newline at end of file diff --git a/src/slskd/Users/Blacklist.cs b/src/slskd/Users/Blacklist.cs deleted file mode 100644 index de8b567b..00000000 --- a/src/slskd/Users/Blacklist.cs +++ /dev/null @@ -1,73 +0,0 @@ -// -// 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.Users; - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Net; - -/// -/// A managed blacklist for CIDRs. -/// -public class Blacklist -{ - private Dictionary Cache { get; } - - public void Load(string filename) - { - // todo: - // _stream_ contents of file into a ConcurrentDictionary> - // load that dictionary into the Cache - // enjoy - } - - public bool Contains(IPAddress ip) - { - int first = ip.GetAddressBytes()[0]; - - if (!Cache.TryGetValue(first, out (uint, uint)[] cidrs)) - { - return false; - } - - var ipAsUint32 = ToUint32(ip); - - foreach (var cidr in cidrs) - { - if (ipAsUint32 >= cidr.Item1 && ipAsUint32 <= cidr.Item2) - { - return true; - } - } - - return false; - } - - private uint ToUint32(IPAddress ip) - { - byte[] bytes = ip.GetAddressBytes(); - - if (BitConverter.IsLittleEndian) - { - Array.Reverse(bytes); - } - - return BitConverter.ToUInt32(bytes, 0); - } -} \ 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') 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') 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') 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') 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') 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') 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