Struct IPFilter

Source
pub struct IPFilter { /* private fields */ }
Expand description

Middleware for filter IP of HTTP requests

Implementations§

Source§

impl IPFilter

Source

pub fn new() -> Self

Construct IPFilter middleware with no arguments

Source

pub fn new_with_opts( allowlist: Vec<&str>, blocklist: Vec<&str>, use_x_real_ip: bool, ) -> Self

Construct IPFilter middleware with the provided arguments and no limiting pattern.

Source

pub fn new_with_opts_limited( allowlist: Vec<&str>, blocklist: Vec<&str>, limitlist: Vec<&str>, use_x_real_ip: bool, ) -> Self

Construct IPFilter middleware with the provided arguments and limiting patterns.

Source

pub fn x_real_ip(self, enabled: bool) -> Self

Use X-REAL-IP header to check IP if it is found in request.

Source

pub fn use_realip_remote_addr(self, enabled: bool) -> Self

Use Actix’s ConnectionInfo::realip_remote_addr() to obtain peer address. This will use the first IP in the X-Forwarded-For header when present.

Source

pub fn allow(self, allowlist: Vec<&str>) -> Self

Set allow IP list, it supported glob pattern. It will allow all if vec is empty.

§Example
let middleware = IPFilter::new()
    .allow(vec!["127.??.6*.12", "!1.2.*.4'"]);
Source

pub fn block(self, blocklist: Vec<&str>) -> Self

Set block IP list, it supported glob pattern.

§Example
let middleware = IPFilter::new()
    .block(vec!["127.??.6*.12", "!1.2.*.4'"]);
Source

pub fn limit_to(self, limitlist: Vec<&str>) -> Self

Set endpoint limit list, supporting glob pattern.

§Example
let middleware = IPFilter::new()
    .limit_to(vec!["/path/to/protected/resource*", "/protected/file/type/*.csv"]);
Source

pub fn on_allow(self, handler: fn(&Self, &str, &ServiceRequest)) -> Self

Add allow handler.

§Example

fn my_custom_handler(filter: &IPFilter, ip: &str, req: &ServiceRequest) {
    // Do smth
}

let middleware = IPFilter::new()
    .on_allow(my_custom_handler);
Source

pub fn on_block( self, handler: fn(&Self, &str, &ServiceRequest) -> Option<HttpResponse>, ) -> Self

Add block handler.

§Example
use actix_web::error::ErrorForbidden;
use actix_web::HttpResponse;

fn my_custom_handler(filter: &IPFilter, ip: &str, req: &ServiceRequest) -> Option<HttpResponse> {
    Some(HttpResponse::Forbidden().body("My custom forbidden message!"))
}

let middleware = IPFilter::new()
    .on_block(my_custom_handler);

Trait Implementations§

Source§

impl Default for IPFilter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S, B> Transform<S, ServiceRequest> for IPFilter
where S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, S::Future: 'static, B: MessageBody + 'static,

Source§

type Response = ServiceResponse<EitherBody<B>>

Responses produced by the service.
Source§

type Error = Error

Errors produced by the service.
Source§

type Transform = IPFilterMiddleware<S>

The TransformService value created by this factory
Source§

type InitError = ()

Errors produced while building a transform service.
Source§

type Future = Ready<Result<<IPFilter as Transform<S, ServiceRequest>>::Transform, <IPFilter as Transform<S, ServiceRequest>>::InitError>>

The future response value.
Source§

fn new_transform(&self, service: S) -> Self::Future

Creates and returns a new Transform component, asynchronously

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,