1pub mod resp;
2pub mod mysql;
3pub mod amqp;
4pub mod postgres;
5pub mod mongodb;
6pub mod http;
7pub mod handler;
8pub mod handlers;
9
10pub use resp::{RespValue, parse_resp};
11pub use mysql::{parse_mysql_request, parse_mysql_response};
12pub use amqp::{parse_amqp_request, parse_amqp_response, format_amqp_response_detail, parse_amqp_frame, parse_amqp_request_full, is_async_method, frame_len as amqp_frame_len};
13pub use handler::ProtocolHandler;
14pub use handlers::*;
15
16use std::time::{Duration, SystemTime};
17
18#[derive(Debug, Clone)]
20pub struct ProxyEvent {
21 pub timestamp: SystemTime,
22 pub component: String,
23 pub protocol: Protocol,
24 pub command: String,
26 pub full_command: String,
28 pub response: String,
30 pub response_detail: String,
32 pub latency: Duration,
34 pub process: Option<String>,
36 pub src: Option<String>,
38 pub dest: Option<String>,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum Direction {
44 Request,
45 Response,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum Protocol {
50 Redis,
51 Mysql,
52 Amqp,
53 Postgres,
54 Mongodb,
55 Http,
56}
57
58impl Protocol {
59 pub fn parse(s: &str) -> Option<Self> {
60 match s.to_lowercase().as_str() {
61 "redis" => Some(Protocol::Redis),
62 "mysql" => Some(Protocol::Mysql),
63 "amqp" | "rabbitmq" => Some(Protocol::Amqp),
64 "postgres" | "postgresql" => Some(Protocol::Postgres),
65 "mongodb" | "mongo" => Some(Protocol::Mongodb),
66 "http" | "elasticsearch" | "es" => Some(Protocol::Http),
67 _ => None,
68 }
69 }
70}
71
72pub fn parse_request(protocol: Protocol, buf: &[u8]) -> Option<String> {
74 get_handler(protocol).parse_request(buf)
75}
76
77pub fn extract_full_command(protocol: Protocol, buf: &[u8]) -> Option<String> {
79 get_handler(protocol).extract_full_command(buf)
80}
81
82pub fn parse_response(protocol: Protocol, buf: &[u8]) -> Option<String> {
84 get_handler(protocol).parse_response(buf)
85}
86
87pub fn format_response_detail(protocol: Protocol, buf: &[u8]) -> Option<String> {
89 get_handler(protocol).format_response_detail(buf)
90}
91
92pub fn get_handler(protocol: Protocol) -> &'static dyn ProtocolHandler {
94 match protocol {
95 Protocol::Redis => &RedisHandler,
96 Protocol::Mysql => &MysqlHandler,
97 Protocol::Amqp => &AmqpHandler,
98 Protocol::Postgres => &PostgresHandler,
99 Protocol::Mongodb => &MongodbHandler,
100 Protocol::Http => &HttpHandler,
101 }
102}