-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from sebadob/global-ip-blacklist-middleware
Global ip blacklist middleware
- Loading branch information
Showing
8 changed files
with
158 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use crate::real_ip_from_svc_req; | ||
use actix_web::{ | ||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, | ||
web, Error, | ||
}; | ||
use chrono::Utc; | ||
use futures::future::LocalBoxFuture; | ||
use rauthy_common::error_response::{ErrorResponse, ErrorResponseType}; | ||
use rauthy_common::ip_blacklist_handler::{IpBlacklistCheck, IpBlacklistReq}; | ||
use rauthy_models::app_state::AppState; | ||
use rauthy_models::templates::TooManyRequestsHtml; | ||
use std::future::{ready, Ready}; | ||
use std::rc::Rc; | ||
use tokio::sync::oneshot; | ||
use tracing::error; | ||
|
||
pub struct RauthyIpBlacklistMiddleware; | ||
|
||
// `S` - type of the next service | ||
// `B` - type of response's body | ||
impl<S, B> Transform<S, ServiceRequest> for RauthyIpBlacklistMiddleware | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<B>; | ||
type Error = Error; | ||
type Transform = IpBlacklistMiddleware<S>; | ||
type InitError = (); | ||
type Future = Ready<Result<Self::Transform, Self::InitError>>; | ||
|
||
fn new_transform(&self, service: S) -> Self::Future { | ||
ready(Ok(IpBlacklistMiddleware { | ||
service: Rc::new(service), | ||
})) | ||
} | ||
} | ||
|
||
pub struct IpBlacklistMiddleware<S> { | ||
service: Rc<S>, | ||
} | ||
|
||
impl<S, B> Service<ServiceRequest> for IpBlacklistMiddleware<S> | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<B>; | ||
type Error = Error; | ||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
forward_ready!(service); | ||
|
||
fn call(&self, req: ServiceRequest) -> Self::Future { | ||
let service = Rc::clone(&self.service); | ||
|
||
Box::pin(async move { | ||
let app_state = req | ||
.app_data::<web::Data<AppState>>() | ||
.expect("AppState to be in the Actix context"); | ||
|
||
if let Some(ip) = real_ip_from_svc_req(&req) { | ||
let (tx, rx) = oneshot::channel(); | ||
app_state | ||
.tx_ip_blacklist | ||
.send_async(IpBlacklistReq::BlacklistCheck(IpBlacklistCheck { | ||
ip: ip.clone(), | ||
tx, | ||
})) | ||
.await | ||
.unwrap(); | ||
match rx.await { | ||
Ok(exp) => { | ||
if let Some(exp) = exp { | ||
if exp > Utc::now() { | ||
let ts = exp.timestamp(); | ||
return Err(Error::from(ErrorResponse::new( | ||
ErrorResponseType::TooManyRequests(ts), | ||
TooManyRequestsHtml::build(&ip, ts), | ||
))); | ||
} | ||
} | ||
} | ||
Err(err) => { | ||
error!("Checking IP Blacklist status in middleware - this should never happen: {:?}", err); | ||
} | ||
} | ||
} else { | ||
error!("Cannot extract IP from HttpRequest - check your Reverse Proxy settings"); | ||
} | ||
|
||
service.call(req).await | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub mod ip_blacklist; | ||
pub mod logging; | ||
//github.com/ # Rate limiting extractors | ||
pub mod rate_limit; | ||
pub mod session; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters