From 42176782c3459698d67ea314633ef751a2bc9d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= Date: Fri, 22 Dec 2023 18:50:33 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=AE=9E=E7=8E=B0=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E4=B8=9A=E5=8A=A1=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 117 +++++++++++++++++++++++ Cargo.toml | 3 + oblivion-codegen/.gitignore | 1 + oblivion-codegen/Cargo.lock | 182 ++++++++++++++++++++++++++++++++++++ oblivion-codegen/Cargo.toml | 16 ++++ oblivion-codegen/src/lib.rs | 25 +++++ src/bin/main.rs | 20 +++- src/lib.rs | 3 + src/models/handler.rs | 15 +++ src/models/render.rs | 1 + src/models/router.rs | 43 ++++----- src/models/server.rs | 6 +- 12 files changed, 399 insertions(+), 33 deletions(-) create mode 100644 oblivion-codegen/.gitignore create mode 100644 oblivion-codegen/Cargo.lock create mode 100644 oblivion-codegen/Cargo.toml create mode 100644 oblivion-codegen/src/lib.rs create mode 100644 src/models/handler.rs diff --git a/Cargo.lock b/Cargo.lock index f5614a9..94deeca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,6 +210,95 @@ dependencies = [ "subtle", ] +[[package]] +name = "futures" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" + +[[package]] +name = "futures-executor" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" + +[[package]] +name = "futures-macro" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" + +[[package]] +name = "futures-task" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" + +[[package]] +name = "futures-util" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -354,7 +443,10 @@ name = "oblivion" version = "1.1.0" dependencies = [ "elliptic-curve", + "futures", + "oblivion-codegen", "p256", + "proc-macro2", "rand", "regex", "ring", @@ -364,6 +456,16 @@ dependencies = [ "tokio", ] +[[package]] +name = "oblivion-codegen" +version = "0.1.0" +dependencies = [ + "futures", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "p256" version = "0.13.2" @@ -435,6 +537,12 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkcs8" version = "0.10.2" @@ -684,6 +792,15 @@ dependencies = [ "rand_core", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.11.2" diff --git a/Cargo.toml b/Cargo.toml index d4cccb1..27e13de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ sha2 = "0.10.8" scrypt = "0.11.0" regex = "1.10.2" serde_json = "1.0.108" +oblivion-codegen = { path = "oblivion-codegen" } +proc-macro2 = "1" +futures = "0.3" [lib] name = "oblivion" diff --git a/oblivion-codegen/.gitignore b/oblivion-codegen/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/oblivion-codegen/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/oblivion-codegen/Cargo.lock b/oblivion-codegen/Cargo.lock new file mode 100644 index 0000000..7ad6d12 --- /dev/null +++ b/oblivion-codegen/Cargo.lock @@ -0,0 +1,182 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "async-trait" +version = "0.1.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdf6721fb0140e4f897002dd086c06f6c27775df19cfe1fccb21181a48fd2c98" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "futures" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" + +[[package]] +name = "futures-executor" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" + +[[package]] +name = "futures-macro" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" + +[[package]] +name = "futures-task" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" + +[[package]] +name = "futures-util" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "oblivion-codegen" +version = "0.1.0" +dependencies = [ + "async-trait", + "futures", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "syn" +version = "2.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/oblivion-codegen/Cargo.toml b/oblivion-codegen/Cargo.toml new file mode 100644 index 0000000..e96def7 --- /dev/null +++ b/oblivion-codegen/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "oblivion-codegen" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +futures = "0.3.29" +quote = "1.0" +proc-macro2 = "1" +syn = { version = "2.0", features = ["full"] } + +[lib] +bin = "oblivion_codegen" +proc-macro = true diff --git a/oblivion-codegen/src/lib.rs b/oblivion-codegen/src/lib.rs new file mode 100644 index 0000000..5d51078 --- /dev/null +++ b/oblivion-codegen/src/lib.rs @@ -0,0 +1,25 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, ItemFn}; + +#[proc_macro_attribute] +pub fn async_route(_: TokenStream, item: TokenStream) -> TokenStream { + let input = parse_macro_input!(item as ItemFn); + + let func_name = &input.sig.ident; + let func_args = &input.sig.inputs; + let func_block = input.block; + + let expanded = quote! { + + + pub fn #func_name(#func_args) -> BoxFuture<'static, BaseResponse> + { + async move { + #func_block + }.boxed() + } + }; + + TokenStream::from(expanded) +} diff --git a/src/bin/main.rs b/src/bin/main.rs index ec59818..bb63bd3 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,14 +1,16 @@ +use futures::future::{BoxFuture, FutureExt}; use oblivion::api::get; use oblivion::models::render::BaseResponse; use oblivion::models::router::Router; use oblivion::models::server::Server; use oblivion::route; use oblivion::utils::parser::OblivionRequest; -use std::collections::HashMap; +use oblivion_codegen::async_route; use std::env::args; use std::time::Instant; -fn test2(_: &mut OblivionRequest) -> BaseResponse { +#[async_route] +fn handler(mut _req: OblivionRequest) -> BaseResponse { BaseResponse::TextResponse( "每一个人都应该拥有守护信息与获得真实信息的神圣权利, 任何与之对抗的都是我们的敌人" .to_string(), @@ -16,6 +18,14 @@ fn test2(_: &mut OblivionRequest) -> BaseResponse { ) } +#[async_route] +fn welcome(mut req: OblivionRequest) -> BaseResponse { + BaseResponse::TextResponse( + format!("欢迎进入信息绝对安全区, 来自[{}]的朋友", req.get_ip()), + 200, + ) +} + #[tokio::main] async fn main() { let args: Vec = args().collect(); @@ -27,10 +37,10 @@ async fn main() { println!("执行时间: {}", now.elapsed().as_millis()); } } else { - let mut router = Router::new(Some(HashMap::new())); + let mut router = Router::new(); - router.route("/test2", test2); - route!(&mut router, "/path" => test2); + router.route("/handler", handler); + route!(&mut router, "/welcome" => welcome); let mut server = Server::new("127.0.0.1", 813, router); server.run().await; diff --git a/src/lib.rs b/src/lib.rs index bafb27e..0207fbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +pub extern crate oblivion_codegen; +pub extern crate proc_macro; pub mod api; pub mod exceptions; pub mod sessions; @@ -10,6 +12,7 @@ pub mod utils { } pub mod models { pub mod client; + pub mod handler; pub mod packet; pub mod render; pub mod router; diff --git a/src/models/handler.rs b/src/models/handler.rs new file mode 100644 index 0000000..04d36dc --- /dev/null +++ b/src/models/handler.rs @@ -0,0 +1,15 @@ +use super::render::BaseResponse; +use crate::utils::parser::OblivionRequest; +use futures::future::{BoxFuture, FutureExt}; +use oblivion_codegen::async_route; + +#[async_route] +pub fn not_found(mut request: OblivionRequest) -> BaseResponse { + BaseResponse::TextResponse( + format!( + "Path {} is not found, error with code 404.", + request.get_olps() + ), + 404, + ) +} diff --git a/src/models/render.rs b/src/models/render.rs index 45f4fe7..11ef269 100644 --- a/src/models/render.rs +++ b/src/models/render.rs @@ -2,6 +2,7 @@ use serde_json::Value; use crate::exceptions::OblivionException; +#[derive(Clone)] pub enum BaseResponse { FileResponse(String, i32), TextResponse(String, i32), diff --git a/src/models/router.rs b/src/models/router.rs index 8aa2dff..8e620ee 100644 --- a/src/models/router.rs +++ b/src/models/router.rs @@ -1,26 +1,16 @@ -use std::collections::HashMap; - -use crate::utils::parser::OblivionRequest; - +use super::handler::not_found; use super::render::BaseResponse; - -fn not_found(request: &mut OblivionRequest) -> BaseResponse { - BaseResponse::TextResponse( - format!( - "Path {} is not found, error with code 404.", - request.get_olps() - ), - 404, - ) -} +use crate::utils::parser::OblivionRequest; +use futures::future::BoxFuture; +use std::collections::HashMap; #[derive(Clone)] pub struct Route { - handler: fn(&mut OblivionRequest) -> BaseResponse, + handler: fn(OblivionRequest) -> BoxFuture<'static, BaseResponse>, } impl Route { - pub fn new(handler: fn(&mut OblivionRequest) -> BaseResponse) -> Self { + pub fn new(handler: fn(OblivionRequest) -> BoxFuture<'static, BaseResponse>) -> Self { Self { handler: handler } } @@ -30,7 +20,7 @@ impl Route { } } - pub fn get_handler(&mut self) -> fn(&mut OblivionRequest) -> BaseResponse { + pub fn get_handler(&mut self) -> fn(OblivionRequest) -> BoxFuture<'static, BaseResponse> { self.handler.clone() } } @@ -38,19 +28,22 @@ impl Route { #[derive(Clone)] pub struct Router { routes: HashMap, + // not_found_route: Route, } impl Router { - pub fn new(routes: Option>) -> Self { - let routes = if routes.is_none() { - HashMap::new() - } else { - routes.unwrap() - }; - Self { routes: routes } + pub fn new() -> Self { + Self { + routes: HashMap::new(), + // not_found_route: Route { handler: not_found }, + } } - pub fn route(&mut self, path: &str, handler: fn(&mut OblivionRequest) -> BaseResponse) { + pub fn route( + &mut self, + path: &str, + handler: fn(OblivionRequest) -> BoxFuture<'static, BaseResponse>, + ) { self.routes .insert(path.to_owned(), Route { handler: handler }); } diff --git a/src/models/server.rs b/src/models/server.rs index 6854ef4..62e8882 100644 --- a/src/models/server.rs +++ b/src/models/server.rs @@ -81,11 +81,11 @@ impl ServerConnection { pub async fn response( route: &mut Route, stream: &mut Socket, - request: &mut OblivionRequest, + request: OblivionRequest, aes_key: Vec, ) -> Result { let handler = route.get_handler(); - let mut callback = handler(request); + let mut callback = handler(request).await; let mut oed = OED::new(Some(aes_key)).from_bytes(callback.as_bytes()?)?; oed.to_stream(stream, 5).await?; @@ -111,7 +111,7 @@ async fn _handle( let status_code = match response( &mut route, stream, - &mut request, + request.clone(), connection.aes_key.unwrap(), ) .await