mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew
synced 2026-08-26 15:02:18 +08:00
Move all encryption to a session handler
This commit is contained in:
@@ -35,14 +35,66 @@ mod tools;
|
||||
use actix_web::{
|
||||
HttpResponse,
|
||||
HttpRequest,
|
||||
FromRequest,
|
||||
body::{BoxBody, MessageBody},
|
||||
dev::{ServiceRequest, ServiceResponse},
|
||||
dev::{Payload, ServiceRequest, ServiceResponse},
|
||||
middleware::{from_fn, Next},
|
||||
http::header::HeaderMap
|
||||
};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
use futures_util::FutureExt;
|
||||
use jzon::{JsonValue, object};
|
||||
use crate::encryption;
|
||||
|
||||
pub struct Body(pub JsonValue);
|
||||
|
||||
pub struct Login(pub String);
|
||||
|
||||
pub struct Session {
|
||||
pub key: String,
|
||||
pub body: JsonValue,
|
||||
}
|
||||
|
||||
impl FromRequest for Body {
|
||||
type Error = actix_web::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
let fut = String::from_request(req, payload);
|
||||
async move {
|
||||
Ok(Body(jzon::parse(&encryption::decrypt_packet(&fut.await?).unwrap()).unwrap()))
|
||||
}.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for Login {
|
||||
type Error = actix_web::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
let headers = req.headers().clone();
|
||||
let fut = String::from_request(req, payload);
|
||||
async move {
|
||||
Ok(Login(global::get_login(&headers, &encryption::decrypt_packet(&fut.await?).unwrap())))
|
||||
}.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for Session {
|
||||
type Error = actix_web::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
let headers = req.headers().clone();
|
||||
let fut = String::from_request(req, payload);
|
||||
async move {
|
||||
let body = encryption::decrypt_packet(&fut.await?).unwrap();
|
||||
let key = global::get_login(&headers, &body);
|
||||
Ok(Session { key, body: jzon::parse(&body).unwrap() })
|
||||
}.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
// Requests without client headers (a browser) get the webui
|
||||
async fn webui_fallback(req: ServiceRequest, next: Next<impl MessageBody + 'static>) -> Result<ServiceResponse<BoxBody>, actix_web::Error> {
|
||||
let is_from_game = req.headers().get("aoharu-asset-version").is_some() || req.path().starts_with("/api/webui");
|
||||
|
||||
Reference in New Issue
Block a user