Support TCP port range limitation
This commit is contained in:
+72
-68
@@ -8,89 +8,93 @@ import TunnelAgent from './TunnelAgent';
|
||||
//
|
||||
// A client is a "user session" established to service a remote localtunnel client
|
||||
class ClientManager {
|
||||
constructor(opt) {
|
||||
this.opt = opt || {};
|
||||
constructor(opt) {
|
||||
this.opt = opt || {};
|
||||
|
||||
// id -> client instance
|
||||
this.clients = new Map();
|
||||
// id -> client instance
|
||||
this.clients = new Map();
|
||||
|
||||
// statistics
|
||||
this.stats = {
|
||||
tunnels: 0
|
||||
};
|
||||
// statistics
|
||||
this.stats = {
|
||||
tunnels: 0
|
||||
};
|
||||
|
||||
this.debug = Debug('lt:ClientManager');
|
||||
this.debug = Debug('lt:ClientManager');
|
||||
|
||||
// This is totally wrong :facepalm: this needs to be per-client...
|
||||
this.graceTimeout = null;
|
||||
// This is totally wrong :facepalm: this needs to be per-client...
|
||||
this.graceTimeout = null;
|
||||
}
|
||||
|
||||
// create a new tunnel with `id`
|
||||
// if the id is already used, a random id is assigned
|
||||
// if the tunnel could not be created, throws an error
|
||||
async newClient(id) {
|
||||
const clients = this.clients;
|
||||
const stats = this.stats;
|
||||
|
||||
// can't ask for id already is use
|
||||
if (clients[id]) {
|
||||
id = hri.random();
|
||||
}
|
||||
|
||||
// create a new tunnel with `id`
|
||||
// if the id is already used, a random id is assigned
|
||||
// if the tunnel could not be created, throws an error
|
||||
async newClient(id) {
|
||||
const clients = this.clients;
|
||||
const stats = this.stats;
|
||||
const maxSockets = this.opt.maxTcpSockets;
|
||||
const minPort = this.opt.minPort;
|
||||
const maxPort = this.opt.maxPort;
|
||||
const agent = new TunnelAgent({
|
||||
clientId: id,
|
||||
maxSockets: 10,
|
||||
minPort,
|
||||
maxPort
|
||||
});
|
||||
|
||||
// can't ask for id already is use
|
||||
if (clients[id]) {
|
||||
id = hri.random();
|
||||
}
|
||||
const client = new Client({
|
||||
id,
|
||||
agent,
|
||||
});
|
||||
|
||||
const maxSockets = this.opt.max_tcp_sockets;
|
||||
const agent = new TunnelAgent({
|
||||
clientId: id,
|
||||
maxSockets: 10,
|
||||
});
|
||||
// add to clients map immediately
|
||||
// avoiding races with other clients requesting same id
|
||||
clients[id] = client;
|
||||
|
||||
const client = new Client({
|
||||
id,
|
||||
agent,
|
||||
});
|
||||
client.once('close', () => {
|
||||
this.removeClient(id);
|
||||
});
|
||||
|
||||
// add to clients map immediately
|
||||
// avoiding races with other clients requesting same id
|
||||
clients[id] = client;
|
||||
|
||||
client.once('close', () => {
|
||||
this.removeClient(id);
|
||||
});
|
||||
|
||||
// try/catch used here to remove client id
|
||||
try {
|
||||
const info = await agent.listen();
|
||||
++stats.tunnels;
|
||||
return {
|
||||
id: id,
|
||||
port: info.port,
|
||||
max_conn_count: maxSockets,
|
||||
};
|
||||
}
|
||||
catch (err) {
|
||||
this.removeClient(id);
|
||||
// rethrow error for upstream to handle
|
||||
throw err;
|
||||
}
|
||||
// try/catch used here to remove client id
|
||||
try {
|
||||
const info = await agent.listen();
|
||||
++stats.tunnels;
|
||||
return {
|
||||
id: id,
|
||||
port: info.port,
|
||||
max_conn_count: maxSockets,
|
||||
};
|
||||
}
|
||||
|
||||
removeClient(id) {
|
||||
this.debug('removing client: %s', id);
|
||||
const client = this.clients[id];
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
--this.stats.tunnels;
|
||||
delete this.clients[id];
|
||||
client.close();
|
||||
catch (err) {
|
||||
this.removeClient(id);
|
||||
// rethrow error for upstream to handle
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
hasClient(id) {
|
||||
return !!this.clients[id];
|
||||
removeClient(id) {
|
||||
this.debug('removing client: %s', id);
|
||||
const client = this.clients[id];
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
--this.stats.tunnels;
|
||||
delete this.clients[id];
|
||||
client.close();
|
||||
}
|
||||
|
||||
getClient(id) {
|
||||
return this.clients[id];
|
||||
}
|
||||
hasClient(id) {
|
||||
return !!this.clients[id];
|
||||
}
|
||||
|
||||
getClient(id) {
|
||||
return this.clients[id];
|
||||
}
|
||||
}
|
||||
|
||||
export default ClientManager;
|
||||
|
||||
Reference in New Issue
Block a user