@@ -4,6 +4,7 @@ mod proxy;
44mod tls;
55
66use std:: net:: SocketAddr ;
7+ use std:: sync:: Arc ;
78use std:: time:: Duration ;
89
910use color_eyre:: eyre:: Result ;
@@ -27,8 +28,11 @@ async fn handle_connection(
2728 mut stream : TcpStream ,
2829 addr : SocketAddr ,
2930) -> Result < ( ) , ProxyError > {
30- let ( hostname, initial_buf) =
31- tokio:: time:: timeout ( Duration :: from_millis ( config. timeout_ms ) , parse_sni ( & mut stream) ) . await ??;
31+ let ( hostname, initial_buf) = tokio:: time:: timeout (
32+ Duration :: from_millis ( config. timeout_ms ) ,
33+ parse_sni ( & mut stream) ,
34+ )
35+ . await ??;
3236
3337 let forward = config
3438 . forwards
@@ -56,20 +60,25 @@ async fn main() -> Result<()> {
5660 tracing_subscriber:: fmt:: init ( ) ;
5761 color_eyre:: install ( ) ?;
5862
59- let config: Config = Figment :: new ( )
60- . merge ( Json :: file ( "sniproxy.json" ) )
61- . merge ( Env :: prefixed ( "TRIANGLE_" ) )
62- . extract ( ) ?;
63+ let config: Arc < Config > = Arc :: new (
64+ Figment :: new ( )
65+ . merge ( Json :: file ( "sniproxy.json" ) )
66+ . merge ( Env :: prefixed ( "TRIANGLE_" ) )
67+ . extract ( ) ?,
68+ ) ;
6369
6470 tracing:: info!( "{config:?}" ) ;
6571
6672 let lc = TcpListener :: bind ( config. listen_addr ) . await . unwrap ( ) ;
6773 loop {
6874 match lc. accept ( ) . await {
6975 Ok ( ( stream, addr) ) => {
70- if let Err ( err) = handle_connection ( & config, stream, addr) . await {
71- tracing:: error!( "error proxying connection: {err}" ) ;
72- }
76+ let config = config. clone ( ) ;
77+ tokio:: spawn ( async move {
78+ if let Err ( err) = handle_connection ( & config. clone ( ) , stream, addr) . await {
79+ tracing:: error!( "error proxying connection: {err}" ) ;
80+ }
81+ } ) ;
7382 }
7483 Err ( err) => tracing:: error!( "error accepting connection: {err}" ) ,
7584 }
0 commit comments