use thiserror::Error;
use std::net::IpAddr;
use tracing::{debug, error, warn};
#[derive(Debug, Error, Clone)]
pub enum ProtocolError {
#[error("👻 Crypto operation failed: {source}")]
Crypto {
#[from]
source: CryptoError,
},
#[error("👻 IO error: {details}")]
IoError {
details: String,
},
#[error("👻 IO error: {0}")]
Io(String),
#[error("👻 Packet format error: {details}")]
MalformedPacket { details: String },
#[error("👻 Handshake failed: {reason}")]
HandshakeFailed { reason: String },
#[error("👻 Rate limit exceeded for {ip}")]
RateLimitExceeded { ip: IpAddr },
#[error("👻 Session error: {details}")]
SessionError { details: String },
#[error("👻 Authentication failed: {reason}")]
AuthenticationFailed { reason: String },
#[error("👻 Configuration error: {details}")]
ConfigError { details: String },
#[error("👻 Timeout occurred after {duration:?}")]
Timeout { duration: std::time::Duration },
#[error("👻 Internal server error: {details}")]
InternalError { details: String },
#[error("👻 Phantom crypto error: {details}")]
PhantomCryptoError { details: String },
#[error("👻 Memory scatter error: {details}")]
MemoryScatterError { details: String },
#[error("👻 Hardware acceleration unavailable")]
HardwareAccelerationUnavailable,
#[error("👻 Connection closed")]
ConnectionClosed,
}
#[derive(Debug, Error, Clone)]
pub enum CryptoError {
#[error("Encryption failed: {reason}")]
EncryptionFailed { reason: String },
#[error("Decryption failed: {reason}")]
DecryptionFailed { reason: String },
#[error("Key derivation failed")]
KeyDerivationFailed,
#[error("Invalid key length: expected {expected}, got {actual}")]
InvalidKeyLength { expected: usize, actual: usize },
#[error("HMAC verification failed")]
HmacVerificationFailed,
#[error("Nonce reuse detected")]
NonceReuse,
#[error("Phantom key assembly failed: {reason}")]
PhantomKeyAssemblyFailed { reason: String },
#[error("Memory scattering failed: {reason}")]
MemoryScatteringFailed { reason: String },
}
impl ProtocolError {
pub fn log(self) -> Self {
match &self {
ProtocolError::RateLimitExceeded { ip } => {
warn!("👻 Rate limit exceeded for IP: {}", ip);
}
ProtocolError::HandshakeFailed { reason } => {
warn!("👻 Handshake failed: {}", reason);
}
ProtocolError::Crypto { source } => {
error!("👻 Crypto error: {}", source);
}
ProtocolError::PhantomCryptoError { details } => {
error!("👻 Phantom crypto error: {}", details);
}
ProtocolError::ConnectionClosed => {
debug!("👻 Connection closed");
}
_ => {
error!("👻 Protocol error: {}", self);
}
}
self
}
}
impl From<hkdf::InvalidLength> for ProtocolError {
fn from(_err: hkdf::InvalidLength) -> Self {
ProtocolError::Crypto {
source: CryptoError::InvalidKeyLength {
expected: 32, actual: 0, },
}
}
}
impl From<aes_gcm::Error> for ProtocolError {
fn from(err: aes_gcm::Error) -> Self {
ProtocolError::Crypto {
source: CryptoError::DecryptionFailed {
reason: err.to_string(),
},
}
}
}
impl From<std::io::Error> for ProtocolError {
fn from(err: std::io::Error) -> Self {
match err.kind() {
std::io::ErrorKind::UnexpectedEof |
std::io::ErrorKind::ConnectionReset |
std::io::ErrorKind::ConnectionAborted |
std::io::ErrorKind::BrokenPipe => {
ProtocolError::ConnectionClosed
}
_ => {
ProtocolError::Io(err.to_string())
}
}
}
}
impl From<anyhow::Error> for ProtocolError {
fn from(err: anyhow::Error) -> Self {
ProtocolError::InternalError {
details: err.to_string(),
}
}
}
impl From<digest::InvalidLength> for ProtocolError {
fn from(_err: digest::InvalidLength) -> Self {
ProtocolError::Crypto {
source: CryptoError::InvalidKeyLength {
expected: 0, actual: 0,
},
}
}
}
impl From<rand_core::Error> for ProtocolError {
fn from(err: rand_core::Error) -> Self {
ProtocolError::PhantomCryptoError {
details: format!("Random generation failed: {}", err),
}
}
}
impl From<std::array::TryFromSliceError> for ProtocolError {
fn from(err: std::array::TryFromSliceError) -> Self {
ProtocolError::MalformedPacket {
details: format!("Array conversion failed: {}", err),
}
}
}
impl From<tokio::time::error::Elapsed> for ProtocolError {
fn from(_: tokio::time::error::Elapsed) -> Self {
ProtocolError::Timeout {
duration: std::time::Duration::from_secs(5), }
}
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for ProtocolError {
fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self {
ProtocolError::InternalError {
details: format!("Channel send error: {}", err),
}
}
}
impl From<tokio::sync::oneshot::error::RecvError> for ProtocolError {
fn from(err: tokio::sync::oneshot::error::RecvError) -> Self {
ProtocolError::InternalError {
details: format!("Oneshot receive error: {}", err),
}
}
}
pub type ProtocolResult<T> = Result<T, ProtocolError>;