psi_detector 0.1.3

Protocol detection and upgrade framework inspired by Yuri's PSI Detector
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
//! 协议探测器核心接口
//!
//! 定义协议探测的核心trait和相关类型。

use crate::core::protocol::{ProtocolType, ProtocolInfo};
use crate::core::probe::{ProbeRegistry, ProbeConfig, ProbeContext, ProbeAggregator};
use crate::core::magic::MagicDetector;
use crate::error::{DetectorError, Result};
use std::time::{Duration, Instant};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::collections::HashMap;

/// 协议代理角色
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Role {
    /// 服务器模式 - 被动探测传入连接
    Server,
    /// 客户端模式 - 主动探测服务端能力
    Client,
}

impl Role {
    /// 检查是否为服务器角色
    pub fn is_server(&self) -> bool {
        matches!(self, Role::Server)
    }
    
    /// 检查是否为客户端角色
    pub fn is_client(&self) -> bool {
        matches!(self, Role::Client)
    }
}

/// 协议代理配置
#[derive(Debug, Clone)]
pub struct AgentConfig {
    /// 代理角色
    pub role: Role,
    /// 实例ID(用于多实例场景)
    pub instance_id: String,
    /// 探测配置
    pub detection_config: DetectionConfig,
    /// 探测器配置
    pub probe_config: ProbeConfig,
    /// 启用的协议列表
    pub enabled_protocols: Vec<ProtocolType>,
    /// 是否启用协议升级
    pub enable_upgrade: bool,
    /// 负载均衡配置(仅服务器模式)
    pub load_balancer_config: Option<LoadBalancerConfig>,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            role: Role::Server,
            instance_id: uuid::Uuid::new_v4().to_string(),
            detection_config: DetectionConfig::default(),
            probe_config: ProbeConfig::default(),
            enabled_protocols: vec![
                ProtocolType::HTTP1_1,
                ProtocolType::HTTP2,
                ProtocolType::TLS,
            ],
            enable_upgrade: true,
            load_balancer_config: None,
        }
    }
}

/// 负载均衡配置
#[derive(Debug, Clone)]
pub struct LoadBalancerConfig {
    /// 是否作为负载均衡器
    pub is_load_balancer: bool,
    /// 后端实例列表
    pub backend_instances: Vec<String>,
    /// 负载均衡策略
    pub strategy: LoadBalanceStrategy,
}

/// 负载均衡策略
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadBalanceStrategy {
    /// 轮询
    RoundRobin,
    /// 最少连接
    LeastConnections,
    /// 加权轮询
    WeightedRoundRobin,
    /// 一致性哈希
    ConsistentHash,
}

/// 探测结果
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DetectionResult {
    /// 探测到的协议信息
    pub protocol_info: ProtocolInfo,
    /// 探测耗时
    pub detection_time: Duration,
    /// 使用的探测方法
    pub detection_method: DetectionMethod,
    /// 探测器名称
    pub detector_name: String,
}

impl DetectionResult {
    /// 创建新的探测结果
    pub fn new(
        protocol_info: ProtocolInfo,
        detection_time: Duration,
        detection_method: DetectionMethod,
        detector_name: String,
    ) -> Self {
        Self {
            protocol_info,
            detection_time,
            detection_method,
            detector_name,
        }
    }
    
    /// 获取协议类型
    pub fn protocol_type(&self) -> ProtocolType {
        self.protocol_info.protocol_type
    }
    
    /// 获取置信度
    pub fn confidence(&self) -> f32 {
        self.protocol_info.confidence
    }
    
    /// 检查是否为高置信度结果
    pub fn is_high_confidence(&self) -> bool {
        self.confidence() >= 0.8
    }
    
    /// 检查是否为可接受的结果
    pub fn is_acceptable(&self, min_confidence: f32) -> bool {
        self.confidence() >= min_confidence
    }
}

/// 默认协议探测器实现
#[derive(Debug)]
pub struct DefaultProtocolDetector {
    registry: ProbeRegistry,
    probe_config: ProbeConfig,
    detection_config: DetectionConfig,
    enabled_protocols: Vec<ProtocolType>,
    aggregator: ProbeAggregator,
    magic_detector: MagicDetector,
}

impl DefaultProtocolDetector {
    /// 创建新的协议探测器
    pub fn new(
        registry: ProbeRegistry,
        probe_config: ProbeConfig,
        detection_config: DetectionConfig,
        enabled_protocols: Vec<ProtocolType>,
    ) -> Result<Self> {
        let aggregator = ProbeAggregator::new(probe_config.clone());
        
        // 验证配置:必须至少启用一个协议
        if enabled_protocols.is_empty() {
            return Err(DetectorError::config_error(
                "至少需要启用一个协议,否则无法进行协议检测"
            ));
        }
        
        // 创建魔法包检测器并设置启用的协议
        let magic_detector = MagicDetector::new()
            .with_enabled_protocols(enabled_protocols.clone());
        
        Ok(Self {
            registry,
            probe_config,
            detection_config,
            enabled_protocols,
            aggregator,
            magic_detector,
        })
    }
    
