Skip to content

Commit

Permalink
Add client-min-port-range & client-max-port-range options (localtunne…
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-bessei committed Mar 31, 2023
1 parent a9b3dce commit 5d37bf3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
10 changes: 10 additions & 0 deletions bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const argv = optimist
default: 10,
describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)'
})
.options('client-min-port-range', {
default: 1024,
describe: 'Port start range to use for localtunnel clients to connect to'
})
.options('client-max-port-range', {
default: 65535,
describe: 'Port end range to use for localtunnel clients to connect to'
})
.argv;

if (argv.help) {
Expand All @@ -35,6 +43,8 @@ const server = CreateServer({
max_tcp_sockets: argv['max-sockets'],
secure: argv.secure,
domain: argv.domain,
client_min_port_range: argv['client-min-port-range'],
client_max_port_range: argv['client-max-port-range']
});

server.listen(argv.port, argv.address, () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/ClientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,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,
client_min_port_range: client_min_port_range,
client_max_port_range: client_max_port_range
});

const client = new Client({
Expand Down
21 changes: 13 additions & 8 deletions lib/TunnelAgent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Agent } from 'http';
import net from 'net';
import { getPort } from 'portfinder';

const DEFAULT_MAX_SOCKETS = 10;

Expand All @@ -26,6 +27,8 @@ class TunnelAgent extends Agent {
this.connectedSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;

this.client_min_port_range = options.client_min_port_range;
this.client_max_port_range = options.client_max_port_range;
// new tcp server to service requests for this client
this.server = net.createServer();

Expand Down Expand Up @@ -58,14 +61,16 @@ class TunnelAgent extends Agent {
});

return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
console.debug('tcp server listening on port: %d', port);

resolve({
// port for lt client tcp connections
port: port,
});
getPort({ port: this.client_min_port_range, stopPort: this.client_max_port_range }, (err,port) => {
server.listen(port,() => {
const port = server.address().port;
console.debug('tcp server listening on port: %d', port);

resolve({
// port for lt client tcp connections
port: port,
});
});
});
});
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"koa-router": "7.4.0",
"localenv": "0.2.2",
"optimist": "0.6.1",
"portfinder": "1.0.32",
"pump": "3.0.0",
"tldjs": "2.3.1"
},
Expand Down
45 changes: 45 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ async-limiter@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"

async@^2.6.4:
version "2.6.4"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.4.tgz#706b7ff6084664cd7eae713f6f965433b5504221"
integrity sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==
dependencies:
lodash "^4.17.14"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
Expand Down Expand Up @@ -173,6 +180,13 @@ debug@*, debug@3.1.0, debug@^3.1.0:
dependencies:
ms "2.0.0"

debug@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==
dependencies:
ms "^2.1.1"

decamelize@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down Expand Up @@ -574,6 +588,11 @@ lodash.toarray@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"

lodash@^4.17.14:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

loud-rejection@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/loud-rejection/-/loud-rejection-1.6.0.tgz#5b46f80147edee578870f086d04821cf998e551f"
Expand Down Expand Up @@ -650,6 +669,11 @@ minimist@^1.1.1, minimist@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

minimist@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
Expand All @@ -660,6 +684,13 @@ mkdirp@0.5.1:
dependencies:
minimist "0.0.8"

mkdirp@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
dependencies:
minimist "^1.2.6"

mocha@5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.1.1.tgz#b774c75609dac05eb48f4d9ba1d827b97fde8a7b"
Expand All @@ -680,6 +711,11 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

ms@^2.1.1:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"
Expand Down Expand Up @@ -813,6 +849,15 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

portfinder@1.0.32:
version "1.0.32"
resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.32.tgz#2fe1b9e58389712429dc2bea5beb2146146c7f81"
integrity sha512-on2ZJVVDXRADWE6jnQaX0ioEylzgBpQk8r55NE4wjXW1ZxO+BgDlY6DXwj20i0V8eB4SenDQ00WEaxfiIQPcxg==
dependencies:
async "^2.6.4"
debug "^3.2.7"
mkdirp "^0.5.6"

process-nextick-args@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
Expand Down

0 comments on commit 5d37bf3

Please sign in to comment.