Add a range option to server to specify specific port client attribution

This commit is contained in:
Olivier MICHAUD
2021-05-17 21:35:38 +02:00
parent 3ebcab8830
commit 3c06736520
3 changed files with 20 additions and 4 deletions
+11 -4
View File
@@ -20,6 +20,9 @@ class TunnelAgent extends Agent {
// sockets we can hand out via createConnection
this.availableSockets = [];
this.port = null;
this.clientId = options.clientId
this.portManager = options.portManager || null;
// when a createConnection cannot return a socket, it goes into a queue
// once a socket is available it is handed out to the next callback
@@ -63,13 +66,14 @@ class TunnelAgent extends Agent {
});
return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
this.debug('tcp server listening on port: %d', port);
const port = this.portManager ? this.portManager.getNextAvailable(this.options.clientId) : null;
server.listen(port,() => {
this.port = server.address().port
this.debug('tcp server listening on port: %d (%s)', this.port, this.clientId);
resolve({
// port for lt client tcp connections
port: port,
port: this.port,
});
});
});
@@ -115,6 +119,9 @@ class TunnelAgent extends Agent {
socket.once('error', (err) => {
// we do not log these errors, sessions can drop from clients for many reasons
// these are not actionable errors for our server
if(this.portManager){
this.portManager.release(this.port);
}
socket.destroy();
});