    /// 获取探测配置
    pub fn probe_config(&self) -> &ProbeConfig {
        &self.probe_config
    }
    
    /// 获取检测配置
    pub fn detection_config(&self) -> &DetectionConfig {
        &self.detection_config
    }
    
    /// 获取启用的协议列表
    pub fn enabled_protocols(&self) -> &[ProtocolType] {
        &self.enabled_protocols
    }
}

impl ProtocolDetector for DefaultProtocolDetector {
    fn detect(&self, data: &[u8]) -> Result<DetectionResult> {
        let start_time = Instant::now();
        let mut context = ProbeContext::new();
        context.bytes_read = data.len();
        
        // 检查数据大小
        if data.len() < self.min_probe_size() {
            return Err(DetectorError::InsufficientData(
                format!("需要至少 {} 字节,但只有 {} 字节", self.min_probe_size(), data.len())
            ));
        }
        
        if data.len() > self.max_probe_size() {
            return Err(DetectorError::DataTooLarge(
                format!("数据大小 {} 字节超过最大限制 {} 字节", data.len(), self.max_probe_size())
            ));
        }
        
        // 🚀 第一阶段:超快速魔法包检测(前几个字节启发式判断)
        if let Some(magic_result) = self.magic_detector.quick_detect(data) {
            // 如果魔法包检测置信度很高,直接返回结果
            if magic_result.confidence >= 0.95 {
                let detection_time = start_time.elapsed();
                return Ok(DetectionResult::new(
                    magic_result,
                    detection_time,
                    DetectionMethod::SimdAccelerated, // 魔法包检测视为SIMD加速
                    "MagicBytesDetector".to_string(),
                ));
            }
            
            // 中等置信度的魔法包结果作为候选
            context.add_candidate(magic_result);
        }
        
        // 预分配结果容器以减少内存重分配
        let mut all_results = Vec::with_capacity(self.enabled_protocols.len());
        
        // 缓存超时时间以避免重复访问
        let max_detection_time = self.detection_config.timeout;
        
        // 优化探测器循环:避免重复探测,快速失败策略
        let mut processed_probes = std::collections::HashSet::new();
        
        // 🎯 严格协议过滤:只运行启用协议的探测器(核心性能优化)
        for &protocol in &self.enabled_protocols {
            // 快速超时检查
            if start_time.elapsed() > max_detection_time {
                break;
            }
            
            let probes = self.registry.get_probes_for_enabled_protocol(protocol, &self.enabled_protocols);
            for probe in probes {
                let probe_name = probe.name();
                
                // 避免重复运行同一探测器
                if processed_probes.contains(probe_name) {
                    continue;
                }
                processed_probes.insert(probe_name);
                
                // 检查是否需要更多数据(快速失败)
                if probe.needs_more_data(data) {
                    continue;
                }
                
                // 执行探测
                match probe.probe(data, &mut context) {
                    Ok(Some(protocol_info)) => {
                        // 只接受启用协议的结果
                        if self.enabled_protocols.contains(&protocol_info.protocol_type) {
                            let high_confidence = protocol_info.confidence >= 0.9;
                            all_results.push(protocol_info);
                            
                            // 如果找到高置信度结果,可以提前结束
                            if high_confidence {
                                break;
                            }
                        }
                    }
                    Ok(None) => {
                        // 探测器没有检测到协议,继续
                    }
                    Err(_) => {
                        // 静默忽略错误,避免性能开销
                    }
                }
                
                // 每5个探测器检查一次超时
                if processed_probes.len() % 5 == 0 && start_time.elapsed() > max_detection_time {
                    break;
                }
            }
        }
        
        // 🚨 性能优化:不再运行全局探测器,严格按配置执行
        // 如果没有找到结果但有启用的协议,说明可能是不匹配的流量
        // 在严格模式下,这种流量应该被拒绝而不是继续探测
        
        // 🔍 第三阶段:如果没有找到结果,尝试深度魔法包检测
        if all_results.is_empty() {
            let deep_magic_results = self.magic_detector.deep_detect(data);
            all_results.extend(deep_magic_results);
        }
        
        // 合并魔法包候选结果
        all_results.extend(context.candidates.clone());
        
        // 聚合结果
        let best_result = self.aggregator.aggregate(all_results)
            .ok_or_else(|| DetectorError::NoProtocolDetected("未检测到任何协议".to_string()))?;
        
        // 创建最终结果
        let detection_time = start_time.elapsed();
        Ok(self.aggregator.create_result(
            best_result,
            detection_time,
            "DefaultProtocolDetector".to_string(),
        ))
    }
    
    fn min_probe_size(&self) -> usize {
        self.detection_config.min_probe_size
    }
    
