better unauthorized ip, and adding responder

This commit is contained in:
2025-01-18 04:04:07 +01:00
parent f5f3ee67a5
commit 3499f430b1
6 changed files with 99 additions and 19 deletions

View File

@ -1,6 +1,7 @@
mod builder;
mod config;
mod server;
mod responder;
mod server;
pub use responder::Responder;
pub use server::Server;

View File

@ -3,7 +3,7 @@ use hyper::{
body::{Bytes, Incoming},
Request, Response,
};
use servme::Server;
use servme::{Responder, Server};
use std::convert::Infallible;
#[tokio::main]
@ -16,8 +16,5 @@ async fn main() {
}
async fn handler(req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from(format!(
"Hello World! {}",
req.uri()
)))))
Responder::text(format!("Hello World! {}", req.uri()))
}

View File

@ -1,15 +1,55 @@
use http::Response;
use http::{header::CONTENT_TYPE, response::Builder, Response};
use http_body_util::Full;
use hyper::body::Bytes;
use serde::Serialize;
use std::convert::Infallible;
pub struct Responder;
impl Responder {
pub fn not_found() -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(401), "Not Found".to_string())
}
pub fn unathorized() -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::builder()
.status(401)
.body(Full::new(Bytes::from("Unauthorized")))
.unwrap())
Self::response_using_builder(Self::create_builder(401), "Unathorized".to_string())
}
pub fn text(response: String) -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(200), response)
}
pub fn json<T>(json: T) -> Result<Response<Full<Bytes>>, Infallible>
where
T: Serialize,
{
Self::json_using_status(200, json)
}
pub fn text_using_status(
status: u16,
response: String,
) -> Result<Response<Full<Bytes>>, Infallible> {
Self::response_using_builder(Self::create_builder(status), response)
}
pub fn json_using_status<T>(status: u16, json: T) -> Result<Response<Full<Bytes>>, Infallible>
where
T: Serialize,
{
let builder = Self::create_builder(status).header(CONTENT_TYPE, "application/json");
Self::response_using_builder(builder, serde_json::to_string(&json).unwrap())
}
fn response_using_builder(
builder: Builder,
response: String,
) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(builder.body(Full::new(Bytes::from(response))).unwrap())
}
fn create_builder(status: u16) -> Builder {
Response::builder().status(status)
}
}

View File

@ -28,8 +28,8 @@ impl Server {
.parse()
.expect("Invalid IP or port");
let listener = TcpListener::bind(addr).await.unwrap();
let handler = Arc::new(handler);
let handler = Arc::new(handler);
loop {
let listener_res = listener.accept().await;
if listener_res.is_err() {
@ -37,12 +37,9 @@ impl Server {
}
let (tcp, client_addr) = listener_res.unwrap();
let client_ip = client_addr.ip();
let io = TokioIo::new(tcp);
if !self.config.is_ip_authorized(&client_addr.ip()) {
continue;
}
let config = Arc::clone(&self.config);
let handler = Arc::clone(&handler);
spawn(async move {
@ -55,10 +52,12 @@ impl Server {
let handler = Arc::clone(&handler);
async move {
if config.is_req_authorized(&req) {
handler(req).await
} else {
if !config.is_ip_authorized(&client_ip)
|| !config.is_req_authorized(&req)
{
Responder::unathorized()
} else {
handler(req).await
}
}
}),