[WIP] WTransport

This commit is contained in:
Alex Emmet 2026-03-04 17:12:22 +01:00
commit f379a0c015
3 changed files with 31 additions and 49 deletions

View file

@ -6,22 +6,14 @@ use crate::{
sql::{self, get_by_user_id, get_by_username, get_iota_by_id, get_omikron_by_id},
user_online_tracker::{self},
},
util::{
crypto_helper::encrypt,
file_util::{load_file_buf, load_file_vec},
logger::PrintType,
},
util::{crypto_helper::encrypt, file_util::load_file_vec, logger::PrintType},
};
use base64::{Engine as _, engine::general_purpose::STANDARD};
use dashmap::DashMap;
use epsilon_core::{CommunicationType, CommunicationValue, DataTypes, DataValue};
use epsilon_native::{Host, Receiver, Sender};
use quinn::crypto::rustls::QuicServerConfig;
use quinn::{Endpoint, ServerConfig};
use rand::{Rng, distributions::Alphanumeric};
use rustls::{ServerConfig as CryptoConfig, crypto::aws_lc_rs};
use std::{
net::SocketAddr,
sync::Arc,
time::{Duration, Instant},
};
@ -152,7 +144,7 @@ impl OmikronConnection {
// Main Handler Loop
// -------------------------------------------------------------------------
pub async fn handle(self: Arc<Self>, mut receiver: Receiver) {
pub async fn handle(self: Arc<Self>, receiver: Receiver) {
log_in!(
self.id as i64,
PrintType::Omega,
@ -227,7 +219,8 @@ impl OmikronConnection {
async fn handle_unauthenticated(self: Arc<Self>, cv: CommunicationValue) -> OmikronResult<()> {
if !cv.is_type(CommunicationType::identification) {
self.send_error_response(cv.get_id(), CommunicationType::error_not_authenticated)
let _ = self
.send_error_response(cv.get_id(), CommunicationType::error_not_authenticated)
.await;
return Err(OmikronError::NotAuthenticated);
}
@ -280,7 +273,8 @@ impl OmikronConnection {
async fn handle_identified(self: Arc<Self>, cv: CommunicationValue) -> OmikronResult<()> {
if !cv.is_type(CommunicationType::challenge_response) {
self.send_error_response(cv.get_id(), CommunicationType::error_not_authenticated)
let _ = self
.send_error_response(cv.get_id(), CommunicationType::error_not_authenticated)
.await;
return Err(OmikronError::NotAuthenticated);
}
@ -306,7 +300,8 @@ impl OmikronConnection {
log_in!(omikron_id, PrintType::Omega, "Omikron authenticated");
Ok(())
} else {
self.send_error_response(cv.get_id(), CommunicationType::error_invalid_challenge)
let _ = self
.send_error_response(cv.get_id(), CommunicationType::error_invalid_challenge)
.await;
Err(OmikronError::AuthenticationFailed)
}
@ -1087,12 +1082,6 @@ impl OmikronConnection {
}
}
fn arc_self(self: Arc<Self>) -> Arc<Self> {
// This is a bit of a hack - in practice you'd store the Arc in the struct
// or use weak references. For now, we rely on the caller having the Arc.
panic!("Use the Arc<OmikronConnection> directly")
}
// Public API for external use
pub async fn is_authenticated(self: Arc<Self>) -> bool {
self.state.read().await.is_authenticated()
@ -1112,13 +1101,13 @@ impl OmikronConnection {
// ============================================================================
pub async fn start(port: u16) -> Result<(), Box<dyn std::error::Error>> {
let _ = aws_lc_rs::default_provider().install_default();
let cert_pem = load_file_vec("certs", "cert.pem")
.map_err(|e| format!("Failed to load certificate: {}", e))?;
let key_pem = load_file_vec("certs", "key.pem")
.map_err(|e| format!("Failed to load private key: {}", e))?;
let tls_cfg = load_tls().expect("TLS config failed");
let server_crypto = quinn::crypto::rustls::QuicServerConfig::try_from(tls_cfg)?;
let server_cfg = ServerConfig::with_crypto(Arc::new(server_crypto));
let mut host: Host = epsilon_native::host(port, server_cfg).await?;
log!("OmikronServer starting on port {}", port);
let mut host: Host = epsilon_native::host(port, cert_pem, key_pem).await?;
log!("OmikronServer listening on port {}", port);
while let Some((sender, receiver)) = host.next().await {
@ -1130,22 +1119,3 @@ pub async fn start(port: u16) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn load_tls() -> Option<CryptoConfig> {
let _ = aws_lc_rs::default_provider().install_default();
let mut cert_pem = load_file_buf("certs", "cert.pem").ok()?;
let cert_chain = rustls_pemfile::certs(&mut cert_pem)
.collect::<Result<Vec<_>, _>>()
.ok()?;
let key_pem = load_file_vec("certs", "key.pem").ok()?;
let key_der = rustls_pemfile::private_key(&mut &*key_pem).ok()??;
let cfg = CryptoConfig::builder()
.with_no_client_auth()
.with_single_cert(cert_chain, key_der)
.ok()?;
Some(cfg)
}