finishing touches, for real this time
This commit is contained in:
parent
a1c1bc5809
commit
7de2d876ff
50
Cargo.lock
generated
50
Cargo.lock
generated
@ -292,6 +292,27 @@ dependencies = [
|
|||||||
"crypto-common",
|
"crypto-common",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs"
|
||||||
|
version = "5.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
|
||||||
|
dependencies = [
|
||||||
|
"dirs-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs-sys"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"option-ext",
|
||||||
|
"redox_users",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "encoding_rs"
|
name = "encoding_rs"
|
||||||
version = "0.8.33"
|
version = "0.8.33"
|
||||||
@ -755,6 +776,17 @@ version = "0.2.153"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libredox"
|
||||||
|
version = "0.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.4.2",
|
||||||
|
"libc",
|
||||||
|
"redox_syscall",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
@ -823,6 +855,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"data-encoding",
|
"data-encoding",
|
||||||
|
"dirs",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
"hyper 1.2.0",
|
"hyper 1.2.0",
|
||||||
"hyper-util",
|
"hyper-util",
|
||||||
@ -896,6 +929,12 @@ version = "1.19.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "option-ext"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
@ -1017,6 +1056,17 @@ dependencies = [
|
|||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_users"
|
||||||
|
version = "0.4.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"libredox",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reqwest"
|
name = "reqwest"
|
||||||
version = "0.11.26"
|
version = "0.11.26"
|
||||||
|
@ -19,3 +19,4 @@ data-encoding = "2.5"
|
|||||||
stderrlog = "0.6.0"
|
stderrlog = "0.6.0"
|
||||||
log = { version = "0.4.21", features = ["serde"] }
|
log = { version = "0.4.21", features = ["serde"] }
|
||||||
thiserror = "1.0.37"
|
thiserror = "1.0.37"
|
||||||
|
dirs = "5.0"
|
111
src/main.rs
111
src/main.rs
@ -1,4 +1,4 @@
|
|||||||
use std::{convert::Infallible, fs, net::SocketAddr};
|
use std::{convert::Infallible, fs, net::SocketAddr, path::PathBuf};
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use http_body_util::{BodyExt, Full};
|
use http_body_util::{BodyExt, Full};
|
||||||
@ -21,23 +21,48 @@ mod config;
|
|||||||
mod discord;
|
mod discord;
|
||||||
mod miniflux_requests;
|
mod miniflux_requests;
|
||||||
|
|
||||||
//FIXME!
|
|
||||||
const CONFIG_PATH: &'static str = "./config.json";
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let config: Config = serde_json::from_slice(&fs::read(CONFIG_PATH).unwrap()).unwrap();
|
let path_opt = {
|
||||||
|
let default_path = dirs::config_dir().map(|p| p.join("miniflux_discord.json"));
|
||||||
|
let path = std::env::var("CONFIG_PATH").ok().map(|s| PathBuf::from(s)).or(default_path);
|
||||||
|
path
|
||||||
|
};
|
||||||
|
let path = match path_opt {
|
||||||
|
Some(p) => p,
|
||||||
|
None => panic!("config path could not be resolved! Use CONFIG_PATH environment variable"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res: Result<_, StartingError> = fs::read(path)
|
||||||
|
.map_err(|e| e.into())
|
||||||
|
.and_then(|s| serde_json::from_slice(&s).map_err(|e| e.into()));
|
||||||
|
|
||||||
|
let config: Config = match res {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => panic!("{}", e),
|
||||||
|
};
|
||||||
|
|
||||||
stderrlog::new()
|
stderrlog::new()
|
||||||
.module(module_path!())
|
.module(module_path!())
|
||||||
.verbosity(config.log_level)
|
.verbosity(config.log_level)
|
||||||
.timestamp(stderrlog::Timestamp::Second)
|
.timestamp(stderrlog::Timestamp::Second)
|
||||||
.init()
|
.init()
|
||||||
.unwrap();
|
.expect("stderrlog could not be initialized!");
|
||||||
|
|
||||||
let mut client = Client::builder(&config.discord_token, GatewayIntents::empty())
|
dbg!("hello?");
|
||||||
.await
|
match start_serving(config).await {
|
||||||
.expect("Error creating Discord client");
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("{}", e);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start_serving(config: Config) -> Result<(), StartingError> {
|
||||||
|
dbg!("hello?");
|
||||||
|
|
||||||
|
let mut client = Client::builder(&config.discord_token, GatewayIntents::empty()).await?;
|
||||||
|
|
||||||
let addr = SocketAddr::from((config.host, config.port));
|
let addr = SocketAddr::from((config.host, config.port));
|
||||||
let listener = TcpListener::bind(addr).await?;
|
let listener = TcpListener::bind(addr).await?;
|
||||||
@ -92,33 +117,47 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum StartingError {
|
||||||
|
#[error("configuration file path not found!")]
|
||||||
|
ConfigPathNotFound,
|
||||||
|
#[error("IO Error: {0}")]
|
||||||
|
IOError(#[from] std::io::Error),
|
||||||
|
#[error("Config Json deserialization error: {0}")]
|
||||||
|
ConfigJsonError(#[from] serde_json::Error),
|
||||||
|
#[error("Logger init failed: {0}")]
|
||||||
|
LoggerError(#[from] log::SetLoggerError),
|
||||||
|
#[error("Discord Client Error: {0}")]
|
||||||
|
DiscordClientError(#[from] serenity::Error),
|
||||||
|
}
|
||||||
|
|
||||||
async fn process_webhook(
|
async fn process_webhook(
|
||||||
req: Request<body::Incoming>,
|
req: Request<body::Incoming>,
|
||||||
holder: &DiscordHolder,
|
holder: &DiscordHolder,
|
||||||
) -> Result<Response<Full<Bytes>>, CustomError> {
|
) -> Result<Response<Full<Bytes>>, ServingError> {
|
||||||
let method = req.method();
|
let method = req.method();
|
||||||
if method != Method::POST {
|
if method != Method::POST {
|
||||||
Err(CustomError::InvalidMethod(method.clone()))?;
|
Err(ServingError::InvalidMethod(method.clone()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let userid: u64 =
|
let userid: u64 =
|
||||||
req.uri().path().split("/").nth(1).ok_or(CustomError::IdPathNotFound)?.parse()?;
|
req.uri().path().split("/").nth(1).ok_or(ServingError::IdPathNotFound)?.parse()?;
|
||||||
|
|
||||||
if !holder.config.whitelisted_user_ids.contains(&userid) {
|
if !holder.config.whitelisted_user_ids.contains(&userid) {
|
||||||
Err(CustomError::NotWhitelistedUser(userid))?;
|
Err(ServingError::NotWhitelistedUser(userid))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = req.headers();
|
let headers = req.headers();
|
||||||
|
|
||||||
let signature_bytes =
|
let signature_bytes =
|
||||||
headers.get("x-miniflux-signature").ok_or(CustomError::SignatureMissing)?.as_bytes();
|
headers.get("x-miniflux-signature").ok_or(ServingError::SignatureMissing)?.as_bytes();
|
||||||
let signature = data_encoding::HEXLOWER.decode(signature_bytes)?;
|
let signature = data_encoding::HEXLOWER.decode(signature_bytes)?;
|
||||||
let event_type =
|
let event_type =
|
||||||
headers.get("x-miniflux-event-type").ok_or(CustomError::EventTypeMissing)?.clone();
|
headers.get("x-miniflux-event-type").ok_or(ServingError::EventTypeMissing)?.clone();
|
||||||
|
|
||||||
let upper = req.body().size_hint().upper().unwrap_or(u64::MAX);
|
let upper = req.body().size_hint().upper().unwrap_or(u64::MAX);
|
||||||
if upper > holder.config.payload_max_size {
|
if upper > holder.config.payload_max_size {
|
||||||
Err(CustomError::PayloadTooLarge(upper))?;
|
Err(ServingError::PayloadTooLarge(upper))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let whole_body = req.collect().await?.to_bytes();
|
let whole_body = req.collect().await?.to_bytes();
|
||||||
@ -127,7 +166,7 @@ async fn process_webhook(
|
|||||||
let key = hmac::Key::new(hmac::HMAC_SHA256, &holder.config.miniflux_webhook_secret.as_bytes());
|
let key = hmac::Key::new(hmac::HMAC_SHA256, &holder.config.miniflux_webhook_secret.as_bytes());
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
hmac::verify(&key, &bytes, &signature).map_err(|_| CustomError::HmacValidationError)?;
|
hmac::verify(&key, &bytes, &signature).map_err(|_| ServingError::HmacValidationError)?;
|
||||||
|
|
||||||
let event: MinifluxEvent = serde_json::from_slice(&bytes)?;
|
let event: MinifluxEvent = serde_json::from_slice(&bytes)?;
|
||||||
match event {
|
match event {
|
||||||
@ -146,7 +185,7 @@ async fn process_webhook(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum CustomError {
|
pub enum ServingError {
|
||||||
#[error("id path not found")]
|
#[error("id path not found")]
|
||||||
IdPathNotFound,
|
IdPathNotFound,
|
||||||
#[error("invalid method: {0}")]
|
#[error("invalid method: {0}")]
|
||||||
@ -171,31 +210,31 @@ pub enum CustomError {
|
|||||||
JsonError(#[from] serde_json::Error),
|
JsonError(#[from] serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<Response<Full<Bytes>>> for CustomError {
|
impl Into<Response<Full<Bytes>>> for ServingError {
|
||||||
fn into(self) -> Response<Full<Bytes>> {
|
fn into(self) -> Response<Full<Bytes>> {
|
||||||
match &self {
|
match &self {
|
||||||
CustomError::JsonError(_) => log::error!("{}", self),
|
ServingError::JsonError(_) => log::error!("{}", self),
|
||||||
CustomError::HmacValidationError => log::warn!("{}", self),
|
ServingError::HmacValidationError => log::warn!("{}", self),
|
||||||
CustomError::HyperError(_) => log::warn!("{}", self),
|
ServingError::HyperError(_) => log::warn!("{}", self),
|
||||||
CustomError::PayloadTooLarge(_) => log::warn!("{}", self),
|
ServingError::PayloadTooLarge(_) => log::warn!("{}", self),
|
||||||
CustomError::SignatureMissing => log::warn!("{}", self),
|
ServingError::SignatureMissing => log::warn!("{}", self),
|
||||||
CustomError::EventTypeMissing => log::warn!("{}", self),
|
ServingError::EventTypeMissing => log::warn!("{}", self),
|
||||||
_ => log::debug!("{}", self),
|
_ => log::debug!("{}", self),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut resp = Response::new(Full::from(Bytes::from(self.to_string())));
|
let mut resp = Response::new(Full::from(Bytes::from(self.to_string())));
|
||||||
let status_code = match self {
|
let status_code = match self {
|
||||||
CustomError::IdPathNotFound => StatusCode::NOT_FOUND,
|
ServingError::IdPathNotFound => StatusCode::NOT_FOUND,
|
||||||
CustomError::InvalidMethod(_) => StatusCode::METHOD_NOT_ALLOWED,
|
ServingError::InvalidMethod(_) => StatusCode::METHOD_NOT_ALLOWED,
|
||||||
CustomError::ParseIntError(_) => StatusCode::BAD_REQUEST,
|
ServingError::ParseIntError(_) => StatusCode::BAD_REQUEST,
|
||||||
CustomError::NotWhitelistedUser(_) => StatusCode::FORBIDDEN,
|
ServingError::NotWhitelistedUser(_) => StatusCode::FORBIDDEN,
|
||||||
CustomError::SignatureMissing => StatusCode::BAD_REQUEST,
|
ServingError::SignatureMissing => StatusCode::BAD_REQUEST,
|
||||||
CustomError::EventTypeMissing => StatusCode::BAD_REQUEST,
|
ServingError::EventTypeMissing => StatusCode::BAD_REQUEST,
|
||||||
CustomError::DecodeError(_) => StatusCode::BAD_REQUEST,
|
ServingError::DecodeError(_) => StatusCode::BAD_REQUEST,
|
||||||
CustomError::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
|
ServingError::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
|
||||||
CustomError::HyperError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
ServingError::HyperError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
CustomError::HmacValidationError => StatusCode::FORBIDDEN,
|
ServingError::HmacValidationError => StatusCode::FORBIDDEN,
|
||||||
CustomError::JsonError(_) => StatusCode::BAD_REQUEST,
|
ServingError::JsonError(_) => StatusCode::BAD_REQUEST,
|
||||||
};
|
};
|
||||||
|
|
||||||
*resp.status_mut() = status_code;
|
*resp.status_mut() = status_code;
|
||||||
|
Loading…
Reference in New Issue
Block a user