added option to specify port range for client ports

This commit is contained in:
Jonathan Chan
2019-01-21 22:59:01 -06:00
parent d9fca62c17
commit ee77d6a41b
5 changed files with 56 additions and 11 deletions
+5 -1
View File
@@ -38,9 +38,13 @@ class ClientManager {
}
const maxSockets = this.opt.max_tcp_sockets;
const client_min_port_range = this.opt.client_min_port_range;
const client_max_port_range = this.opt.client_max_port_range;
const agent = new TunnelAgent({
clientId: id,
maxSockets: 10,
maxSockets: maxSockets,
client_min_port_range: client_min_port_range,
client_max_port_range: client_max_port_range
});
const client = new Client({
+19 -7
View File
@@ -4,8 +4,12 @@ import assert from 'assert';
import log from 'book';
import Debug from 'debug';
import { getPort } from 'portfinder';
const DEFAULT_MAX_SOCKETS = 10;
const DEFAULT_MIN_PORT_RANGE = 1024;
const DEFAULT_MAX_PORT_RANGE = 65535;
// Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will
// service http requests. This agent is usable wherever one can use an http.Agent
@@ -31,6 +35,9 @@ class TunnelAgent extends Agent {
this.connectedSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
this.client_min_port_range = options.client_min_port_range || DEFAULT_MIN_PORT_RANGE;
this.client_max_port_range = options.client_max_port_range || DEFAULT_MAX_PORT_RANGE;
// new tcp server to service requests for this client
this.server = net.createServer();
@@ -63,14 +70,19 @@ class TunnelAgent extends Agent {
});
return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
this.debug('tcp server listening on port: %d', port);
getPort({
port: this.client_min_port_range, // minimum port
stopPort: this.client_max_port_range // maximum port
}, (err,port) => {
server.listen(port,() => {
const port = server.address().port;
this.debug('tcp server listening on port: %d', port);
resolve({
// port for lt client tcp connections
port: port,
});
resolve({
// port for lt client tcp connections
port: port,
});
});
});
});
}