mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
restructure aquatic_common_tcp, move more into it from aquatic_http
This commit is contained in:
parent
f3dcc8762e
commit
1dc2f44d9c
9 changed files with 78 additions and 68 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -62,6 +62,7 @@ dependencies = [
|
||||||
"aquatic_common",
|
"aquatic_common",
|
||||||
"mio",
|
"mio",
|
||||||
"native-tls",
|
"native-tls",
|
||||||
|
"parking_lot",
|
||||||
"serde",
|
"serde",
|
||||||
"socket2",
|
"socket2",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,6 @@ anyhow = "1"
|
||||||
aquatic_common = { path = "../aquatic_common" }
|
aquatic_common = { path = "../aquatic_common" }
|
||||||
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] }
|
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] }
|
||||||
native-tls = "0.2"
|
native-tls = "0.2"
|
||||||
|
parking_lot = "0.10"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
socket2 = { version = "0.3", features = ["reuseport"] }
|
socket2 = { version = "0.3", features = ["reuseport"] }
|
||||||
7
aquatic_common_tcp/src/common.rs
Normal file
7
aquatic_common_tcp/src/common.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
|
|
||||||
|
pub type SocketWorkerStatus = Option<Result<(), String>>;
|
||||||
|
pub type SocketWorkerStatuses = Arc<Mutex<Vec<SocketWorkerStatus>>>;
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod common;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
|
@ -1,66 +1,2 @@
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
|
pub mod utils;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::net::SocketAddr;
|
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use native_tls::{Identity, TlsAcceptor};
|
|
||||||
use socket2::{Socket, Domain, Type, Protocol};
|
|
||||||
|
|
||||||
use crate::config::TlsConfig;
|
|
||||||
|
|
||||||
|
|
||||||
pub fn create_tls_acceptor(
|
|
||||||
config: &TlsConfig,
|
|
||||||
) -> anyhow::Result<Option<TlsAcceptor>> {
|
|
||||||
if config.use_tls {
|
|
||||||
let mut identity_bytes = Vec::new();
|
|
||||||
let mut file = File::open(&config.tls_pkcs12_path)
|
|
||||||
.context("Couldn't open pkcs12 identity file")?;
|
|
||||||
|
|
||||||
file.read_to_end(&mut identity_bytes)
|
|
||||||
.context("Couldn't read pkcs12 identity file")?;
|
|
||||||
|
|
||||||
let identity = Identity::from_pkcs12(
|
|
||||||
&mut identity_bytes,
|
|
||||||
&config.tls_pkcs12_password
|
|
||||||
).context("Couldn't parse pkcs12 identity file")?;
|
|
||||||
|
|
||||||
let acceptor = TlsAcceptor::new(identity)
|
|
||||||
.context("Couldn't create TlsAcceptor from pkcs12 identity")?;
|
|
||||||
|
|
||||||
Ok(Some(acceptor))
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn create_listener(
|
|
||||||
address: SocketAddr,
|
|
||||||
ipv6_only: bool
|
|
||||||
) -> ::anyhow::Result<::std::net::TcpListener> {
|
|
||||||
let builder = if address.is_ipv4(){
|
|
||||||
Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp()))
|
|
||||||
} else {
|
|
||||||
Socket::new(Domain::ipv6(), Type::stream(), Some(Protocol::tcp()))
|
|
||||||
}.context("Couldn't create socket2::Socket")?;
|
|
||||||
|
|
||||||
if ipv6_only {
|
|
||||||
builder.set_only_v6(true)
|
|
||||||
.context("Couldn't put socket in ipv6 only mode")?
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.set_nonblocking(true)
|
|
||||||
.context("Couldn't put socket in non-blocking mode")?;
|
|
||||||
builder.set_reuse_port(true)
|
|
||||||
.context("Couldn't put socket in reuse_port mode")?;
|
|
||||||
builder.bind(&address.into()).with_context(||
|
|
||||||
format!("Couldn't bind socket to address {}", address)
|
|
||||||
)?;
|
|
||||||
builder.listen(128)
|
|
||||||
.context("Couldn't listen for connections on socket")?;
|
|
||||||
|
|
||||||
Ok(builder.into_tcp_listener())
|
|
||||||
}
|
|
||||||
65
aquatic_common_tcp/src/network/utils.rs
Normal file
65
aquatic_common_tcp/src/network/utils.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use native_tls::{Identity, TlsAcceptor};
|
||||||
|
use socket2::{Socket, Domain, Type, Protocol};
|
||||||
|
|
||||||
|
use crate::config::TlsConfig;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn create_tls_acceptor(
|
||||||
|
config: &TlsConfig,
|
||||||
|
) -> anyhow::Result<Option<TlsAcceptor>> {
|
||||||
|
if config.use_tls {
|
||||||
|
let mut identity_bytes = Vec::new();
|
||||||
|
let mut file = File::open(&config.tls_pkcs12_path)
|
||||||
|
.context("Couldn't open pkcs12 identity file")?;
|
||||||
|
|
||||||
|
file.read_to_end(&mut identity_bytes)
|
||||||
|
.context("Couldn't read pkcs12 identity file")?;
|
||||||
|
|
||||||
|
let identity = Identity::from_pkcs12(
|
||||||
|
&mut identity_bytes,
|
||||||
|
&config.tls_pkcs12_password
|
||||||
|
).context("Couldn't parse pkcs12 identity file")?;
|
||||||
|
|
||||||
|
let acceptor = TlsAcceptor::new(identity)
|
||||||
|
.context("Couldn't create TlsAcceptor from pkcs12 identity")?;
|
||||||
|
|
||||||
|
Ok(Some(acceptor))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn create_listener(
|
||||||
|
address: SocketAddr,
|
||||||
|
ipv6_only: bool
|
||||||
|
) -> ::anyhow::Result<::std::net::TcpListener> {
|
||||||
|
let builder = if address.is_ipv4(){
|
||||||
|
Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp()))
|
||||||
|
} else {
|
||||||
|
Socket::new(Domain::ipv6(), Type::stream(), Some(Protocol::tcp()))
|
||||||
|
}.context("Couldn't create socket2::Socket")?;
|
||||||
|
|
||||||
|
if ipv6_only {
|
||||||
|
builder.set_only_v6(true)
|
||||||
|
.context("Couldn't put socket in ipv6 only mode")?
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.set_nonblocking(true)
|
||||||
|
.context("Couldn't put socket in non-blocking mode")?;
|
||||||
|
builder.set_reuse_port(true)
|
||||||
|
.context("Couldn't put socket in reuse_port mode")?;
|
||||||
|
builder.bind(&address.into()).with_context(||
|
||||||
|
format!("Couldn't bind socket to address {}", address)
|
||||||
|
)?;
|
||||||
|
builder.listen(128)
|
||||||
|
.context("Couldn't listen for connections on socket")?;
|
||||||
|
|
||||||
|
Ok(builder.into_tcp_listener())
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,6 @@ pub use aquatic_common::ValidUntil;
|
||||||
use crate::protocol::*;
|
use crate::protocol::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// identical to ws version
|
// identical to ws version
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct ConnectionMeta {
|
pub struct ConnectionMeta {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use anyhow::Context;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use privdrop::PrivDrop;
|
use privdrop::PrivDrop;
|
||||||
|
|
||||||
use aquatic_common_tcp::network::create_tls_acceptor;
|
use aquatic_common_tcp::network::utils::create_tls_acceptor;
|
||||||
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use native_tls::TlsAcceptor;
|
||||||
use mio::{Events, Poll, Interest, Token};
|
use mio::{Events, Poll, Interest, Token};
|
||||||
use mio::net::TcpListener;
|
use mio::net::TcpListener;
|
||||||
|
|
||||||
use aquatic_common_tcp::network::create_listener;
|
use aquatic_common_tcp::network::utils::create_listener;
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue