@@ -4,23 +4,23 @@ use http_body_util::Either;
4
4
use std:: fmt;
5
5
use std:: fmt:: { Debug , Formatter } ;
6
6
7
- use bytes:: Bytes ;
8
- use http_body_util:: combinators:: BoxBody ;
9
- use std:: net:: SocketAddr ;
10
- use std:: path:: PathBuf ;
11
- use std:: pin:: Pin ;
12
- use std:: sync:: { Arc , LazyLock } ;
13
- use std:: time:: Duration ;
14
-
15
7
use crate :: protocols;
16
8
use crate :: tunnel:: { try_to_sock_addr, LocalProtocol , RemoteAddr } ;
9
+ use arc_swap:: ArcSwap ;
10
+ use bytes:: Bytes ;
11
+ use http_body_util:: combinators:: BoxBody ;
17
12
use hyper:: body:: Incoming ;
18
13
use hyper:: server:: conn:: { http1, http2} ;
19
14
use hyper:: service:: service_fn;
20
15
use hyper:: { http, Request , Response , StatusCode , Version } ;
21
16
use hyper_util:: rt:: { TokioExecutor , TokioTimer } ;
22
17
use parking_lot:: Mutex ;
23
18
use socket2:: SockRef ;
19
+ use std:: net:: SocketAddr ;
20
+ use std:: path:: PathBuf ;
21
+ use std:: pin:: Pin ;
22
+ use std:: sync:: { Arc , LazyLock } ;
23
+ use std:: time:: Duration ;
24
24
25
25
use crate :: protocols:: dns:: DnsResolver ;
26
26
use crate :: protocols:: tls;
@@ -37,7 +37,6 @@ use crate::tunnel::server::utils::{
37
37
use crate :: tunnel:: tls_reloader:: TlsReloader ;
38
38
use tokio:: io:: { AsyncRead , AsyncWrite , AsyncWriteExt } ;
39
39
use tokio:: net:: TcpListener ;
40
- use tokio:: select;
41
40
use tokio_rustls:: rustls:: pki_types:: { CertificateDer , PrivateKeyDer } ;
42
41
use tokio_rustls:: TlsAcceptor ;
43
42
use tracing:: { error, info, span, warn, Instrument , Level , Span } ;
@@ -285,29 +284,41 @@ impl WsServer {
285
284
286
285
// setup upgrade request handler
287
286
let mk_websocket_upgrade_fn = |server : WsServer ,
288
- restrictions : Arc < RestrictionsRules > ,
287
+ restrictions : Arc < ArcSwap < RestrictionsRules > > ,
289
288
restrict_path : Option < String > ,
290
289
client_addr : SocketAddr | {
291
290
move |req : Request < Incoming > | {
292
- ws_server_upgrade ( server. clone ( ) , restrictions. clone ( ) , restrict_path. clone ( ) , client_addr, req)
293
- . map :: < anyhow:: Result < _ > , _ > ( Ok )
294
- . instrument ( mk_span ( ) )
291
+ ws_server_upgrade (
292
+ server. clone ( ) ,
293
+ restrictions. load ( ) . clone ( ) ,
294
+ restrict_path. clone ( ) ,
295
+ client_addr,
296
+ req,
297
+ )
298
+ . map :: < anyhow:: Result < _ > , _ > ( Ok )
299
+ . instrument ( mk_span ( ) )
295
300
}
296
301
} ;
297
302
298
303
let mk_http_upgrade_fn = |server : WsServer ,
299
- restrictions : Arc < RestrictionsRules > ,
304
+ restrictions : Arc < ArcSwap < RestrictionsRules > > ,
300
305
restrict_path : Option < String > ,
301
306
client_addr : SocketAddr | {
302
307
move |req : Request < Incoming > | {
303
- http_server_upgrade ( server. clone ( ) , restrictions. clone ( ) , restrict_path. clone ( ) , client_addr, req)
304
- . map :: < anyhow:: Result < _ > , _ > ( Ok )
305
- . instrument ( mk_span ( ) )
308
+ http_server_upgrade (
309
+ server. clone ( ) ,
310
+ restrictions. load ( ) . clone ( ) ,
311
+ restrict_path. clone ( ) ,
312
+ client_addr,
313
+ req,
314
+ )
315
+ . map :: < anyhow:: Result < _ > , _ > ( Ok )
316
+ . instrument ( mk_span ( ) )
306
317
}
307
318
} ;
308
319
309
320
let mk_auto_upgrade_fn = |server : WsServer ,
310
- restrictions : Arc < RestrictionsRules > ,
321
+ restrictions : Arc < ArcSwap < RestrictionsRules > > ,
311
322
restrict_path : Option < String > ,
312
323
client_addr : SocketAddr | {
313
324
move |req : Request < Incoming > | {
@@ -316,13 +327,13 @@ impl WsServer {
316
327
let restrict_path = restrict_path. clone ( ) ;
317
328
async move {
318
329
if fastwebsockets:: upgrade:: is_upgrade_request ( & req) {
319
- ws_server_upgrade ( server. clone ( ) , restrictions. clone ( ) , restrict_path, client_addr, req)
330
+ ws_server_upgrade ( server. clone ( ) , restrictions. load ( ) . clone ( ) , restrict_path, client_addr, req)
320
331
. map :: < anyhow:: Result < _ > , _ > ( Ok )
321
332
. await
322
333
} else if req. version ( ) == Version :: HTTP_2 {
323
334
http_server_upgrade (
324
335
server. clone ( ) ,
325
- restrictions. clone ( ) ,
336
+ restrictions. load ( ) . clone ( ) ,
326
337
restrict_path. clone ( ) ,
327
338
client_addr,
328
339
req,
@@ -357,25 +368,11 @@ impl WsServer {
357
368
} ;
358
369
359
370
// Bind server and run forever to serve incoming connections.
360
- let mut restrictions = RestrictionsRulesReloader :: new ( restrictions, self . config . restriction_config . clone ( ) ) ?;
361
- let mut await_config_reload = Box :: pin ( restrictions. reload_notifier ( ) ) ;
371
+ let restrictions = RestrictionsRulesReloader :: new ( restrictions, self . config . restriction_config . clone ( ) ) ?;
362
372
let listener = TcpListener :: bind ( & self . config . bind ) . await ?;
363
373
364
374
loop {
365
- let cnx = select ! {
366
- biased;
367
-
368
- _ = & mut await_config_reload => {
369
- drop( await_config_reload) ;
370
- restrictions. reload_restrictions_config( ) ;
371
- await_config_reload = Box :: pin( restrictions. reload_notifier( ) ) ;
372
- continue ;
373
- } ,
374
-
375
- cnx = listener. accept( ) => { cnx }
376
- } ;
377
-
378
- let ( stream, peer_addr) = match cnx {
375
+ let ( stream, peer_addr) = match listener. accept ( ) . await {
379
376
Ok ( ret) => ret,
380
377
Err ( err) => {
381
378
warn ! ( "Error while accepting connection {:?}" , err) ;
@@ -423,7 +420,7 @@ impl WsServer {
423
420
}
424
421
425
422
let http_upgrade_fn =
426
- mk_http_upgrade_fn ( server, restrictions. clone ( ) , restrict_path, peer_addr) ;
423
+ mk_http_upgrade_fn ( server, restrictions, restrict_path, peer_addr) ;
427
424
let con_fut = conn_builder. serve_connection ( tls_stream, service_fn ( http_upgrade_fn) ) ;
428
425
if let Err ( e) = con_fut. await {
429
426
error ! ( "Error while upgrading cnx to http: {:?}" , e) ;
@@ -432,7 +429,7 @@ impl WsServer {
432
429
// websocket
433
430
_ => {
434
431
let websocket_upgrade_fn =
435
- mk_websocket_upgrade_fn ( server, restrictions. clone ( ) , restrict_path, peer_addr) ;
432
+ mk_websocket_upgrade_fn ( server, restrictions, restrict_path, peer_addr) ;
436
433
let conn_fut = http1:: Builder :: new ( )
437
434
. timer ( TokioTimer :: new ( ) )
438
435
// https://github.com/erebe/wstunnel/issues/358
@@ -460,7 +457,7 @@ impl WsServer {
460
457
conn_fut. http2 ( ) . keep_alive_interval ( ping) ;
461
458
}
462
459
463
- let websocket_upgrade_fn = mk_auto_upgrade_fn ( server, restrictions. clone ( ) , None , peer_addr) ;
460
+ let websocket_upgrade_fn = mk_auto_upgrade_fn ( server, restrictions, None , peer_addr) ;
464
461
let upgradable =
465
462
conn_fut. serve_connection_with_upgrades ( stream, service_fn ( websocket_upgrade_fn) ) ;
466
463
0 commit comments