Skip to content

Commit f602b1c

Browse files
committed
server: port range is end inclusive, improve validation
1 parent ad6d175 commit f602b1c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/server-main.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ async function start() {
147147
}
148148

149149
/**
150-
* If `--pick - port` and `--port` is specified, connect to that port.
150+
* If `--pick-port` and `--port` is specified, connect to that port.
151151
*
152-
* If not and a port range is specified through `--pick - port`
152+
* If not and a port range is specified through `--pick-port`
153153
* then find a free port in that range. Throw error if no
154154
* free port available in range.
155155
*
@@ -176,14 +176,15 @@ async function parsePort(host, strPort, strPickPort) {
176176
if (port !== undefined) {
177177
return port;
178178
}
179-
console.warn(`--port: Could not find free port in range: ${range.start} - ${range.end}.`);
179+
console.warn(`--port: Could not find free port in range: ${range.start} - ${range.end} (inclusive).`);
180180
process.exit(1);
181181

182182
} else {
183-
console.warn(`--port "${strPort}" is not a valid number or range.`);
183+
console.warn(`--port "${strPort}" is not a valid number or range. Ranges must be in the form 'from-to' with 'from' an integer larger than 0 and not larger than 'end'.`);
184184
process.exit(1);
185185
}
186186
}
187+
// pick-port is deprected and will be removed soon
187188
if (strPickPort) {
188189
const range = parseRange(strPickPort);
189190
if (range) {
@@ -194,11 +195,11 @@ async function parsePort(host, strPort, strPickPort) {
194195
if (port !== undefined) {
195196
return port;
196197
}
197-
console.log(`--pick - port: Could not find free port in range: ${range.start} - ${range.end}.`);
198+
console.log(`--pick-port: Could not find free port in range: ${range.start} - ${range.end}.`);
198199
process.exit(1);
199200
}
200201
} else {
201-
console.log(`--pick - port "${strPickPort}" is not properly formatted.`);
202+
console.log(`--pick-port "${strPickPort}" is not a valid range. Ranges must be in the form 'from-to' with 'from' an integer larger than 0 and not larger than 'end'.`);
202203
process.exit(1);
203204
}
204205
}
@@ -212,7 +213,10 @@ async function parsePort(host, strPort, strPickPort) {
212213
function parseRange(strRange) {
213214
const match = strRange.match(/^(\d+)-(\d+)$/);
214215
if (match) {
215-
return { start: parseInt(match[1], 10), end: parseInt(match[2], 10) };
216+
const start = parseInt(match[1], 10), end = parseInt(match[2], 10);
217+
if (start > 0 && start <= end && end <= 65535) {
218+
return { start, end };
219+
}
216220
}
217221
return undefined;
218222
}
@@ -239,7 +243,7 @@ async function findFreePort(host, start, end) {
239243
});
240244
});
241245
};
242-
for (let port = start; port < end; port++) {
246+
for (let port = start; port <= end; port++) {
243247
if (await testPort(port)) {
244248
return port;
245249
}

src/vs/server/node/serverEnvironmentService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export const serverOptions: OptionDescriptions<ServerParsedArgs> = {
1515
/* ----- server setup ----- */
1616

1717
'host': { type: 'string', cat: 'o', args: 'ip-address', description: nls.localize('host', "The host name or IP address the server should listen to. If not set, defaults to 'localhost'.") },
18-
'port': { type: 'string', cat: 'o', args: 'port | port range', description: nls.localize('port', "The port the server should listen to. If 0 is passed a random free port is picked. If a range in the format num-num is passed, a free port from the range is selected.") },
19-
'pick-port': { type: 'string', deprecationMessage: "Use the range notation in 'port' instead." },
18+
'port': { type: 'string', cat: 'o', args: 'port | port range', description: nls.localize('port', "The port the server should listen to. If 0 is passed a random free port is picked. If a range in the format num-num is passed, a free port from the range (end inclusive) is selected.") },
19+
'pick-port': { type: 'string', deprecationMessage: 'Use the range notation in \'port\' instead.' },
2020
'socket-path': { type: 'string', cat: 'o', args: 'path', description: nls.localize('socket-path', "The path to a socket file for the server to listen to.") },
2121
'connection-token': { type: 'string', cat: 'o', args: 'token', deprecates: ['connectionToken'], description: nls.localize('connection-token', "A secret that must be included with all requests.") },
2222
'connection-token-file': { type: 'string', cat: 'o', args: 'path', deprecates: ['connection-secret', 'connectionTokenFile'], description: nls.localize('connection-token-file', "Path to a file that contains the connection token.") },

0 commit comments

Comments
 (0)