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') 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