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
//! MITM Interceptor - Main interception logic
//!
//! This module handles the core MITM interception flow:
//! 1. Accept client TLS connection with fake certificate
//! 2. Establish upstream TLS connection with real verification
//! 3. Proxy data bidirectionally with optional inspection
use crate::connection_pool::ConnectionPool;
use crate::mitm::{
bypass::{BypassManager, BypassReason},
certificate_authority::{CertificateAuthority, HostIdentifier, MitmError},
hsts::HstsManager,
http_parser::{parse_http1_request, parse_http1_response, ParseError},
log_storage::LogStorage,
logging::{LoggingPolicy, PiiRedactor, RequestMetadata},
pinning::{PinningDetector, PinningPatterns},
tls_config::{ClientTlsConfig, SniUtils, UpstreamTlsConfig},
};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio_rustls::{TlsAcceptor, TlsConnector};
use tracing::{debug, error, info, warn};
/// Interception errors
#[derive(Debug, Error)]
pub enum InterceptionError {
#[error("MITM error: {0}")]
MitmError(#[from] MitmError),
#[error("TLS handshake failed: {0}")]
TlsHandshakeFailed(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Bypass required: {0}")]
BypassRequired(BypassReason),
#[error("Logging failed: {0}")]
LoggingFailed(String),
}
/// Result of interception attempt
#[derive(Debug)]
pub enum InterceptionResult {
/// Successfully intercepted
Intercepted { metadata: RequestMetadata },
/// Bypassed (tunneled without inspection)
Bypassed {
reason: BypassReason,
metadata: RequestMetadata,
},
/// Failed
Failed { error: InterceptionError },
}
/// MITM Interceptor
pub struct MitmInterceptor {
/// Certificate authority for generating fake certs
ca: Arc<CertificateAuthority>,
/// Bypass manager for smart bypass decisions
bypass_manager: Arc<BypassManager>,
/// Logging policy
logging_policy: Arc<LoggingPolicy>,
/// Upstream TLS config
upstream_tls: Arc<UpstreamTlsConfig>,
/// HSTS manager
hsts_manager: Arc<HstsManager>,
/// Pinning detector
pinning_detector: Arc<PinningDetector>,
/// Log storage (optional)
log_storage: Option<Arc<LogStorage>>,
/// Connection pool for reusing upstream TLS connections
connection_pool: Arc<ConnectionPool>,
}
impl MitmInterceptor {
/// Create new interceptor
pub fn new(
ca: Arc<CertificateAuthority>,
bypass_manager: Arc<BypassManager>,
logging_policy: Arc<LoggingPolicy>,
) -> Result<Self, InterceptionError> {
let upstream_tls = Arc::new(
UpstreamTlsConfig::new()
.map_err(|e| InterceptionError::TlsHandshakeFailed(e.to_string()))?,
);
let hsts_manager = Arc::new(HstsManager::new());
let pinning_detector =
Arc::new(PinningDetector::new().with_bypass_manager(Arc::clone(&bypass_manager)));
// Create connection pool with default config
let connection_pool = Arc::new(ConnectionPool::new());
// Start background cleanup task
crate::connection_pool::start_cleanup_task(Arc::clone(&connection_pool));
Ok(Self {
ca,
bypass_manager,
logging_policy,
upstream_tls,
hsts_manager,
pinning_detector,
log_storage: None,
connection_pool,
})
}
/// Create new interceptor with custom HSTS manager
pub fn with_hsts(
ca: Arc<CertificateAuthority>,
bypass_manager: Arc<BypassManager>,
logging_policy: Arc<LoggingPolicy>,
hsts_manager: Arc<HstsManager>,
) -> Result<Self, InterceptionError> {
let upstream_tls = Arc::new(
UpstreamTlsConfig::new()
.map_err(|e| InterceptionError::TlsHandshakeFailed(e.to_string()))?,
);
let pinning_detector =
Arc::new(PinningDetector::new().with_bypass_manager(Arc::clone(&bypass_manager)));
// Create connection pool with default config
let connection_pool = Arc::new(ConnectionPool::new());
// Start background cleanup task
crate::connection_pool::start_cleanup_task(Arc::clone(&connection_pool));
Ok(Self {
ca,
bypass_manager,
logging_policy,
upstream_tls,
hsts_manager,
pinning_detector,
log_storage: None,
connection_pool,
})
}
/// Create new interceptor with custom pinning detector
pub fn with_pinning(
ca: Arc<CertificateAuthority>,
bypass_manager: Arc<BypassManager>,
logging_policy: Arc<LoggingPolicy>,
hsts_manager: Arc<HstsManager>,
pinning_detector: Arc<PinningDetector>,
) -> Result<Self, InterceptionError> {
let upstream_tls = Arc::new(
UpstreamTlsConfig::new()
.map_err(|e| InterceptionError::TlsHandshakeFailed(e.to_string()))?,
);
// Create connection pool with default config
let connection_pool = Arc::new(ConnectionPool::new());
// Start background cleanup task
crate::connection_pool::start_cleanup_task(Arc::clone(&connection_pool));
Ok(Self {
ca,
bypass_manager,
logging_policy,
upstream_tls,
hsts_manager,
pinning_detector,
log_storage: None,
connection_pool,
})
}
/// Enable SQLite logging
///
/// This method enables request/response logging to SQLite database.
/// Logging is performed asynchronously and does not block the proxy.
pub async fn with_logging(mut self, db_path: &str) -> Result<Self, InterceptionError> {
let storage = LogStorage::new(db_path)
.await
.map_err(|e| InterceptionError::LoggingFailed(e.to_string()))?;
self.log_storage = Some(Arc::new(storage));
Ok(self)
}
/// Attempt to intercept connection
pub async fn intercept(
&self,
client_stream: TcpStream,
target_host: String,
target_port: u16,
) -> InterceptionResult {
debug!(
target_host = %target_host,
target_port = target_port,
"Attempting MITM interception"
);
// Check HSTS protection
if self.hsts_manager.is_hsts_domain(&target_host).await {
info!(
target_host = %target_host,
"Domain is HSTS-protected, bypassing MITM"
);
match self
.tunnel_connection(client_stream, &target_host, target_port)
.await
{
Ok(metadata) => {
return InterceptionResult::Bypassed {
reason: BypassReason::HstsPolicy,
metadata,
};
}
Err(e) => {
return InterceptionResult::Failed { error: e };
}
}
}
// Check if should bypass
if let Some(reason) = self.bypass_manager.should_bypass(&target_host).await {
info!(
target_host = %target_host,
reason = ?reason,
"Bypassing MITM"
);
// Implement transparent tunneling (no inspection)
match self
.tunnel_connection(client_stream, &target_host, target_port)
.await
{
Ok(metadata) => {
return InterceptionResult::Bypassed { reason, metadata };
}
Err(e) => {
return InterceptionResult::Failed { error: e };
}
}
}
// Parse host identifier
let host_id = HostIdentifier::from_hostname(&target_host);
// Check for localhost bypass
if matches!(host_id, HostIdentifier::Localhost) {
warn!("Localhost bypass triggered");
match self
.tunnel_connection(client_stream, &target_host, target_port)
.await
{
Ok(metadata) => {
return InterceptionResult::Bypassed {
reason: BypassReason::Localhost,
metadata,
};
}
Err(e) => {
return InterceptionResult::Failed { error: e };
}
}
}
// Generate fake certificate
let cert = match self.ca.get_or_generate(host_id).await {
Ok(cert) => cert,
Err(e) => {
return InterceptionResult::Failed {
error: InterceptionError::MitmError(e),
}
}
};
// Perform MITM interception
match self
.intercept_with_inspection(client_stream, &target_host, target_port, cert)
.await
{
Ok(metadata) => InterceptionResult::Intercepted { metadata },
Err(e) => InterceptionResult::Failed { error: e },
}
}
/// Intercept connection with full TLS inspection
async fn intercept_with_inspection(
&self,
client_stream: TcpStream,
target_host: &str,
target_port: u16,
fake_cert: Arc<rcgen::Certificate>,
) -> Result<RequestMetadata, InterceptionError> {
debug!(
target_host = %target_host,
target_port = target_port,
"Starting TLS inspection"
);
// 1. Build client-facing TLS config with fake certificate
let client_tls_config = self.build_client_tls_config(&fake_cert)?;
let tls_acceptor = TlsAcceptor::from(client_tls_config.server_config());
// 2. Accept client TLS connection
let mut client_tls = match tls_acceptor.accept(client_stream).await {
Ok(stream) => stream,
Err(e) => {
error!(
target_host = %target_host,
error = %e,
"Client TLS handshake failed"
);
return Err(InterceptionError::TlsHandshakeFailed(e.to_string()));
}
};
info!(
target_host = %target_host,
"Client TLS handshake successful"
);
// Detect ALPN protocol negotiation
let alpn_protocol = client_tls.get_ref().1.alpn_protocol();
debug!(
target_host = %target_host,
alpn = ?alpn_protocol,
"ALPN protocol detected"
);
// Clone ALPN protocol for later use (after client_tls is moved/borrowed)
let alpn_protocol_owned = alpn_protocol.map(|p| p.to_vec());
// 3. Establish upstream TLS connection (try pool first)
let upstream_addr = format!("{}:{}", target_host, target_port);
let pool_key = upstream_addr.clone();
// Try to get connection from pool first
let mut upstream_tls = match self.connection_pool.get(&pool_key).await {
Some(stream) => {
debug!(
upstream_addr = %upstream_addr,
"Reusing pooled TLS connection (cache hit)"
);
stream
}
None => {
debug!(
upstream_addr = %upstream_addr,
"No pooled connection available, creating new connection (cache miss)"
);
// Record cache miss
self.connection_pool.record_miss().await;
// Create new TCP connection
let upstream_stream = match TcpStream::connect(&upstream_addr).await {
Ok(stream) => stream,
Err(e) => {
error!(
upstream_addr = %upstream_addr,
error = %e,
"Failed to connect to upstream"
);
return Err(InterceptionError::IoError(e));
}
};
// Parse server name for SNI
let server_name = match SniUtils::parse_server_name(target_host) {
Ok(name) => name,
Err(e) => {
error!(
target_host = %target_host,
error = %e,
"Invalid server name for SNI"
);
return Err(InterceptionError::TlsHandshakeFailed(e.to_string()));
}
};
// Establish TLS handshake
let tls_connector = TlsConnector::from(self.upstream_tls.client_config());
match tls_connector.connect(server_name, upstream_stream).await {
Ok(stream) => stream,
Err(e) => {
let error_str = e.to_string();
error!(
target_host = %target_host,
error = %error_str,
"Upstream TLS handshake failed"
);
// Check if this looks like certificate pinning
if PinningPatterns::is_pinning_error(&error_str) {
let detection = self
.pinning_detector
.record_failure(target_host, &error_str)
.await;
if detection.detected {
warn!(
target_host = %target_host,
count = detection.failure_count,
auto_bypassed = detection.auto_bypassed,
"Certificate pinning detected"
);
}
}
return Err(InterceptionError::TlsHandshakeFailed(error_str));
}
}
}
};
info!(
target_host = %target_host,
"Upstream TLS handshake successful"
);
// 4. Proxy data bidirectionally with inspection based on ALPN protocol
let bytes_transferred = match alpn_protocol_owned.as_deref() {
Some(b"h2") => {
debug!(target_host = %target_host, "Using production HTTP/2 MITM with flow control");
// Use production-grade h2 crate handler with proper flow control
// Note: HTTP/2 handler takes ownership of both streams, cannot return to pool
match crate::mitm::handle_http2_mitm(
client_tls,
upstream_tls,
target_host.to_string(),
target_port,
Arc::clone(&self.logging_policy),
self.log_storage.clone(),
crate::mitm::Http2Config::default(),
)
.await
{
Ok(()) => {
debug!(target_host = %target_host, "HTTP/2 connection consumed by handler");
0 // HTTP/2 handler manages its own byte counting
}
Err(e) => {
error!(
target_host = %target_host,
error = %e,
"HTTP/2 MITM failed"
);
return Err(InterceptionError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("HTTP/2 MITM error: {}", e),
)));
}
}
}
Some(b"http/1.1") | None => {
debug!(target_host = %target_host, "Using HTTP/1.1 inspection");
let bytes = match self
.proxy_with_inspection(
&mut client_tls,
&mut upstream_tls,
target_host,
target_port,
)
.await
{
Ok(bytes) => bytes,
Err(e) => {
error!(
target_host = %target_host,
error = %e,
"HTTP/1.1 proxying failed"
);
return Err(e);
}
};
// Return connection to pool (we still own it for HTTP/1.1)
debug!(target_host = %target_host, "Returning HTTP/1.1 connection to pool");
self.connection_pool
.put(pool_key.clone(), upstream_tls)
.await;
bytes
}
Some(proto) => {
warn!(
target_host = %target_host,
protocol = ?proto,
"Unknown ALPN protocol, using bidirectional copy without inspection"
);
// For unknown protocols, just forward data without inspection
let bytes =
match tokio::io::copy_bidirectional(&mut client_tls, &mut upstream_tls).await {
Ok((client_bytes, upstream_bytes)) => client_bytes + upstream_bytes,
Err(e) => {
debug!(error = %e, "Bidirectional copy failed");
0
}
};
// Return connection to pool (we still own it for unknown protocols)
debug!(target_host = %target_host, protocol = ?proto, "Returning connection to pool");
self.connection_pool.put(pool_key, upstream_tls).await;
bytes
}
};
info!(
target_host = %target_host,
bytes_transferred = bytes_transferred,
"MITM interception completed"
);
// 5. Build metadata
let metadata = RequestMetadata {
timestamp: chrono::Utc::now().timestamp(),
method: "CONNECT".to_string(),
host: target_host.to_string(),
port: target_port,
path: "/".to_string(), // TLS-level inspection, no HTTP path yet
http_version: "HTTPS".to_string(),
status_code: None,
request_size: 0, // TODO: Track in future
response_size: 0,
duration_ms: 0, // TODO: Track timing
tls_version: Some("TLS 1.2+".to_string()),
mitm_applied: true,
bypass_reason: None,
};
Ok(metadata)
}
/// Tunnel connection without inspection (bypass mode)
async fn tunnel_connection(
&self,
mut client_stream: TcpStream,
target_host: &str,
target_port: u16,
) -> Result<RequestMetadata, InterceptionError> {
debug!(
target_host = %target_host,
target_port = target_port,
"Tunneling without inspection"
);
// Establish upstream connection
let upstream_addr = format!("{}:{}", target_host, target_port);
let mut upstream_stream = TcpStream::connect(&upstream_addr).await?;
// Proxy data bidirectionally (no inspection)
let bytes_transferred =
tokio::io::copy_bidirectional(&mut client_stream, &mut upstream_stream).await?;
info!(
target_host = %target_host,
bytes_up = bytes_transferred.0,
bytes_down = bytes_transferred.1,
"Tunnel completed"
);
let metadata = RequestMetadata {
timestamp: chrono::Utc::now().timestamp(),
method: "CONNECT".to_string(),
host: target_host.to_string(),
port: target_port,
path: "/".to_string(), // Tunneled, no HTTP inspection
http_version: "HTTPS".to_string(),
status_code: None,
request_size: bytes_transferred.0 as usize,
response_size: bytes_transferred.1 as usize,
duration_ms: 0, // TODO: Track timing
tls_version: None, // Tunneled, not inspected
mitm_applied: false,
bypass_reason: None, // Set by caller
};
Ok(metadata)
}
/// Proxy data with inspection capabilities
async fn proxy_with_inspection<C, U>(
&self,
client: &mut C,
upstream: &mut U,
target_host: &str,
target_port: u16,
) -> Result<u64, InterceptionError>
where
C: AsyncRead + AsyncWrite + Unpin,
U: AsyncRead + AsyncWrite + Unpin,
{
let start_time = std::time::Instant::now();
// Read initial HTTP request (buffer up to 8KB for headers)
let mut request_buffer = vec![0u8; 8192];
let mut request_bytes = match client.read(&mut request_buffer).await {
Ok(0) => {
debug!(target_host = %target_host, "Client closed connection");
return Ok(0);
}
Ok(n) => n,
Err(e) => {
warn!(target_host = %target_host, error = %e, "Failed to read request");
return Err(InterceptionError::IoError(e));
}
};
// Try to parse HTTP request (may fail for incomplete/binary data)
let http_request = match parse_http1_request(&request_buffer[..request_bytes]) {
Ok(req) => Some(req),
Err(ParseError::Incomplete) if request_bytes < 8192 => {
// Buffer not full - truly incomplete, not large headers
debug!(target_host = %target_host, "Incomplete HTTP request (partial read), proxying transparently");
None
}
Err(ParseError::Incomplete) => {
// Buffer is full but headers incomplete - try reading more (up to 32KB total)
debug!(target_host = %target_host, "Headers may be larger than 8KB, attempting extended read");
// Expand buffer to 32KB and try reading more
request_buffer.resize(32768, 0);
match client.read(&mut request_buffer[request_bytes..]).await {
Ok(0) => {
debug!(target_host = %target_host, "No additional data available, proxying transparently");
None
}
Ok(n) => {
request_bytes += n;
// Retry parsing with extended buffer
match parse_http1_request(&request_buffer[..request_bytes]) {
Ok(req) => {
debug!(target_host = %target_host, bytes = request_bytes, "Successfully parsed large headers");
Some(req)
}
Err(e) => {
debug!(target_host = %target_host, error = %e, "Still cannot parse after extended read, proxying transparently");
None
}
}
}
Err(e) => {
warn!(target_host = %target_host, error = %e, "Failed to read additional header data");
None
}
}
}
Err(e) => {
debug!(target_host = %target_host, error = %e, "HTTP parse error, proxying transparently");
None
}
};
// Forward request to upstream
upstream.write_all(&request_buffer[..request_bytes]).await?;
// Read initial HTTP response (buffer up to 8KB for headers)
let mut response_buffer = vec![0u8; 8192];
let mut response_bytes = match upstream.read(&mut response_buffer).await {
Ok(0) => {
debug!(target_host = %target_host, "Upstream closed connection");
return Ok(request_bytes as u64);
}
Ok(n) => n,
Err(e) => {
warn!(target_host = %target_host, error = %e, "Failed to read response");
return Err(InterceptionError::IoError(e));
}
};
// Try to parse HTTP response
let http_response = match parse_http1_response(&response_buffer[..response_bytes]) {
Ok(resp) => Some(resp),
Err(ParseError::Incomplete) if response_bytes < 8192 => {
// Buffer not full - truly incomplete, not large headers
debug!(target_host = %target_host, "Incomplete HTTP response (partial read), proxying transparently");
None
}
Err(ParseError::Incomplete) => {
// Buffer is full but headers incomplete - try reading more (up to 32KB total)
debug!(target_host = %target_host, "Response headers may be larger than 8KB, attempting extended read");
// Expand buffer to 32KB and try reading more
response_buffer.resize(32768, 0);
match upstream.read(&mut response_buffer[response_bytes..]).await {
Ok(0) => {
debug!(target_host = %target_host, "No additional response data available, proxying transparently");
None
}
Ok(n) => {
response_bytes += n;
// Retry parsing with extended buffer
match parse_http1_response(&response_buffer[..response_bytes]) {
Ok(resp) => {
debug!(target_host = %target_host, bytes = response_bytes, "Successfully parsed large response headers");
Some(resp)
}
Err(e) => {
debug!(target_host = %target_host, error = %e, "Still cannot parse response after extended read, proxying transparently");
None
}
}
}
Err(e) => {
warn!(target_host = %target_host, error = %e, "Failed to read additional response header data");
None
}
}
}
Err(e) => {
debug!(target_host = %target_host, error = %e, "HTTP response parse error, proxying transparently");
None
}
};
// Forward response to client
client.write_all(&response_buffer[..response_bytes]).await?;
// Continue proxying remaining data bidirectionally
let (bytes_up_remaining, bytes_down_remaining) =
tokio::io::copy_bidirectional(client, upstream).await?;
let total_bytes_up = request_bytes as u64 + bytes_up_remaining;
let total_bytes_down = response_bytes as u64 + bytes_down_remaining;
let duration_ms = start_time.elapsed().as_millis() as u64;
debug!(
target_host = %target_host,
bytes_up = total_bytes_up,
bytes_down = total_bytes_down,
duration_ms = duration_ms,
"Data proxied with inspection"
);
// Log request if policy allows and we successfully parsed HTTP
if let (Some(req), Some(resp)) = (&http_request, &http_response) {
if PiiRedactor::should_sample(self.logging_policy.sampling_rate) {
let mut metadata = RequestMetadata {
timestamp: chrono::Utc::now().timestamp(),
method: req.method.clone(),
host: target_host.to_string(),
port: target_port,
path: req.path.clone(),
http_version: req.version.clone(),
status_code: Some(resp.status_code),
request_size: total_bytes_up as usize,
response_size: total_bytes_down as usize,
duration_ms,
tls_version: Some("TLS 1.2+".to_string()),
mitm_applied: true,
bypass_reason: None,
};
// Apply PII redaction if enabled
if self.logging_policy.enable_pii_redaction {
metadata.path = PiiRedactor::redact(&metadata.path);
}
// Log asynchronously (non-blocking)
if let Some(storage) = &self.log_storage {
let storage_clone = Arc::clone(storage);
tokio::spawn(async move {
if let Err(e) = storage_clone.log_request(&metadata).await {
warn!(error = %e, "Failed to log request to database");
}
});
}
}
}
Ok(total_bytes_up + total_bytes_down)
}
/// Proxy data bidirectionally with HTTP/2 inspection and logging
async fn proxy_with_http2_inspection<C, U>(
&self,
client: &mut C,
upstream: &mut U,
target_host: &str,
target_port: u16,
) -> Result<u64, InterceptionError>
where
C: AsyncRead + AsyncWrite + Unpin,
U: AsyncRead + AsyncWrite + Unpin,
{
use crate::mitm::http2_parser::{
extract_http2_request, extract_http2_response, has_end_stream, is_client_stream,
parse_frame_header, FrameType, HpackDecoder, Http2Frame,
};
let start_time = Instant::now();
let mut total_bytes = 0u64;
// Initialize HPACK decoders (one for client, one for upstream)
let mut client_decoder = HpackDecoder::new();
let mut upstream_decoder = HpackDecoder::new();
// Track frames per stream
let mut stream_buffers: HashMap<u32, Vec<Http2Frame>> = HashMap::new();
// Track pending requests (waiting for response) with start time
// Key: stream_id, Value: (RequestMetadata, request_start_time)
let mut pending_requests: HashMap<u32, (RequestMetadata, Instant)> = HashMap::new();
// Buffers for reading frame headers (9 bytes)
let mut client_frame_header = vec![0u8; 9];
let mut upstream_frame_header = vec![0u8; 9];
loop {
tokio::select! {
// Read from client, forward to upstream
result = client.read_exact(&mut client_frame_header) => {
match result {
Ok(_) => {
// Parse frame header
let (frame_type, flags, stream_id, length) = match parse_frame_header(&client_frame_header) {
Ok(header) => header,
Err(e) => {
warn!(error = ?e, "Failed to parse client HTTP/2 frame header");
break;
}
};
// Read frame payload
let mut payload = vec![0u8; length];
if let Err(e) = client.read_exact(&mut payload).await {
debug!(error = %e, "Failed to read client HTTP/2 frame payload");
break;
}
total_bytes += (9 + length) as u64;
// Buffer HEADERS and DATA frames for request extraction
if matches!(frame_type, FrameType::Headers | FrameType::Data | FrameType::Continuation) && is_client_stream(stream_id) {
let frame = Http2Frame {
frame_type,
flags,
stream_id,
payload_length: length,
payload: payload.clone(),
};
stream_buffers.entry(stream_id).or_insert_with(Vec::new).push(frame.clone());
// If END_STREAM flag set on client stream, extract request
if has_end_stream(&frame) {
if let Some(frames) = stream_buffers.get(&stream_id) {
match extract_http2_request(frames, &mut client_decoder) {
Ok(request) => {
debug!(
method = %request.method,
path = %request.path,
stream_id = stream_id,
"HTTP/2 request extracted"
);
// Create metadata and store for correlation with response
let metadata = RequestMetadata {
timestamp: chrono::Utc::now().timestamp(),
host: target_host.to_string(),
port: target_port,
method: request.method.clone(),
path: request.path.clone(),
http_version: "HTTP/2".to_string(),
status_code: None, // Will be filled when response arrives
request_size: request.content_length.unwrap_or(0),
response_size: 0, // Will be filled when response arrives
duration_ms: 0, // Will be calculated when response arrives
tls_version: Some("TLS 1.3".to_string()),
mitm_applied: true,
bypass_reason: None,
};
// Store request metadata and start time for correlation
pending_requests.insert(stream_id, (metadata, Instant::now()));
// Clear request frames from buffer (keep space for response)
stream_buffers.get_mut(&stream_id).unwrap().clear();
}
Err(e) => {
debug!(error = ?e, stream_id = stream_id, "Failed to extract HTTP/2 request");
// Clean up on error
stream_buffers.remove(&stream_id);
}
}
}
}
}
// Forward frame to upstream
if let Err(e) = upstream.write_all(&client_frame_header).await {
debug!(error = %e, "Failed to forward frame header to upstream");
break;
}
if let Err(e) = upstream.write_all(&payload).await {
debug!(error = %e, "Failed to forward frame payload to upstream");
break;
}
}
Err(_) => {
debug!("Client closed HTTP/2 connection");
break;
}
}
}
// Read from upstream, forward to client
result = upstream.read_exact(&mut upstream_frame_header) => {
match result {
Ok(_) => {
// Parse frame header
let (frame_type, flags, stream_id, length) = match parse_frame_header(&upstream_frame_header) {
Ok(header) => header,
Err(e) => {
warn!(error = ?e, "Failed to parse upstream HTTP/2 frame header");
break;
}
};
// Read frame payload
let mut payload = vec![0u8; length];
if let Err(e) = upstream.read_exact(&mut payload).await {
debug!(error = %e, "Failed to read upstream HTTP/2 frame payload");
break;
}
total_bytes += (9 + length) as u64;
// Buffer HEADERS and DATA frames for response extraction
// Responses come on the SAME client-initiated (odd) stream ID
if matches!(frame_type, FrameType::Headers | FrameType::Data | FrameType::Continuation) && is_client_stream(stream_id) {
let frame = Http2Frame {
frame_type,
flags,
stream_id,
payload_length: length,
payload: payload.clone(),
};
stream_buffers.entry(stream_id).or_insert_with(Vec::new).push(frame.clone());
// If END_STREAM flag set, extract response and correlate with request
if has_end_stream(&frame) {
if let Some(frames) = stream_buffers.get(&stream_id) {
match extract_http2_response(frames, &mut upstream_decoder) {
Ok(response) => {
debug!(
status = response.status,
stream_id = stream_id,
"HTTP/2 response extracted"
);
// Correlate with pending request
if let Some((mut metadata, request_start)) = pending_requests.remove(&stream_id) {
// Fill in response details
metadata.status_code = Some(response.status);
metadata.response_size = response.content_length.unwrap_or(0);
metadata.duration_ms = request_start.elapsed().as_millis() as u64;
// Apply PII redaction if enabled
if self.logging_policy.enable_pii_redaction {
metadata.path = PiiRedactor::redact(&metadata.path);
}
// Log complete request/response
if let Some(storage) = &self.log_storage {
let storage_clone = Arc::clone(storage);
tokio::spawn(async move {
if let Err(e) = storage_clone.log_request(&metadata).await {
warn!(error = %e, "Failed to log HTTP/2 request/response");
}
});
}
} else {
debug!(stream_id = stream_id, "Received response without matching request");
}
}
Err(e) => {
debug!(error = ?e, stream_id = stream_id, "Failed to extract HTTP/2 response");
}
}
// Clear stream buffer to free memory
stream_buffers.remove(&stream_id);
}
}
}
// Forward frame to client
if let Err(e) = client.write_all(&upstream_frame_header).await {
debug!(error = %e, "Failed to forward frame header to client");
break;
}
if let Err(e) = client.write_all(&payload).await {
debug!(error = %e, "Failed to forward frame payload to client");
break;
}
}
Err(_) => {
debug!("Upstream closed HTTP/2 connection");
break;
}
}
}
}
}
let duration = start_time.elapsed();
debug!(
target_host = %target_host,
bytes_transferred = total_bytes,
duration_ms = duration.as_millis(),
pending_requests = pending_requests.len(),
buffered_streams = stream_buffers.len(),
"HTTP/2 proxying completed"
);
Ok(total_bytes)
}
/// Build client-facing TLS config from fake certificate
fn build_client_tls_config(
&self,
fake_cert: &rcgen::Certificate,
) -> Result<ClientTlsConfig, InterceptionError> {
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
// Serialize certificate
let cert_der = fake_cert.serialize_der().map_err(|e| {
InterceptionError::MitmError(MitmError::CertGenerationFailed(e.to_string()))
})?;
let cert_chain = vec![CertificateDer::from(cert_der)];
// Serialize private key
let key_der = fake_cert.serialize_private_key_der();
let private_key = PrivateKeyDer::Pkcs8(key_der.into());
// Build TLS config
ClientTlsConfig::new(cert_chain, private_key)
.map_err(|e| InterceptionError::TlsHandshakeFailed(e.to_string()))
}
}
#[cfg(test)]
mod tests {
// TODO: Add tests for:
// - Successful interception flow
// - Bypass logic (static rules, localhost)
// - TLS handshake (client and upstream)
// - Certificate generation
// - Logging policy enforcement
}