BROKEN, BUT - filter works !! Needs cleanup and correct logic

This commit is contained in:
Brandon Thomas committed 2021-11-12 20:05:54 -05:00
1 parent 6849e6d051
commit 4aa9724d41
1 file changed
+51 -1
@@ -22,9 +22,50 @@ use std::task::{Context, Poll};
use actix_web::dev::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::Error;
use actix_web::{Error, HttpResponse};
use futures_util::future::{err, ok, Either, Ready};
use derive_more::{Display, Error as ErrorE};
use actix_web::{ResponseError, HttpMessage, HttpRequest, HttpResponseBuilder};
use actix_http::StatusCode;
use actix_http::{error, body::Body, Response};
use actix_web::web::{BytesMut, Buf, BufMut};
use actix_http::http::{header, HeaderMap, HeaderValue};
use std::io::Write;
#[derive(Debug, Display, ErrorE)]
pub struct BannedError {
}
impl ResponseError for BannedError {
fn status_code(&self) -> StatusCode {
StatusCode::UNAUTHORIZED
}
fn error_response(&self) -> HttpResponse<Body> {
// let mut res = Response::new(self.status_code());
// res.headers_mut().insert(
// header::CONTENT_TYPE,
// header::HeaderValue::from_static("application/json"),
// );
// res.set_body(Body::from(self.to_string())).into()
/*let mut res = Response::new(self.status_code());
let mut buf = BytesMut::new().writer();
let _ = write!(buf, "{}", self);
res.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
);
res.set_body(Body::from(buf.into_inner())).into()*/
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "application/json")
.body("TODO")
}
}
// There are two steps in middleware processing.
// 1. Middleware initialization, middleware factory gets called with
// next service in chain as parameter.
@@ -88,6 +129,15 @@ impl<S> Service<ServiceRequest> for IpFilterMiddleware<S>
// NB: Fail open.
let is_banned = self.ip_banlist.is_banned(&ip_address).unwrap_or(false);
if is_banned {
let res = req.into_response(
HttpResponse::Unauthorized()
.finish());
//return Either::Right(err(Error::from(error)));
//return Err(Error::from(UserError::ValidationError { field: "token invalid".to_string() }));
return Either::Right(err(Error::from(BannedError {})))
}
Either::Left(self.service.call(req))
}
}