From 179bd2e395b48c475a61c57c36f6812dfafc5312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 22 May 2023 00:37:40 +0200 Subject: [PATCH] Add webhook cli arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/main.rs | 42 +++++++++++++++++++++++++++--------------- src/web.rs | 9 +++++---- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 02bb5db..2cef85f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,14 @@ struct Args { /// Nickname to use in rooms #[arg(short, long, default_value = "bot")] nickname: String, + + /// Token to match the one provided by the Webhook service + #[arg(short, long)] + webhook_token: Option, + + /// HTTP Webhook listening address and port, e.g., 127.0.0.1:1234 or [::1]:1234 + #[arg(long, default_value = "[::1]:3000")] + addr: SocketAddr, } #[tokio::main] @@ -61,23 +69,27 @@ async fn main() { pretty_env_logger::init(); let args = Args::parse(); - - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let (value_tx, mut value_rx) = mpsc::unbounded_channel::(); - let value_tx = Arc::new(Mutex::new(value_tx)); - let make_svc = make_service_fn(move |_conn| { - let value_tx = value_tx.clone(); - async move { - Ok::<_, Infallible>(service_fn(move |req| { - let value_tx = value_tx.clone(); - webhooks(req, value_tx) - })) - } - }); - let server = Server::bind(&addr).serve(make_svc); - println!("Listening on http://{}", addr); - let _join = tokio::spawn(server); + if let Some(token) = args.webhook_token { + let value_tx = Arc::new(Mutex::new(value_tx)); + let make_svc = make_service_fn(move |_conn| { + let value_tx = value_tx.clone(); + let token = token.clone(); + async move { + Ok::<_, Infallible>(service_fn(move |req| { + let value_tx = value_tx.clone(); + let token = token.clone(); + webhooks(req, token, value_tx) + })) + } + }); + let server = Server::bind(&args.addr).serve(make_svc); + println!("Listening on http://{}", &args.addr); + + let _join = tokio::spawn(server); + } + let mut client = XmppClient::new( &String::from(args.jid), args.password.as_str(), diff --git a/src/web.rs b/src/web.rs index d3df0f9..77994df 100644 --- a/src/web.rs +++ b/src/web.rs @@ -35,7 +35,7 @@ fn error_res(e: E) -> Result, Infallible> { Ok(res) } -async fn webhooks_inner(req: Request) -> Result { +async fn webhooks_inner(req: Request, token: &str) -> Result { match req.method() { &Method::POST => (), _ => return Err(Error::MethodMismatch), @@ -45,12 +45,12 @@ async fn webhooks_inner(req: Request) -> Result { let headers = req.headers(); if let Some(content_type) = headers.get(header::CONTENT_TYPE) && - let Some(token) = headers.get("X-Gitlab-Token") { + let Some(header_token) = headers.get("X-Gitlab-Token") { if content_type != "application/json" { return Err(Error::InvalidContentType); } - if token != "secret" { + if header_token != token { return Err(Error::InvalidToken); } } @@ -62,9 +62,10 @@ async fn webhooks_inner(req: Request) -> Result { pub async fn webhooks( req: Request, + token: String, value_tx: Arc>>, ) -> Result, Infallible> { - match webhooks_inner(req).await { + match webhooks_inner(req, token.as_ref()).await { Ok(wh) => { debug!("Passed: {:?}", wh);