1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use puffin::codec::Reader;

use crate::tls::rustls::msgs::message::{Message, MessagePayload, OpaqueMessage};

pub fn debug_binary_message(buffer: &dyn AsRef<[u8]>) {
    debug_binary_message_with_info("", buffer);
}

pub fn debug_binary_message_with_info(info: &'static str, buffer: &dyn AsRef<[u8]>) {
    let mut reader = Reader::init(buffer.as_ref());
    match OpaqueMessage::read(&mut reader) {
        Ok(opaque_message) => match Message::try_from(opaque_message.into_plain_message()) {
            Ok(message) => {
                debug_message_with_info(info, &message);
            }
            Err(err) => {
                log::debug!(
                    "Failed to debug message as decoding to an Message failed: {}",
                    err
                );
            }
        },
        Err(err) => {
            log::debug!(
                "Failed to debug message as decoding to an OpaqueMessage failed: {:?}",
                err
            );
        }
    }
}

pub fn debug_message(message: &Message) {
    debug_message_with_info("", message);
}

pub fn debug_opaque_message_with_info(info: &str, message: &OpaqueMessage) {
    log::debug!(
        "{}Opaque Message  ({} bytes) ({:?}): {:?}",
        if info.is_empty() {
            info.to_string()
        } else {
            info.to_string() + " | "
        },
        message.clone().encode().len(),
        message.version,
        message.typ,
    );
    //info!("{:?}", hexdump::hexdump(message.payload.0.as_slice()))
}

pub fn debug_message_with_info(info: &str, message: &Message) {
    let msg = match &message.payload {
        MessagePayload::Alert(payload) => {
            format!("Alert with Level: {:?}", payload.level)
        }
        MessagePayload::Handshake(payload) => {
            format!("{:?}", payload.payload)
        }
        MessagePayload::TLS12EncryptedHandshake(_) => "TLS12EncryptedHandshake".to_string(),
        MessagePayload::ChangeCipherSpec(_) => "ChangeCipherSpec".to_string(),
        MessagePayload::ApplicationData(_) => "ApplicationData".to_string(),
        MessagePayload::Heartbeat(_) => "Heartbeat".to_string(),
    };

    log::debug!(
        "{}Message ({:?}): {}",
        if info.is_empty() {
            info.to_string()
        } else {
            info.to_string() + " | "
        },
        message.version,
        msg
    );
}