From 2bed6ccdc5ae835b7022b0dfdb45ca67c41aec67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Fri, 5 Nov 2021 13:18:22 +0100 Subject: [PATCH] WIP: ws: split into features, other fixes --- Cargo.lock | 1 + aquatic_ws/Cargo.toml | 32 +++++++++++++++--------- aquatic_ws/src/lib/common/mod.rs | 2 -- aquatic_ws/src/lib/config.rs | 36 +++++++++++++++++++++++---- aquatic_ws/src/lib/glommio/common.rs | 2 ++ aquatic_ws/src/lib/glommio/mod.rs | 3 +-- aquatic_ws/src/lib/glommio/network.rs | 2 +- aquatic_ws/src/lib/lib.rs | 19 ++++++++++++-- aquatic_ws/src/lib/mio/mod.rs | 2 +- scripts/gen-tls.sh | 2 ++ 10 files changed, 76 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fa200b..4a7a0d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,6 +240,7 @@ dependencies = [ "aquatic_common", "aquatic_ws_protocol", "async-tungstenite", + "cfg-if", "core_affinity", "crossbeam-channel", "either", diff --git a/aquatic_ws/Cargo.toml b/aquatic_ws/Cargo.toml index 9fdd95b..98844d5 100644 --- a/aquatic_ws/Cargo.toml +++ b/aquatic_ws/Cargo.toml @@ -15,36 +15,44 @@ path = "src/lib/lib.rs" name = "aquatic_ws" path = "src/bin/main.rs" +[features] +default = ["with-mio"] +with-glommio = ["async-tungstenite", "futures-lite", "futures", "futures-rustls", "glommio", "rustls-pemfile"] +with-mio = ["crossbeam-channel", "histogram", "mio", "native-tls", "parking_lot", "socket2"] + [dependencies] anyhow = "1" -async-tungstenite = "0.15" aquatic_cli_helpers = "0.1.0" aquatic_common = "0.1.0" aquatic_ws_protocol = "0.1.0" +cfg-if = "1" core_affinity = "0.5" either = "1" -futures-lite = "1" -futures = "0.3" -futures-rustls = "0.22" -glommio = { git = "https://github.com/DataDog/glommio.git", rev = "4e6b14772da2f4325271fbcf12d24cf91ed466e5" } hashbrown = { version = "0.11.2", features = ["serde"] } log = "0.4" mimalloc = { version = "0.1", default-features = false } privdrop = "0.5" rand = { version = "0.8", features = ["small_rng"] } -rustls-pemfile = "0.2" serde = { version = "1", features = ["derive"] } signal-hook = { version = "0.3" } slab = "0.4" tungstenite = "0.15" +# mio +crossbeam-channel = { version = "0.5", optional = true } +histogram = { version = "0.6", optional = true } +mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"], optional = true } +native-tls = { version = "0.2", optional = true } +parking_lot = { version = "0.11", optional = true } +socket2 = { version = "0.4.1", features = ["all"], optional = true } -crossbeam-channel = "0.5" -histogram = "0.6" -mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] } -native-tls = "0.2" -parking_lot = "0.11" -socket2 = { version = "0.4.1", features = ["all"] } +# glommio +async-tungstenite = { version = "0.15", optional = true } +futures-lite = { version = "1", optional = true } +futures = { version = "0.3", optional = true } +futures-rustls = { version = "0.22", optional = true } +glommio = { git = "https://github.com/DataDog/glommio.git", rev = "4e6b14772da2f4325271fbcf12d24cf91ed466e5", optional = true } +rustls-pemfile = { version = "0.2", optional = true } [dev-dependencies] quickcheck = "1.0" diff --git a/aquatic_ws/src/lib/common/mod.rs b/aquatic_ws/src/lib/common/mod.rs index be82373..2b7d1a6 100644 --- a/aquatic_ws/src/lib/common/mod.rs +++ b/aquatic_ws/src/lib/common/mod.rs @@ -13,8 +13,6 @@ use aquatic_ws_protocol::*; use crate::config::Config; -pub type TlsConfig = futures_rustls::rustls::ServerConfig; - #[derive(Copy, Clone, Debug)] pub struct PendingScrapeId(pub usize); diff --git a/aquatic_ws/src/lib/config.rs b/aquatic_ws/src/lib/config.rs index 32ceb26..7d3a62f 100644 --- a/aquatic_ws/src/lib/config.rs +++ b/aquatic_ws/src/lib/config.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +#[cfg(feature = "with-glommio")] use std::path::PathBuf; use aquatic_common::cpu_pinning::CpuPinningConfig; @@ -20,11 +21,13 @@ pub struct Config { pub log_level: LogLevel, pub network: NetworkConfig, pub protocol: ProtocolConfig, + #[cfg(feature = "with-mio")] pub handlers: HandlerConfig, pub cleaning: CleaningConfig, pub privileges: PrivilegeConfig, pub access_list: AccessListConfig, pub cpu_pinning: CpuPinningConfig, + #[cfg(feature = "with-mio")] pub statistics: StatisticsConfig, } @@ -40,16 +43,23 @@ pub struct NetworkConfig { /// Bind to this address pub address: SocketAddr, pub ipv6_only: bool, - - pub tls_certificate_path: PathBuf, - pub tls_private_key_path: PathBuf, pub websocket_max_message_size: usize, pub websocket_max_frame_size: usize, + #[cfg(feature = "with-glommio")] + pub tls_certificate_path: PathBuf, + #[cfg(feature = "with-glommio")] + pub tls_private_key_path: PathBuf, + + #[cfg(feature = "with-mio")] pub use_tls: bool, + #[cfg(feature = "with-mio")] pub tls_pkcs12_path: String, + #[cfg(feature = "with-mio")] pub tls_pkcs12_password: String, + #[cfg(feature = "with-mio")] pub poll_event_capacity: usize, + #[cfg(feature = "with-mio")] pub poll_timeout_microseconds: u64, } @@ -64,6 +74,7 @@ pub struct ProtocolConfig { pub peer_announce_interval: usize, } +#[cfg(feature = "with-mio")] #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct HandlerConfig { @@ -82,9 +93,11 @@ pub struct CleaningConfig { pub max_peer_age: u64, /// Remove connections that are older than this (seconds) + #[cfg(feature = "with-mio")] pub max_connection_age: u64, } +#[cfg(feature = "with-mio")] #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct StatisticsConfig { @@ -100,11 +113,13 @@ impl Default for Config { log_level: LogLevel::default(), network: NetworkConfig::default(), protocol: ProtocolConfig::default(), + #[cfg(feature = "with-mio")] handlers: Default::default(), cleaning: CleaningConfig::default(), privileges: PrivilegeConfig::default(), access_list: AccessListConfig::default(), cpu_pinning: Default::default(), + #[cfg(feature = "with-mio")] statistics: Default::default(), } } @@ -115,15 +130,23 @@ impl Default for NetworkConfig { Self { address: SocketAddr::from(([0, 0, 0, 0], 3000)), ipv6_only: false, - tls_certificate_path: "".into(), - tls_private_key_path: "".into(), websocket_max_message_size: 64 * 1024, websocket_max_frame_size: 16 * 1024, + #[cfg(feature = "with-glommio")] + tls_certificate_path: "".into(), + #[cfg(feature = "with-glommio")] + tls_private_key_path: "".into(), + + #[cfg(feature = "with-mio")] use_tls: false, + #[cfg(feature = "with-mio")] tls_pkcs12_path: "".into(), + #[cfg(feature = "with-mio")] tls_pkcs12_password: "".into(), + #[cfg(feature = "with-mio")] poll_event_capacity: 4096, + #[cfg(feature = "with-mio")] poll_timeout_microseconds: 200_000, } } @@ -139,6 +162,7 @@ impl Default for ProtocolConfig { } } +#[cfg(feature = "with-mio")] impl Default for HandlerConfig { fn default() -> Self { Self { @@ -154,11 +178,13 @@ impl Default for CleaningConfig { torrent_cleaning_interval: 30, max_peer_age: 1800, + #[cfg(feature = "with-mio")] max_connection_age: 1800, } } } +#[cfg(feature = "with-mio")] impl Default for StatisticsConfig { fn default() -> Self { Self { interval: 0 } diff --git a/aquatic_ws/src/lib/glommio/common.rs b/aquatic_ws/src/lib/glommio/common.rs index 3506b09..539176e 100644 --- a/aquatic_ws/src/lib/glommio/common.rs +++ b/aquatic_ws/src/lib/glommio/common.rs @@ -2,6 +2,8 @@ use std::sync::Arc; use aquatic_common::access_list::AccessListArcSwap; +pub type TlsConfig = futures_rustls::rustls::ServerConfig; + #[derive(Default, Clone)] pub struct State { pub access_list: Arc, diff --git a/aquatic_ws/src/lib/glommio/mod.rs b/aquatic_ws/src/lib/glommio/mod.rs index 8dac78f..0c84499 100644 --- a/aquatic_ws/src/lib/glommio/mod.rs +++ b/aquatic_ws/src/lib/glommio/mod.rs @@ -11,9 +11,8 @@ use std::{ use crate::config::Config; use aquatic_common::privileges::drop_privileges_after_socket_binding; -use self::common::State; +use self::common::*; -use super::common::TlsConfig; use glommio::{channels::channel_mesh::MeshBuilder, prelude::*}; const SHARED_CHANNEL_SIZE: usize = 1024; diff --git a/aquatic_ws/src/lib/glommio/network.rs b/aquatic_ws/src/lib/glommio/network.rs index 0f84b11..3dccf25 100644 --- a/aquatic_ws/src/lib/glommio/network.rs +++ b/aquatic_ws/src/lib/glommio/network.rs @@ -29,7 +29,7 @@ use crate::config::Config; use crate::common::*; -use super::common::State; +use super::common::*; struct PendingScrapeResponse { pending_worker_out_messages: usize, diff --git a/aquatic_ws/src/lib/lib.rs b/aquatic_ws/src/lib/lib.rs index 9b96d00..57e10a7 100644 --- a/aquatic_ws/src/lib/lib.rs +++ b/aquatic_ws/src/lib/lib.rs @@ -1,11 +1,14 @@ use aquatic_common::access_list::update_access_list; +use cfg_if::cfg_if; use signal_hook::{consts::SIGUSR1, iterator::Signals}; use crate::config::Config; pub mod common; pub mod config; +#[cfg(feature = "with-glommio")] pub mod glommio; +#[cfg(feature = "with-mio")] pub mod mio; pub const APP_NAME: &str = "aquatic_ws: WebTorrent tracker"; @@ -17,7 +20,13 @@ pub fn run(config: Config) -> ::anyhow::Result<()> { }); } - let state = glommio::common::State::default(); + cfg_if!( + if #[cfg(feature = "with-glommio")] { + let state = glommio::common::State::default(); + } else { + let state = mio::common::State::default(); + } + ); update_access_list(&config.access_list, &state.access_list)?; @@ -27,7 +36,13 @@ pub fn run(config: Config) -> ::anyhow::Result<()> { let config = config.clone(); let state = state.clone(); - ::std::thread::spawn(move || glommio::run_inner(config, state)); + cfg_if!( + if #[cfg(feature = "with-glommio")] { + ::std::thread::spawn(move || glommio::run_inner(config, state)); + } else { + ::std::thread::spawn(move || mio::run(config, state)); + } + ); } for signal in &mut signals { diff --git a/aquatic_ws/src/lib/mio/mod.rs b/aquatic_ws/src/lib/mio/mod.rs index b90ef8b..f793823 100644 --- a/aquatic_ws/src/lib/mio/mod.rs +++ b/aquatic_ws/src/lib/mio/mod.rs @@ -21,7 +21,7 @@ use common::*; pub const APP_NAME: &str = "aquatic_ws: WebTorrent tracker"; pub fn run(config: Config, state: State) -> anyhow::Result<()> { - start_workers(config.clone(), state.clone())?; + start_workers(config.clone(), state.clone()).expect("couldn't start workers"); // TODO: privdrop here instead diff --git a/scripts/gen-tls.sh b/scripts/gen-tls.sh index 5fd4512..12a57a6 100755 --- a/scripts/gen-tls.sh +++ b/scripts/gen-tls.sh @@ -14,3 +14,5 @@ sudo cp cert.crt /usr/local/share/ca-certificates/snakeoil.crt sudo update-ca-certificates openssl pkcs8 -in key.pem -topk8 -nocrypt -out key.pk8 + +# openssl pkcs12 -export -passout "pass:p" -out identity.pfx -inkey key.pem -in cert.crt \ No newline at end of file