example_custom_composer/
example_custom_composer.rs

1use std::{fmt, io::Cursor};
2
3use byteorder::{BigEndian, WriteBytesExt};
4use haprox_rs::
5{
6    HaProxRes, 
7    HdrV2OpProxy, 
8    PP2TlvClient, 
9    PP2TlvDump, 
10    ProxyHdrV2, 
11    ProxyTransportFam, 
12    ProxyV2Addr, 
13    common::map_io_err
14};
15
16
17
18#[derive(Clone, Debug)]
19pub enum ProxyV2Dummy2 
20{
21    SomeTlvName(u32, u32),
22}
23
24impl fmt::Display for ProxyV2Dummy2
25{
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
27    {
28        write!(f, "DUMMY external reader")
29    }
30}
31
32impl PP2TlvDump for ProxyV2Dummy2
33{
34    fn get_type(&self) -> u8 
35    {
36        let Self::SomeTlvName(..) = self else { panic!("wrong") };
37
38        return 0xE0;
39    }
40
41    fn dump(&self, cur: &mut Cursor<Vec<u8>>) -> HaProxRes<()> 
42    {
43        match self
44        {
45            Self::SomeTlvName(arg0, arg1) =>
46            {
47                cur.write_u32::<BigEndian>(*arg0).map_err(map_io_err)?;
48                cur.write_u32::<BigEndian>(*arg1).map_err(map_io_err)?;
49            }
50        }
51
52        return Ok(());
53    }
54}
55
56fn main()
57{
58    let addr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
59        
60    let mut comp = 
61        ProxyHdrV2::<HdrV2OpProxy>::new(ProxyTransportFam::STREAM, addr).unwrap();
62
63    let plts = comp.set_plts();
64
65    let mut ssl = plts.add_ssl(PP2TlvClient::PP2_CLIENT_SSL, 0).unwrap();
66
67    ssl.add_ssl_sub_version("TLSv1.2").unwrap();
68    
69    let mut plts = ssl.done().unwrap();
70
71
72    let cust_plt = ProxyV2Dummy2::SomeTlvName(0x01020304, 0x05060708);
73
74    plts.add_tlv(cust_plt, Some(&[0xE0..=0xE0])).unwrap();
75
76    drop(plts);
77
78    let pkt: Vec<u8> = comp.try_into().unwrap();
79
80    let ctrl = 
81b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x29\
82\x7f\x00\x00\x01\x7f\x00\x00\x43\x9b\x4a\x2e\x6b\x20\x00\x0f\x01\
83\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\xE0\x00\
84\x08\x01\x02\x03\x04\x05\x06\x07\x08";
85
86    assert_eq!(pkt.as_slice(), ctrl.as_slice());
87}