haprox_rs/
lib.rs

1/*-
2 * haprox-rs - a HaProxy protocol parser.
3 * 
4 * Copyright 2025 (c) Aleksandr Morozov
5 * The scram-rs crate can be redistributed and/or modified
6 * under the terms of either of the following licenses:
7 *
8 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
9 *
10 *   2. The MIT License (MIT)
11 *                     
12 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
13 */
14
15/*!
16 <img src="https://cdn.4neko.org/haprox_rs_logo.webp" width="180"/> <img src="https://cdn.4neko.org/source_avail.webp" width="180"/> <img src="https://cdn.4neko.org/mit_mpl_eupl_2.webp" width="200"/>
17 
18A HaProxy V2 parser.
19
20- the [ProxyHdrV2] to compose the header.
21
22- the [ProxyV2Parser] to parse the HaProxy V2 header.
23
24- the [ProxyHdrV1] to compose the HaProxy V1 header.
25
26- the [ProxyV1Parser] - to parse HaProxy V1 header.
27
28## Examples:
29
30Composer: 
31```rust
32use haprox_rs::{ProxyV2Addr, ProxyHdrV2, HdrV2OpProxy, ProxyTransportFam, PP2TlvClient};
33
34let addr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
35    
36let mut comp = 
37    ProxyHdrV2::<HdrV2OpProxy>::new(ProxyTransportFam::STREAM, addr).unwrap();
38
39let plts = comp.set_plts();
40
41let mut ssl = plts.add_ssl(PP2TlvClient::PP2_CLIENT_SSL, 0).unwrap();
42
43ssl.add_ssl_sub_version("TLSv1.2").unwrap();
44
45ssl.done().unwrap();
46
47let pkt: Vec<u8> = comp.try_into().unwrap();
48```
49
50Parser:
51
52```rust
53use haprox_rs::{ProxyV2Addr, ProxyHdrV2, HdrV2OpProxy, ProxyTransportFam, 
54    PP2TlvClient, ProxyV2Parser, ProtocolVersion, HdrV2Command, ProxyV2AddrType, PP2Tlvs};
55use haprox_rs::protocol::protocol_raw;
56use crate::haprox_rs::protocol::PP2TlvDump;
57
58let pkt_ssl = 
59b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x2a\
60\x7f\x00\x00\x01\x7f\x00\x00\x43\x9d\xd2\x2e\x6b\x20\x00\x1b\x07\
61\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\x22\x00\
62\x09\x6d\x71\x74\x74\x75\x73\x65\x72\x31";
63
64let dec = ProxyV2Parser::try_from_slice(pkt_ssl.as_slice()).unwrap();
65
66assert_eq!(dec.get_transport().is_ok(), true);
67assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
68
69assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
70assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
71
72assert_eq!(dec.get_address_family().is_ok(), true);
73assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
74
75assert_eq!(dec.get_palyload_len() as usize, pkt_ssl.len() - size_of::<protocol_raw::ProxyHdrV2>());
76
77let addr = dec.get_address().unwrap();
78
79assert_eq!(addr.is_some(), true);
80
81let addr = addr.unwrap();
82let maddr = ProxyV2Addr::try_from(("127.0.0.1:40402", "127.0.0.67:11883")).unwrap();
83
84assert_eq!(addr, maddr);
85
86let tlv_iter = dec.get_tlvs_iter();
87
88assert_eq!(tlv_iter.is_some(), true);
89
90let mut tlv_iter = tlv_iter.unwrap();
91
92let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
93
94assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
95let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
96
97assert_eq!(client, PP2TlvClient::all());
98assert_eq!(verify, 0);
99```
100 */
101extern crate bitflags;
102pub extern crate byteorder;
103extern crate crc32fast;
104
105mod protocol_raw;
106
107pub mod protocol_v1;
108pub mod protocol_v2;
109pub mod error;
110pub mod common;
111
112pub use common::ReadExtZeroCopy;
113
114pub use crate::error::HaProxRes;
115
116pub use crate::protocol_v1::{HapProtoV1, ProtocolV1Inet};
117pub use crate::protocol_v1::protocol_parser::ProxyV1Parser;
118pub use crate::protocol_v1::protocol_composer::{ProxyHdrV1, TryIntoSockAddr};
119
120pub use crate::protocol_v2::{PP2TlvDump, PP2TlvUniqId, PP2TlvRestore};
121pub use crate::protocol_v2::protocol::*;
122pub use crate::protocol_v2::protocol_parser::{ProxyV2Parser, ProxyV2Dummy, PPV2ParserZeroCopy, PPV2ParserCopying, ProxyV2TlvSource, ProxyV2TlvIter};
123pub use crate::protocol_v2::protocol_composer::{HdrV2OpLocal, HdrV2OpProxy, TlvSubTypeSsl, TlType, ProxyHdrV2};
124
125pub use byteorder::{BigEndian, WriteBytesExt};