mirror of
https://github.com/kristoferssolo/tls-pq-bench.git
synced 2026-03-22 00:36:21 +00:00
feat(common,server): add ProtocolMode and route server through protocol dispatch
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use miette::Diagnostic;
|
||||
use thiserror::Error;
|
||||
|
||||
/// Result type using the servers's custom error type.
|
||||
/// Result type using the `servers`'s custom error type.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
|
||||
@@ -20,11 +20,15 @@ use tracing_subscriber::EnvFilter;
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(name = "server", version, about)]
|
||||
struct Args {
|
||||
/// Key exchange mode.
|
||||
/// Key exchange mode
|
||||
#[arg(long, default_value = "x25519")]
|
||||
mode: KeyExchangeMode,
|
||||
|
||||
/// Address to listen on.
|
||||
/// Protocol carrier
|
||||
#[arg(long, default_value = "raw")]
|
||||
pub proto: ProtocolMode,
|
||||
|
||||
/// Address to listen on
|
||||
#[arg(long, default_value = "127.0.0.1:4433")]
|
||||
listen: SocketAddr,
|
||||
}
|
||||
@@ -65,7 +69,7 @@ async fn main() -> miette::Result<()> {
|
||||
"CA cert (truncated)"
|
||||
);
|
||||
|
||||
Ok(run_server(args, tls_config).await?)
|
||||
Ok(run_server(&args, tls_config).await?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
32
server/src/server/mod.rs
Normal file
32
server/src/server/mod.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
mod raw;
|
||||
|
||||
use crate::{Args, error, server::raw::handle_raw_connection};
|
||||
use common::prelude::*;
|
||||
use rustls::ServerConfig;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::info;
|
||||
|
||||
pub async fn run_server(args: &Args, tls_config: Arc<ServerConfig>) -> error::Result<()> {
|
||||
let listener = TcpListener::bind(args.listen)
|
||||
.await
|
||||
.map_err(|e| error::Error::network(format!("failed to bind to {}: {e}", args.listen)))?;
|
||||
|
||||
info!(listen = %args.listen, mode = %args.mode, "listening");
|
||||
|
||||
loop {
|
||||
let (stream, peer) = match listener.accept().await {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
error!(error = %e, "accept error");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let config = tls_config.clone();
|
||||
tokio::spawn(match args.proto {
|
||||
ProtocolMode::Raw => handle_raw_connection(stream, peer, config),
|
||||
ProtocolMode::Http1 => todo!(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
use crate::{Args, error};
|
||||
use common::prelude::*;
|
||||
use rustls::{ServerConfig, server::Acceptor};
|
||||
use std::{io::ErrorKind, net::SocketAddr, sync::Arc};
|
||||
use tokio::{
|
||||
io::AsyncWriteExt,
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
use tokio::{io::AsyncWriteExt, net::TcpStream};
|
||||
use tokio_rustls::LazyConfigAcceptor;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
pub async fn handle_connection(stream: TcpStream, peer: SocketAddr, tls_config: Arc<ServerConfig>) {
|
||||
pub async fn handle_raw_connection(
|
||||
stream: TcpStream,
|
||||
peer: SocketAddr,
|
||||
tls_config: Arc<ServerConfig>,
|
||||
) {
|
||||
let acceptor = LazyConfigAcceptor::new(Acceptor::default(), stream);
|
||||
let start_handshake = match acceptor.await {
|
||||
Ok(sh) => sh,
|
||||
@@ -57,24 +57,3 @@ pub async fn handle_connection(stream: TcpStream, peer: SocketAddr, tls_config:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_server(args: Args, tls_config: Arc<ServerConfig>) -> error::Result<()> {
|
||||
let listener = TcpListener::bind(args.listen)
|
||||
.await
|
||||
.map_err(|e| error::Error::network(format!("failed to bind to {}: {e}", args.listen)))?;
|
||||
|
||||
info!(listen = %args.listen, mode = %args.mode, "listening");
|
||||
|
||||
loop {
|
||||
let (stream, peer) = match listener.accept().await {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
error!(error = %e, "accept error");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let config = tls_config.clone();
|
||||
tokio::spawn(handle_connection(stream, peer, config));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user