From f1f708465aad701098e3facd1d0d8e912b83effa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Tue, 21 Jul 2020 23:01:34 +0200 Subject: [PATCH] remove aquatic_common_tcp crate, move contents into aquatic_http It doesn't make a lot of sense to make a separate crate for the few things here. I don't really want tight coupling between the crates anyway, since it impedes making changes in them and makes understanding them more difficult. --- Cargo.lock | 15 +-- Cargo.toml | 1 - aquatic_common_tcp/Cargo.toml | 18 --- aquatic_common_tcp/src/common.rs | 7 -- aquatic_common_tcp/src/config.rs | 106 ----------------- aquatic_common_tcp/src/lib.rs | 3 - aquatic_common_tcp/src/network/mod.rs | 2 - aquatic_http/Cargo.toml | 2 +- aquatic_http/src/lib/common.rs | 2 +- aquatic_http/src/lib/config.rs | 108 +++++++++++++++++- aquatic_http/src/lib/lib.rs | 3 +- aquatic_http/src/lib/network/connection.rs | 3 +- aquatic_http/src/lib/network/mod.rs | 8 +- .../src/lib}/network/stream.rs | 0 .../src/lib}/network/utils.rs | 1 - 15 files changed, 117 insertions(+), 162 deletions(-) delete mode 100644 aquatic_common_tcp/Cargo.toml delete mode 100644 aquatic_common_tcp/src/common.rs delete mode 100644 aquatic_common_tcp/src/config.rs delete mode 100644 aquatic_common_tcp/src/lib.rs delete mode 100644 aquatic_common_tcp/src/network/mod.rs rename {aquatic_common_tcp/src => aquatic_http/src/lib}/network/stream.rs (100%) rename {aquatic_common_tcp/src => aquatic_http/src/lib}/network/utils.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 792e410..b12f8ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,19 +54,6 @@ dependencies = [ "rand", ] -[[package]] -name = "aquatic_common_tcp" -version = "0.1.0" -dependencies = [ - "anyhow", - "aquatic_common", - "mio", - "native-tls", - "parking_lot", - "serde", - "socket2", -] - [[package]] name = "aquatic_http" version = "0.1.0" @@ -74,7 +61,6 @@ dependencies = [ "anyhow", "aquatic_cli_helpers", "aquatic_common", - "aquatic_common_tcp", "aquatic_http_protocol", "either", "flume", @@ -94,6 +80,7 @@ dependencies = [ "serde", "simplelog", "smartstring", + "socket2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 45307c2..3fdc42e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,6 @@ members = [ "aquatic_cli_helpers", "aquatic_common", - "aquatic_common_tcp", "aquatic_http", "aquatic_http_load_test", "aquatic_http_protocol", diff --git a/aquatic_common_tcp/Cargo.toml b/aquatic_common_tcp/Cargo.toml deleted file mode 100644 index d11ff20..0000000 --- a/aquatic_common_tcp/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "aquatic_common_tcp" -version = "0.1.0" -authors = ["Joakim FrostegÄrd "] -edition = "2018" -license = "Apache-2.0" - -[lib] -name = "aquatic_common_tcp" - -[dependencies] -anyhow = "1" -aquatic_common = { path = "../aquatic_common" } -mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] } -native-tls = "0.2" -parking_lot = "0.11" -serde = { version = "1", features = ["derive"] } -socket2 = { version = "0.3", features = ["reuseport"] } \ No newline at end of file diff --git a/aquatic_common_tcp/src/common.rs b/aquatic_common_tcp/src/common.rs deleted file mode 100644 index 7fa0974..0000000 --- a/aquatic_common_tcp/src/common.rs +++ /dev/null @@ -1,7 +0,0 @@ -use std::sync::Arc; - -use parking_lot::Mutex; - - -pub type SocketWorkerStatus = Option>; -pub type SocketWorkerStatuses = Arc>>; \ No newline at end of file diff --git a/aquatic_common_tcp/src/config.rs b/aquatic_common_tcp/src/config.rs deleted file mode 100644 index 8cd23e3..0000000 --- a/aquatic_common_tcp/src/config.rs +++ /dev/null @@ -1,106 +0,0 @@ -use serde::{Serialize, Deserialize}; - - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum LogLevel { - Off, - Error, - Warn, - Info, - Debug, - Trace -} - - -impl Default for LogLevel { - fn default() -> Self { - Self::Error - } -} - - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(default)] -pub struct HandlerConfig { - /// Maximum number of requests to receive from channel before locking - /// mutex and starting work - pub max_requests_per_iter: usize, - pub channel_recv_timeout_microseconds: u64, -} - - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(default)] -pub struct TlsConfig { - pub use_tls: bool, - pub tls_pkcs12_path: String, - pub tls_pkcs12_password: String, -} - - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(default)] -pub struct CleaningConfig { - /// Clean peers this often (seconds) - pub interval: u64, - /// Remove peers that haven't announced for this long (seconds) - pub max_peer_age: u64, - /// Remove connections that are older than this (seconds) - pub max_connection_age: u64, -} - - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(default)] -pub struct PrivilegeConfig { - /// Chroot and switch user after binding to sockets - pub drop_privileges: bool, - /// Chroot to this path - pub chroot_path: String, - /// User to switch to after chrooting - pub user: String, -} - - -impl Default for HandlerConfig { - fn default() -> Self { - Self { - max_requests_per_iter: 10000, - channel_recv_timeout_microseconds: 200, - } - } -} - - -impl Default for TlsConfig { - fn default() -> Self { - Self { - use_tls: false, - tls_pkcs12_path: "".into(), - tls_pkcs12_password: "".into(), - } - } -} - - -impl Default for CleaningConfig { - fn default() -> Self { - Self { - interval: 30, - max_peer_age: 180, - max_connection_age: 180, - } - } -} - - -impl Default for PrivilegeConfig { - fn default() -> Self { - Self { - drop_privileges: false, - chroot_path: ".".to_string(), - user: "nobody".to_string(), - } - } -} \ No newline at end of file diff --git a/aquatic_common_tcp/src/lib.rs b/aquatic_common_tcp/src/lib.rs deleted file mode 100644 index 0bbe482..0000000 --- a/aquatic_common_tcp/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod config; -pub mod common; -pub mod network; \ No newline at end of file diff --git a/aquatic_common_tcp/src/network/mod.rs b/aquatic_common_tcp/src/network/mod.rs deleted file mode 100644 index 1ad3a64..0000000 --- a/aquatic_common_tcp/src/network/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod stream; -pub mod utils; \ No newline at end of file diff --git a/aquatic_http/Cargo.toml b/aquatic_http/Cargo.toml index c787281..fad01d0 100644 --- a/aquatic_http/Cargo.toml +++ b/aquatic_http/Cargo.toml @@ -17,7 +17,6 @@ path = "src/bin/main.rs" anyhow = "1" aquatic_cli_helpers = { path = "../aquatic_cli_helpers" } aquatic_common = { path = "../aquatic_common" } -aquatic_common_tcp = { path = "../aquatic_common_tcp" } aquatic_http_protocol = { path = "../aquatic_http_protocol" } either = "1" flume = "0.7" @@ -35,6 +34,7 @@ rand = { version = "0.7", features = ["small_rng"] } serde = { version = "1", features = ["derive"] } simplelog = "0.8" smartstring = "0.2" +socket2 = { version = "0.3", features = ["reuseport"] } [dev-dependencies] quickcheck = "0.9" diff --git a/aquatic_http/src/lib/common.rs b/aquatic_http/src/lib/common.rs index 0843c6a..ada603e 100644 --- a/aquatic_http/src/lib/common.rs +++ b/aquatic_http/src/lib/common.rs @@ -169,4 +169,4 @@ impl ResponseChannelSender { pub type SocketWorkerStatus = Option>; -pub type SocketWorkerStatuses = Arc>>; +pub type SocketWorkerStatuses = Arc>>; \ No newline at end of file diff --git a/aquatic_http/src/lib/config.rs b/aquatic_http/src/lib/config.rs index e956012..a2e5504 100644 --- a/aquatic_http/src/lib/config.rs +++ b/aquatic_http/src/lib/config.rs @@ -2,7 +2,24 @@ use std::net::SocketAddr; use serde::{Serialize, Deserialize}; -pub use aquatic_common_tcp::config::*; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum LogLevel { + Off, + Error, + Warn, + Info, + Debug, + Trace +} + + +impl Default for LogLevel { + fn default() -> Self { + Self::Error + } +} #[derive(Clone, Debug, Serialize, Deserialize)] @@ -21,6 +38,15 @@ pub struct Config { } +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct TlsConfig { + pub use_tls: bool, + pub tls_pkcs12_path: String, + pub tls_pkcs12_password: String, +} + + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct NetworkConfig { @@ -46,6 +72,41 @@ pub struct ProtocolConfig { } + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct HandlerConfig { + /// Maximum number of requests to receive from channel before locking + /// mutex and starting work + pub max_requests_per_iter: usize, + pub channel_recv_timeout_microseconds: u64, +} + + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct CleaningConfig { + /// Clean peers this often (seconds) + pub interval: u64, + /// Remove peers that haven't announced for this long (seconds) + pub max_peer_age: u64, + /// Remove connections that are older than this (seconds) + pub max_connection_age: u64, +} + + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct PrivilegeConfig { + /// Chroot and switch user after binding to sockets + pub drop_privileges: bool, + /// Chroot to this path + pub chroot_path: String, + /// User to switch to after chrooting + pub user: String, +} + + impl Default for Config { fn default() -> Self { Self { @@ -82,4 +143,47 @@ impl Default for ProtocolConfig { peer_announce_interval: 120, } } -} \ No newline at end of file +} + + +impl Default for HandlerConfig { + fn default() -> Self { + Self { + max_requests_per_iter: 10000, + channel_recv_timeout_microseconds: 200, + } + } +} + + +impl Default for CleaningConfig { + fn default() -> Self { + Self { + interval: 30, + max_peer_age: 180, + max_connection_age: 180, + } + } +} + + +impl Default for PrivilegeConfig { + fn default() -> Self { + Self { + drop_privileges: false, + chroot_path: ".".to_string(), + user: "nobody".to_string(), + } + } +} + + +impl Default for TlsConfig { + fn default() -> Self { + Self { + use_tls: false, + tls_pkcs12_path: "".into(), + tls_pkcs12_password: "".into(), + } + } +} diff --git a/aquatic_http/src/lib/lib.rs b/aquatic_http/src/lib/lib.rs index bff7d53..a18cc02 100644 --- a/aquatic_http/src/lib/lib.rs +++ b/aquatic_http/src/lib/lib.rs @@ -6,8 +6,6 @@ use anyhow::Context; use parking_lot::Mutex; use privdrop::PrivDrop; -use aquatic_common_tcp::network::utils::create_tls_acceptor; - pub mod common; pub mod config; pub mod handler; @@ -16,6 +14,7 @@ pub mod tasks; use common::*; use config::Config; +use network::utils::create_tls_acceptor; pub fn run(config: Config) -> anyhow::Result<()> { diff --git a/aquatic_http/src/lib/network/connection.rs b/aquatic_http/src/lib/network/connection.rs index 419a0bc..93e811a 100644 --- a/aquatic_http/src/lib/network/connection.rs +++ b/aquatic_http/src/lib/network/connection.rs @@ -8,11 +8,12 @@ use mio::Token; use mio::net::TcpStream; use native_tls::{TlsAcceptor, MidHandshakeTlsStream}; -use aquatic_common_tcp::network::stream::Stream; use aquatic_http_protocol::request::{Request, RequestParseError}; use crate::common::*; +use super::stream::Stream; + #[derive(Debug)] pub enum RequestReadError { diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index 16f4e88..6cc677f 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -1,5 +1,3 @@ -pub mod connection; - use std::time::{Duration, Instant}; use std::io::ErrorKind; use std::sync::Arc; @@ -11,13 +9,17 @@ use native_tls::TlsAcceptor; use mio::{Events, Poll, Interest, Token}; use mio::net::TcpListener; -use aquatic_common_tcp::network::utils::create_listener; use aquatic_http_protocol::response::*; use crate::common::*; use crate::config::Config; +pub mod connection; +pub mod stream; +pub mod utils; + use connection::*; +use utils::*; pub fn run_socket_worker( diff --git a/aquatic_common_tcp/src/network/stream.rs b/aquatic_http/src/lib/network/stream.rs similarity index 100% rename from aquatic_common_tcp/src/network/stream.rs rename to aquatic_http/src/lib/network/stream.rs diff --git a/aquatic_common_tcp/src/network/utils.rs b/aquatic_http/src/lib/network/utils.rs similarity index 99% rename from aquatic_common_tcp/src/network/utils.rs rename to aquatic_http/src/lib/network/utils.rs index 63a46fb..7f05926 100644 --- a/aquatic_common_tcp/src/network/utils.rs +++ b/aquatic_http/src/lib/network/utils.rs @@ -1,4 +1,3 @@ - use std::fs::File; use std::io::Read; use std::net::SocketAddr;