11import { ChildProcess , spawn } from 'child_process' ;
22import * as net from 'net' ;
33import { promises as fs , unlink } from 'fs' ;
4- import Log from '@secret-agent/commons/Logger' ;
5- import { IBoundLog } from '@secret-agent/core-interfaces/ILog' ;
6- import { EventEmitter } from 'events' ;
7- import { createPromise } from '@secret-agent/commons/utils' ;
84import * as os from 'os' ;
9-
105import { v1 as uuid } from 'uuid' ;
6+ import Log from '@secret-agent/commons/Logger' ;
7+ import { createPromise } from '@secret-agent/commons/utils' ;
8+ import { TypedEventEmitter } from '@secret-agent/commons/eventUtils' ;
119
1210const { log } = Log ( module ) ;
1311
1412const ext = os . platform ( ) === 'win32' ? '.exe' : '' ;
1513const libPath = `${ __dirname } /dist/connect${ ext } ` ;
1614
17- export default class MitmSocket {
15+ let idCounter = 0 ;
16+
17+ export default class MitmSocket extends TypedEventEmitter < {
18+ connect : void ;
19+ dial : void ;
20+ close : void ;
21+ } > {
1822 public readonly socketPath : string ;
1923 public alpn = 'http/1.1' ;
2024 public socket : net . Socket ;
2125 public remoteAddress : string ;
2226 public localAddress : string ;
27+ public serverName : string ;
28+
29+ public id = ( idCounter += 1 ) ;
30+
31+ public spawnTime : Date ;
32+ public dialTime : Date ;
33+ public connectTime : Date ;
34+ public closeTime : Date ;
35+
36+ public get pid ( ) {
37+ return this . child ?. pid ;
38+ }
2339
2440 private isClosing = false ;
2541 private isConnected = false ;
2642 private child : ChildProcess ;
27- private emitter = new EventEmitter ( ) ;
2843 private connectError ?: string ;
2944
30- private readonly logger : IBoundLog ;
31-
3245 constructor ( readonly sessionId : string , readonly connectOpts : IGoTlsSocketConnectOpts ) {
46+ super ( ) ;
3347 const id = uuid ( ) ;
48+ this . serverName = connectOpts . servername ;
3449 this . socketPath =
3550 os . platform ( ) === 'win32' ? `\\\\.\\pipe\\sa-${ id } ` : `${ os . tmpdir ( ) } /sa-${ id } .sock` ;
3651 this . logger = log . createChild ( module , { sessionId } ) ;
@@ -55,18 +70,15 @@ export default class MitmSocket {
5570 this . connectOpts . tcpWindowSize = tcpVars . windowSize ;
5671 }
5772
58- public on ( event : 'close' , listener : ( socket : net . Socket ) => any ) {
59- this . emitter . on ( event , listener ) ;
60- }
61-
6273 public isHttp2 ( ) {
6374 return this . alpn === 'h2' ;
6475 }
6576
6677 public close ( ) {
6778 if ( this . isClosing ) return ;
79+ this . closeTime = new Date ( ) ;
6880 this . isClosing = true ;
69- this . emitter . emit ( 'close' ) ;
81+ this . emit ( 'close' ) ;
7082 this . cleanupSocket ( ) ;
7183 this . closeChild ( ) ;
7284 }
@@ -94,6 +106,7 @@ export default class MitmSocket {
94106 const child = spawn ( libPath , [ this . socketPath , JSON . stringify ( this . connectOpts ) ] , {
95107 stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
96108 } ) ;
109+ this . spawnTime = new Date ( ) ;
97110 this . child = child ;
98111 child . stdout . setEncoding ( 'utf8' ) ;
99112 child . stderr . setEncoding ( 'utf8' ) ;
@@ -168,19 +181,23 @@ export default class MitmSocket {
168181 private onChildProcessMessage ( messages : string ) {
169182 for ( const message of messages . split ( / \r ? \n / ) ) {
170183 if ( message . startsWith ( '[DomainSocketPiper.Dialed]' ) ) {
184+ this . dialTime = new Date ( ) ;
171185 const matches = message . match ( / R e m o t e : ( .+ ) , L o c a l : ( .+ ) / ) ;
172186 if ( matches ?. length ) {
173187 this . remoteAddress = matches [ 1 ] ;
174188 this . localAddress = matches [ 2 ] ;
175189 }
190+ this . emit ( 'dial' ) ;
176191 } else if ( message === '[DomainSocketPiper.ReadyForConnect]' ) {
177192 this . onListening ( ) ;
178193 } else if ( message . startsWith ( '[DomainSocketPiper.Connected]' ) ) {
179194 this . isConnected = true ;
195+ this . connectTime = new Date ( ) ;
180196 const matches = message . match ( / A L P N : ( .+ ) / ) ;
181197 if ( matches ?. length ) {
182198 this . alpn = matches [ 1 ] ;
183199 }
200+ this . emit ( 'connect' ) ;
184201 this . logger . stats ( 'SocketHandler.Connected' , {
185202 alpn : this . alpn ,
186203 host : this . connectOpts ?. host ,
0 commit comments