Skip to main content

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, protocol_raw, 
55    PP2TlvDump};
56
57let pkt_ssl = 
58b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x2a\
59\x7f\x00\x00\x01\x7f\x00\x00\x43\x9d\xd2\x2e\x6b\x20\x00\x1b\x07\
60\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\x22\x00\
61\x09\x6d\x71\x74\x74\x75\x73\x65\x72\x31";
62
63let dec = ProxyV2Parser::try_from_slice(pkt_ssl.as_slice()).unwrap();
64
65assert_eq!(dec.get_transport().is_ok(), true);
66assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);
67
68assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
69assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);
70
71assert_eq!(dec.get_address_family().is_ok(), true);
72assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);
73
74assert_eq!(dec.get_palyload_len() as usize, pkt_ssl.len() - size_of::<protocol_raw::ProxyHdrV2>());
75
76let addr = dec.get_address().unwrap();
77
78assert_eq!(addr.is_some(), true);
79
80let addr = addr.unwrap();
81let maddr = ProxyV2Addr::try_from(("127.0.0.1:40402", "127.0.0.67:11883")).unwrap();
82
83assert_eq!(addr, maddr);
84
85let tlv_iter = dec.get_tlvs_iter();
86
87assert_eq!(tlv_iter.is_some(), true);
88
89let mut tlv_iter = tlv_iter.unwrap();
90
91let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();
92
93assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
94let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};
95
96assert_eq!(client, PP2TlvClient::all());
97assert_eq!(verify, 0);
98```
99 */
100extern crate bitflags;
101pub extern crate byteorder;
102extern crate crc32fast;
103
104pub mod protocol_raw;
105
106pub mod protocol_v1;
107pub mod protocol_v2;
108pub mod error;
109pub mod common;
110
111pub use common::ReadExtZeroCopy;
112
113pub use crate::error::HaProxRes;
114
115pub use crate::protocol_v1::{HapProtoV1, ProtocolV1Inet};
116pub use crate::protocol_v1::protocol_parser::ProxyV1Parser;
117pub use crate::protocol_v1::protocol_composer::{ProxyHdrV1, TryIntoSockAddr};
118
119pub use crate::protocol_v2::{PP2TlvDump, PP2TlvUniqId, PP2TlvRestore};
120pub use crate::protocol_v2::protocol::*;
121pub use crate::protocol_v2::protocol_parser::{ProxyV2Parser, ProxyV2Dummy, PPV2ParserZeroCopy, PPV2ParserCopying, ProxyV2TlvSource, ProxyV2TlvIter};
122pub use crate::protocol_v2::protocol_composer::{HdrV2OpLocal, HdrV2OpProxy, TlvSubTypeSsl, TlType, ProxyHdrV2};
123
124pub use byteorder::{BigEndian, WriteBytesExt};