@@ -182,31 +182,30 @@ impl Server {
182182 . clone ( )
183183 . map_or ( false , |p| p == ProtoType :: Tcp || p == ProtoType :: Udp ) ;
184184
185- let mut quic_client_join_handle = None ;
185+ let mut require_quic_client = false ;
186186 if let Some ( upstream_addr) = & cfg. upstream_addr {
187187 // connect to QUIC server if it is +quic protocols
188- let require_quic_client = upstream_addr. is_quic_proto ;
188+ require_quic_client = upstream_addr. is_quic_proto ;
189189 if require_quic_client {
190190 // connecting to quic server, and it will set relevant upstream address
191- let join_handle = self
192- . start_quic_client (
193- upstream_addr. net_addr . clone ( ) ,
194- self . common_quic_config . clone ( ) ,
195- )
196- . await ?;
191+ self . start_quic_client (
192+ upstream_addr. net_addr . clone ( ) ,
193+ self . common_quic_config . clone ( ) ,
194+ )
195+ . await ?;
197196
198197 if is_tcp_or_udp_proto {
199198 info ! (
200199 "start serving {} through quic client" ,
201200 server_proto. unwrap( ) . format_as_string( false )
202201 ) ;
203- join_handle. await . ok ( ) ;
202+
203+ // wait indefinitely here...
204+ std:: future:: pending :: < ( ) > ( ) . await ;
205+
204206 // directly use the quic client's tcp server or udp server, and return early
205207 return Ok ( ( ) ) ;
206208 }
207-
208- // self.tcp_upstream or self.udp_upstream is set accordingly when reaching here
209- quic_client_join_handle = Some ( join_handle) ;
210209 } else if upstream_addr. proto == Some ( ProtoType :: Udp ) {
211210 // non-quic upstream can only use IP address instead of domain
212211 inner_state ! ( self , udp_upstream) = upstream_addr. net_addr . to_socket_addr ( ) ;
@@ -253,10 +252,9 @@ impl Server {
253252 } ;
254253
255254 // join on the QUIC tunnel after the proxy server is started
256- if let Some ( quic_client_join_handle) = quic_client_join_handle {
257- info ! ( "join on the quic tunnel..." , ) ;
258- quic_client_join_handle. await . ok ( ) ;
259- info ! ( "quic tunnel quit" ) ;
255+ if require_quic_client {
256+ // wait indefinitely here...
257+ std:: future:: pending :: < ( ) > ( ) . await ;
260258 } else if require_quic_server {
261259 let quic_server_config = QuicServerConfig {
262260 server_addr : orig_server_addr,
@@ -335,7 +333,7 @@ impl Server {
335333 & self ,
336334 quic_server_addr : NetAddr ,
337335 common_quic_config : CommonQuicConfig ,
338- ) -> Result < JoinHandle < ( ) > > {
336+ ) -> Result < ( ) > {
339337 // if we have to forward tcp/udp through quic tunnel, we can directly use the
340338 // quic client's tcp/udp entry without creating another layer of traffic relay
341339 let cfg = & self . config ;
@@ -374,36 +372,23 @@ impl Server {
374372 } ) ;
375373 }
376374
377- let ( require_tcp, require_udp) = self . is_tcp_or_udp_server_required ( ) ;
378-
379- if require_tcp {
380- let tcp_server_addr = client. start_tcp_server ( ) . await ?;
375+ if let Some ( addr) = tcp_server_addr {
376+ let tcp_server_addr = client. start_tcp_server ( addr) . await ?;
381377 inner_state ! ( self , tcp_upstream) = Some ( tcp_server_addr) ;
382378 info ! ( "started quic tcp server: {tcp_server_addr}" ) ;
383379 }
384380
385- if require_udp {
386- let udp_server_addr = client. start_udp_server ( ) . await ?;
381+ if let Some ( addr ) = udp_server_addr {
382+ let udp_server_addr = client. start_udp_server ( addr ) . await ?;
387383 inner_state ! ( self , udp_upstream) = Some ( udp_server_addr) ;
388384 info ! ( "started quic udp server: {udp_server_addr}" ) ;
389385 }
390386
391- // will handover the handle to the caller, so we don't block here
392- let join_handle = client. connect_and_serve_async ( ) ;
387+ client. connect_and_serve_async ( ) ;
393388
394389 inner_state ! ( self , quic_client) = Some ( Arc :: new ( client) ) ;
395390
396- Ok ( join_handle)
397- }
398-
399- fn is_tcp_or_udp_server_required ( & self ) -> ( bool , bool ) {
400- self . config
401- . server_addr
402- . proto
403- . as_ref ( )
404- . map_or ( ( true , false ) , |p| {
405- ( * p != ProtoType :: Udp , * p == ProtoType :: Udp )
406- } )
391+ Ok ( ( ) )
407392 }
408393
409394 async fn init_resolver ( self : & mut Arc < Self > ) {
@@ -671,13 +656,17 @@ impl Server {
671656 }
672657
673658 MatchResult :: Proxy if upstream. is_some ( ) => {
674- outbound_type = match params. upstream_type . as_ref ( ) . unwrap ( ) {
675- ProtoType :: Http => OutboundType :: HttpProxy ,
676- ProtoType :: Socks4 => OutboundType :: SocksProxy ( SocksVersion :: V4 ) ,
677- ProtoType :: Socks5 => OutboundType :: SocksProxy ( SocksVersion :: V5 ) ,
678- ProtoType :: Tcp | ProtoType :: Udp => {
659+ outbound_type = match params. upstream_type . as_ref ( ) {
660+ Some ( ProtoType :: Http ) => OutboundType :: HttpProxy ,
661+ Some ( ProtoType :: Socks4 ) => OutboundType :: SocksProxy ( SocksVersion :: V4 ) ,
662+ Some ( ProtoType :: Socks5 ) => OutboundType :: SocksProxy ( SocksVersion :: V5 ) ,
663+ Some ( ProtoType :: Tcp | ProtoType :: Udp ) => {
679664 unreachable ! ( "not valid proxy type" )
680665 }
666+ None => {
667+ error ! ( "protocol is required for upstream" ) ;
668+ return Err ( ProxyError :: InternalError ) ;
669+ }
681670 } ;
682671
683672 debug ! (
@@ -1263,13 +1252,8 @@ impl Api for Server {
12631252 base_common_quic_config. quic_timeout_ms = config. idle_timeout ;
12641253 base_common_quic_config. retry_interval_ms = config. retry_interval ;
12651254 let upstream_addr = NetAddr :: from_str ( config. upstream_addr . as_str ( ) ) ?;
1266- let quic_client_join_handle = self
1267- . start_quic_client ( upstream_addr, base_common_quic_config)
1268- . await ;
1269-
1270- tokio:: spawn ( async move { quic_client_join_handle. unwrap ( ) . await } ) ;
1271-
1272- Ok ( ( ) )
1255+ self . start_quic_client ( upstream_addr, base_common_quic_config)
1256+ . await
12731257 }
12741258}
12751259
0 commit comments