Added transport layer and configured extension commands to call mTLS socket connection

This commit is contained in:
2026-03-24 16:55:36 -03:00
parent 61ba9f6d63
commit b816144fb0
5 changed files with 259 additions and 91 deletions

62
src/tcp/transport.rs Normal file
View File

@@ -0,0 +1,62 @@
use rustls::{ClientConnection, StreamOwned};
use std::io::Write;
use std::net::TcpStream;
use super::config::ConnectionConfig;
use super::tls::{connect_mtls, enroll_and_connect};
pub enum TransportStream {
Plain(TcpStream),
Mtls(StreamOwned<ClientConnection, TcpStream>),
}
impl TransportStream {
pub fn write_message(&mut self, message: &[u8]) -> Result<(), std::io::Error> {
match self {
Self::Plain(stream) => {
stream.write_all(message)?;
stream.flush()
}
Self::Mtls(stream) => {
stream.write_all(message)?;
stream.flush()
}
}
}
}
pub fn connect_stream(config: &ConnectionConfig) -> Result<TransportStream, String> {
match config {
ConnectionConfig::Plain { address } => TcpStream::connect(address)
.map(TransportStream::Plain)
.map_err(|e| format!("failed to connect to {}: {}", address, e)),
ConnectionConfig::Mtls {
address,
server_name,
ca_cert_path,
client_cert_path,
client_key_path,
} => connect_mtls(
address,
server_name,
ca_cert_path,
client_cert_path,
client_key_path,
),
ConnectionConfig::EnrollMtls {
host,
server_name,
enroll_port,
username,
password,
client_uid,
} => enroll_and_connect(
host,
server_name,
enroll_port,
username,
password,
client_uid,
),
}
}