@@ -42,11 +42,6 @@ public class IPBanFilter : IIPBanFilter
42
42
/// </summary>
43
43
public const char ItemDelimiter = ',' ;
44
44
45
- /// <summary>
46
- /// Item pieces delimiters including the legacy ? and the current |
47
- /// </summary>
48
- public static readonly char [ ] ItemPiecesDelimitersLegacy = [ '?' , '|' ] ;
49
-
50
45
/// <summary>
51
46
/// Item pieces delimiter |
52
47
/// </summary>
@@ -57,6 +52,11 @@ public class IPBanFilter : IIPBanFilter
57
52
/// </summary>
58
53
public const char SubEntryDelimiter = ';' ;
59
54
55
+ /// <summary>
56
+ /// Legacy item pieces delimiter ?
57
+ /// </summary>
58
+ private const char itemPiecesDelimiterLegacy = '?' ;
59
+
60
60
private static readonly HashSet < string > ignoreListEntries =
61
61
[
62
62
"0.0.0.0" ,
@@ -146,13 +146,7 @@ public IPBanFilter(string value, string regexValue, IHttpRequestMaker httpReques
146
146
// | can be used as a sub delimiter instead of ? mark
147
147
foreach ( string entry in value . Split ( ItemDelimiter , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
148
148
{
149
- string entryWithoutComment = entry ;
150
- int pos = entryWithoutComment . IndexOfAny ( ItemPiecesDelimitersLegacy ) ;
151
- if ( pos >= 0 )
152
- {
153
- entryWithoutComment = entryWithoutComment [ ..pos ] ;
154
- }
155
- entryWithoutComment = entryWithoutComment . Trim ( ) ;
149
+ string entryWithoutComment = GetEntryWithoutComment ( entry ) ;
156
150
157
151
// sub entries (multiple ip addresses) are delimited by semi-colon
158
152
foreach ( string subEntry in entryWithoutComment . Split ( SubEntryDelimiter , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
@@ -338,6 +332,63 @@ public bool IsFiltered(IPAddressRange range)
338
332
return false ;
339
333
}
340
334
335
+ /// <summary>
336
+ /// Get entry without comment
337
+ /// </summary>
338
+ /// <param name="entry">Entry</param>
339
+ /// <returns>Entry without comment</returns>
340
+ public static string GetEntryWithoutComment ( string entry )
341
+ {
342
+ string entryWithoutComment = entry ;
343
+ int pos = entryWithoutComment . IndexOf ( ItemPiecesDelimiter ) ;
344
+
345
+ // if using new delimiter, remove it and everything after
346
+ if ( pos >= 0 )
347
+ {
348
+ entryWithoutComment = entryWithoutComment [ ..pos ] ;
349
+ }
350
+ // if using two or more of old delimiter, remove it and everything after
351
+ else if ( entryWithoutComment . Count ( e => e == itemPiecesDelimiterLegacy ) > 1 )
352
+ {
353
+ pos = entryWithoutComment . IndexOf ( itemPiecesDelimiterLegacy ) ;
354
+ entryWithoutComment = entryWithoutComment [ ..pos ] ;
355
+ }
356
+ entryWithoutComment = entryWithoutComment . Trim ( ) ;
357
+
358
+ return entryWithoutComment ;
359
+ }
360
+
361
+ /// <summary>
362
+ /// Split entry on new delimiter. If it doesn't exist, legacy delimiter is used.
363
+ /// </summary>
364
+ /// <param name="entry">Entry</param>
365
+ /// <returns>Pieces (always at least 3 items)</returns>
366
+ public static string [ ] SplitEntry ( string entry )
367
+ {
368
+ entry ??= string . Empty ;
369
+ string [ ] pieces = [ ] ;
370
+
371
+ // split on new delimiter if we have it
372
+ if ( entry . Contains ( ItemPiecesDelimiter ) )
373
+ {
374
+ pieces = entry . Split ( ItemPiecesDelimiter , StringSplitOptions . TrimEntries ) ;
375
+ }
376
+
377
+ // split on legacy delimiter if there's two or more
378
+ else if ( entry . Count ( e => e == itemPiecesDelimiterLegacy ) > 1 )
379
+ {
380
+ pieces = entry . Split ( itemPiecesDelimiterLegacy , StringSplitOptions . TrimEntries ) ;
381
+ }
382
+
383
+ // the split should have at least 3 pieces, if not, return the original entry and two empty ones
384
+ if ( pieces . Length < 3 )
385
+ {
386
+ return [ entry , string . Empty , string . Empty ] ;
387
+ }
388
+
389
+ return pieces ;
390
+ }
391
+
341
392
/// <summary>
342
393
/// Get all ip address ranges in the filter
343
394
/// </summary>
0 commit comments