2021-02-02 11:05:50 +01:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2021-03-03 16:14:54 +01:00
|
|
|
use log::{debug, info, trace};
|
2021-03-03 15:24:46 +01:00
|
|
|
use regex::Regex;
|
|
|
|
use rocket::{fairing::AdHoc, get, post, routes, State};
|
2021-02-02 11:05:50 +01:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
use std::{collections::HashMap, fs::File, io::BufReader, net::SocketAddr, process::Command};
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
struct Config {
|
|
|
|
hooks: HashMap<String, Hook>,
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:05:50 +01:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2021-03-03 15:24:46 +01:00
|
|
|
struct Hook {
|
|
|
|
action: Option<String>,
|
|
|
|
filters: HashMap<String, Filter>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
struct Filter {
|
|
|
|
pointer: String,
|
|
|
|
regex: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
struct Data(serde_json::Value);
|
2021-02-02 11:05:50 +01:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> &'static str {
|
|
|
|
"Hello, webhookey!"
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()> {
|
2021-03-03 16:14:54 +01:00
|
|
|
debug!("Running hook `{}`", name);
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
for (filter_name, filter) in hook.filters.iter() {
|
2021-03-03 16:14:54 +01:00
|
|
|
debug!("Matching filter `{}`", filter_name);
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
if let Some(value) = data.pointer(&filter.pointer) {
|
|
|
|
let regex = Regex::new(&filter.regex)?;
|
|
|
|
|
|
|
|
if let Some(value) = value.as_str() {
|
|
|
|
if !regex.is_match(value) {
|
2021-03-03 16:14:54 +01:00
|
|
|
info!("Filter `{}` in hook `{}` did not match", filter_name, name);
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
anyhow!(
|
|
|
|
"Could not parse pointer in hook `{}` from filter `{}`",
|
|
|
|
name,
|
|
|
|
filter_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(action) = &hook.action {
|
2021-03-03 16:14:54 +01:00
|
|
|
info!("Execute `{}` from hook `{}`", action, name);
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
Command::new(action).spawn()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:05:50 +01:00
|
|
|
#[post("/", format = "json", data = "<data>")]
|
2021-03-03 16:14:54 +01:00
|
|
|
fn receive_hook(address: SocketAddr, config: State<Config>, data: Json<Data>) -> Result<String> {
|
|
|
|
info!("POST request received from: {}", address);
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
let data = serde_json::to_value(data.0)?;
|
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
trace!("Data received from: {}\n{}", address, data);
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
for (hook_name, hook) in config.hooks.iter() {
|
|
|
|
execute_hook(&hook_name, &hook, &data)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok("Request received.".to_string())
|
2021-02-02 11:05:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
fn get_config() -> Result<File> {
|
|
|
|
if let Ok(config) = File::open("/etc/webhookey/config.yml") {
|
2021-03-03 16:14:54 +01:00
|
|
|
info!("Loading configuration from `/etc/webhookey/config.yml`");
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut path) = dirs::config_dir() {
|
|
|
|
path.push("webhookey/config.yml");
|
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
if let Ok(config) = File::open(&path) {
|
|
|
|
info!(
|
|
|
|
"Loading configuration from `{}`",
|
|
|
|
path.to_str().unwrap_or("path not printable"),
|
|
|
|
);
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(config) = File::open("config.yml") {
|
2021-03-03 16:14:54 +01:00
|
|
|
info!("Loading configuration from `./config.yml`");
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("No configuration files found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2021-03-03 16:14:54 +01:00
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let config = get_config()?;
|
|
|
|
|
|
|
|
let config: Config = serde_yaml::from_reader(BufReader::new(config))?;
|
2021-03-03 15:24:46 +01:00
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?);
|
2021-03-03 15:24:46 +01:00
|
|
|
|
2021-02-02 11:05:50 +01:00
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", routes![index, receive_hook])
|
2021-03-03 15:24:46 +01:00
|
|
|
.attach(AdHoc::on_attach("webhookey config", move |rocket| {
|
|
|
|
Ok(rocket.manage(config))
|
|
|
|
}))
|
2021-02-02 11:05:50 +01:00
|
|
|
.launch();
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2021-02-02 11:05:50 +01:00
|
|
|
}
|