    fn max_probe_size(&self) -> usize {
        self.detection_config.max_probe_size
    }
    
    fn supported_protocols(&self) -> Vec<ProtocolType> {
        self.enabled_protocols.clone()
    }
    
    fn name(&self) -> &str {
        "DefaultProtocolDetector"
    }
}

/// 探测方法
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DetectionMethod {
    /// 被动探测(仅分析数据)
    Passive,
    /// 主动探测(发送探测包)
    Active,
    /// 启发式探测
    Heuristic,
    /// SIMD加速探测
    SimdAccelerated,
    /// 混合探测
    Hybrid,
}

/// 协议代理trait - 统一的双向框架接口
pub trait ProtocolAgent: Send + Sync + std::fmt::Debug {
    /// 探测协议类型(被动模式 - 服务器角色)
    fn detect(&self, data: &[u8]) -> Result<DetectionResult>;
    
    /// 主动探测协议能力(主动模式 - 客户端角色)
    fn probe_capabilities(&self, transport: &mut dyn Transport) -> Result<Vec<ProtocolType>> {
        match self.role() {
            Role::Client => {
                // 客户端模式:主动发送探测请求
                self.active_probe(transport)
            },
            Role::Server => {
                // 服务器模式:不支持主动探测
                Err(DetectorError::unsupported_protocol(
                    "Server role does not support active probing"
                ))
            },
        }
    }
    
    /// 主动探测实现(客户端专用)
    fn active_probe(&self, transport: &mut dyn Transport) -> Result<Vec<ProtocolType>> {
        let mut supported_protocols = Vec::new();
        
        // 按优先级顺序探测协议
        let probe_order = [
            ProtocolType::HTTP3,
            ProtocolType::HTTP2, 
            ProtocolType::HTTP1_1,
            ProtocolType::TLS,
            ProtocolType::QUIC,
        ];
        
        for protocol in probe_order {
            if self.supports_protocol(protocol) {
                match self.send_protocol_probe(transport, protocol) {
                    Ok(true) => {
                        supported_protocols.push(protocol);
                        // 找到最高优先级协议后可以选择停止或继续探测
                        if matches!(protocol, ProtocolType::HTTP3 | ProtocolType::QUIC) {
                            break;
                        }
                    },
                    Ok(false) => continue,
                    Err(_) => continue, // 探测失败,继续下一个协议
                }
            }
        }
        
        if supported_protocols.is_empty() {
            // 保底协议
            supported_protocols.push(ProtocolType::HTTP1_1);
        }
        
        Ok(supported_protocols)
    }
    
    /// 发送特定协议的探测请求
    fn send_protocol_probe(&self, transport: &mut dyn Transport, protocol: ProtocolType) -> Result<bool> {
        match protocol {
            ProtocolType::HTTP2 => {
                // HTTP/2 连接前言探测
                let h2_preface = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
                transport.write(h2_preface)?;
                
                // 读取响应
                let mut response = vec![0u8; 24];
                match transport.read(&mut response) {
                    Ok(n) if n > 0 => {
                        // 检查是否收到HTTP/2 SETTINGS帧
                        Ok(response.len() >= 9 && response[3] == 0x04) // SETTINGS帧类型
                    },
                    _ => Ok(false),
                }
            },
            ProtocolType::HTTP3 => {
                // HTTP/3 over QUIC探测
                // 这里需要QUIC握手逻辑,简化实现
                Ok(false) // 暂时返回false,需要完整的QUIC实现
            },
            ProtocolType::HTTP1_1 => {
                // HTTP/1.1 OPTIONS请求探测
                let options_request = b"OPTIONS * HTTP/1.1\r\nHost: probe\r\n\r\n";
                transport.write(options_request)?;
                
                let mut response = vec![0u8; 256];
                match transport.read(&mut response) {
                    Ok(n) if n > 0 => {
                        let response_str = String::from_utf8_lossy(&response[..n]);
                        Ok(response_str.starts_with("HTTP/1.1") || response_str.starts_with("HTTP/1.0"))
                    },
                    _ => Ok(false),
                }
            },
            ProtocolType::TLS => {
                // TLS ClientHello探测
                let client_hello = self.create_tls_client_hello();
                transport.write(&client_hello)?;
                
                let mut response = vec![0u8; 1024];
                match transport.read(&mut response) {
                    Ok(n) if n >= 5 => {
                        // 检查TLS ServerHello响应
                        Ok(response[0] == 0x16 && response[1] == 0x03) // TLS握手记录
                    },
                    _ => Ok(false),
                }
            },
            _ => Ok(false), // 其他协议暂不支持主动探测
        }
    }
    
    /// 创建TLS ClientHello消息
    fn create_tls_client_hello(&self) -> Vec<u8> {
        // 简化的TLS 1.2 ClientHello
        vec![
            0x16, 0x03, 0x01, 0x00, 0x2f, // TLS记录头
            0x01, 0x00, 0x00, 0x2b,       // 握手消息头
            0x03, 0x03,                   // TLS版本1.2
            // 32字节随机数(简化)
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00,                         // 会话ID长度
            0x00, 0x02,                   // 密码套件长度
            0x00, 0x35,                   // AES128-SHA
            0x01, 0x00,                   // 压缩方法
        ]
    }
    
    /// 协议升级(主动/被动模式)
    fn upgrade(
        &self,
        transport: Box<dyn Transport>,
        role: Role,
    ) -> Result<Box<dyn Transport>>;
    
    /// 协议协商(客户端模式)
    fn negotiate_protocol(&self, max_supported: ProtocolType) -> ProtocolType {
        let candidates = [
            ProtocolType::HTTP3,
            ProtocolType::HTTP2,
            ProtocolType::HTTP1_1,
        ];
        
        for proto in candidates {
            if self.supports_protocol(proto) && proto <= max_supported {
                return proto;
            }
        }
        ProtocolType::HTTP1_1 // 保底协议
    }
    
    /// 智能降级策略(客户端模式)
    fn auto_fallback(&self, transport: &mut dyn Transport, preferred: ProtocolType) -> Result<ProtocolType> {
        match self.role() {
            Role::Client => {
                // 尝试首选协议
                if self.send_protocol_probe(transport, preferred)? {
                    return Ok(preferred);
                }
                
                // 自动降级
                let fallback_chain = match preferred {
                    ProtocolType::HTTP3 => vec![ProtocolType::HTTP2, ProtocolType::HTTP1_1],
                    ProtocolType::HTTP2 => vec![ProtocolType::HTTP1_1],
                    ProtocolType::QUIC => vec![ProtocolType::TLS, ProtocolType::TCP],
                    _ => vec![ProtocolType::HTTP1_1],
                };
                
                for fallback in fallback_chain {
                    if self.supports_protocol(fallback) && 
                       self.send_protocol_probe(transport, fallback)? {
                        return Ok(fallback);
                    }
                }
                
                // 最终保底
                Ok(ProtocolType::HTTP1_1)
            },
            Role::Server => {
                Err(DetectorError::unsupported_protocol(
                    "Server role does not support auto fallback"
                ))
            },
        }
    }
    
    /// 检查是否支持指定协议
    fn supports_protocol(&self, protocol: ProtocolType) -> bool;
    
    /// 获取代理角色
    fn role(&self) -> Role;
    
    /// 获取实例ID
    fn instance_id(&self) -> &str;
    
    /// 获取代理名称
    fn name(&self) -> &str;
}

/// 传输层抽象trait
pub trait Transport: Send + Sync {
    /// 读取数据
    fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
    
    /// 写入数据
    fn write(&mut self, data: &[u8]) -> Result<usize>;
    
    /// 预览数据(不消费)
    fn peek(&self, size: usize) -> Result<Vec<u8>>;
    
    /// 关闭连接
    fn close(&mut self) -> Result<()>;
    
    /// 获取传输层类型
    fn transport_type(&self) -> &str;
}

/// 协议探测器trait
pub trait ProtocolDetector: Send + Sync + std::fmt::Debug {
    /// 探测协议类型
    fn detect(&self, data: &[u8]) -> Result<DetectionResult>;
    
    /// 获取协议特征置信度
    fn confidence(&self, data: &[u8]) -> Result<f32> {
        self.detect(data).map(|result| result.confidence())
    }
    
    /// 最小探测数据要求
    fn min_probe_size(&self) -> usize {
        64 // 默认64字节
    }
    
    /// 最大探测数据大小
    fn max_probe_size(&self) -> usize {
        4096 // 默认4KB
    }
    
    /// 支持的协议类型
    fn supported_protocols(&self) -> Vec<ProtocolType>;
    
    /// 探测器名称
    fn name(&self) -> &str;
    
    /// 检查是否可以探测指定协议
    fn can_detect(&self, protocol: ProtocolType) -> bool {
        self.supported_protocols().contains(&protocol)
    }
    
    /// 批量探测(可选实现)
    fn detect_batch(&self, data_chunks: &[&[u8]]) -> Result<Vec<DetectionResult>> {
        data_chunks
            .iter()
            .map(|chunk| self.detect(chunk))
            .collect()
    }
}

/// 异步协议探测器trait
#[cfg(any(feature = "runtime-tokio", feature = "runtime-async-std"))]
#[async_trait::async_trait]
pub trait AsyncProtocolDetector: Send + Sync {
    /// 异步探测协议类型
    async fn detect_async(&self, data: &[u8]) -> Result<DetectionResult>;
    
    /// 异步获取置信度
    async fn confidence_async(&self, data: &[u8]) -> Result<f32> {
        self.detect_async(data).await.map(|result| result.confidence())
    }
    
