Skip to content

Commit 3fde76d

Browse files
committed
fix the logs
1 parent 55af6f8 commit 3fde76d

File tree

3 files changed

+144
-16
lines changed

3 files changed

+144
-16
lines changed

router-api/src/module/ai_security/thread_xgboost.rs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,99 @@ use std::sync::mpsc::Receiver;
1111
/// Represents a parsed ML feature log for inference
1212
#[derive(Debug, Clone)]
1313
pub struct MlFeatureLog {
14+
// Identification
1415
pub conn_id: String,
16+
17+
// Timing
1518
pub duration_ms: f32,
19+
20+
// HTTP
21+
pub http_status: f32,
22+
pub http_method: String,
23+
pub protocol: String,
24+
25+
// Size metrics
26+
pub size_in: f32,
27+
pub size_out: f32,
28+
29+
// TCP metrics (10 fields)
1630
pub tcp_rtt: f32,
1731
pub tcp_retrans: f32,
1832
pub tcp_lost: f32,
19-
pub http_status: f32,
20-
// Add more features as needed to match your 43 features
33+
pub tcp_send_wnd: f32,
34+
pub tcp_recv_wnd: f32,
35+
pub tcp_send_mss: f32,
36+
pub tcp_recv_mss: f32,
37+
pub tcp_bytes_acked: f32,
38+
pub tcp_segs_in: f32,
39+
pub tcp_segs_out: f32,
40+
41+
// TLS
42+
pub tls_version: String,
43+
44+
// Network
45+
pub client_ip: String,
46+
pub client_port: f32,
47+
pub server_ip: String,
48+
pub server_port: f32,
2149
}
2250

2351
impl MlFeatureLog {
2452
/// Converts the log into a feature vector for model inference
2553
pub fn to_feature_vector(&self) -> Vec<f32> {
2654
vec![
2755
self.duration_ms,
56+
self.http_status,
57+
self.size_in,
58+
self.size_out,
2859
self.tcp_rtt,
2960
self.tcp_retrans,
3061
self.tcp_lost,
31-
self.http_status,
32-
// Add all 43 features here in the correct order
62+
self.tcp_send_wnd,
63+
self.tcp_recv_wnd,
64+
self.tcp_send_mss,
65+
self.tcp_recv_mss,
66+
self.tcp_bytes_acked,
67+
self.tcp_segs_in,
68+
self.tcp_segs_out,
69+
self.client_port,
70+
self.server_port,
71+
self.encode_protocol(),
72+
self.encode_method(),
73+
self.encode_tls(),
3374
]
3475
}
76+
77+
fn encode_protocol(&self) -> f32 {
78+
match self.protocol.as_str() {
79+
"HTTP/1.0" => 0.0,
80+
"HTTP/1.1" => 1.0,
81+
"HTTP/2" => 2.0,
82+
"HTTP/3" => 3.0,
83+
_ => 0.0,
84+
}
85+
}
86+
87+
fn encode_method(&self) -> f32 {
88+
match self.http_method.as_str() {
89+
"GET" => 0.0,
90+
"POST" => 1.0,
91+
"PUT" => 2.0,
92+
"DELETE" => 3.0,
93+
"PATCH" => 4.0,
94+
"HEAD" => 5.0,
95+
"OPTIONS" => 6.0,
96+
_ => 7.0,
97+
}
98+
}
99+
100+
fn encode_tls(&self) -> f32 {
101+
if self.tls_version == "-" || self.tls_version.is_empty() {
102+
0.0
103+
} else {
104+
1.0
105+
}
106+
}
35107
}
36108

37109
/// XGBoost thread handler that processes ML logs and runs inference

