NOTHING EVER WFUCKIGN WORKS

Fortune for datse's current commit: Small blessing − 小吉
simple
Avril 3 years ago
parent 2cd0f81155
commit 382027647b
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -85,25 +85,22 @@ impl Banlist
/// ///
/// # Lifetime /// # Lifetime
/// The filter holds a weak reference to this list. If the list is dropped, then the filter will panic. /// The filter holds a weak reference to this list. If the list is dropped, then the filter will panic.
//TODO: How do we make this just return `impl Filter`? We need it to take the client_info as param... pub fn filter(&self, client_info: ClientInfo) -> impl Future<Output = Result<ClientInfo, warp::reject::Rejection>> + 'static
pub fn filter(&self) -> impl Fn(ClientInfo) -> futures::future::BoxFuture<'static, Result<ClientInfo, warp::reject::Rejection>> + Clone + 'static
{ {
let refer = Arc::downgrade(&self.0); let refer = Arc::downgrade(&self.0);
move |client_info: ClientInfo| { async move {
let refer = refer.upgrade().unwrap(); let refer = refer.upgrade().unwrap();
async move {
let bans = refer.read().await; let bans = refer.read().await;
//XXX: FUCK there has to be a better way to check this right? //XXX: FUCK there has to be a better way to check this right?
for fuck in bans.iter() for fuck in bans.iter()
{ {
if fuck == &client_info { if fuck == &client_info {
return Err(warp::reject::custom(ClientBannedError)); return Err(warp::reject::custom(ClientBannedError));
}
} }
Ok(client_info)
} }
//XXX: This doesn't seem good Ok(client_info)
}.boxed() }
} }
} }

@ -27,19 +27,41 @@ mod ban;
mod source; mod source;
mod forwarded_list; mod forwarded_list;
fn routing<'a>() -> impl warp::Filter<Error = warp::reject::Rejection> + Clone + 'a mod state;
use state::State;
async fn auth(state: State) -> Result<String, warp::Rejection>
{
Ok("".to_owned())
}
async fn push(state: State) -> Result<&'static str, warp::Rejection>
{
Ok("")
}
async fn get(state: State) -> Result<impl warp::Reply, warp::Rejection>
{ {
Ok(warp::reply())
}
fn routing<'a>(state: State) -> impl warp::Filter<Extract = (impl warp::Reply,), Error = warp::reject::Rejection> + Clone + 'a
{
let state = warp::any().map(move || state.clone());
let auth = warp::path("auth").and({ let auth = warp::path("auth").and({
//let req = warp::path("req") //let req = warp::path("req")
warp::post() warp::post().and(state.clone()).and_then(|state: State| auth(state))
}); });
let push = warp::path("push").and({ let push = warp::path("push").and({
warp::post() warp::post().and(state.clone()).and_then(|state: State| self::push(state))
}); });
let get = warp::path("get").and({ let get = warp::path("get").and({
warp::get() warp::post().and(state.clone()).and_then(|state: State| self::get(state))
}); });
auth.or(push).or(get) auth.or(push).or(get)
@ -47,26 +69,24 @@ fn routing<'a>() -> impl warp::Filter<Error = warp::reject::Rejection> + Clone +
pub async fn serve(cfg: Config) -> eyre::Result<()> pub async fn serve(cfg: Config) -> eyre::Result<()>
{ {
let bans: ban::Banlist = cfg.static_bans.iter().collect(); //Create state
let state = State::new(cfg);
//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`. // 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() let client_ip = warp::addr::remote()
.and(warp::header("X-Forwarded-For")) .and(warp::header("X-Forwarded-For"))
.map(source::extract(cfg.trust_x_forwarded_for)) .map(source::extract(state.cfg().trust_x_forwarded_for))
// Extract the IP // Extract the IP
.and_then(|req: Result<source::ClientInfo, _>| async move { req.map_err(warp::reject::custom) }) .and_then(|req: Result<source::ClientInfo, _>| async move { req.map_err(warp::reject::custom) })
// Enforce banlist // Enforce banlist
.and_then(bans.filter()); .and_then({ let state = state.clone(); move |c| { state.clone().banlist().filter(c) } });
//.and_then(|req: source::ClientInfo| async move { Result::<_, std::convert::Infallible>::Ok(req) }); //.and_then(|req: source::ClientInfo| async move { Result::<_, std::convert::Infallible>::Ok(req) });
let filter = warp::path("api") let filter = warp::path("api")
//how the FUCK do we insert shit into this???????????????????????? .and(routing(state.clone()));
.and(routing());
warp::serve(client_ip).bind_with_graceful_shutdown(([127,0,0,1], 8001), tokio::signal::ctrl_c()).await; warp::serve(filter).bind_with_graceful_shutdown(([127,0,0,1], 8001), tokio::signal::ctrl_c()).await;
Ok(()) Ok(())
} }

@ -0,0 +1,39 @@
//! Program state management
use super::*;
/// Inner (immutable) state.
#[derive(Debug)]
struct StateInner
{
cfg: Config,
banlist: ban::Banlist,
}
/// Whole program state
#[derive(Debug, Clone)]
pub struct State(Arc<StateInner>);
impl State
{
pub fn new(cfg: Config) -> Self
{
let banlist: ban::Banlist = cfg.static_bans.iter().collect();
Self(Arc::new(StateInner {
banlist,
cfg,
}))
}
/// The static config the program was started on.
#[inline] pub fn cfg(&self) -> &Config
{
&self.0.cfg
}
#[inline] pub fn banlist(&self) -> &ban::Banlist
{
&self.0.banlist
}
}
Loading…
Cancel
Save