diff options
author | metamuffin <metamuffin@disroot.org> | 2022-10-17 11:28:03 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-10-17 11:28:03 +0200 |
commit | 6e50a340ca698ed94c11cfb156e3f5e498807e4d (patch) | |
tree | 75f680108e488572439b05359059cdf3fa6d450c | |
parent | b739641a78bad5381aa2a8f7de3d464f9c5ce01f (diff) | |
download | trash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar trash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar.bz2 trash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar.zst |
clean
-rw-r--r-- | Cargo.lock | 40 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/config.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 57 |
4 files changed, 71 insertions, 63 deletions
@@ -1649,26 +1649,6 @@ dependencies = [ ] [[package]] -name = "rubbish-proxy" -version = "0.1.0" -dependencies = [ - "anyhow", - "azalea", - "azalea-chat", - "azalea-protocol", - "bytes 1.2.1", - "env_logger", - "future-utils", - "inotify", - "log 0.4.17", - "parking_lot 0.12.1", - "serde", - "serde_yaml", - "tokio 1.21.2", - "uuid", -] - -[[package]] name = "rustc_version" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2203,6 +2183,26 @@ dependencies = [ ] [[package]] +name = "trash-proxy" +version = "0.1.0" +dependencies = [ + "anyhow", + "azalea", + "azalea-chat", + "azalea-protocol", + "bytes 1.2.1", + "env_logger", + "future-utils", + "inotify", + "log 0.4.17", + "parking_lot 0.12.1", + "serde", + "serde_yaml", + "tokio 1.21.2", + "uuid", +] + +[[package]] name = "trust-dns-proto" version = "0.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1,9 +1,8 @@ [package] -name = "rubbish-proxy" +name = "trash-proxy" version = "0.1.0" edition = "2021" - [dependencies] azalea = { git = "https://github.com/mat-1/azalea/" } azalea-protocol = { git = "https://github.com/mat-1/azalea/" } @@ -23,4 +22,3 @@ serde_yaml = "0.9.13" future-utils = "0.12.1" bytes = "1.2.1" inotify = "0.10.0" - diff --git a/src/config.rs b/src/config.rs index b3d4e31..8bbb052 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ -use std::net::SocketAddr; - +use inotify::{EventMask, Inotify, WatchMask}; +use log::{error, info}; use serde::{Deserialize, Serialize}; +use std::{fs::read_to_string, net::SocketAddr, sync::{Arc, RwLock}}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { @@ -15,3 +16,31 @@ pub struct PlayerConfig { pub token: Option<String>, pub username: String, } + +pub fn watch(config: Arc<RwLock<Arc<Config>>>) { + std::thread::spawn(move || { + let mut inotify = Inotify::init().unwrap(); + inotify + .add_watch( + ".", + WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, + ) + .unwrap(); + let mut buffer = [0u8; 4096]; + loop { + let events = inotify + .read_events_blocking(&mut buffer) + .expect("Failed to read inotify events"); + + for event in events { + if event.mask.contains(EventMask::MODIFY) { + info!("reloading config"); + match serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()) { + Ok(conf) => *config.write().unwrap() = Arc::new(conf), + Err(e) => error!("config has errors: {e}"), + } + } + } + } + }); +} diff --git a/src/main.rs b/src/main.rs index 16006cb..47cec90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![feature(never_type)] + pub mod config; use anyhow::bail; @@ -12,7 +14,6 @@ use azalea_protocol::{ }; use bytes::BytesMut; use config::Config; -use inotify::{EventMask, Inotify, WatchMask}; use log::{error, info, warn}; use std::{ fs::read_to_string, @@ -26,50 +27,30 @@ use tokio::{ }, }; -#[tokio::main] -async fn main() { +fn main() { env_logger::builder() .filter_level(log::LevelFilter::Info) .parse_env("LOG") .init(); - - let config = Arc::new(RwLock::new(Arc::new( - serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()).unwrap(), - ))); - - { - let config = config.clone(); - std::thread::spawn(move || { - let mut inotify = Inotify::init().unwrap(); - inotify - .add_watch( - ".", - WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, - ) - .unwrap(); - let mut buffer = [0u8; 4096]; - loop { - let events = inotify - .read_events_blocking(&mut buffer) - .expect("Failed to read inotify events"); - - for event in events { - if event.mask.contains(EventMask::MODIFY) { - info!("reloading config"); - match serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()) - { - Ok(conf) => *config.write().unwrap() = Arc::new(conf), - Err(e) => error!("config has errors: {e}"), - } - } - } + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(async move { + match run().await { + Ok(_) => {} + Err(err) => error!("fatal error: {err}"), } }); - } +} + +async fn run() -> anyhow::Result<!> { + let config = Arc::new(RwLock::new(Arc::new(serde_yaml::from_str::<Config>( + &read_to_string("proxy.yaml")?, + )?))); + config::watch(config.clone()); - let listener = TcpListener::bind(config.read().unwrap().bind) - .await - .unwrap(); + let listener = TcpListener::bind(config.read().unwrap().bind).await?; info!("listening"); loop { match listener.accept().await { |