use psi_detector::{
DetectorBuilder, ProtocolDetector, ProtocolType,
core::ProbeStrategy
};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("⚙️ PSI-Detector 自定义配置示例");
println!("\n🔧 1. 基础配置示例");
demonstrate_basic_configuration()?;
println!("\n🎯 2. 高级策略配置");
demonstrate_advanced_strategies()?;
println!("\n⚡ 3. 性能调优配置");
demonstrate_performance_tuning()?;
println!("\n🎉 自定义配置示例完成!");
Ok(())
}
fn demonstrate_basic_configuration() -> Result<(), Box<dyn std::error::Error>> {
println!(" 📝 创建基础配置");
let minimal_detector = DetectorBuilder::new()
.enable_http()
.build()?;
println!(" ✅ 最小配置探测器创建成功");
let full_detector = DetectorBuilder::new()
.enable_http()
.enable_tls()
.enable_ssh()
.with_strategy(ProbeStrategy::Passive)
.with_timeout(Duration::from_millis(100))
.with_buffer_size(8192)
.with_min_confidence(0.8)
.build()?;
println!(" ✅ 完整配置探测器创建成功");
let test_data = b"GET /api/test HTTP/1.1\r\nHost: example.com\r\n\r\n";
let minimal_result = minimal_detector.detect(test_data)?;
let full_result = full_detector.detect(test_data)?;
println!(" 📊 配置对比:");
println!(" 最小配置 - 协议: {:?}, 置信度: {:.1}%",
minimal_result.protocol_type(), minimal_result.confidence() * 100.0);
println!(" 完整配置 - 协议: {:?}, 置信度: {:.1}%",
full_result.protocol_type(), full_result.confidence() * 100.0);
Ok(())
}
fn demonstrate_advanced_strategies() -> Result<(), Box<dyn std::error::Error>> {
println!(" 🎯 测试不同探测策略");
let test_data = b"SSH-2.0-OpenSSH_8.0\r\n";
let strategies = vec![
(ProbeStrategy::Passive, "被动探测"),
(ProbeStrategy::Active, "主动探测"),
];
for (strategy, name) in strategies {
let detector = DetectorBuilder::new()
.enable_ssh()
.with_strategy(strategy)
.with_timeout(Duration::from_millis(50))
.build()?;
let start_time = std::time::Instant::now();
let result = detector.detect(test_data)?;
let detection_time = start_time.elapsed();
println!(" 📈 {} 策略:", name);
println!(" 协议: {:?}", result.protocol_type());
println!(" 置信度: {:.1}%", result.confidence() * 100.0);
println!(" 检测时间: {:?}", detection_time);
println!(" 检测方法: {:?}", result.detection_method);
println!();
}
Ok(())
}
fn demonstrate_performance_tuning() -> Result<(), Box<dyn std::error::Error>> {
println!(" ⚡ 性能调优配置");
let high_performance = DetectorBuilder::new()
.enable_http()
.enable_tls()
.enable_ssh()
.with_strategy(ProbeStrategy::Passive)
.with_timeout(Duration::from_millis(10))
.with_buffer_size(4096)
.with_min_confidence(0.6)
.build()?;
let high_accuracy = DetectorBuilder::new()
.enable_http()
.enable_tls()
.enable_ssh()
.with_strategy(ProbeStrategy::Active)
.with_timeout(Duration::from_millis(500))
.with_buffer_size(16384)
.with_min_confidence(0.9)
.build()?;
let balanced = DetectorBuilder::new()
.enable_http()
.enable_tls()
.enable_ssh()
.with_strategy(ProbeStrategy::Passive)
.with_timeout(Duration::from_millis(100))
.with_buffer_size(8192)
.with_min_confidence(0.75)
.build()?;
let tls_data = create_tls_client_hello();
let test_scenarios = vec![
("HTTP", b"GET /test HTTP/1.1\r\nHost: example.com\r\n\r\n".as_slice()),
("TLS", tls_data.as_slice()),
("SSH", b"SSH-2.0-OpenSSH_8.0\r\n"),
];
fn create_tls_client_hello() -> Vec<u8> {
vec![
0x16, 0x03, 0x03, 0x00, 0x40,
0x01, 0x00, 0x00, 0x3C,
0x03, 0x03,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x00,
0x00, 0x02,
0x00, 0x35,
0x01,
0x00,
0x00, 0x00, ]
}
let configurations = vec![
(&high_performance, "高性能"),
(&high_accuracy, "高精度"),
(&balanced, "平衡"),
];
println!(" 📊 性能对比测试:");
println!(" ┌─────────────┬──────────┬──────────────┬──────────────┬──────────────┐");
println!(" │ 配置 │ 协议 │ 检测时间 │ 置信度 │ 准确性 │");
println!(" ├─────────────┼──────────┼──────────────┼──────────────┼──────────────┤");
for (detector, config_name) in &configurations {
for (protocol_name, test_data) in &test_scenarios {
let start_time = std::time::Instant::now();
let result = detector.detect(test_data);
let detection_time = start_time.elapsed();
match result {
Ok(detection_result) => {
let accuracy = if detection_result.is_high_confidence() { "高" } else { "中" };
println!(" │ {:>9} │ {:>6} │ {:>10.2?} │ {:>9.1}% │ {:>10} │",
config_name,
protocol_name,
detection_time,
detection_result.confidence() * 100.0,
accuracy
);
}
Err(_) => {
println!(" │ {:>9} │ {:>6} │ {:>10.2?} │ {:>9} │ {:>10} │",
config_name,
protocol_name,
detection_time,
"失败",
"低"
);
}
}
}
}
println!(" └─────────────┴──────────┴──────────────┴──────────────┴──────────────┘");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_configuration() {
let detector = DetectorBuilder::new()
.enable_http()
.with_timeout(Duration::from_millis(100))
.build()
.expect("Failed to build detector");
let test_data = b"GET / HTTP/1.1\r\nHost: test.com\r\n\r\n";
let result = detector.detect(test_data)
.expect("Detection failed");
assert_eq!(result.protocol_type(), ProtocolType::HTTP1_1);
}
#[test]
fn test_strategy_comparison() {
let strategies = vec![
ProbeStrategy::Passive,
ProbeStrategy::Active,
];
let test_data = b"SSH-2.0-OpenSSH_8.0\r\n";
for strategy in strategies {
let detector = DetectorBuilder::new()
.enable_ssh()
.with_strategy(strategy)
.build()
.expect("Failed to build detector");
let result = detector.detect(test_data)
.expect("Detection failed");
assert_eq!(result.protocol_type(), ProtocolType::SSH);
}
}
}