From 675b807ac0d356702fff34ee541fb49d647a7d47 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Tue, 14 Dec 2021 19:46:10 +0800 Subject: [PATCH] feat(core): Prepare return the decision to http server Signed-off-by: KunoiSayami --- Cargo.lock | 2 +- Cargo.toml | 2 +- SECURITY.md | 4 ++-- src/main.rs | 28 +++++++++++++++++++++------- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1f2cc5..7e3a695 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -787,7 +787,7 @@ dependencies = [ [[package]] name = "github-webhook-notification" -version = "2.0.0" +version = "2.1.0-alpha" dependencies = [ "actix", "actix-rt", diff --git a/Cargo.toml b/Cargo.toml index e3efe01..6e9d4be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "github-webhook-notification" -version = "2.0.0" +version = "2.1.0-alpha" edition = "2021" [dependencies] diff --git a/SECURITY.md b/SECURITY.md index f43bbcf..41794a3 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -4,8 +4,8 @@ | Version | Supported | |---------|--------------------| -| 2.0.x | :white_check_mark: | -| < 2.0 | :x: | +| 2.1.x | :white_check_mark: | +| < 2.1 | :x: | ## Reporting a Vulnerability diff --git a/src/main.rs b/src/main.rs index e57f8b5..0382bbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ ** along with this program. If not, see . */ -use crate::configure::Repository; +use crate::configure::{Config, Repository}; use crate::datastructures::{DisplayableEvent, GitHubPingEvent, GitHubPushEvent, Response}; use actix_web::http::Method; use actix_web::web::Data; @@ -42,11 +42,12 @@ const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION"); #[derive(Debug)] enum Command { Terminate, + #[deprecated(since = "2.1.0", note = "You should use Bundle instead send GitHub Data directly, this field will removed in next version.")] Data(Box), + Bundle((Vec, String)), } struct ExtraData { - secrets: String, bot_tx: mpsc::Sender, } @@ -102,7 +103,17 @@ async fn process_send_message( } } } + Command::Bundle((receiver, text)) => { + for send_to in receiver { + let mut payload = bot.send_message(send_to, &text); + payload.disable_web_page_preview = Option::from(true); + if let Err(e) = payload.send().await { + error!("Got error in send message {:?}", e); + } + } + }, Command::Terminate => break, + } } debug!("Send message daemon exiting..."); @@ -116,6 +127,7 @@ fn check_0(s: &str) -> bool { async fn route_post( request: HttpRequest, mut payload: web::Payload, + configure: web::Data, data: web::Data>>, ) -> actix_web::Result { let mut body = web::BytesMut::new(); @@ -130,9 +142,10 @@ async fn route_post( } let sender = data.lock().await; - if !sender.secrets.is_empty() { + let secrets =configure.server().secrets(); + if !secrets.is_empty() { type HmacSha256 = Hmac; - let mut h = HmacSha256::new_from_slice(sender.secrets.as_bytes()).unwrap(); + let mut h = HmacSha256::new_from_slice(secrets.as_bytes()).unwrap(); h.update(&*body); let result = h.finalize(); let sha256val = format!("sha256={:x}", result.into_bytes()).to_lowercase(); @@ -192,7 +205,6 @@ async fn async_main>(path: P) -> anyhow::Result<()> { crate::datastructures::AuthorizationGuard::from(config.server().token()); let extra_data = Arc::new(Mutex::new(ExtraData { - secrets: config.server().secrets().clone(), bot_tx: bot_tx.clone(), })); let msg_sender = tokio::spawn(process_send_message( @@ -203,7 +215,8 @@ async fn async_main>(path: P) -> anyhow::Result<()> { bot_rx, )); - info!("Bind address: {}", config.server().bind()); + let bind = config.server().bind().clone(); + info!("Bind address: {}", bind); let server = tokio::spawn( HttpServer::new(move || { @@ -212,6 +225,7 @@ async fn async_main>(path: P) -> anyhow::Result<()> { .service( web::scope("/") .guard(authorization_guard.to_owned()) + .app_data(Data::new(config.clone())) .app_data(Data::new(extra_data.clone())) .route("", web::method(Method::POST).to(route_post)), ) @@ -221,7 +235,7 @@ async fn async_main>(path: P) -> anyhow::Result<()> { )) .route("/", web::to(HttpResponse::Forbidden)) }) - .bind(config.server().bind())? + .bind(bind)? .run(), );