router-api/src/module/memory_log/logging/gateway.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ async fn process_batch(
152152
let mut tcp_rtt: u32 = 0;
153153
let mut tcp_retrans: u32 = 0;
154154
let mut tcp_lost: u32 = 0;
155+
let mut protocol = String::new();
156+
let mut http_method = String::new();
157+
let mut tcp_send_wnd: u32 = 0;
158+
let mut tcp_recv_wnd: u32 = 0;
159+
let mut tcp_send_mss: u32 = 0;
160+
let mut tcp_recv_mss: u32 = 0;
161+
let mut tcp_bytes_acked: u64 = 0;
162+
let mut tcp_segs_in: u32 = 0;
163+
let mut tcp_segs_out: u32 = 0;
164+
let mut tls_version = String::new();
165+
let mut client_ip = String::new();
166+
let mut client_port: u16 = 0;
167+
let mut server_ip = String::new();
168+
let mut server_port: u16 = 0;
155169

156170
// Direct field extraction
157171
for field in message_inner.split(',') {
@@ -178,13 +192,35 @@ async fn process_batch(
178192
"TCP_RTT" if ai_enabled => tcp_rtt = value.parse().unwrap_or(0),
179193
"TCP_RETRANS" if ai_enabled => tcp_retrans = value.parse().unwrap_or(0),
180194
"TCP_LOST" if ai_enabled => tcp_lost = value.parse().unwrap_or(0),
195+
"PROTO" if ai_enabled => protocol = value.to_string(),
196+
"METHOD" if ai_enabled => http_method = value.to_string(),
197+
"TCP_SND_WND" if ai_enabled => tcp_send_wnd = value.parse().unwrap_or(0),
198+
"TCP_RCV_WND" if ai_enabled => tcp_recv_wnd = value.parse().unwrap_or(0),
199+
"TCP_SND_MSS" if ai_enabled => tcp_send_mss = value.parse().unwrap_or(0),
200+
"TCP_RCV_MSS" if ai_enabled => tcp_recv_mss = value.parse().unwrap_or(0),
201+
"TCP_BYTES_ACKED" if ai_enabled => tcp_bytes_acked = value.parse().unwrap_or(0),
202+
"TCP_SEGS_IN" if ai_enabled => tcp_segs_in = value.parse().unwrap_or(0),
203+
"TCP_SEGS_OUT" if ai_enabled => tcp_segs_out = value.parse().unwrap_or(0),
204+
"TLS_VER" if ai_enabled => tls_version = value.to_string(),
205+
"CLIENT" if ai_enabled => {
206+
if let Some((ip, port)) = value.rsplit_once(':') {
207+
client_ip = ip.to_string();
208+
client_port = port.parse().unwrap_or(0);
209+
}
210+
},
211+
"SERVER" if ai_enabled => {
212+
if let Some((ip, port)) = value.rsplit_once(':') {
213+
server_ip = ip.to_string();
214+
server_port = port.parse().unwrap_or(0);
215+
}
216+
},
181217

182-
// Skip ML fields when disabled (zero overhead)
183-
"DUR" | "PROTO" | "METHOD" |
184-
"TCP_RTT" | "TCP_RETRANS" | "TCP_LOST" |
185-
"TCP_SND_WND" | "TCP_RCV_WND" | "TCP_SND_MSS" | "TCP_RCV_MSS" |
186-
"TCP_BYTES_ACKED" | "TCP_SEGS_IN" | "TCP_SEGS_OUT" |
187-
"TLS_VER" | "CLIENT" | "SERVER" => {},
218+
// All ML fields are now parsed above when ai_enabled
219+
_ if !ai_enabled && matches!(*key, "DUR" | "PROTO" | "METHOD" |
220+
"TCP_RTT" | "TCP_RETRANS" | "TCP_LOST" |
221+
"TCP_SND_WND" | "TCP_RCV_WND" | "TCP_SND_MSS" | "TCP_RCV_MSS" |
222+
"TCP_BYTES_ACKED" | "TCP_SEGS_IN" | "TCP_SEGS_OUT" |
223+
"TLS_VER" | "CLIENT" | "SERVER") => {},
188224

189225
_ => {} // Ignore unknown fields
190226
}
@@ -228,10 +264,26 @@ async fn process_batch(
228264
let ml_log = MlFeatureLog {
229265
conn_id: conn_id.clone(),
230266
duration_ms,
267+
http_status: status_code as f32,
268+
http_method,
269+
protocol,
270+
size_in: bytes_in as f32,
271+
size_out: bytes_out as f32,
231272
tcp_rtt: tcp_rtt as f32,
232273
tcp_retrans: tcp_retrans as f32,
233274
tcp_lost: tcp_lost as f32,
234-
http_status: status_code as f32,
275+
tcp_send_wnd: tcp_send_wnd as f32,
276+
tcp_recv_wnd: tcp_recv_wnd as f32,
277+
tcp_send_mss: tcp_send_mss as f32,
278+
tcp_recv_mss: tcp_recv_mss as f32,
279+
tcp_bytes_acked: tcp_bytes_acked as f32,
280+
tcp_segs_in: tcp_segs_in as f32,
281+
tcp_segs_out: tcp_segs_out as f32,
282+
tls_version,
283+
client_ip,
284+
client_port: client_port as f32,
285+
server_ip,
286+
server_port: server_port as f32,
235287
};
236288
ai_security::send_to_inference(ml_log);
237289
}

router-core/src/app/gateway_fast.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,19 @@ impl ProxyHttp for GatewayApp {
642642
// Socket addresses
643643
if let Some(socket_digest) = &digest.socket_digest {
644644
if let Some(peer_addr) = socket_digest.peer_addr() {
645-
// Convert entire address to string, parse later if needed
646645
let addr_str = peer_addr.to_string();
647-
_ctx.client_ip = Some(addr_str.clone());
648-
_ctx.client_port = None; // Parse from addr_str if needed
646+
// Use rsplit_once to handle IPv6 addresses like [::1]:8080
647+
if let Some((ip, port)) = addr_str.rsplit_once(':') {
648+
_ctx.client_ip = Some(ip.to_string());
649+
_ctx.client_port = port.parse().ok();
650+
}
649651
}
650652
if let Some(local_addr) = socket_digest.local_addr() {
651653
let addr_str = local_addr.to_string();
652-
_ctx.server_ip = Some(addr_str.clone());
653-
_ctx.server_port = None; // Parse from addr_str if needed
654+
if let Some((ip, port)) = addr_str.rsplit_once(':') {
655+
_ctx.server_ip = Some(ip.to_string());
656+
_ctx.server_port = port.parse().ok();
657+
}
654658
}
655659

656660
// TCP_INFO (Linux only)

0 commit comments

Comments
 (0)