Skip to content

Commit 3c06736

Browse files
author
Olivier MICHAUD
committed
Add a range option to server to specify specific port client attribution
1 parent 3ebcab8 commit 3c06736

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

bin/server

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const argv = optimist
3131
default: 10,
3232
describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)'
3333
})
34+
.options('range', {
35+
default: null,
36+
describe: 'will bind incoming connections only on ports in range xxx:xxxx'
37+
})
3438
.argv;
3539

3640
if (argv.help) {
@@ -42,6 +46,7 @@ const server = CreateServer({
4246
max_tcp_sockets: argv['max-sockets'],
4347
secure: argv.secure,
4448
domain: argv.domain,
49+
range: argv.range,
4550
});
4651

4752
server.listen(argv.port, argv.address, () => {

lib/ClientManager.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Debug from 'debug';
33

44
import Client from './Client';
55
import TunnelAgent from './TunnelAgent';
6+
import PortManager from "./PortManager";
67

78
// Manage sets of clients
89
//
@@ -13,6 +14,7 @@ class ClientManager {
1314

1415
// id -> client instance
1516
this.clients = new Map();
17+
this.portManager = new PortManager({range: this.opt.range||null})
1618

1719
// statistics
1820
this.stats = {
@@ -39,6 +41,7 @@ class ClientManager {
3941

4042
const maxSockets = this.opt.max_tcp_sockets;
4143
const agent = new TunnelAgent({
44+
portManager: this.portManager,
4245
clientId: id,
4346
maxSockets: 10,
4447
});
@@ -79,6 +82,7 @@ class ClientManager {
7982
if (!client) {
8083
return;
8184
}
85+
this.portManager.release(client.agent.port);
8286
--this.stats.tunnels;
8387
delete this.clients[id];
8488
client.close();

lib/TunnelAgent.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class TunnelAgent extends Agent {
2020

2121
// sockets we can hand out via createConnection
2222
this.availableSockets = [];
23+
this.port = null;
24+
this.clientId = options.clientId
25+
this.portManager = options.portManager || null;
2326

2427
// when a createConnection cannot return a socket, it goes into a queue
2528
// once a socket is available it is handed out to the next callback
@@ -63,13 +66,14 @@ class TunnelAgent extends Agent {
6366
});
6467

6568
return new Promise((resolve) => {
66-
server.listen(() => {
67-
const port = server.address().port;
68-
this.debug('tcp server listening on port: %d', port);
69+
const port = this.portManager ? this.portManager.getNextAvailable(this.options.clientId) : null;
70+
server.listen(port,() => {
71+
this.port = server.address().port
72+
this.debug('tcp server listening on port: %d (%s)', this.port, this.clientId);
6973

7074
resolve({
7175
// port for lt client tcp connections
72-
port: port,
76+
port: this.port,
7377
});
7478
});
7579
});
@@ -115,6 +119,9 @@ class TunnelAgent extends Agent {
115119
socket.once('error', (err) => {
116120
// we do not log these errors, sessions can drop from clients for many reasons
117121
// these are not actionable errors for our server
122+
if(this.portManager){
123+
this.portManager.release(this.port);
124+
}
118125
socket.destroy();
119126
});
120127

0 commit comments

Comments
 (0)