({ uid, host, port }: SocksSocketRequestedPayload)
| 532 | } |
| 533 | |
| 534 | async socketRequested({ uid, host, port }: SocksSocketRequestedPayload): Promise<void> { |
| 535 | debugLogger.log('socks', `[${uid}] => request ${host}:${port}`); |
| 536 | if (!this._patternMatcher(host, port)) { |
| 537 | const payload: SocksSocketFailedPayload = { uid, errorCode: 'ERULESET' }; |
| 538 | debugLogger.log('socks', `[${uid}] <= pattern error ${payload.errorCode}`); |
| 539 | this.emit(SocksProxyHandler.Events.SocksFailed, payload); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | if (host === 'local.playwright') |
| 544 | host = 'localhost'; |
| 545 | try { |
| 546 | if (this._redirectPortForTest) |
| 547 | port = this._redirectPortForTest; |
| 548 | const socket = await createSocket(host, port); |
| 549 | socket.on('data', data => { |
| 550 | const payload: SocksSocketDataPayload = { uid, data }; |
| 551 | this.emit(SocksProxyHandler.Events.SocksData, payload); |
| 552 | }); |
| 553 | socket.on('error', error => { |
| 554 | const payload: SocksSocketErrorPayload = { uid, error: error.message }; |
| 555 | debugLogger.log('socks', `[${uid}] <= network socket error ${payload.error}`); |
| 556 | this.emit(SocksProxyHandler.Events.SocksError, payload); |
| 557 | this._sockets.delete(uid); |
| 558 | }); |
| 559 | socket.on('end', () => { |
| 560 | const payload: SocksSocketEndPayload = { uid }; |
| 561 | debugLogger.log('socks', `[${uid}] <= network socket closed`); |
| 562 | this.emit(SocksProxyHandler.Events.SocksEnd, payload); |
| 563 | this._sockets.delete(uid); |
| 564 | }); |
| 565 | const localAddress = socket.localAddress; |
| 566 | const localPort = socket.localPort; |
| 567 | this._sockets.set(uid, socket); |
| 568 | const payload: SocksSocketConnectedPayload = { uid, host: localAddress!, port: localPort! }; |
| 569 | debugLogger.log('socks', `[${uid}] <= connected to network ${payload.host}:${payload.port}`); |
| 570 | this.emit(SocksProxyHandler.Events.SocksConnected, payload); |
| 571 | } catch (error) { |
| 572 | const payload: SocksSocketFailedPayload = { uid, errorCode: error.code }; |
| 573 | debugLogger.log('socks', `[${uid}] <= connect error ${payload.errorCode}`); |
| 574 | this.emit(SocksProxyHandler.Events.SocksFailed, payload); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | sendSocketData({ uid, data }: SocksSocketDataPayload): void { |
| 579 | this._sockets.get(uid)?.write(data); |
no test coverage detected