@@ -1434,6 +1434,7 @@ pub fn connectTunnel(
1434
1434
proxy : * Proxy ,
1435
1435
tunnel_host : []const u8 ,
1436
1436
tunnel_port : u16 ,
1437
+ tunnel_protocol : Connection.Protocol ,
1437
1438
) ! * Connection {
1438
1439
if (! proxy .supports_connect ) return error .TunnelNotSupported ;
1439
1440
@@ -1487,6 +1488,38 @@ pub fn connectTunnel(
1487
1488
conn .port = tunnel_port ;
1488
1489
conn .closing = false ;
1489
1490
1491
+ if (tunnel_protocol == .tls ) {
1492
+ if (disable_tls ) unreachable ;
1493
+
1494
+ conn .tls_client = try client .allocator .create (std .crypto .tls .Client );
1495
+ errdefer client .allocator .destroy (conn .tls_client );
1496
+
1497
+ const ssl_key_log_file : ? std.fs.File = if (std .options .http_enable_ssl_key_log_file ) ssl_key_log_file : {
1498
+ const ssl_key_log_path = std .process .getEnvVarOwned (client .allocator , "SSLKEYLOGFILE" ) catch | err | switch (err ) {
1499
+ error .EnvironmentVariableNotFound , error .InvalidWtf8 = > break :ssl_key_log_file null ,
1500
+ error .OutOfMemory = > return error .OutOfMemory ,
1501
+ };
1502
+ defer client .allocator .free (ssl_key_log_path );
1503
+ break :ssl_key_log_file std .fs .cwd ().createFile (ssl_key_log_path , .{
1504
+ .truncate = false ,
1505
+ .mode = switch (builtin .os .tag ) {
1506
+ .windows , .wasi = > 0 ,
1507
+ else = > 0o600 ,
1508
+ },
1509
+ }) catch null ;
1510
+ } else null ;
1511
+ errdefer if (ssl_key_log_file ) | key_log_file | key_log_file .close ();
1512
+
1513
+ conn .tls_client .* = std .crypto .tls .Client .init (conn .stream , .{
1514
+ .host = .{ .explicit = tunnel_host },
1515
+ .ca = .{ .bundle = client .ca_bundle },
1516
+ .ssl_key_log_file = ssl_key_log_file ,
1517
+ }) catch return error .TlsInitializationFailed ;
1518
+ // This is appropriate for HTTPS because the HTTP headers contain
1519
+ // the content length which is used to detect truncation attacks.
1520
+ conn .tls_client .allow_truncation_attacks = true ;
1521
+ conn .protocol = .tls ;
1522
+ }
1490
1523
return conn ;
1491
1524
}) catch {
1492
1525
// something went wrong with the tunnel
@@ -1524,7 +1557,7 @@ pub fn connect(
1524
1557
}
1525
1558
1526
1559
if (proxy .supports_connect ) tunnel : {
1527
- return connectTunnel (client , proxy , host , port ) catch | err | switch (err ) {
1560
+ return connectTunnel (client , proxy , host , port , protocol ) catch | err | switch (err ) {
1528
1561
error .TunnelNotSupported = > break :tunnel ,
1529
1562
else = > | e | return e ,
1530
1563
};
0 commit comments