1- // __ _ __ __ ___ __ ___ ___
1+ // __ _ __ __ ___ __ ___ ___
22// | \| |/__\ /' _/ / _//__\| _ \ __|
33// | | ' | \/ |`._`.| \_| \/ | v / _|
44// |_|\__|\__/ |___/ \__/\__/|_|_\___|
77using System ;
88using System . Collections . Generic ;
99using System . Net ;
10- using DotNetty . Transport . Channels ;
1110using Microsoft . Extensions . Logging ;
1211using NodaTime ;
1312using NosCore . Networking . Resource ;
@@ -18,19 +17,14 @@ namespace NosCore.Networking.Encoding.Filter
1817 /// <summary>
1918 /// Filters spam requests by rate-limiting connections from the same IP address.
2019 /// </summary>
21- public class SpamRequestFilter : RequestFilter
20+ public class SpamRequestFilter : IRequestFilter
2221 {
2322 private readonly Dictionary < EndPoint , Instant > _connectionsByIp = new ( ) ;
2423 private readonly TimeSpan _timeBetweenConnection = TimeSpan . FromMilliseconds ( 1000 ) ;
2524 private readonly IClock _clock ;
2625 private readonly ILogger < SpamRequestFilter > _logger ;
2726 private readonly ILogLanguageLocalizer < LogLanguageKey > _logLanguage ;
2827
29- /// <summary>
30- /// Gets a value indicating whether this handler can be shared across multiple channels.
31- /// </summary>
32- public override bool IsSharable => true ;
33-
3428 /// <summary>
3529 /// Initializes a new instance of the <see cref="SpamRequestFilter"/> class.
3630 /// </summary>
@@ -47,21 +41,21 @@ public SpamRequestFilter(IClock clock, ILogger<SpamRequestFilter> logger, ILogLa
4741 /// <summary>
4842 /// Filters incoming requests based on connection rate from the same IP address.
4943 /// </summary>
50- /// <param name="context ">The channel handler context .</param>
44+ /// <param name="remoteEndPoint ">The remote endpoint of the connection .</param>
5145 /// <param name="message">The incoming message bytes.</param>
5246 /// <returns>The message bytes if allowed, or null if blocked by the spam filter.</returns>
53- public override byte [ ] ? Filter ( IChannelHandlerContext context , Span < byte > message )
47+ public byte [ ] ? Filter ( EndPoint remoteEndPoint , Span < byte > message )
5448 {
55- if ( _connectionsByIp . TryGetValue ( context . Channel . RemoteAddress , out var date ) )
49+ if ( _connectionsByIp . TryGetValue ( remoteEndPoint , out var date ) )
5650 {
5751 if ( date . Plus ( Duration . FromTimeSpan ( _timeBetweenConnection ) ) > _clock . GetCurrentInstant ( ) )
5852 {
59- _logger . LogWarning ( _logLanguage [ LogLanguageKey . BLOCKED_BY_SPAM_FILTER ] , context . Channel . RemoteAddress ) ;
53+ _logger . LogWarning ( _logLanguage [ LogLanguageKey . BLOCKED_BY_SPAM_FILTER ] , remoteEndPoint ) ;
6054 return null ;
6155 }
6256 }
6357
64- _connectionsByIp [ context . Channel . RemoteAddress ] = _clock . GetCurrentInstant ( ) ;
58+ _connectionsByIp [ remoteEndPoint ] = _clock . GetCurrentInstant ( ) ;
6559 return message . ToArray ( ) ;
6660 }
6761 }
0 commit comments