use std::sync::Arc;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::timeout;
use tokio_rustls::client::TlsStream;
use tokio_rustls::TlsConnector;
use rustls::pki_types::ServerName;
use super::tcp::insecure_client_config;
use crate::error::{NoctisError, Result};
const READ_IDLE_MS: u64 = 500;
const MAX_REPLY_BYTES: usize = 64 * 1024;
pub enum Conn {
Plain(TcpStream),
Tls(Box<TlsStream<TcpStream>>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenResult {
pub connected: bool,
pub banner: Option<String>,
pub banner_hex: Option<String>,
pub duration_ms: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendResult {
pub sent: bool,
pub responded: bool,
pub response: Option<String>,
pub response_hex: Option<String>,
pub length: usize,
pub duration_ms: u64,
}
#[cfg(test)]
fn to_text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).trim_end().to_string()
}
#[cfg(test)]
fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
async fn read_reply<S: AsyncReadExt + Unpin>(stream: &mut S) -> Vec<u8> {
let mut acc = Vec::new();
let mut chunk = [0u8; 4096];
loop {
match timeout(Duration::from_millis(READ_IDLE_MS), stream.read(&mut chunk)).await {
Ok(Ok(0)) => break, Ok(Ok(n)) => {
acc.extend_from_slice(&chunk[..n]);
if acc.len() >= MAX_REPLY_BYTES {
break;
}
}
Ok(Err(_)) => break, Err(_) => break, }
}
acc
}
impl Conn {
pub async fn open(host: &str, port: u16, use_tls: bool, timeout_secs: u64) -> Result<Conn> {
let addr = format!("{host}:{port}");
let tcp = match timeout(Duration::from_secs(timeout_secs), TcpStream::connect(&addr)).await
{
Ok(Ok(s)) => s,
Ok(Err(e)) => return Err(NoctisError::Tcp(format!("connect {addr}: {e}"))),
Err(_) => return Err(NoctisError::Tcp(format!("connect {addr}: timed out"))),
};
if use_tls {
let connector = TlsConnector::from(Arc::new(insecure_client_config()?));
let server_name = ServerName::try_from(host.to_string())
.map_err(|e| NoctisError::Tcp(format!("invalid host for TLS SNI: {e}")))?;
let stream = connector
.connect(server_name, tcp)
.await
.map_err(|e| NoctisError::Tcp(format!("TLS handshake {addr}: {e}")))?;
Ok(Conn::Tls(Box::new(stream)))
} else {
Ok(Conn::Plain(tcp))
}
}
pub async fn send(&mut self, bytes: &[u8]) -> Result<()> {
let r = match self {
Conn::Plain(s) => s.write_all(bytes).await,
Conn::Tls(s) => s.write_all(bytes).await,
};
r.map_err(|e| NoctisError::Tcp(format!("send: {e}")))
}
pub async fn read_reply(&mut self) -> Vec<u8> {
match self {
Conn::Plain(s) => read_reply(s).await,
Conn::Tls(s) => read_reply(s).await,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::net::TcpListener;
async fn challenge_server() -> std::net::SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
if let Ok((mut sock, _)) = listener.accept().await {
let _ = sock.write_all(b"HELLO\r\n").await;
let mut buf = [0u8; 1024];
if let Ok(n) = sock.read(&mut buf).await {
if n > 0 {
let _ = sock.write_all(&[0x00, 0x01, 0x02, 0x03, 0x04]).await;
}
}
if let Ok(n) = sock.read(&mut buf).await {
if n >= 4 && buf[n - 4..n] == [0xde, 0xad, 0xbe, 0xef] {
let _ = sock.write_all(b"accepted").await;
} else {
let _ = sock.write_all(b"denied").await;
}
}
}
});
addr
}
#[tokio::test]
async fn multi_round_trip_computed_frame() {
let addr = challenge_server().await;
let mut c = Conn::open(&addr.ip().to_string(), addr.port(), false, 5)
.await
.unwrap();
let banner = c.read_reply().await;
assert_eq!(to_text(&banner), "HELLO");
c.send(&[0xaa]).await.unwrap();
let reply = c.read_reply().await;
let hex = to_hex(&reply);
assert_eq!(hex, "0001020304");
assert_eq!(&hex[2..], "01020304");
c.send(&[0xde, 0xad, 0xbe, 0xef]).await.unwrap();
let result = c.read_reply().await;
assert_eq!(to_text(&result), "accepted");
}
#[tokio::test]
async fn open_failure_is_an_error() {
let err = Conn::open("127.0.0.1", 1, false, 2).await;
assert!(err.is_err());
}
}