@@ -26,6 +26,8 @@ import * as nodesUtils from '../nodes/utils';
2626import { promisify } from '../utils' ;
2727import { timedCancellable , context } from '../contexts' ;
2828
29+ const clientConnectionClosedReason = Symbol ( 'clientConnectionClosedReason' ) ;
30+
2931interface Proxy extends StartStop { }
3032@StartStop ( )
3133class Proxy {
@@ -316,23 +318,32 @@ class Proxy {
316318 * Set timer to `null` explicitly to wait forever
317319 */
318320 public openConnectionForward (
319- nodeId : NodeId ,
321+ nodeIds : Array < NodeId > ,
320322 proxyHost : Host ,
321323 proxyPort : Port ,
322324 ctx ?: Partial < ContextTimed > ,
323- ) : PromiseCancellable < void > ;
325+ ) : PromiseCancellable < NodeId > ;
324326 @ready ( new networkErrors . ErrorProxyNotRunning ( ) , true )
325327 @timedCancellable ( true , ( proxy : Proxy ) => proxy . connConnectTime )
326328 public async openConnectionForward (
327- nodeId : NodeId ,
329+ nodeId : Array < NodeId > ,
328330 proxyHost : Host ,
329331 proxyPort : Port ,
330332 @context ctx : ContextTimed ,
331- ) : Promise < void > {
333+ ) : Promise < NodeId > {
332334 const proxyAddress = networkUtils . buildAddress ( proxyHost , proxyPort ) ;
333- await this . connectionLocksForward . withF ( [ proxyAddress , Lock ] , async ( ) => {
334- await this . establishConnectionForward ( nodeId , proxyHost , proxyPort , ctx ) ;
335- } ) ;
335+ return await this . connectionLocksForward . withF (
336+ [ proxyAddress , Lock ] ,
337+ async ( ) => {
338+ const connectionForward = await this . establishConnectionForward (
339+ nodeId ,
340+ proxyHost ,
341+ proxyPort ,
342+ ctx ,
343+ ) ;
344+ return connectionForward . nodeId ;
345+ } ,
346+ ) ;
336347 }
337348
338349 @ready ( new networkErrors . ErrorProxyNotRunning ( ) , true )
@@ -409,10 +420,22 @@ class Proxy {
409420 }
410421 await this . connectionLocksForward . withF ( [ proxyAddress , Lock ] , async ( ) => {
411422 const timer = new Timer ( { delay : this . connConnectTime } ) ;
423+ let cleanUpConnectionListener = ( ) => { } ;
412424 try {
413- await this . connectForward ( nodeId , proxyHost , proxyPort , clientSocket , {
414- timer,
415- } ) ;
425+ const connectForwardProm = this . connectForward (
426+ [ nodeId ] ,
427+ proxyHost ,
428+ proxyPort ,
429+ clientSocket ,
430+ {
431+ timer,
432+ } ,
433+ ) ;
434+ cleanUpConnectionListener = ( ) => {
435+ connectForwardProm . cancel ( clientConnectionClosedReason ) ;
436+ } ;
437+ clientSocket . addListener ( 'close' , cleanUpConnectionListener ) ;
438+ await connectForwardProm ;
416439 } catch ( e ) {
417440 if ( e instanceof networkErrors . ErrorProxyConnectInvalidUrl ) {
418441 if ( ! clientSocket . destroyed ) {
@@ -471,6 +494,7 @@ class Proxy {
471494 return ;
472495 } finally {
473496 timer . cancel ( ) ;
497+ clientSocket . removeListener ( 'close' , cleanUpConnectionListener ) ;
474498 }
475499 // After composing, switch off this error handler
476500 clientSocket . off ( 'error' , handleConnectError ) ;
@@ -482,22 +506,22 @@ class Proxy {
482506 } ;
483507
484508 protected connectForward (
485- nodeId : NodeId ,
509+ nodeIds : Array < NodeId > ,
486510 proxyHost : Host ,
487511 proxyPort : Port ,
488512 clientSocket : Socket ,
489513 ctx ?: Partial < ContextTimed > ,
490- ) : PromiseCancellable < void > ;
514+ ) : PromiseCancellable < NodeId > ;
491515 @timedCancellable ( true , ( proxy : Proxy ) => proxy . connConnectTime )
492516 protected async connectForward (
493- nodeId : NodeId ,
517+ nodeIds : Array < NodeId > ,
494518 proxyHost : Host ,
495519 proxyPort : Port ,
496520 clientSocket : Socket ,
497521 @context ctx : ContextTimed ,
498- ) : Promise < void > {
522+ ) : Promise < NodeId > {
499523 const conn = await this . establishConnectionForward (
500- nodeId ,
524+ nodeIds ,
501525 proxyHost ,
502526 proxyPort ,
503527 ctx ,
@@ -511,10 +535,11 @@ class Proxy {
511535 remotePort : conn . port ,
512536 type : 'forward' ,
513537 } ) ;
538+ return conn . nodeId ;
514539 }
515540
516541 protected async establishConnectionForward (
517- nodeId : NodeId ,
542+ nodeIds : Array < NodeId > ,
518543 proxyHost : Host ,
519544 proxyPort : Port ,
520545 ctx : ContextTimed ,
@@ -530,7 +555,7 @@ class Proxy {
530555 return conn ;
531556 }
532557 conn = new ConnectionForward ( {
533- nodeId ,
558+ nodeIds ,
534559 connections : this . connectionsForward ,
535560 utpSocket : this . utpSocket ,
536561 host : proxyHost ,
0 commit comments