    /// 最小探测数据要求
    fn min_probe_size(&self) -> usize {
        64
    }
    
    /// 最大探测数据大小
    fn max_probe_size(&self) -> usize {
        4096
    }
    
    /// 支持的协议类型
    fn supported_protocols(&self) -> Vec<ProtocolType>;
    
    /// 探测器名称
    fn name(&self) -> &str;
    
    /// 异步批量探测
    async fn detect_batch_async(&self, data_chunks: &[&[u8]]) -> Result<Vec<DetectionResult>> {
        let mut results = Vec::new();
        for chunk in data_chunks {
            results.push(self.detect_async(chunk).await?);
        }
        Ok(results)
    }
}

/// 探测配置
#[derive(Debug, Clone)]
pub struct DetectionConfig {
    /// 最小置信度阈值
    pub min_confidence: f32,
    /// 探测超时时间
    pub timeout: Duration,
    /// 是否启用启发式探测
    pub enable_heuristic: bool,
    /// 是否启用主动探测
    pub enable_active_probing: bool,
    /// 最大探测数据大小
    pub max_probe_size: usize,
    /// 最小探测数据大小
    pub min_probe_size: usize,
    /// 是否启用SIMD加速
    pub enable_simd: bool,
}

impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            min_confidence: 0.7,
            timeout: Duration::from_millis(1000),
            enable_heuristic: true,
            enable_active_probing: false,
            max_probe_size: 1024 * 1024,  // 增加到1MB以支持gRPC大帧
            min_probe_size: 16,  // 默认16字节,适合大多数协议
            enable_simd: true,
        }
    }
}

/// 协议代理实现 - 支持双向框架和多实例
#[derive(Debug)]
pub struct Agent {
    /// 代理配置
    config: AgentConfig,
    /// 协议探测器
    detector: Arc<dyn ProtocolDetector>,
    /// 协议升级器
    upgrader: Option<Arc<dyn crate::upgrade::ProtocolUpgrader>>,
    /// 实例状态
    state: Arc<std::sync::RwLock<AgentState>>,
    /// 负载均衡器(仅服务器模式)
    load_balancer: Option<Arc<LoadBalancer>>,
}

/// 代理状态
#[derive(Debug, Clone)]
pub struct AgentState {
    /// 活跃连接数
    pub active_connections: usize,
    /// 处理的总请求数
    pub total_requests: u64,
    /// 成功的协议升级数
    pub successful_upgrades: u64,
    /// 失败的协议升级数
    pub failed_upgrades: u64,
    /// 最后活动时间
    pub last_activity: Instant,
    /// 是否健康
    pub is_healthy: bool,
}

impl Default for AgentState {
    fn default() -> Self {
        Self {
            active_connections: 0,
            total_requests: 0,
            successful_upgrades: 0,
            failed_upgrades: 0,
            last_activity: Instant::now(),
            is_healthy: true,
        }
    }
}

/// 负载均衡器
#[derive(Debug)]
pub struct LoadBalancer {
    /// 配置
    config: LoadBalancerConfig,
    /// 后端实例状态
    backends: Arc<std::sync::RwLock<HashMap<String, BackendState>>>,
    /// 当前轮询索引
    round_robin_index: Arc<std::sync::atomic::AtomicUsize>,
}

/// 后端实例状态
#[derive(Debug, Clone)]
pub struct BackendState {
    /// 实例ID
    pub instance_id: String,
    /// 活跃连接数
    pub active_connections: usize,
    /// 权重
    pub weight: u32,
    /// 是否健康
    pub is_healthy: bool,
    /// 最后健康检查时间
    pub last_health_check: Instant,
}

impl Agent {
    /// 创建新的协议代理
    pub fn new(
        config: AgentConfig,
        detector: Arc<dyn ProtocolDetector>,
        upgrader: Option<Arc<dyn crate::upgrade::ProtocolUpgrader>>,
    ) -> Self {
        let load_balancer = config.load_balancer_config.as_ref().map(|lb_config| {
            Arc::new(LoadBalancer::new(lb_config.clone()))
        });
        
        Self {
            config,
            detector,
            upgrader,
            state: Arc::new(std::sync::RwLock::new(AgentState::default())),
            load_balancer,
        }
    }
    
    /// 获取代理配置
    pub fn config(&self) -> &AgentConfig {
        &self.config
    }
    
    /// 获取代理状态
    pub fn state(&self) -> Result<AgentState> {
        self.state.read()
            .map_err(|_| DetectorError::internal_error("Failed to read agent state"))
            .map(|state| state.clone())
    }
    
    /// 更新连接计数
    pub fn update_connection_count(&self, delta: i32) -> Result<()> {
        // 使用 try_write 避免阻塞,提高并发性能
        if let Ok(mut state) = self.state.try_write() {
            if delta > 0 {
                state.active_connections += delta as usize;
            } else {
                state.active_connections = state.active_connections.saturating_sub((-delta) as usize);
            }
            state.last_activity = Instant::now();
        } else {
            // 如果获取锁失败,记录警告但不阻塞
            rat_logger::warn!("Failed to acquire lock for connection count update");
        }
        Ok(())
    }
    
