pub struct IPFilter { /* private fields */ }
Expand description
Middleware for filter IP of HTTP requests
Implementations§
Source§impl IPFilter
impl IPFilter
Sourcepub fn new_with_opts(
allowlist: Vec<&str>,
blocklist: Vec<&str>,
use_x_real_ip: bool,
) -> Self
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.
Sourcepub fn new_with_opts_limited(
allowlist: Vec<&str>,
blocklist: Vec<&str>,
limitlist: Vec<&str>,
use_x_real_ip: bool,
) -> Self
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.
Sourcepub fn x_real_ip(self, enabled: bool) -> Self
pub fn x_real_ip(self, enabled: bool) -> Self
Use X-REAL-IP
header to check IP if it is found in request.
Sourcepub fn use_realip_remote_addr(self, enabled: bool) -> Self
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.
Sourcepub fn allow(self, allowlist: Vec<&str>) -> Self
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'"]);
Sourcepub fn block(self, blocklist: Vec<&str>) -> Self
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'"]);
Sourcepub fn limit_to(self, limitlist: Vec<&str>) -> Self
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"]);
Sourcepub fn on_allow(self, handler: fn(&Self, &str, &ServiceRequest)) -> Self
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);
Sourcepub fn on_block(
self,
handler: fn(&Self, &str, &ServiceRequest) -> Option<HttpResponse>,
) -> Self
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<S, B> Transform<S, ServiceRequest> for IPFilterwhere
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
impl<S, B> Transform<S, ServiceRequest> for IPFilterwhere
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
Source§type Response = ServiceResponse<EitherBody<B>>
type Response = ServiceResponse<EitherBody<B>>
Responses produced by the service.
Source§type Transform = IPFilterMiddleware<S>
type Transform = IPFilterMiddleware<S>
The
TransformService
value created by this factorySource§type Future = Ready<Result<<IPFilter as Transform<S, ServiceRequest>>::Transform, <IPFilter as Transform<S, ServiceRequest>>::InitError>>
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
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
Auto Trait Implementations§
impl Freeze for IPFilter
impl RefUnwindSafe for IPFilter
impl !Send for IPFilter
impl !Sync for IPFilter
impl Unpin for IPFilter
impl UnwindSafe for IPFilter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more