use std::net::SocketAddr;
use cfg_if::cfg_if;
cfg_if!(
if #[cfg(feature = "capture")] {
use log::trace;
use crate::utils::unix_timestamp_ms;
}
);
#[cfg(feature = "capture")]
pub(crate) struct CaptureContext {
flow_addr: SocketAddr,
}
#[cfg(all(not(feature = "capture"), feature = "client"))]
pub(crate) struct CaptureContext;
#[cfg(any(feature = "capture", feature = "client"))]
impl CaptureContext {
#[cfg(feature = "capture")]
#[inline]
pub(crate) fn new(flow_addr: SocketAddr) -> Self {
Self {
flow_addr,
}
}
#[cfg(not(feature = "capture"))]
#[inline]
pub(crate) fn new(_: SocketAddr) -> Self {
Self
}
#[cfg(feature = "capture")]
pub(crate) fn record_send<F>(&self, f: F)
where
F: FnOnce() -> (&'static str, usize, usize, usize, usize, usize),
{
let (kind, tailer, crypto, header, payload, body) = f();
trace!(
target: "typhoon::capture",
"{{\"t\":{},\"dir\":\"c2s\",\"flow\":\"{}\",\"kind\":\"{kind}\",\"tailer\":{tailer},\"crypto\":{crypto},\"header\":{header},\"payload\":{payload},\"body\":{body}}}",
unix_timestamp_ms(),
self.flow_addr,
);
}
#[allow(clippy::unused_self)]
#[cfg(not(feature = "capture"))]
#[inline(always)]
pub(crate) fn record_send<F>(&self, _: F)
where
F: FnOnce() -> (&'static str, usize, usize, usize, usize, usize),
{
}
}
#[cfg(feature = "capture")]
pub(crate) fn record_flow_config<F>(flow_addr: SocketAddr, dir: &str, f: F)
where
F: FnOnce() -> (String, usize, &'static str),
{
let (body_mode, header_len, decoy) = f();
trace!(
target: "typhoon::capture",
"{{\"t\":{},\"kind\":\"Config\",\"dir\":\"{dir}\",\"flow\":\"{flow_addr}\",\"body_mode\":\"{body_mode}\",\"header_len\":{header_len},\"decoy\":\"{decoy}\"}}",
unix_timestamp_ms(),
);
}
#[cfg(not(feature = "capture"))]
#[inline(always)]
pub(crate) fn record_flow_config<F>(_: SocketAddr, _: &str, _: F)
where
F: FnOnce() -> (String, usize, &'static str),
{
}
#[cfg(feature = "capture")]
pub(crate) fn record_server_send<F>(addr: SocketAddr, f: F)
where
F: FnOnce() -> (&'static str, usize, usize, usize, usize, usize),
{
let (kind, tailer, crypto, header, payload, body) = f();
trace!(
target: "typhoon::capture",
"{{\"t\":{},\"dir\":\"s2c\",\"flow\":\"{addr}\",\"kind\":\"{kind}\",\"tailer\":{tailer},\"crypto\":{crypto},\"header\":{header},\"payload\":{payload},\"body\":{body}}}",
unix_timestamp_ms(),
);
}
#[cfg(all(not(feature = "capture"), feature = "server"))]
#[inline(always)]
pub(crate) fn record_server_send<F>(_: SocketAddr, _: F)
where
F: FnOnce() -> (&'static str, usize, usize, usize, usize, usize),
{
}