From 8815f5042ca19cb082e966497d088ca9b3d79f28 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 20 Apr 2024 17:42:49 -0500 Subject: make format detection public --- src/slskd/Core/Blacklist.cs | 107 +++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Blacklist.cs b/src/slskd/Core/Blacklist.cs index d7a680aa..dc8a9162 100644 --- a/src/slskd/Core/Blacklist.cs +++ b/src/slskd/Core/Blacklist.cs @@ -82,6 +82,72 @@ public class Blacklist private ILogger Log { get; set; } = Serilog.Log.ForContext(); private ConcurrentDictionary Cache { get; set; } = new(); + /// + /// Examines the contents of the specified and attempts to determine the format. + /// + /// The fully qualified path to the file to examine. + /// The detected format. + /// Thrown if the specified filename can't be accessed. + /// Thrown if the format can't be determined. + public static async Task DetectFormat(string filename) + { + using var reader = new StreamReader(filename, options: new FileStreamOptions + { + Access = FileAccess.Read, + Mode = FileMode.Open, + Share = FileShare.Read, + }); + + string line = default; + + // read the first non-empty, non-commented line from the file + while ((line = await reader.ReadLineAsync()) != null) + { + if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#')) + { + continue; + } + + try + { + // CIDR format: 1.2.4.0/24 + if (IPAddressRange.TryParse(line, out _)) + { + return BlacklistFormat.CIDR; + } + } + catch + { + } + + try + { + // P2P format: China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255 + if (IPAddressRange.TryParse(line.Split(':')[1], out _)) + { + return BlacklistFormat.P2P; + } + } + catch + { + } + + try + { + // DAT format: 001.002.004.000 - 001.002.004.255 , 000 , China Internet Information Center (CNNIC) + if (IPAddressRange.TryParse(line.Split(",")[0], out _)) + { + return BlacklistFormat.DAT; + } + } + catch + { + } + } + + throw new FormatException($"Failed to auto-detect blacklist format. Only CIDR, P2P and DAT formats are supported"); + } + /// /// Loads the contents of the specified into the Blacklist. /// @@ -227,47 +293,6 @@ public class Blacklist return false; } - private async Task AutoDetectBlacklistFormat(string filename) - { - using var reader = new StreamReader(filename, options: new FileStreamOptions - { - Access = FileAccess.Read, - Mode = FileMode.Open, - Share = FileShare.Read, - }); - - string line = default; - - // read the first non-empty, non-commented line from the file - while ((line = await reader.ReadLineAsync()) != null) - { - if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#')) - { - continue; - } - - // CIDR format: 1.2.4.0/24 - if (IPAddressRange.TryParse(line, out _)) - { - return BlacklistFormat.CIDR; - } - - // P2P format: China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255 - if (IPAddressRange.TryParse(line.Split(':')[1], out _)) - { - return BlacklistFormat.P2P; - } - - // DAT format: 001.002.004.000 - 001.002.004.255 , 000 , China Internet Information Center (CNNIC) - if (IPAddressRange.TryParse(line.Split(",")[0], out _)) - { - return BlacklistFormat.DAT; - } - } - - throw new FormatException($"Failed to auto-detect blacklist format. Only CIDR, P2P and DAT formats are supported"); - } - private uint ToUint32(IPAddress ip) { byte[] bytes = ip.GetAddressBytes(); -- cgit v1.2.3