From f1a95e428a852bce612ec90d239c985be04e5f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= Date: Tue, 13 Feb 2024 21:44:33 +0800 Subject: [PATCH] feat(server): add aes_key to request --- src/models/router.rs | 8 ++++---- src/models/server.rs | 10 +++++----- src/utils/parser.rs | 12 +++++++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/models/router.rs b/src/models/router.rs index 00e4779..5593fcf 100644 --- a/src/models/router.rs +++ b/src/models/router.rs @@ -48,10 +48,10 @@ impl RoutePath { } } - pub fn check(&mut self, olps: String) -> bool { + pub fn check(&mut self, olps: &str) -> bool { if self.route_type == RouteType::RegexPath { let regex = Regex::new(&self.route).unwrap(); - regex.is_match(&olps) + regex.is_match(olps) } else if self.route_type == RouteType::StartswithPath { olps.starts_with(&self.route) } else { @@ -86,10 +86,10 @@ impl Router { self.routes.insert(path.clone(), route); } - pub fn get_handler(&self, path: String) -> Route { + pub fn get_handler(&self, path: &str) -> Route { for (route_path, route) in &self.routes { let mut route_path = route_path.clone(); - if route_path.check(path.clone()) { + if route_path.check(path) { return route.clone(); }; } diff --git a/src/models/server.rs b/src/models/server.rs index f4eacd7..852ee10 100644 --- a/src/models/server.rs +++ b/src/models/server.rs @@ -50,6 +50,8 @@ impl ServerConnection { let mut oke = OKE::new(Some(&self.private_key), Some(self.public_key))?; oke.to_stream_with_salt(stream).await?; oke.from_stream(stream).await?; + + request.aes_key = Some(oke.get_aes_key()); self.aes_key = Some(oke.get_aes_key()); if request.method == "POST" { @@ -122,7 +124,7 @@ async fn _handle( } }; - let mut route = router.get_handler(request.get_olps()); + let mut route = router.get_handler(&request.olps); let status_code = match response( &mut route, stream, @@ -142,7 +144,7 @@ async fn _handle( } }; - Ok((request.clone(), status_code)) + Ok((request, status_code)) } pub async fn handle(router: Router, stream: TcpStream, peer: SocketAddr) { @@ -160,9 +162,7 @@ pub async fn handle(router: Router, stream: TcpStream, peer: SocketAddr) { status_code ); } - Err(error) => { - println!("{}", error); - } + Err(error) => println!("{}", error), } } diff --git a/src/utils/parser.rs b/src/utils/parser.rs index 404d87c..e3b8869 100644 --- a/src/utils/parser.rs +++ b/src/utils/parser.rs @@ -158,7 +158,7 @@ impl Oblivion { #[derive(Clone, Debug, PartialEq)] pub struct OblivionRequest { pub(crate) method: String, - olps: String, + pub(crate) olps: String, protocol: String, version: String, data: Option, @@ -167,6 +167,7 @@ pub struct OblivionRequest { put: Option>, remote_addr: Option, remote_port: Option, + pub(crate) aes_key: Option>, } impl OblivionRequest { @@ -207,16 +208,17 @@ impl OblivionRequest { .unwrap_or_default() .to_string(); Ok(Self { - method: method, - olps: olps, - protocol: protocol, - version: version, + method, + olps, + protocol, + version, data: None, plain_text: plain_text.to_owned(), post: None, put: None, remote_addr: None, remote_port: None, + aes_key: None, }) } else { Err(OblivionException::BadProtocol {