Unosquare
    Show / Hide Table of Contents

    Class IPBanningModule

    A module to ban clients by IP address, based on TCP requests-per-second or RegEx matches on log messages.

    Inheritance
    Object
    WebModuleBase
    IPBanningModule
    Implements
    IWebModuleImpl
    IWebModule
    IDisposable
    Inherited Members
    WebModuleBase.Container
    WebModuleBase.IWebModuleImpl.SetContainer(IWebModuleContainer)
    WebModuleBase.BaseRoute
    WebModuleBase.OnUnhandledException
    WebModuleBase.OnHttpException
    WebModuleBase.LogSource
    WebModuleBase.Start(CancellationToken)
    WebModuleBase.MatchUrlPath(String)
    WebModuleBase.HandleRequestAsync(IHttpContext)
    WebModuleBase.SetContainer(IWebModuleContainer)
    Namespace: EmbedIO.Security
    Syntax
    public class IPBanningModule : WebModuleBase, IWebModuleImpl, IWebModule, IDisposable

    Constructors

    IPBanningModule(String, Nullable<IEnumerable<String>>, Int32)

    Initializes a new instance of the IPBanningModule class.

    Declaration
    public IPBanningModule(string baseRoute = "/", IEnumerable<string>? whitelist = default(IEnumerable<string>? ), int banMinutes = 30)
    Parameters
    Type Name Description
    String baseRoute

    The base route.

    Nullable<IEnumerable<String>> whitelist

    A collection of valid IPs that never will be banned.

    Int32 banMinutes

    Minutes that an IP will remain banned.

    Fields

    DefaultBanMinutes

    The default ban minutes.

    Declaration
    public const int DefaultBanMinutes = 30
    Field Value
    Type Description
    Int32

    Properties

    ClientAddress

    Gets the client address.

    Declaration
    public IPAddress? ClientAddress { get; }
    Property Value
    Type Description
    Nullable<IPAddress>

    The client address.

    IsFinalHandler

    Gets a value indicating whether processing of a request should stop after a module has handled it.

    Declaration
    public override bool IsFinalHandler { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    WebModuleBase.IsFinalHandler
    Remarks

    If this property is true, a HTTP context's SetHandled() method will be automatically called immediately after after the returned by HandleRequestAsync(IHttpContext) is completed. This will prevent the context from being passed further along to other modules.

    See Also
    IsHandled
    SetHandled()

    Methods

    Dispose()

    Declaration
    public void Dispose()

    Dispose(Boolean)

    Releases unmanaged and - optionally - managed resources.

    Declaration
    protected virtual void Dispose(bool disposing)
    Parameters
    Type Name Description
    Boolean disposing

    true to release both managed and unmanaged resources; false to release only unmanaged resources.

    Finalize()

    Finalizes an instance of the IPBanningModule class.

    Declaration
    protected void Finalize()

    GetBannedIPs(String)

    Gets the list of current banned IPs.

    Declaration
    public static IEnumerable<BanInfo> GetBannedIPs(string baseRoute = "/")
    Parameters
    Type Name Description
    String baseRoute

    The base route.

    Returns
    Type Description
    IEnumerable<BanInfo>

    A collection of BanInfo in the blacklist.

    OnRequestAsync(IHttpContext)

    Called to handle a request from a client.

    Declaration
    protected override Task OnRequestAsync(IHttpContext context)
    Parameters
    Type Name Description
    IHttpContext context

    The context of the request being handled.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    Overrides
    WebModuleBase.OnRequestAsync(IHttpContext)

    OnStart(CancellationToken)

    Called when a module is started, immediately before locking the module's configuration.

    Declaration
    protected override void OnStart(CancellationToken cancellationToken)
    Parameters
    Type Name Description
    CancellationToken cancellationToken

    A used to stop the web server.

    Overrides
    WebModuleBase.OnStart(CancellationToken)

    RegisterCriterion(IIPBanningCriterion)

    Registers the criterion.

    Declaration
    public void RegisterCriterion(IIPBanningCriterion criterion)
    Parameters
    Type Name Description
    IIPBanningCriterion criterion

    The criterion.

    TryBanIP(IPAddress, DateTime, String, Boolean)

    Tries to ban an IP explicitly.

    Declaration
    public static bool TryBanIP(IPAddress address, DateTime banUntil, string baseRoute = "/", bool isExplicit = true)
    Parameters
    Type Name Description
    IPAddress address

    The IP address to ban.

    DateTime banUntil

    A specifying the expiration time of the ban.

    String baseRoute

    The base route.

    Boolean isExplicit

    true if the IP was explicitly banned.

    Returns
    Type Description
    Boolean

    true if the IP was added to the blacklist; otherwise, false.

    TryBanIP(IPAddress, Int32, String, Boolean)

    Tries to ban an IP explicitly.

    Declaration
    public static bool TryBanIP(IPAddress address, int banMinutes, string baseRoute = "/", bool isExplicit = true)
    Parameters
    Type Name Description
    IPAddress address

    The IP address to ban.

    Int32 banMinutes

    Minutes that the IP will remain banned.

    String baseRoute

    The base route.

    Boolean isExplicit

    true if the IP was explicitly banned.

    Returns
    Type Description
    Boolean

    true if the IP was added to the blacklist; otherwise, false.

    TryBanIP(IPAddress, TimeSpan, String, Boolean)

    Tries to ban an IP explicitly.

    Declaration
    public static bool TryBanIP(IPAddress address, TimeSpan banDuration, string baseRoute = "/", bool isExplicit = true)
    Parameters
    Type Name Description
    IPAddress address

    The IP address to ban.

    TimeSpan banDuration

    A specifying the duration that the IP will remain banned.

    String baseRoute

    The base route.

    Boolean isExplicit

    true if the IP was explicitly banned.

    Returns
    Type Description
    Boolean

    true if the IP was added to the blacklist; otherwise, false.

    TryUnbanIP(IPAddress, String)

    Tries to unban an IP explicitly.

    Declaration
    public static bool TryUnbanIP(IPAddress address, string baseRoute = "/")
    Parameters
    Type Name Description
    IPAddress address

    The IP address.

    String baseRoute

    The base route.

    Returns
    Type Description
    Boolean

    true if the IP was removed from the blacklist; otherwise, false.

    Implements

    IWebModuleImpl
    IWebModule
    IDisposable

    See Also

    WebModuleBase

    Comments

    Back to top Copyright © 2017-2019 Unosquare