Crate haprox_rs

Source
Expand description

A HaProxy V2 parser.

Use the ProxyHdrV2 to compose the header.

Use the ProxyV2Parser to parse the Ha proxy V2 header.

§Examples:

Composer:

use haprox_rs::{ProxyV2Addr, ProxyHdrV2, HdrV2OpProxy, ProxyTransportFam, PP2TlvClient};

let addr = ProxyV2Addr::try_from(("127.0.0.1:39754", "127.0.0.67:11883")).unwrap();
    
let mut comp = 
    ProxyHdrV2::<HdrV2OpProxy>::new(ProxyTransportFam::STREAM, addr).unwrap();

let plts = comp.set_plts();

let mut ssl = plts.add_ssl(PP2TlvClient::PP2_CLIENT_SSL, 0).unwrap();

ssl.add_ssl_sub_version("TLSv1.2").unwrap();

ssl.done().unwrap();

let pkt: Vec<u8> = comp.try_into().unwrap();

Parser:

use haprox_rs::{ProxyV2Addr, ProxyHdrV2, HdrV2OpProxy, ProxyTransportFam, 
    PP2TlvClient, ProxyV2Parser, ProtocolVersion, HdrV2Command, ProxyV2AddrType, PP2Tlvs};
use haprox_rs::protocol::protocol_raw;
use crate::haprox_rs::protocol::PP2TlvDump;

let pkt_ssl = 
b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a\x21\x11\x00\x2a\
\x7f\x00\x00\x01\x7f\x00\x00\x43\x9d\xd2\x2e\x6b\x20\x00\x1b\x07\
\x00\x00\x00\x00\x21\x00\x07\x54\x4c\x53\x76\x31\x2e\x32\x22\x00\
\x09\x6d\x71\x74\x74\x75\x73\x65\x72\x31";

let dec = ProxyV2Parser::try_from_slice(pkt_ssl.as_slice()).unwrap();

assert_eq!(dec.get_transport().is_ok(), true);
assert_eq!(dec.get_transport().unwrap(), ProxyTransportFam::STREAM);

assert_eq!(dec.get_proto_version(), ProtocolVersion::V2);
assert_eq!(dec.get_proto_command(), HdrV2Command::PROXY);

assert_eq!(dec.get_address_family().is_ok(), true);
assert_eq!(dec.get_address_family().unwrap(), ProxyV2AddrType::AfInet);

assert_eq!(dec.get_palyload_len() as usize, pkt_ssl.len() - size_of::<protocol_raw::ProxyHdrV2>());

let addr = dec.get_address().unwrap();

assert_eq!(addr.is_some(), true);

let addr = addr.unwrap();
let maddr = ProxyV2Addr::try_from(("127.0.0.1:40402", "127.0.0.67:11883")).unwrap();

assert_eq!(addr, maddr);

let tlv_iter = dec.get_tlvs_iter();

assert_eq!(tlv_iter.is_some(), true);

let mut tlv_iter = tlv_iter.unwrap();

let type_ssl = tlv_iter.next().unwrap().take_internal().unwrap();

assert_eq!(type_ssl.get_type(), PP2Tlvs::TYPE_SSL);
let PP2Tlvs::TypeSsl { client, verify } = type_ssl else {panic!("wrong")};

assert_eq!(client, PP2TlvClient::all());
assert_eq!(verify, 0);

Re-exports§

pub extern crate byteorder;
pub use crate::error::HaProxRes;
pub use crate::protocol::protocol_parser::ProxyV2Parser;
pub use crate::protocol::protocol_parser::ProxyV2TlvSource;
pub use crate::protocol::protocol_parser::ProxyV2TlvIter;
pub use crate::protocol::protocol_composer::HdrV2OpLocal;
pub use crate::protocol::protocol_composer::HdrV2OpProxy;
pub use crate::protocol::protocol_composer::TlvSubTypeSsl;
pub use crate::protocol::protocol_composer::TlType;
pub use crate::protocol::protocol_composer::ProxyHdrV2;
pub use crate::protocol::protocol::*;

Modules§

common
error
protocol

Macros§

map_error
return_error

Enums§

BigEndian
Defines big-endian serialization.

Traits§

WriteBytesExt
Extends Write with methods for writing numbers. (For std::io.)