    /// 选择后端实例(负载均衡)
    pub fn select_backend(&self) -> Option<String> {
        self.load_balancer.as_ref()?.select_backend()
    }
    
    /// 健康检查
    pub fn health_check(&self) -> bool {
        if let Ok(state) = self.state.read() {
            state.is_healthy && state.last_activity.elapsed() < Duration::from_secs(300)
        } else {
            false
        }
    }
}

impl LoadBalancer {
    /// 创建新的负载均衡器
    pub fn new(config: LoadBalancerConfig) -> Self {
        let backends = config.backend_instances.iter()
            .map(|instance_id| {
                let state = BackendState {
                    instance_id: instance_id.clone(),
                    active_connections: 0,
                    weight: 1,
                    is_healthy: true,
                    last_health_check: Instant::now(),
                };
                (instance_id.clone(), state)
            })
            .collect();
        
        Self {
            config,
            backends: Arc::new(std::sync::RwLock::new(backends)),
            round_robin_index: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        }
    }
    
    /// 选择后端实例
    pub fn select_backend(&self) -> Option<String> {
        let backends = self.backends.read().ok()?;
        let healthy_backends: Vec<_> = backends.values()
            .filter(|backend| backend.is_healthy)
            .collect();
        
        if healthy_backends.is_empty() {
            return None;
        }
        
        match self.config.strategy {
            LoadBalanceStrategy::RoundRobin => {
                let index = self.round_robin_index.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Some(healthy_backends[index % healthy_backends.len()].instance_id.clone())
            },
            LoadBalanceStrategy::LeastConnections => {
                healthy_backends.iter()
                    .min_by_key(|backend| backend.active_connections)
                    .map(|backend| backend.instance_id.clone())
            },
            LoadBalanceStrategy::WeightedRoundRobin => {
                // 简化实现,后续可以优化
                let total_weight: u32 = healthy_backends.iter().map(|b| b.weight).sum();
                if total_weight == 0 {
                    return None;
                }
                
                let mut target = (self.round_robin_index.load(std::sync::atomic::Ordering::Relaxed) as u32) % total_weight;
                self.round_robin_index.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                
                for backend in healthy_backends {
                    if target < backend.weight {
                        return Some(backend.instance_id.clone());
                    }
                    target -= backend.weight;
                }
                None
            },
            LoadBalanceStrategy::ConsistentHash => {
                // 简化实现,使用第一个健康实例
                healthy_backends.first().map(|backend| backend.instance_id.clone())
            },
        }
    }
}

/// 为Agent实现ProtocolAgent trait
impl ProtocolAgent for Agent {
    /// 探测协议类型(被动模式 - 服务器角色)
    fn detect(&self, data: &[u8]) -> Result<DetectionResult> {
        // 更新请求计数
        if let Ok(mut state) = self.state.write() {
            state.total_requests += 1;
            state.last_activity = Instant::now();
        }
        
        // 根据角色选择探测策略
        match self.config.role {
            Role::Server => {
                // 服务器模式:被动探测传入数据
                self.detector.detect(data)
            },
            Role::Client => {
                // 客户端模式:通常不需要被动探测,但可以用于验证
                self.detector.detect(data)
            },
        }
    }
    
    /// 主动探测协议能力(客户端专用)
    fn probe_capabilities(&self, transport: &mut dyn Transport) -> Result<Vec<ProtocolType>> {
        match self.config.role {
            Role::Client => {
                // 更新请求计数
                if let Ok(mut state) = self.state.write() {
                    state.total_requests += 1;
                    state.last_activity = Instant::now();
                }
                
                self.active_probe(transport)
            },
            Role::Server => {
                Err(DetectorError::unsupported_protocol(
                    "Server role does not support active probing"
                ))
            },
        }
    }
    
    /// 主动探测实现(客户端专用)
    fn active_probe(&self, transport: &mut dyn Transport) -> Result<Vec<ProtocolType>> {
        let mut supported_protocols = Vec::new();
        
        // 只探测配置中启用的协议
        let probe_order = [
            ProtocolType::HTTP3,
            ProtocolType::HTTP2, 
            ProtocolType::HTTP1_1,
            ProtocolType::TLS,
            ProtocolType::QUIC,
        ];
        
        for protocol in probe_order {
            if self.config.enabled_protocols.contains(&protocol) {
                match self.send_protocol_probe(transport, protocol) {
                    Ok(true) => {
                        supported_protocols.push(protocol);
                        // 找到最高优先级协议后可以选择停止或继续探测
                        if matches!(protocol, ProtocolType::HTTP3 | ProtocolType::QUIC) {
                            break;
                        }
                    },
                    Ok(false) => continue,
                    Err(_) => continue, // 探测失败,继续下一个协议
                }
            }
        }
        
        if supported_protocols.is_empty() {
            // 保底协议
            supported_protocols.push(ProtocolType::HTTP1_1);
        }
        
        Ok(supported_protocols)
    }
    
