Patriotism is a superstition artificially created and maintained through a network of lies and falsehoods; a superstition that robs man of his self-respect and dignity, and increases his arrogance and conceit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
commit
b8aefb0159
2 changed files with 146 additions and 0 deletions
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
[package]
|
||||||
|
name = "cuskurs"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Webhook to XMPP"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["Maxime “pep” Buquet <pep@bouah.net>"]
|
||||||
|
license = "AGPL-3.0+"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gitlab = "0.1511.0"
|
||||||
|
hyper = { version = "0.14", features = [ "full" ] }
|
||||||
|
log = "0.4"
|
||||||
|
tokio = { version = "1", features = [ "full" ] }
|
||||||
|
pretty_env_logger = "0.5"
|
||||||
|
serde_json = "1.0"
|
131
src/main.rs
Normal file
131
src/main.rs
Normal file
|
@ -0,0 +1,131 @@
|
||||||
|
// Copyright (C) 2023-2099 The crate authors.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Affero General Public License as published by the
|
||||||
|
// Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
// option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||||
|
// for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#![feature(let_chains)]
|
||||||
|
|
||||||
|
use std::convert::Infallible;
|
||||||
|
use std::error::Error as StdError;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::str::{from_utf8, Utf8Error};
|
||||||
|
|
||||||
|
use gitlab::webhooks::WebHook;
|
||||||
|
use hyper::{
|
||||||
|
body, Body, header, Method, Request, Response, Server,
|
||||||
|
service::{make_service_fn, service_fn},
|
||||||
|
};
|
||||||
|
use log::{debug, error};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Error {
|
||||||
|
MethodMismatch,
|
||||||
|
InvalidToken,
|
||||||
|
InvalidContentType,
|
||||||
|
Hyper(hyper::Error),
|
||||||
|
SerdeJson(serde_json::Error),
|
||||||
|
Utf8(Utf8Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for Error {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::MethodMismatch => write!(fmt, "the method is invalid"),
|
||||||
|
Error::InvalidToken => write!(fmt, "the token is invalid"),
|
||||||
|
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||||
|
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
||||||
|
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
||||||
|
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<hyper::Error> for Error {
|
||||||
|
fn from(err: hyper::Error) -> Error {
|
||||||
|
Error::Hyper(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<serde_json::Error> for Error {
|
||||||
|
fn from(err: serde_json::Error) -> Error {
|
||||||
|
Error::SerdeJson(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Utf8Error> for Error {
|
||||||
|
fn from(err: Utf8Error) -> Error {
|
||||||
|
Error::Utf8(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
|
||||||
|
error!("error response: {:?}", e);
|
||||||
|
|
||||||
|
let text = format!("{:?}", e);
|
||||||
|
let res = Response::builder()
|
||||||
|
.status(400)
|
||||||
|
.body(Body::from(Vec::from(text.as_bytes())))
|
||||||
|
.unwrap();
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn webhooks(req: Request<Body>) -> Result<Response<Body>, Error> {
|
||||||
|
match req.method() {
|
||||||
|
&Method::POST => (),
|
||||||
|
_ => return Err(Error::MethodMismatch),
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Headers: {:?}", req.headers());
|
||||||
|
|
||||||
|
let headers = req.headers();
|
||||||
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE) &&
|
||||||
|
let Some(token) = headers.get("X-Gitlab-Token") {
|
||||||
|
if content_type != "application/json" {
|
||||||
|
return Err(Error::InvalidContentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
if token != "secret" {
|
||||||
|
return Err(Error::InvalidToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let tmp = body::to_bytes(req.into_body()).await?;
|
||||||
|
let text: &str = from_utf8(&tmp)?;
|
||||||
|
let json: WebHook = serde_json::from_str(text)?;
|
||||||
|
debug!("Passed: {:?}", json);
|
||||||
|
|
||||||
|
Ok(Response::new("Hello world".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wrapper(req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||||
|
webhooks(req).await.or_else(error_res)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
let make_svc = make_service_fn(|_conn| async {
|
||||||
|
Ok::<_, Infallible>(service_fn(wrapper))
|
||||||
|
});
|
||||||
|
let server = Server::bind(&addr).serve(make_svc);
|
||||||
|
|
||||||
|
println!("Listening on http://{}", addr);
|
||||||
|
|
||||||
|
if let Err(e) = server.await {
|
||||||
|
eprintln!("Server error: {e}");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue