fix: trace debug — show Stranger_NNN for unnamed traces instead of unknown

This commit is contained in:
Accusys
2026-05-14 15:12:21 +08:00
parent 8f013cbdbc
commit c90394897d
42 changed files with 1806 additions and 1722 deletions

View File

@@ -345,6 +345,8 @@ async fn trace_video(
trace_frames.entry(*tid).or_default().push(*fn_);
if let Some(name) = name_opt {
trace_identity.entry(*tid).or_insert_with(|| name.clone());
} else {
trace_identity.entry(*tid).or_insert_with(|| format!("Stranger_{:03}", tid));
}
}

View File

@@ -46,7 +46,8 @@ pub async fn api_key_validation(
let headers = request.headers();
tracing::info!("[MIDDLEWARE] All headers: {:?}", headers);
let api_key = match extract_api_key(headers) {
let uri = request.uri().clone();
let api_key = match extract_api_key(headers, &uri) {
Ok(key) => {
tracing::info!("[MIDDLEWARE] API key extracted, length: {}", key.len());
if key.len() > 8 {
@@ -128,12 +129,61 @@ pub async fn api_key_validation(
response
}
fn extract_api_key(headers: &HeaderMap) -> Result<String, StatusCode> {
headers
fn extract_api_key(headers: &HeaderMap, uri: &axum::http::Uri) -> Result<String, StatusCode> {
// 1. X-API-Key header
if let Some(key) = headers
.get("X-API-Key")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
.ok_or(StatusCode::UNAUTHORIZED)
{
return Ok(key.to_string());
}
// 2. Authorization: Bearer <key>
if let Some(auth) = headers
.get("Authorization")
.and_then(|v| v.to_str().ok())
{
if let Some(key) = auth.strip_prefix("Bearer ") {
return Ok(key.to_string());
}
}
// 3. ?api_key=<key> query parameter
if let Some(query) = uri.query() {
for pair in query.split('&') {
let mut parts = pair.splitn(2, '=');
if let (Some(k), Some(v)) = (parts.next(), parts.next()) {
if k == "api_key" {
return Ok(percent_decode(v));
}
}
}
}
Err(StatusCode::UNAUTHORIZED)
}
fn percent_decode(s: &str) -> String {
let mut result = String::new();
let mut chars = s.bytes();
while let Some(b) = chars.next() {
match b {
b'%' => {
let hi = chars.next().and_then(|c| hex_val(c)).unwrap_or(0);
let lo = chars.next().and_then(|c| hex_val(c)).unwrap_or(0);
result.push((hi << 4 | lo) as char);
}
b'+' => result.push(' '),
_ => result.push(b as char),
}
}
result
}
fn hex_val(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
b'A'..=b'F' => Some(c - b'A' + 10),
_ => None,
}
}
fn hash_key(key: &str) -> String {