    /// 发送特定协议的探测请求
    fn send_protocol_probe(&self, transport: &mut dyn Transport, protocol: ProtocolType) -> Result<bool> {
        match protocol {
            ProtocolType::HTTP2 => {
                // HTTP/2 连接前言探测
                let h2_preface = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
                transport.write(h2_preface)?;
                
                // 读取响应
                let mut response = vec![0u8; 24];
                match transport.read(&mut response) {
                    Ok(n) if n > 0 => {
                        // 检查是否收到HTTP/2 SETTINGS帧
                        Ok(response.len() >= 9 && response[3] == 0x04) // SETTINGS帧类型
                    },
                    _ => Ok(false),
                }
            },
            ProtocolType::HTTP3 => {
                // HTTP/3 over QUIC探测
                // 这里需要QUIC握手逻辑,简化实现
                Ok(false) // 暂时返回false,需要完整的QUIC实现
            },
            ProtocolType::HTTP1_1 => {
                // HTTP/1.1 OPTIONS请求探测
                let options_request = b"OPTIONS * HTTP/1.1\r\nHost: probe\r\n\r\n";
                transport.write(options_request)?;
                
                let mut response = vec![0u8; 256];
                match transport.read(&mut response) {
                    Ok(n) if n > 0 => {
                        let response_str = String::from_utf8_lossy(&response[..n]);
                        Ok(response_str.starts_with("HTTP/1.1") || response_str.starts_with("HTTP/1.0"))
                    },
                    _ => Ok(false),
                }
            },
            ProtocolType::TLS => {
                // TLS ClientHello探测
                let client_hello = self.create_tls_client_hello();
                transport.write(&client_hello)?;
                
                let mut response = vec![0u8; 1024];
                match transport.read(&mut response) {
                    Ok(n) if n >= 5 => {
                        // 检查TLS ServerHello响应
                        Ok(response[0] == 0x16 && response[1] == 0x03) // TLS握手记录
                    },
                    _ => Ok(false),
                }
            },
            _ => Ok(false), // 其他协议暂不支持主动探测
        }
    }
    
    /// 创建TLS ClientHello消息
    fn create_tls_client_hello(&self) -> Vec<u8> {
        // 简化的TLS 1.2 ClientHello
        vec![
            0x16, 0x03, 0x01, 0x00, 0x2f, // TLS记录头
            0x01, 0x00, 0x00, 0x2b,       // 握手消息头
            0x03, 0x03,                   // TLS版本1.2
            // 32字节随机数(简化)
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00,                         // 会话ID长度
            0x00, 0x02,                   // 密码套件长度
            0x00, 0x35,                   // AES128-SHA
            0x01, 0x00,                   // 压缩方法
        ]
    }
    
    /// 智能降级策略(客户端模式)
    fn auto_fallback(&self, transport: &mut dyn Transport, preferred: ProtocolType) -> Result<ProtocolType> {
        match self.config.role {
            Role::Client => {
                // 尝试首选协议
                if self.send_protocol_probe(transport, preferred)? {
                    return Ok(preferred);
                }
                
                // 自动降级
                let fallback_chain = match preferred {
                    ProtocolType::HTTP3 => vec![ProtocolType::HTTP2, ProtocolType::HTTP1_1],
                    ProtocolType::HTTP2 => vec![ProtocolType::HTTP1_1],
                    ProtocolType::QUIC => vec![ProtocolType::TLS, ProtocolType::TCP],
                    _ => vec![ProtocolType::HTTP1_1],
                };
                
                for fallback in fallback_chain {
                    if self.config.enabled_protocols.contains(&fallback) && 
                       self.send_protocol_probe(transport, fallback)? {
                        return Ok(fallback);
                    }
                }
                
                // 最终保底
                Ok(ProtocolType::HTTP1_1)
            },
            Role::Server => {
                Err(DetectorError::unsupported_protocol(
                    "Server role does not support auto fallback"
                ))
            },
        }
    }
    
