add quic base code

This commit is contained in:
aprilthepink 2025-02-22 17:09:33 +01:00
parent f2ec93e0f2
commit 15055d13cb
5 changed files with 502 additions and 28 deletions

View file

@ -8,6 +8,7 @@
mod bininfo;
mod gui;
mod pman;
mod quic;
use bunt::println;
use clap::{Parser, Subcommand};
@ -39,15 +40,14 @@ struct Args {
#[derive(Debug, Subcommand)]
enum Commands {
#[command(arg_required_else_help = true, about = "todo replace this cmd")]
Clone {
/// The remote to clone
remote: String,
#[command(arg_required_else_help = true, about = "Start client without GUI")]
CliClient {
config_file: Option<String>
},
#[command(about = "List compile time backed info to audit binary")]
Shadow { outfile: Option<String> },
#[command(about = "Show test gui")]
Gui,
Shadow,
#[command(about = "Start client as GUI")]
GuiClient,
Devtest,
}
@ -60,14 +60,14 @@ async fn main() {
let _ = init_process_manager();
match args.command {
Commands::Clone { remote } => {}
Commands::CliClient { config_file } => {}
Commands::Shadow { outfile } => {
Commands::Shadow => {
bininfo::print_info();
return;
}
Commands::Gui => {
Commands::GuiClient => {
let res = gui::gui_main();
if let Err(e) = res {
println!("{}", e);

1
src/quic/mod.rs Normal file
View file

@ -0,0 +1 @@
mod server;

48
src/quic/server.rs Normal file
View file

@ -0,0 +1,48 @@
use std::error::Error;
use std::net::SocketAddr;
use std::sync::Arc;
use quinn::{Endpoint, ServerConfig};
use rcgen::Certificate;
use rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer};
use rustls::pki_types::pem::PemObject;
/// Constructs a QUIC endpoint configured to listen for incoming connections on a certain address
/// and port.
///
/// ## Returns
///
/// - a stream of incoming QUIC connections
/// - server certificate serialized into DER format
pub fn make_server_endpoint(
bind_addr: SocketAddr,
) -> Result<(Endpoint, CertificateDer<'static>), Box<dyn Error + Send + Sync + 'static>> {
let (server_config, server_cert) = configure_server()?;
let endpoint = Endpoint::server(server_config, bind_addr)?;
Ok((endpoint, server_cert))
}
/// Returns default server configuration along with its certificate.
fn configure_server(
cert_file: Option<String>
) -> Result<(ServerConfig, CertificateDer<'static>), Box<dyn Error + Send + Sync + 'static>> {
let cert_closure = |cert_file: Option<String>| {
return if (cert_file.is_some()) {
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
let cert_der = CertificateDer::from(cert.cert);
(cert_der, PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der()))
} else {
let mut certs: Vec<_> = CertificateDer::pem_file_iter("tests/data/certificate.chain.pem")
.unwrap()
.collect();
let cert = certs.pop().unwrap().unwrap();
(cert, PrivatePkcs8KeyDer::from_pem_file(cert_file.unwrap()).unwrap())
}
};
let (cert_der, priv_key) = cert_closure(cert_file);
let mut server_config =
ServerConfig::with_single_cert(vec![cert_der.clone()], priv_key.into())?;
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
transport_config.max_concurrent_uni_streams(0_u8.into());
Ok((server_config, cert_der))
}