Add webhook cli arguments

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-05-22 00:37:40 +02:00
parent 40a7c95200
commit 179bd2e395
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 32 additions and 19 deletions

View file

@ -54,6 +54,14 @@ struct Args {
/// Nickname to use in rooms /// Nickname to use in rooms
#[arg(short, long, default_value = "bot")] #[arg(short, long, default_value = "bot")]
nickname: String, nickname: String,
/// Token to match the one provided by the Webhook service
#[arg(short, long)]
webhook_token: Option<String>,
/// 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] #[tokio::main]
@ -61,23 +69,27 @@ async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
let args = Args::parse(); let args = Args::parse();
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>(); let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
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( let mut client = XmppClient::new(
&String::from(args.jid), &String::from(args.jid),
args.password.as_str(), args.password.as_str(),

View file

@ -35,7 +35,7 @@ fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
Ok(res) Ok(res)
} }
async fn webhooks_inner(req: Request<Body>) -> Result<WebHook, Error> { async fn webhooks_inner(req: Request<Body>, token: &str) -> Result<WebHook, Error> {
match req.method() { match req.method() {
&Method::POST => (), &Method::POST => (),
_ => return Err(Error::MethodMismatch), _ => return Err(Error::MethodMismatch),
@ -45,12 +45,12 @@ async fn webhooks_inner(req: Request<Body>) -> Result<WebHook, Error> {
let headers = req.headers(); let headers = req.headers();
if let Some(content_type) = headers.get(header::CONTENT_TYPE) && 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" { if content_type != "application/json" {
return Err(Error::InvalidContentType); return Err(Error::InvalidContentType);
} }
if token != "secret" { if header_token != token {
return Err(Error::InvalidToken); return Err(Error::InvalidToken);
} }
} }
@ -62,9 +62,10 @@ async fn webhooks_inner(req: Request<Body>) -> Result<WebHook, Error> {
pub async fn webhooks( pub async fn webhooks(
req: Request<Body>, req: Request<Body>,
token: String,
value_tx: Arc<Mutex<UnboundedSender<WebHook>>>, value_tx: Arc<Mutex<UnboundedSender<WebHook>>>,
) -> Result<Response<Body>, Infallible> { ) -> Result<Response<Body>, Infallible> {
match webhooks_inner(req).await { match webhooks_inner(req, token.as_ref()).await {
Ok(wh) => { Ok(wh) => {
debug!("Passed: {:?}", wh); debug!("Passed: {:?}", wh);