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);