    /// 协议升级(主动/被动模式)
    fn upgrade(
        &self,
        transport: Box<dyn Transport>,
        role: Role,
    ) -> Result<Box<dyn Transport>> {
        match &self.upgrader {
            Some(upgrader) => {
                let result = match role {
                    Role::Server => {
                        // 服务器模式:响应客户端的升级请求
                        let current_protocol = ProtocolType::HTTP1_1; // 当前协议
                         let target_protocol = ProtocolType::HTTP2; // 目标协议
                        let data = b""; // 升级数据
                        upgrader.upgrade(current_protocol, target_protocol, data)
                    },
                    Role::Client => {
                        // 客户端模式:发起协议升级请求
                        let current_protocol = ProtocolType::HTTP1_1; // 当前协议
                        let target_protocol = ProtocolType::HTTP2; // 目标协议
                        let data = b""; // 升级数据
                        upgrader.upgrade(current_protocol, target_protocol, data)
                    },
                };
                
                // 更新升级统计
                if let Ok(mut state) = self.state.write() {
                    match result {
                        Ok(_) => state.successful_upgrades += 1,
                        Err(_) => state.failed_upgrades += 1,
                    }
                    state.last_activity = Instant::now();
                }
                
                result.map(|_| transport) // 简化实现,实际应返回升级后的transport
            },
            None => Err(DetectorError::unsupported_protocol("Protocol upgrade not supported")),
        }
    }
    
    /// 检查是否支持指定协议
    fn supports_protocol(&self, protocol: ProtocolType) -> bool {
        self.config.enabled_protocols.contains(&protocol)
    }
    
    /// 获取代理角色
    fn role(&self) -> Role {
        self.config.role
    }
    
    /// 获取实例ID
    fn instance_id(&self) -> &str {
        &self.config.instance_id
    }
    
    /// 获取代理名称
    fn name(&self) -> &str {
        match self.config.role {
            Role::Server => "PSI Server Agent",
            Role::Client => "PSI Client Agent",
        }
    }
}

impl DetectionConfig {
    /// 创建新的探测配置
    pub fn new() -> Self {
        Self::default()
    }
    
    /// 设置最小置信度
    pub fn with_min_confidence(mut self, confidence: f32) -> Self {
        self.min_confidence = confidence.clamp(0.0, 1.0);
        self
    }
    
    /// 设置超时时间
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }
    
    /// 启用启发式探测
    pub fn enable_heuristic(mut self) -> Self {
        self.enable_heuristic = true;
        self
    }
    
    /// 禁用启发式探测
    pub fn disable_heuristic(mut self) -> Self {
        self.enable_heuristic = false;
        self
    }
    
    /// 启用主动探测
    pub fn enable_active_probing(mut self) -> Self {
        self.enable_active_probing = true;
        self
    }
    
    /// 禁用主动探测
    pub fn disable_active_probing(mut self) -> Self {
        self.enable_active_probing = false;
        self
    }
    
    /// 设置最大探测数据大小
    pub fn with_max_probe_size(mut self, size: usize) -> Self {
        self.max_probe_size = size;
        self
    }
    
    /// 设置最小探测数据大小
    pub fn with_min_probe_size(mut self, size: usize) -> Self {
        self.min_probe_size = size;
        self
    }
    
    /// 启用SIMD加速
    pub fn enable_simd(mut self) -> Self {
        self.enable_simd = true;
        self
    }
    
    /// 禁用SIMD加速
    pub fn disable_simd(mut self) -> Self {
        self.enable_simd = false;
        self
    }
}

/// 探测统计信息
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DetectionStats {
    /// 总探测次数
    pub total_detections: u64,
    /// 成功探测次数
    pub successful_detections: u64,
    /// 失败探测次数
    pub failed_detections: u64,
    /// 平均探测时间
    pub avg_detection_time: Duration,
    /// 各协议探测次数
    pub protocol_counts: std::collections::HashMap<ProtocolType, u64>,
}

impl DetectionStats {
    /// 创建新的统计信息
    pub fn new() -> Self {
        Self::default()
    }
    
    /// 记录成功探测
    pub fn record_success(&mut self, protocol: ProtocolType, duration: Duration) {
        self.total_detections += 1;
        self.successful_detections += 1;
        self.update_avg_time(duration);
        *self.protocol_counts.entry(protocol).or_insert(0) += 1;
    }
    
    /// 记录失败探测
    pub fn record_failure(&mut self, duration: Duration) {
        self.total_detections += 1;
        self.failed_detections += 1;
        self.update_avg_time(duration);
    }
    
    /// 获取成功率
    pub fn success_rate(&self) -> f64 {
        if self.total_detections == 0 {
            0.0
        } else {
            self.successful_detections as f64 / self.total_detections as f64
        }
    }
    
    /// 获取最常见的协议
    pub fn most_common_protocol(&self) -> Option<ProtocolType> {
        self.protocol_counts
            .iter()
            .max_by_key(|(_, count)| *count)
            .map(|(protocol, _)| *protocol)
    }
    
    fn update_avg_time(&mut self, new_duration: Duration) {
        if self.total_detections == 1 {
            self.avg_detection_time = new_duration;
        } else {
            let total_nanos = self.avg_detection_time.as_nanos() * (self.total_detections - 1) as u128
                + new_duration.as_nanos();
            self.avg_detection_time = Duration::from_nanos((total_nanos / self.total_detections as u128) as u64);
        }
    }
}