-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathproxy_tls.rs
More file actions
946 lines (813 loc) · 32.9 KB
/
proxy_tls.rs
File metadata and controls
946 lines (813 loc) · 32.9 KB
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
use crate::proxy::{
HTTPJAIL_HEADER, HTTPJAIL_HEADER_VALUE, create_connect_403_response_with_context,
create_forbidden_response,
};
use crate::rules::{Action, RuleEngine};
use crate::tls::CertificateManager;
use anyhow::Result;
use bytes::Bytes;
use http_body_util::{BodyExt, combinators::BoxBody};
use hyper::body::Incoming;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Error as HyperError, Method, Request, Response, StatusCode, Uri};
use hyper_util::rt::TokioIo;
use rustls::ServerConfig;
use std::sync::Arc;
use tls_parser::{TlsMessage, parse_tls_plaintext};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::time::{Duration, Instant, timeout};
use tokio_rustls::TlsAcceptor;
use tracing::{debug, error, info, warn};
// Timeout for initial protocol detection
const PROTOCOL_DETECT_TIMEOUT: Duration = Duration::from_secs(5);
// Timeout for reading CONNECT headers
const CONNECT_READ_TIMEOUT: Duration = Duration::from_secs(10);
// Timeout for TLS handshake
const TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
// Timeout for writing responses
const WRITE_TIMEOUT: Duration = Duration::from_secs(5);
// Timeout for reading TLS ClientHello
const CLIENT_HELLO_TIMEOUT: Duration = Duration::from_secs(5);
/// Handle an HTTPS connection with potential CONNECT tunneling and TLS interception
pub async fn handle_https_connection(
stream: TcpStream,
rule_engine: Arc<RuleEngine>,
cert_manager: Arc<CertificateManager>,
remote_addr: std::net::SocketAddr,
) -> Result<()> {
debug!("Handling new HTTPS connection from {}", remote_addr);
// Peek at the first few bytes to determine if this is HTTP or TLS
let mut peek_buf = [0; 6];
let n = match timeout(PROTOCOL_DETECT_TIMEOUT, stream.peek(&mut peek_buf)).await {
Ok(Ok(n)) => n,
Ok(Err(e)) => {
debug!("Failed to peek at stream: {}", e);
return Ok(());
}
Err(_) => {
warn!("Timeout while detecting protocol");
return Ok(());
}
};
if n == 0 {
debug!("Connection closed before protocol detection");
return Ok(());
}
// Check if this looks like a TLS ClientHello (starts with 0x16 for TLS handshake)
// or an HTTP request (starts with ASCII text like "CONNECT", "GET", etc.)
if peek_buf[0] == 0x16 && n > 1 && (peek_buf[1] == 0x03 || peek_buf[1] == 0x02) {
// This is a TLS ClientHello - we're in transparent proxy mode
debug!("Detected TLS ClientHello - transparent proxy mode");
handle_transparent_tls(stream, rule_engine, cert_manager, remote_addr).await
} else if peek_buf[0] >= 0x41 && peek_buf[0] <= 0x5A {
// This looks like HTTP (starts with uppercase ASCII letter)
// Check if it's a CONNECT request
let request_str = String::from_utf8_lossy(&peek_buf);
if request_str.starts_with("CONNEC") {
debug!("Detected CONNECT request - explicit proxy mode");
handle_connect_tunnel(stream, rule_engine, cert_manager, remote_addr).await
} else {
// Regular HTTP on HTTPS port
debug!("Detected plain HTTP on HTTPS port");
handle_plain_http(stream, rule_engine, cert_manager, remote_addr).await
}
} else {
warn!(
"Unknown protocol on HTTPS port, first byte: 0x{:02x}",
peek_buf[0]
);
Ok(())
}
}
/// Extract SNI hostname from a TLS ClientHello
async fn extract_sni_from_stream(stream: &mut TcpStream) -> Result<Option<String>> {
// Read enough bytes to parse the ClientHello
// TLS record header is 5 bytes, ClientHello can be quite large
let mut buf = vec![0u8; 2048];
let Ok(peek_result) = timeout(CLIENT_HELLO_TIMEOUT, stream.peek(&mut buf)).await else {
debug!("Timeout reading ClientHello");
return Ok(None);
};
let Ok(n) = peek_result else {
debug!("Failed to peek ClientHello: {}", peek_result.unwrap_err());
return Ok(None);
};
if n < 5 {
debug!("Not enough data for TLS record header");
return Ok(None);
}
let maybe_record = parse_tls_plaintext(&buf[..n]);
let Ok((_, record)) = maybe_record else {
debug!(
"Failed to parse TLS record: {:?}",
maybe_record.unwrap_err()
);
return Ok(None);
};
// Parse the TLS plaintext record
// Check if this is a handshake message
let Some(TlsMessage::Handshake(tls_parser::TlsMessageHandshake::ClientHello(client_hello))) =
record.msg.first()
else {
return Ok(None);
};
// Look for the SNI extension in the raw extensions
let Some(ext_data) = client_hello.ext else {
debug!("ClientHello has no SNI extension");
return Ok(None);
};
// Parse the extensions
let Ok(exts) = tls_parser::parse_tls_extensions(ext_data) else {
return Ok(None);
};
for ext in exts.1 {
if let tls_parser::TlsExtension::SNI(sni_list) = ext {
// Get the first hostname from the SNI list
for sni in sni_list.iter() {
let (tls_parser::SNIType::HostName, data) = sni else {
continue;
};
let Ok(hostname) = std::str::from_utf8(data) else {
continue;
};
debug!("Extracted SNI hostname: {}", hostname);
return Ok(Some(hostname.to_string()));
}
}
}
Ok(None)
}
/// Handle transparent TLS interception (no CONNECT, direct TLS)
async fn handle_transparent_tls(
mut stream: TcpStream,
rule_engine: Arc<RuleEngine>,
cert_manager: Arc<CertificateManager>,
remote_addr: std::net::SocketAddr,
) -> Result<()> {
debug!("Handling transparent TLS connection");
// Extract SNI from the ClientHello
let hostname = match extract_sni_from_stream(&mut stream).await? {
Some(sni) => {
info!("Extracted SNI hostname: {}", sni);
sni
}
None => {
let default = "example.com".to_string();
warn!("Could not extract SNI, using default host: {}", default);
default
}
};
// Note: We don't check rules here - let downstream handle blocking
// This allows us to send proper HTTP 403 responses after TLS handshake
debug!("Processing transparent TLS for: {}", hostname);
// Get certificate for the host
let (cert_chain, key) = cert_manager
.get_cert_for_host(&hostname)
.map_err(|e| anyhow::anyhow!("Failed to get certificate for {}: {}", hostname, e))?;
// Create TLS config with our certificate
let tls_config = ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(cert_chain, key)
.map_err(|e| anyhow::anyhow!("Failed to create TLS config: {}", e))?;
let acceptor = TlsAcceptor::from(Arc::new(tls_config));
// Perform TLS handshake
debug!("Accepting TLS connection for transparent proxy");
let tls_stream = match timeout(TLS_HANDSHAKE_TIMEOUT, acceptor.accept(stream)).await {
Ok(Ok(s)) => s,
Ok(Err(e)) => {
error!("TLS handshake failed: {}", e);
return Err(e.into());
}
Err(_) => {
error!("TLS handshake timeout");
return Err(anyhow::anyhow!("TLS handshake timeout"));
}
};
debug!("TLS handshake complete for transparent proxy");
// Now handle the decrypted HTTPS requests
let io = TokioIo::new(tls_stream);
let service = service_fn(move |req| {
let host_clone = hostname.clone();
handle_decrypted_https_request(req, Arc::clone(&rule_engine), host_clone, remote_addr)
});
debug!("Starting HTTP/1.1 server for decrypted requests");
http1::Builder::new()
.preserve_header_case(true)
.title_case_headers(false)
.serve_connection(io, service)
.await?;
Ok(())
}
/// Handle a CONNECT tunnel request with TLS interception
async fn handle_connect_tunnel(
stream: TcpStream,
rule_engine: Arc<RuleEngine>,
cert_manager: Arc<CertificateManager>,
remote_addr: std::net::SocketAddr,
) -> Result<()> {
debug!("Handling CONNECT tunnel");
// Buffer the stream for reading lines
let mut reader = BufReader::new(stream);
let mut first_line = String::new();
// Read the first line to get the CONNECT request
let read_result = timeout(CONNECT_READ_TIMEOUT, reader.read_line(&mut first_line)).await;
match read_result {
Ok(Ok(0)) => {
debug!("Connection closed before CONNECT request");
return Ok(());
}
Ok(Ok(_)) => {}
Ok(Err(e)) => {
debug!("Failed to read CONNECT request: {}", e);
return Ok(());
}
Err(_) => {
warn!("Timeout reading CONNECT request");
return Ok(());
}
}
debug!("CONNECT line: {}", first_line.trim());
// Parse the CONNECT target
let parts: Vec<&str> = first_line.split_whitespace().collect();
if parts.len() < 2 {
return Ok(());
}
let target = parts[1];
let host = if target.contains(':') {
target.split(':').next().unwrap_or("unknown")
} else {
target
};
info!("CONNECT request for: {}", target);
// Read the rest of the headers until we find the empty line
let mut headers = vec![first_line.clone()];
let start_time = tokio::time::Instant::now();
loop {
// Check if we've exceeded the total timeout
if start_time.elapsed() > CONNECT_READ_TIMEOUT {
warn!("Timeout reading CONNECT headers");
return Ok(());
}
let mut line = String::new();
let remaining_time = CONNECT_READ_TIMEOUT.saturating_sub(start_time.elapsed());
match timeout(remaining_time, reader.read_line(&mut line)).await {
Ok(Ok(0)) => break,
Ok(Ok(_)) => {
if line == "\r\n" || line == "\n" {
break;
}
headers.push(line);
}
Ok(Err(e)) => {
debug!("Error reading header: {}", e);
break;
}
Err(_) => {
warn!("Timeout reading headers");
break;
}
}
}
// Check if this host is allowed
let full_url = format!("https://{}", target);
let requester_ip = remote_addr.ip().to_string();
let evaluation = rule_engine
.evaluate_with_context_and_ip(Method::GET, &full_url, &requester_ip)
.await;
match evaluation.action {
Action::Allow => {
debug!("CONNECT allowed to: {}", host);
// Get the underlying stream back
let mut stream = reader.into_inner();
// Send 200 Connection Established response
let response = b"HTTP/1.1 200 Connection Established\r\n\r\n";
match timeout(WRITE_TIMEOUT, async {
stream.write_all(response).await?;
stream.flush().await
})
.await
{
Ok(Ok(_)) => {}
Ok(Err(e)) => {
error!("Failed to write CONNECT response: {}", e);
return Err(e.into());
}
Err(_) => {
error!("Timeout writing CONNECT response");
return Err(anyhow::anyhow!("Timeout writing CONNECT response"));
}
}
debug!("Sent 200 Connection Established, starting TLS handshake");
// Now perform TLS handshake with the client
perform_tls_interception(stream, rule_engine, cert_manager, host, remote_addr).await
}
Action::Deny => {
warn!("CONNECT denied to: {}", host);
// Get the underlying stream back
let mut stream = reader.into_inner();
// Send 403 Forbidden response with context
let response = create_connect_403_response_with_context(evaluation.context);
match timeout(WRITE_TIMEOUT, async {
stream.write_all(&response).await?;
stream.flush().await
})
.await
{
Ok(Ok(_)) => {}
Ok(Err(e)) => {
debug!("Failed to write 403 response: {}", e);
}
Err(_) => {
debug!("Timeout writing 403 response");
}
}
Ok(())
}
}
}
/// Perform TLS interception on a stream
async fn perform_tls_interception(
stream: TcpStream,
rule_engine: Arc<RuleEngine>,
cert_manager: Arc<CertificateManager>,
host: &str,
remote_addr: std::net::SocketAddr,
) -> Result<()> {
// On macOS, warn if the CA is not trusted (but still attempt interception)
#[cfg(target_os = "macos")]
{
if !CertificateManager::is_ca_trusted() {
warn!(
"CA not trusted in keychain - {} may fail with certificate errors",
host
);
warn!("Run 'httpjail trust --install' to trust the CA and avoid connection failures");
}
}
// Get certificate for the host
let (cert_chain, key) = cert_manager
.get_cert_for_host(host)
.map_err(|e| anyhow::anyhow!("Failed to get certificate for {}: {}", host, e))?;
// Create TLS config with our certificate
let tls_config = ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(cert_chain, key)
.map_err(|e| anyhow::anyhow!("Failed to create TLS config: {}", e))?;
let acceptor = TlsAcceptor::from(Arc::new(tls_config));
// Perform TLS handshake
debug!("Accepting TLS connection for {}", host);
let tls_stream = match timeout(TLS_HANDSHAKE_TIMEOUT, acceptor.accept(stream)).await {
Ok(Ok(s)) => s,
Ok(Err(e)) => {
error!("TLS handshake failed for {}: {}", host, e);
return Err(e.into());
}
Err(_) => {
error!("TLS handshake timeout for {}", host);
return Err(anyhow::anyhow!("TLS handshake timeout"));
}
};
debug!("TLS handshake complete for {}", host);
// Now handle the decrypted HTTPS requests
let io = TokioIo::new(tls_stream);
let host_string = host.to_string();
let remote_addr_copy = remote_addr; // Copy for the closure
let service = service_fn(move |req| {
let host_clone = host_string.clone();
handle_decrypted_https_request(req, Arc::clone(&rule_engine), host_clone, remote_addr_copy)
});
debug!("Starting HTTP/1.1 server for decrypted requests");
http1::Builder::new()
.preserve_header_case(true)
.title_case_headers(false)
.serve_connection(io, service)
.await?;
Ok(())
}
/// Handle a plain HTTP request on the HTTPS port
async fn handle_plain_http(
stream: TcpStream,
rule_engine: Arc<RuleEngine>,
cert_manager: Arc<CertificateManager>,
remote_addr: std::net::SocketAddr,
) -> Result<()> {
debug!("Handling plain HTTP on HTTPS port");
let io = TokioIo::new(stream);
let service = service_fn(move |req| {
crate::proxy::handle_http_request(
req,
Arc::clone(&rule_engine),
Arc::clone(&cert_manager),
remote_addr,
)
});
http1::Builder::new()
.preserve_header_case(true)
.title_case_headers(false)
.serve_connection(io, service)
.await?;
Ok(())
}
/// Handle a decrypted HTTPS request after TLS interception
async fn handle_decrypted_https_request(
req: Request<Incoming>,
rule_engine: Arc<RuleEngine>,
host: String,
remote_addr: std::net::SocketAddr,
) -> Result<Response<BoxBody<Bytes, HyperError>>, std::convert::Infallible> {
let method = req.method().clone();
let uri = req.uri().clone();
// Build the full URL for rule evaluation
let path = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("/");
let full_url = format!("https://{}{}", host, path);
debug!(
"Proxying HTTPS request: {} {} from {}",
method, full_url, remote_addr
);
// Evaluate rules with method and requester IP
let requester_ip = remote_addr.ip().to_string();
let evaluation = rule_engine
.evaluate_with_context_and_ip(method.clone(), &full_url, &requester_ip)
.await;
match evaluation.action {
Action::Allow => {
debug!("Request allowed: {}", full_url);
match proxy_https_request(req, &host, evaluation.max_tx_bytes).await {
Ok(resp) => Ok(resp),
Err(e) => {
error!("Proxy error: {}", e);
crate::proxy::create_error_response(StatusCode::BAD_GATEWAY, "Proxy error")
}
}
}
Action::Deny => {
debug!("Request denied: {}", full_url);
create_forbidden_response(evaluation.context)
}
}
}
/// Forward an HTTPS request to the target server
async fn proxy_https_request(
req: Request<Incoming>,
host: &str,
max_tx_bytes: Option<u64>,
) -> Result<Response<BoxBody<Bytes, HyperError>>> {
// Build the target URL
let path = req
.uri()
.path_and_query()
.map(|pq| pq.as_str())
.unwrap_or("/");
let target_url = format!("https://{}{}", host, path);
let target_uri = target_url.parse::<Uri>()?;
debug!("Forwarding request to: {}", target_url);
// Prepare request for upstream using common function
let mut new_req = crate::proxy::prepare_upstream_request(req, target_uri);
// Apply byte limit to outgoing request if specified
if let Some(max_bytes) = max_tx_bytes {
use http_body_util::BodyExt;
let (parts, body) = new_req.into_parts();
// Calculate request header size
// Request line: "GET /path HTTP/1.1\r\n"
let method_str = parts.method.as_str();
let path_str = parts
.uri
.path_and_query()
.map(|p| p.as_str())
.unwrap_or("/");
let request_line_size = format!("{} {} HTTP/1.1\r\n", method_str, path_str).len() as u64;
// Headers: each header is "name: value\r\n"
let headers_size: u64 = parts
.headers
.iter()
.map(|(name, value)| name.as_str().len() as u64 + 2 + value.len() as u64 + 2)
.sum();
// Final "\r\n" separator
let total_header_size = request_line_size + headers_size + 2;
debug!(
"Applying request byte limit: {} bytes (headers: {} bytes)",
max_bytes, total_header_size
);
let limited_body = crate::proxy::LimitedBody::new(body, max_bytes, total_header_size);
new_req = Request::from_parts(parts, BodyExt::boxed(limited_body));
}
// Use the shared HTTP/HTTPS client from proxy module
let client = crate::proxy::get_client();
// Forward the request - no timeout to support long-running connections (WebSocket, gRPC, etc.)
debug!("Sending HTTPS request to upstream server: {}", target_url);
debug!(
"Request URI: {}, Method: {}",
new_req.uri(),
new_req.method()
);
let start = Instant::now();
let resp_future = client.request(new_req);
debug!("Request future created, awaiting response...");
let resp = match resp_future.await {
Ok(r) => {
let elapsed = start.elapsed();
if elapsed > Duration::from_secs(2) {
warn!(
"HTTPS request took {}ms: {}",
elapsed.as_millis(),
target_url
);
} else {
debug!("HTTPS request completed in {}ms", elapsed.as_millis());
}
r
}
Err(e) => {
let elapsed = start.elapsed();
// Try to get more detailed error information
let error_details = format!("{:?}", e);
let error_chain = format!("{:#}", e);
error!(
"Failed to forward HTTPS request after {}ms: {}",
elapsed.as_millis(),
e
);
error!("Error details: {}", error_details);
error!("Error chain: {}", error_chain);
// The hyper_util error doesn't expose underlying IO errors directly
return Err(e.into());
}
};
debug!(
"Received response from upstream server: status={:?}, version={:?}",
resp.status(),
resp.version()
);
// Convert the response body to BoxBody for uniform type
let (mut parts, body) = resp.into_parts();
// Add HTTPJAIL header to indicate this response went through our proxy
parts
.headers
.insert(HTTPJAIL_HEADER, HTTPJAIL_HEADER_VALUE.parse().unwrap());
let boxed_body = body.boxed();
Ok(Response::from_parts(parts, boxed_body))
}
#[cfg(test)]
mod tests {
use super::*;
use rustls::ClientConfig;
use std::sync::Arc;
use tempfile::TempDir;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio_rustls::TlsConnector;
async fn create_test_cert_manager() -> Arc<CertificateManager> {
let temp_dir = TempDir::new().unwrap();
let cert_manager = CertificateManager::with_config_dir(Some(
temp_dir
.path()
.to_path_buf()
.try_into()
.expect("tempdir library provided non utf8 tmp dir"),
))
.expect("Failed to create test certificate manager");
Arc::new(cert_manager)
}
fn create_test_rule_engine(allow_all: bool) -> Arc<RuleEngine> {
let js = if allow_all {
"true".to_string()
} else {
"/example\\.com/.test(r.host)".to_string()
};
let engine = crate::rules::v8_js::V8JsRuleEngine::new(js).unwrap();
Arc::new(RuleEngine::from_trait(Box::new(engine), None))
}
/// Create a TLS client config that trusts any certificate (for testing)
fn create_insecure_tls_config() -> Arc<ClientConfig> {
let mut config = ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(Arc::new(InsecureCertVerifier))
.with_no_client_auth();
config.alpn_protocols = vec![];
Arc::new(config)
}
/// A certificate verifier that accepts any certificate (for testing only!)
#[derive(Debug)]
struct InsecureCertVerifier;
impl rustls::client::danger::ServerCertVerifier for InsecureCertVerifier {
fn verify_server_cert(
&self,
_end_entity: &rustls::pki_types::CertificateDer<'_>,
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
_server_name: &rustls::pki_types::ServerName<'_>,
_ocsp_response: &[u8],
_now: rustls::pki_types::UnixTime,
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
Ok(rustls::client::danger::ServerCertVerified::assertion())
}
fn verify_tls12_signature(
&self,
_message: &[u8],
_cert: &rustls::pki_types::CertificateDer<'_>,
_dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
}
fn verify_tls13_signature(
&self,
_message: &[u8],
_cert: &rustls::pki_types::CertificateDer<'_>,
_dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
}
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
vec![
rustls::SignatureScheme::RSA_PKCS1_SHA256,
rustls::SignatureScheme::RSA_PKCS1_SHA384,
rustls::SignatureScheme::RSA_PKCS1_SHA512,
rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
rustls::SignatureScheme::ECDSA_NISTP384_SHA384,
rustls::SignatureScheme::RSA_PSS_SHA256,
rustls::SignatureScheme::RSA_PSS_SHA384,
rustls::SignatureScheme::RSA_PSS_SHA512,
rustls::SignatureScheme::ED25519,
]
}
}
#[tokio::test]
async fn test_connect_tunnel_allowed() {
// Start a test proxy server
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let cert_manager = create_test_cert_manager().await;
let rule_engine = create_test_rule_engine(false); // Allow only example.com
// Spawn proxy handler
tokio::spawn(async move {
let (stream, addr) = listener.accept().await.unwrap();
let _ = handle_connect_tunnel(stream, rule_engine, cert_manager, addr).await;
});
// Connect to proxy
let mut stream = TcpStream::connect(addr).await.unwrap();
// Send CONNECT request for allowed host
let connect_request = "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n";
stream.write_all(connect_request.as_bytes()).await.unwrap();
// Read response
let mut buf = vec![0; 1024];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
// Should get 200 Connection Established
assert!(
response.contains("200 Connection Established"),
"Response: {}",
response
);
}
#[tokio::test]
async fn test_connect_tunnel_denied() {
// Start a test proxy server
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let cert_manager = create_test_cert_manager().await;
let rule_engine = create_test_rule_engine(false); // Allow only example.com
// Spawn proxy handler
tokio::spawn(async move {
let (stream, addr) = listener.accept().await.unwrap();
let _ = handle_connect_tunnel(stream, rule_engine, cert_manager, addr).await;
});
// Connect to proxy
let mut stream = TcpStream::connect(addr).await.unwrap();
// Send CONNECT request for denied host
let connect_request =
"CONNECT malicious.com:443 HTTP/1.1\r\nHost: malicious.com:443\r\n\r\n";
stream.write_all(connect_request.as_bytes()).await.unwrap();
// Read response
let mut buf = vec![0; 1024];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
// Should get 403 Forbidden
assert!(response.contains("403 Forbidden"), "Response: {}", response);
assert!(
response.contains("blocked by httpjail"),
"Response: {}",
response
);
}
#[tokio::test]
async fn test_transparent_tls_with_sni() {
// Start a test proxy server
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let cert_manager = create_test_cert_manager().await;
let rule_engine = create_test_rule_engine(true); // Allow all
// Spawn proxy handler
tokio::spawn(async move {
let (stream, addr) = listener.accept().await.unwrap();
let _ = handle_transparent_tls(stream, rule_engine, cert_manager, addr).await;
});
// Connect to proxy with TLS directly (transparent mode)
let stream = TcpStream::connect(addr).await.unwrap();
// Create TLS connector with insecure config
let tls_config = create_insecure_tls_config();
let connector = TlsConnector::from(tls_config);
// Perform TLS handshake with SNI
let server_name = rustls::pki_types::ServerName::try_from("example.com").unwrap();
let result = tokio::time::timeout(
Duration::from_secs(2),
connector.connect(server_name, stream),
)
.await;
// Should succeed (timeout means handshake succeeded but no HTTP handler after)
assert!(result.is_ok() || result.is_err()); // Either handshake succeeds or times out waiting for response
}
#[tokio::test]
async fn test_extract_sni_from_client_hello() {
// This test verifies that SNI extraction from a TLS ClientHello works correctly
// We'll create a simple test by starting a listener and sending a TLS ClientHello
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
// Spawn a task to accept the connection and extract SNI
let handle = tokio::spawn(async move {
let (mut stream, _) = listener.accept().await.unwrap();
extract_sni_from_stream(&mut stream).await
});
// Connect and send a TLS ClientHello with SNI
let stream = TcpStream::connect(addr).await.unwrap();
let tls_config = create_insecure_tls_config();
let connector = TlsConnector::from(tls_config);
// Try to connect with TLS (this will send a ClientHello with SNI)
let server_name = rustls::pki_types::ServerName::try_from("test.example.com").unwrap();
let _ = tokio::time::timeout(
Duration::from_millis(100),
connector.connect(server_name, stream),
)
.await;
// Get the SNI result from the server side
let sni_result = tokio::time::timeout(Duration::from_millis(100), handle).await;
// The test passes if we can extract SNI or if the connection fails quickly
// (since we're not completing the handshake)
assert!(sni_result.is_ok() || sni_result.is_err());
}
#[tokio::test]
async fn test_protocol_detection() {
// Test that the proxy correctly detects CONNECT vs direct TLS
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let cert_manager = create_test_cert_manager().await;
let rule_engine = create_test_rule_engine(true);
// Test CONNECT detection
{
let cert_manager = cert_manager.clone();
let rule_engine = rule_engine.clone();
tokio::spawn(async move {
let (stream, addr) = listener.accept().await.unwrap();
let _ = handle_https_connection(stream, rule_engine, cert_manager, addr).await;
});
let mut stream = TcpStream::connect(addr).await.unwrap();
stream
.write_all(b"CONNECT example.com:443 HTTP/1.1\r\n\r\n")
.await
.unwrap();
let mut buf = vec![0; 256];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(response.contains("200") || response.contains("403"));
}
}
#[tokio::test]
async fn test_https_client_through_transparent_proxy() {
use bytes::Bytes;
use http_body_util::Empty;
use hyper_util::client::legacy::Client;
use hyper_util::rt::TokioExecutor;
// Start a transparent TLS proxy
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let cert_manager = create_test_cert_manager().await;
let rule_engine = create_test_rule_engine(true); // Allow all
// Start proxy handler
tokio::spawn(async move {
let (stream, addr) = listener.accept().await.unwrap();
// Use the actual transparent TLS handler (which will extract SNI, etc.)
let _ = handle_transparent_tls(stream, rule_engine, cert_manager, addr).await;
});
// Give the server time to start
tokio::time::sleep(Duration::from_millis(100)).await;
// Create HTTPS client that accepts self-signed certificates
let tls_config = create_insecure_tls_config();
let mut http_connector = hyper_util::client::legacy::connect::HttpConnector::new();
http_connector.enforce_http(false);
let https_connector = hyper_rustls::HttpsConnector::from((http_connector, tls_config));
let client = Client::builder(TokioExecutor::new()).build(https_connector);
// Make an HTTPS request directly to the proxy (transparent mode)
let uri = format!("https://127.0.0.1:{}/test", addr.port())
.parse::<hyper::Uri>()
.unwrap();
let request = hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Empty::<Bytes>::new())
.unwrap();
// The test passes if we can establish a TLS connection
// (actual upstream may fail, but TLS interception should work)
let result = tokio::time::timeout(Duration::from_secs(1), client.request(request)).await;
assert!(result.is_ok() || result.is_err()); // Either succeeds or times out
}
}