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,13 +85,12 @@ impl Banlist
///
/// # Lifetime
/// 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) -> impl Fn(ClientInfo) -> futures::future::BoxFuture<'static, Result<ClientInfo, warp::reject::Rejection>> + Clone + 'static
pub fn filter(&self, client_info: ClientInfo) -> impl Future<Output = Result<ClientInfo, warp::reject::Rejection>> + 'static
{
let refer = Arc::downgrade(&self.0);
move |client_info: ClientInfo| {
let refer = refer.upgrade().unwrap();
async move {
let refer = refer.upgrade().unwrap();
let bans = refer.read().await;
//XXX: FUCK there has to be a better way to check this right?
for fuck in bans.iter()
@ -102,8 +101,6 @@ impl Banlist
}
Ok(client_info)
}
//XXX: This doesn't seem good
}.boxed()
}
}

@ -27,19 +27,41 @@ mod ban;
mod source;
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 req = warp::path("req")
warp::post()
warp::post().and(state.clone()).and_then(|state: State| auth(state))
});
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({
warp::get()
warp::post().and(state.clone()).and_then(|state: State| self::get(state))
});
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<()>
{
let bans: ban::Banlist = cfg.static_bans.iter().collect();
//TODO: Create state
//Create state
let state = State::new(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()
.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
.and_then(|req: Result<source::ClientInfo, _>| async move { req.map_err(warp::reject::custom) })
// 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) });
let filter = warp::path("api")
//how the FUCK do we insert shit into this????????????????????????
.and(routing());
.and(routing(state.clone()));
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(())
}

@ -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