bunch of changes

This commit is contained in:
Yaroslav Kukharuk
2021-02-24 13:55:49 +02:00
parent 7003f4cfe6
commit cf0679fa0b
10 changed files with 78 additions and 1201 deletions
+10 -7
View File
@@ -1,10 +1,10 @@
import { Agent } from 'http';
import net from 'net';
import assert from 'assert';
import log from 'book';
import Debug from 'debug';
const DEFAULT_MAX_SOCKETS = 10;
const DEFAULT_SOCKET_TIMEOUT = 10 * 60 * 1000;
// Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will
@@ -33,6 +33,8 @@ class TunnelAgent extends Agent {
// new tcp server to service requests for this client
this.server = net.createServer();
this._socketTimeout = options.socketTimeout || DEFAULT_SOCKET_TIMEOUT;
// flag to avoid double starts
this.started = false;
@@ -46,15 +48,14 @@ class TunnelAgent extends Agent {
}
listen() {
const server = this.server;
if (this.started) {
throw new Error('already started');
}
this.started = true;
server.on('close', this._onClose.bind(this));
server.on('connection', this._onConnection.bind(this));
server.on('error', (err) => {
this.server.on('close', this._onClose.bind(this));
this.server.on('connection', this._onConnection.bind(this));
this.server.on('error', (err) => {
// These errors happen from killed connections, we don't worry about them
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
@@ -63,8 +64,8 @@ class TunnelAgent extends Agent {
});
return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
this.server.listen(() => {
const port = this.server.address().port;
this.debug('tcp server listening on port: %d', port);
resolve({
@@ -88,6 +89,8 @@ class TunnelAgent extends Agent {
// new socket connection from client for tunneling requests to client
_onConnection(socket) {
console.log('!!!!!!', socket);
socket.setTimeout(this._socketTimeout);
// no more socket connections allowed
if (this.connectedSockets >= this.maxTcpSockets) {
this.debug('no more sockets allowed');