|
| 1 | +use std::time::Duration; |
1 | 2 | use std::{net::SocketAddr, sync::Arc};
|
2 | 3 |
|
3 | 4 | use hyper_util::rt::TokioIo;
|
@@ -39,34 +40,52 @@ async fn start_https_server_loop(addr: SocketAddr, app: Arc<AppContext>) {
|
39 | 40 |
|
40 | 41 | println!("Accepted connection from {}", socket_addr);
|
41 | 42 |
|
42 |
| - let result = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream).await; |
| 43 | + let app = app.clone(); |
| 44 | + tokio::spawn(async move { handle_connection(app, endpoint_port, tcp_stream, socket_addr) }); |
| 45 | + } |
| 46 | +} |
43 | 47 |
|
44 |
| - if let Err(err) = &result { |
45 |
| - eprintln!("failed to perform tls handshake: {err:#}"); |
46 |
| - continue; |
47 |
| - } |
| 48 | +async fn handle_connection( |
| 49 | + app: Arc<AppContext>, |
| 50 | + endpoint_port: u16, |
| 51 | + tcp_stream: TcpStream, |
| 52 | + socket_addr: SocketAddr, |
| 53 | +) { |
| 54 | + let future = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream); |
48 | 55 |
|
49 |
| - let (tls_stream, endpoint_info, cn_user_name) = result.unwrap(); |
50 |
| - |
51 |
| - if endpoint_info.http_type.is_protocol_http1() { |
52 |
| - kick_off_https1( |
53 |
| - app.clone(), |
54 |
| - socket_addr, |
55 |
| - endpoint_info, |
56 |
| - tls_stream, |
57 |
| - cn_user_name, |
58 |
| - endpoint_port, |
59 |
| - ); |
60 |
| - } else { |
61 |
| - kick_off_https2( |
62 |
| - app.clone(), |
63 |
| - socket_addr, |
64 |
| - endpoint_info, |
65 |
| - tls_stream, |
66 |
| - cn_user_name, |
67 |
| - endpoint_port, |
68 |
| - ); |
69 |
| - } |
| 56 | + let result = tokio::time::timeout(Duration::from_secs(10), future).await; |
| 57 | + |
| 58 | + if result.is_err() { |
| 59 | + println!("Timeout waiting for tls handshake from {}", socket_addr); |
| 60 | + } |
| 61 | + |
| 62 | + let result = result.unwrap(); |
| 63 | + |
| 64 | + if let Err(err) = &result { |
| 65 | + eprintln!("failed to perform tls handshake: {err:#}"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + let (tls_stream, endpoint_info, cn_user_name) = result.unwrap(); |
| 70 | + |
| 71 | + if endpoint_info.http_type.is_protocol_http1() { |
| 72 | + kick_off_https1( |
| 73 | + app, |
| 74 | + socket_addr, |
| 75 | + endpoint_info, |
| 76 | + tls_stream, |
| 77 | + cn_user_name, |
| 78 | + endpoint_port, |
| 79 | + ); |
| 80 | + } else { |
| 81 | + kick_off_https2( |
| 82 | + app, |
| 83 | + socket_addr, |
| 84 | + endpoint_info, |
| 85 | + tls_stream, |
| 86 | + cn_user_name, |
| 87 | + endpoint_port, |
| 88 | + ); |
70 | 89 | }
|
71 | 90 | }
|
72 | 91 |
|
|
0 commit comments