From 2cd0f811557451841a2b6dae57c00499a822faeb Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 8 Sep 2021 16:02:07 +0100 Subject: [PATCH] Nothing ever works and i want to die MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for datse's current commit: Future blessing − 末吉 --- src/server/mod.rs | 4 ++++ src/server/web/ban.rs | 8 +++++--- src/server/web/mod.rs | 17 ++++++++++++----- src/server/web/source.rs | 8 ++++++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 4c1e347..f8933ef 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,11 +1,14 @@ //! Server for datse use super::*; +use web::ban::BanKind; + /// Configuration for the server. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Config { pub trust_x_forwarded_for: bool, + pub static_bans: Vec, } impl Config @@ -17,6 +20,7 @@ impl Config { Self { trust_x_forwarded_for: false, + static_bans: Vec::new(), } } } diff --git a/src/server/web/ban.rs b/src/server/web/ban.rs index 3e2748e..1435910 100644 --- a/src/server/web/ban.rs +++ b/src/server/web/ban.rs @@ -52,7 +52,7 @@ impl ClientInfo #[derive(Debug, Clone)] pub struct Banlist(Arc>>); -type OpaqueFuture<'a, T> = impl Future + 'a; +//type OpaqueFuture<'a, T> = impl Future + 'a; impl Banlist { @@ -85,7 +85,8 @@ impl Banlist /// /// # Lifetime /// The filter holds a weak reference to this list. If the list is dropped, then the filter will panic. - pub fn filter(&self) -> impl Fn(ClientInfo) -> OpaqueFuture<'static, Result> + Clone + 'static + //TODO: How do we make this just return `impl Filter`? We need it to take the client_info as param... + pub fn filter(&self) -> impl Fn(ClientInfo) -> futures::future::BoxFuture<'static, Result> + Clone + 'static { let refer = Arc::downgrade(&self.0); move |client_info: ClientInfo| { @@ -101,7 +102,8 @@ impl Banlist } Ok(client_info) } - } + //XXX: This doesn't seem good + }.boxed() } } diff --git a/src/server/web/mod.rs b/src/server/web/mod.rs index 44cc6dc..d49d9e8 100644 --- a/src/server/web/mod.rs +++ b/src/server/web/mod.rs @@ -45,10 +45,12 @@ fn routing<'a>() -> impl warp::Filter + Clone + auth.or(push).or(get) } -pub async fn serve(cfg: impl Into) -> eyre::Result<()> +pub async fn serve(cfg: Config) -> eyre::Result<()> { - let cfg = cfg.into(); - let bans = ban::Banlist::new(); + let bans: ban::Banlist = cfg.static_bans.iter().collect(); + + //TODO: Create state + // Filter: Extract the client IP from the remote address of the connection of the X-Forwarded-For header if it is trusted in `cfg`. let client_ip = warp::addr::remote() .and(warp::header("X-Forwarded-For")) @@ -57,10 +59,15 @@ pub async fn serve(cfg: impl Into) -> eyre::Result<()> .and_then(|req: Result| async move { req.map_err(warp::reject::custom) }) // Enforce banlist .and_then(bans.filter()); + //.and_then(|req: source::ClientInfo| async move { Result::<_, std::convert::Infallible>::Ok(req) }); - let r = warp::path("api").and(routing()); - + let filter = warp::path("api") + //how the FUCK do we insert shit into this???????????????????????? + .and(routing()); + + warp::serve(client_ip).bind_with_graceful_shutdown(([127,0,0,1], 8001), tokio::signal::ctrl_c()).await; + Ok(()) } diff --git a/src/server/web/source.rs b/src/server/web/source.rs index 1b2d5a5..4baf3af 100644 --- a/src/server/web/source.rs +++ b/src/server/web/source.rs @@ -70,3 +70,11 @@ pub struct ClientInfo { pub ip_addr: IpAddr } + +impl fmt::Display for ClientInfo +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "ip: {}", self.ip_addr) + } +}