|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Yunshan Networks |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +use nom::FindSubstring; |
| 18 | + |
| 19 | +use super::DubboInfo; |
| 20 | +use crate::config::handler::{L7LogDynamicConfig, TraceType}; |
| 21 | +use crate::utils::bytes::{read_u16_be, read_u32_be}; |
| 22 | + |
| 23 | +const TC_STRING: u8 = 0x74; |
| 24 | +const TC_BLOCKDATA: u8 = 0x77; |
| 25 | + |
| 26 | +fn decode_tc_string(payload: &[u8]) -> Option<String> { |
| 27 | + let offset = 3; |
| 28 | + if payload.len() <= 3 || payload[0] != TC_STRING { |
| 29 | + return None; |
| 30 | + } |
| 31 | + let length = read_u16_be(&payload[1..]) as usize; |
| 32 | + if offset + length >= payload.len() { |
| 33 | + return None; |
| 34 | + } |
| 35 | + let Ok(value) = std::str::from_utf8(&payload[offset..offset + length]) else { |
| 36 | + return None; |
| 37 | + }; |
| 38 | + Some(value.to_string()) |
| 39 | +} |
| 40 | + |
| 41 | +fn lookup_str(payload: &[u8], trace_type: &TraceType) -> Option<String> { |
| 42 | + let tag = match trace_type { |
| 43 | + TraceType::Sw3 | TraceType::Sw8 | TraceType::CloudWise | TraceType::Customize(_) => { |
| 44 | + trace_type.as_str() |
| 45 | + } |
| 46 | + _ => return None, |
| 47 | + }; |
| 48 | + if tag.len() <= 1 { |
| 49 | + return None; |
| 50 | + } |
| 51 | + |
| 52 | + let mut start = 0; |
| 53 | + while start < payload.len() { |
| 54 | + let Some(index) = (&payload[start..]).find_substring(tag) else { |
| 55 | + break; |
| 56 | + }; |
| 57 | + |
| 58 | + start += index + tag.len(); |
| 59 | + if start >= payload.len() { |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + if let Some(s) = decode_tc_string(&payload[start..]) { |
| 64 | + return Some(s); |
| 65 | + } |
| 66 | + } |
| 67 | + return None; |
| 68 | +} |
| 69 | + |
| 70 | +pub fn decode_trace_id(payload: &[u8], trace_type: &TraceType, info: &mut DubboInfo) { |
| 71 | + if let Some(trace_id) = lookup_str(payload, trace_type) { |
| 72 | + info.add_trace_id(trace_id, trace_type); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn decode_span_id(payload: &[u8], trace_type: &TraceType, info: &mut DubboInfo) { |
| 77 | + if let Some(span_id) = lookup_str(payload, trace_type) { |
| 78 | + info.set_span_id(span_id, trace_type); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn decode_header_string(payload: &[u8], offset: usize) -> Option<(String, usize)> { |
| 83 | + if offset + 4 >= payload.len() { |
| 84 | + return None; |
| 85 | + } |
| 86 | + let payload = &payload[offset..]; |
| 87 | + let mut offset = 0; |
| 88 | + let first_length = read_u32_be(&payload[offset..]) as usize; |
| 89 | + offset += 4; |
| 90 | + |
| 91 | + if offset + 2 >= payload.len() { |
| 92 | + return None; |
| 93 | + } |
| 94 | + let second_length = read_u16_be(&payload[offset..]) as usize; |
| 95 | + offset += 2; |
| 96 | + if first_length != second_length |
| 97 | + || second_length == 0 |
| 98 | + || offset + second_length >= payload.len() |
| 99 | + { |
| 100 | + return None; |
| 101 | + } |
| 102 | + |
| 103 | + let Ok(value) = std::str::from_utf8(&payload[offset..offset + second_length]) else { |
| 104 | + return None; |
| 105 | + }; |
| 106 | + |
| 107 | + Some((value.to_string(), offset + second_length)) |
| 108 | +} |
| 109 | + |
| 110 | +pub fn get_req_body_info(config: &L7LogDynamicConfig, payload: &[u8], info: &mut DubboInfo) { |
| 111 | + // Java Body |
| 112 | + // +---------------------------------------------------------------------------------------+ |
| 113 | + // | Magic (2B) | Magic Version (2B) | TC BLOCKDATA Header (2B) | Double version (*) | ... | |
| 114 | + // +---------------------------------------------------------------------------------------+ |
| 115 | + let mut offset = 6; |
| 116 | + let Some(version) = decode_header_string(&payload, offset) else { |
| 117 | + return; |
| 118 | + }; |
| 119 | + info.dubbo_version = version.0; |
| 120 | + offset += version.1; |
| 121 | + |
| 122 | + let Some(service_name) = decode_header_string(&payload, offset) else { |
| 123 | + return; |
| 124 | + }; |
| 125 | + info.service_name = service_name.0; |
| 126 | + offset += service_name.1; |
| 127 | + |
| 128 | + let Some(service_version) = decode_header_string(&payload, offset) else { |
| 129 | + return; |
| 130 | + }; |
| 131 | + info.service_version = service_version.0; |
| 132 | + offset += service_version.1; |
| 133 | + |
| 134 | + let Some(method_name) = decode_header_string(&payload, offset) else { |
| 135 | + return; |
| 136 | + }; |
| 137 | + info.method_name = method_name.0; |
| 138 | + offset += method_name.1; |
| 139 | + |
| 140 | + if config.trace_types.is_empty() || offset >= payload.len() { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + for trace_type in config.trace_types.iter() { |
| 145 | + if trace_type.as_str().len() > u8::MAX as usize { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + decode_trace_id(&payload[offset..], &trace_type, info); |
| 150 | + if !config.multiple_trace_id_collection && !info.trace_ids.0.is_empty() { |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + for span_type in config.span_types.iter() { |
| 155 | + if span_type.as_str().len() > u8::MAX as usize { |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + decode_span_id(&payload[offset..], &span_type, info); |
| 160 | + if info.span_id.get().len() != 0 { |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments